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
|
class PhysXErrorCallback : public PxErrorCallback
{
public:
PhysXErrorCallback()
{
}
~PhysXErrorCallback()
{
}
void reportError(PxErrorCode::Enum Code, const char *Message, const char *File, int Line)
{
io::Log::error("[ PhysX ] " + io::stringc(Message));
}
};
static PxDefaultAllocator DefaultAllocatorCallback;
static PhysXErrorCallback DefaultErrorCallback;
static PxSimulationFilterShader DefaultFilterShader;
PhysXSimulator::PhysXSimulator()
{
PxFoundation_ = PxCreateFoundation(PX_PHYSICS_VERSION, DefaultAllocatorCallback, DefaultErrorCallback);
if (!PxFoundation_)
io::Log::error("Coudl not create PhysX foundation");
PxProfile_ = &PxProfileZoneManager::createProfileZoneManager(PxFoundation_);
if (!PxProfile_)
io::Log::error("Could not create PhysX profile zone manager");
bool RecordMemoryAllocations = true;
PxDevice_ = PxCreatePhysics(
PX_PHYSICS_VERSION, *PxFoundation_, PxTolerancesScale(), RecordMemoryAllocations, PxProfile_
);
if (!PxDevice_)
io::Log::error("Could not create PhysX device");
PxCooking_ = PxCreateCooking(PX_PHYSICS_VERSION, *PxFoundation_, PxCookingParams());
if (!PxCooking_)
io::Log::error("Could not create PhysX cooking device");
if (!PxInitExtensions(*PxDevice_))
io::Log::error("Could not initialize PhysX extensions");
PxScene_ = createScene();
PxMaterial_ = PxDevice->createMaterial(StaticFriction, DynamicFriction, Restitution);
if (!PxMaterial_)
io::Log::error("Could not create PhysX material");
}
PxScene* PhysXSimulator::createScene()
{
PxSceneDesc SceneDesc(PxDevice_->getTolerancesScale());
SceneDesc.gravity = convert(Gravity_);
SceneDesc.cpuDispatcher = PxDefaultCpuDispatcherCreate(1);
SceneDesc.filterShader = DefaultFilterShader;
if (!SceneDesc.cpuDispatcher)
io::Log::error("Could not create PhysX CPU dispatcher");
PxScene* NewScene = PxDevice_->createScene(SceneDesc);
if (!NewScene)
io::Log::error("Could not create PhysX scene");
NewScene->setVisualizationParameter(PxVisualizationParameter::eSCALE, 1.0f);
NewScene->setVisualizationParameter(PxVisualizationParameter::eCOLLISION_SHAPES, 1.0f);
return NewScene;
}
void PhysXSimulator::CreateRigidBody(const float* Matrix4x4, float Mass)
{
PxRigidDynamic* Actor = PxDevice->createRigidDynamic(PxTransform(PxMat44(Matrix4x4)));
if (Actor)
{
Actor->createShape(
PxBoxGeometry(Construct.Size.X, Construct.Size.Y, Construct.Size.Z),
*PxMaterial_
);
Actor->setMass(Mass);
PxRigidBodyExt::updateMassAndInertia(*Actor, Mass);
}
}
void PhysXSimulator::CreateStaticObject()
{
PxRigidStatic* Actor = PxDevice->createRigidStatic(
PxTransform(PxVec3(0.0f), PxQuat(PxHalfPi, PxVec3(0.0f, 0.0f, 1.0f)))
);
if (Actor)
Actor->createShape(PxPlaneGeometry(), *PxMaterial_);
}
void PhysXSimulator::UpdateSimulation()
{
if (PxScene_)
{
PxScene_->simulate(StepTime);
PxScene_->fetchResults(true);
}
}
|