-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathStaticRigidBody.cpp
More file actions
172 lines (147 loc) · 5.82 KB
/
Copy pathStaticRigidBody.cpp
File metadata and controls
172 lines (147 loc) · 5.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#include "StaticRigidBody.h"
#include "PhysicsMathAdapter.h"
#include <mathematics/transform.hpp>
StaticRigidBody::StaticRigidBody(EColliderType collidreType, unsigned int id, unsigned int layerNumber)
: RigidBody(collidreType, id, layerNumber)
, m_rigidStatic(nullptr)
{
}
StaticRigidBody::~StaticRigidBody()
{
if (m_rigidStatic) // 추가: nullptr 체크
{
// Physics->RemoveCollisionData(m_id); // 이 줄 제거
physx::PxShape* shape;
m_rigidStatic->getShapes(&shape, 1);
if (shape) // 추가: shape 유효성 확인
{
m_rigidStatic->detachShape(*shape);
}
}
}
bool StaticRigidBody::Initialize(ColliderInfo colliderInfo, physx::PxShape* shape, physx::PxPhysics* physics, CollisionData* data)
{
if (m_colliderType == EColliderType::COLLISION)
{
shape->setFlag(physx::PxShapeFlag::eSIMULATION_SHAPE, true);
shape->setFlag(physx::PxShapeFlag::eTRIGGER_SHAPE, false);
}
else {
shape->setFlag(physx::PxShapeFlag::eSIMULATION_SHAPE, false);
shape->setFlag(physx::PxShapeFlag::eTRIGGER_SHAPE, true);
}
shape->setContactOffset(0.02f);
shape->setRestOffset(0.01f);
const math::matrix4x4& transform = colliderInfo.collsionTransform.worldMatrix;
math::vector3 scale{};
math::quaternion rot{};
math::vector3 pos{};
math::decompose(transform, scale, rot, pos); // 스케일, 회전, 위치 분해
physx::PxTransform transformPx = PhysicsMath::ToPxTransform(pos, rot); // 회전 변환
if (shape->getGeometry().getType() == physx::PxGeometryType::eHEIGHTFIELD) {
transformPx.p.y += -100.0f;
}
//CopyMatrixDxToPx(transform, transformPx);
m_rigidStatic = physics->createRigidStatic(transformPx);
m_rigidStatic->userData = data;
if (m_rigidStatic == nullptr) {
Debug->LogError("StaticRigidBody::Initialize() : m_rigidStatic is nullptr id :" + std::to_string(m_id));
return false;
}
if (!m_rigidStatic->attachShape(*shape))
{
Debug->LogError("StaticRigidBody::Initialize() : attachShape failed id :" + std::to_string(m_id));
return false;
}
data->thisId = m_id;
data->thisLayerNumber = m_layerNumber;
shape->userData = data; // 이 위치로 이동
return true;
}
void StaticRigidBody::ChangeLayerNumber(const unsigned int& layerNumber, int* collisionMatrix)
{
if (layerNumber == UINT_MAX)
{
return;
}
m_layerNumber = layerNumber;
physx::PxShape* shape;
if (!m_rigidStatic) return; // 추가: m_rigidStatic 유효성 확인
m_rigidStatic->getShapes(&shape, 1);
if (shape && shape->userData != nullptr) // 추가: shape 및 userData 유효성 확인
{
physx::PxFilterData filterData;
filterData.word0 = layerNumber;
filterData.word1 = collisionMatrix[layerNumber];
filterData.word2 = 1 << layerNumber;
shape->setSimulationFilterData(filterData);
shape->setQueryFilterData(filterData);
auto userData = static_cast<CollisionData*>(shape->userData);
userData->thisLayerNumber = layerNumber;
}
}
void StaticRigidBody::SetConvertScale(const math::vector3& scale, physx::PxPhysics* physics, unsigned int* collisionMatrix)
{
if (std::isnan(m_scale.x) || std::isnan(m_scale.y) || std::isnan(m_scale.z))
{
return;
}
if (fabs(m_scale.x - scale.x) < 0.001f && fabs(m_scale.y - scale.y) < 0.001f && fabs(m_scale.z - scale.z) < 0.001f)
{
return;
}
m_scale = scale;
physx::PxShape* shape;
physx::PxMaterial* material;
if (!m_rigidStatic) return; // 추가: m_rigidStatic 유효성 확인
m_rigidStatic->getShapes(&shape, 1);
if (!shape) return; // 추가: shape 유효성 확인
shape->getMaterials(&material, 1);
void* userData = shape->userData;
if (shape->getGeometry().getType() == physx::PxGeometryType::eBOX)
{
physx::PxBoxGeometry boxGeometry = static_cast<const physx::PxBoxGeometry&>(shape->getGeometry());
boxGeometry.halfExtents.x = m_Extent.x * m_scale.x;
boxGeometry.halfExtents.y = m_Extent.y * m_scale.y;
boxGeometry.halfExtents.z = m_Extent.z * m_scale.z;
m_rigidStatic->detachShape(*shape);
if (userData) // 추가: userData 유효성 확인
UpdateShapeGeometry(m_rigidStatic, boxGeometry, physics, material, collisionMatrix, userData);
}
else if (shape->getGeometry().getType() == physx::PxGeometryType::eSPHERE)
{
physx::PxSphereGeometry sphereGeometry = static_cast<const physx::PxSphereGeometry&>(shape->getGeometry());
float maxScale = std::max<float>(m_scale.x, std::max<float>(m_scale.y, m_scale.z));
sphereGeometry.radius = m_radius * maxScale;
m_rigidStatic->detachShape(*shape);
if (userData) // 추가: userData 유효성 확인
UpdateShapeGeometry(m_rigidStatic, sphereGeometry, physics, material, collisionMatrix, userData);
}
else if (shape->getGeometry().getType() == physx::PxGeometryType::eCAPSULE)
{
physx::PxCapsuleGeometry capsuleGeometry = static_cast<const physx::PxCapsuleGeometry&>(shape->getGeometry());
capsuleGeometry.halfHeight = m_halfHeight * m_scale.y;
capsuleGeometry.radius = m_radius * m_scale.x;
m_rigidStatic->detachShape(*shape);
if (userData) // 추가: userData 유효성 확인
UpdateShapeGeometry(m_rigidStatic, capsuleGeometry, physics, material, collisionMatrix, userData);
}
else if (shape->getGeometry().getType() == physx::PxGeometryType::eCONVEXMESH)
{
physx::PxConvexMeshGeometry convexMeshGeometry = static_cast<const physx::PxConvexMeshGeometry&>(shape->getGeometry());
convexMeshGeometry.scale.scale.x = m_scale.x;
convexMeshGeometry.scale.scale.y = m_scale.y;
convexMeshGeometry.scale.scale.z = m_scale.z;
m_rigidStatic->detachShape(*shape);
if (userData) // 추가: userData 유효성 확인
UpdateShapeGeometry(m_rigidStatic, convexMeshGeometry, physics, material, collisionMatrix, userData);
}
else if (shape->getGeometry().getType() == physx::PxGeometryType::eTRIANGLEMESH)
{
Debug->LogError("StaticRigidBody::SetConvertScale() : TriangleMesh is not supported id :" + std::to_string(m_id));
}
else if (shape->getGeometry().getType() == physx::PxGeometryType::eHEIGHTFIELD)
{
Debug->LogError("StaticRigidBody::SetConvertScale() : HeightField is not supported id :" + std::to_string(m_id));
}
}