@@ -49,6 +49,13 @@ using namespace lol::phys; | |||||
int gNumObjects = 64; | int gNumObjects = 64; | ||||
#define USE_WALL 1 | |||||
#define USE_PLATFORM 1 | |||||
#define USE_ROPE 0 | |||||
#define USE_BODIES 0 | |||||
#define USE_ROTATION 0 | |||||
#define USE_CHARACTER 1 | |||||
BtPhysTest::BtPhysTest(bool editor) | BtPhysTest::BtPhysTest(bool editor) | ||||
{ | { | ||||
/* Create a camera that matches the settings of XNA BtPhysTest */ | /* Create a camera that matches the settings of XNA BtPhysTest */ | ||||
@@ -72,48 +79,63 @@ BtPhysTest::BtPhysTest(bool editor) | |||||
float offset = 29.5f; | float offset = 29.5f; | ||||
vec3 pos_offset = vec3(.0f, 30.f, .0f); | vec3 pos_offset = vec3(.0f, 30.f, .0f); | ||||
for (int i=0; i < 6; i++) | |||||
if (USE_WALL) | |||||
{ | { | ||||
vec3 NewPosition = vec3(.0f); | |||||
quat NewRotation = quat(1.f); | |||||
for (int i=0; i < 6; i++) | |||||
{ | |||||
vec3 NewPosition = vec3(.0f); | |||||
quat NewRotation = quat(1.f); | |||||
PhysicsObject* NewPhyobj = new PhysicsObject(m_simulation, NewPosition, NewRotation); | |||||
PhysicsObject* NewPhyobj = new PhysicsObject(m_simulation, NewPosition, NewRotation); | |||||
int idx = i/2; | |||||
NewPosition = pos_offset; | |||||
NewPosition[idx] += offset; | |||||
offset *= -1.f; | |||||
int idx = i/2; | |||||
NewPosition = pos_offset; | |||||
NewPosition[idx] += offset; | |||||
offset *= -1.f; | |||||
if (idx != 1) | |||||
{ | |||||
vec3 axis = vec3(.0f); | |||||
axis[2 - idx] = 1; | |||||
NewRotation = quat::rotate(90.f, axis); | |||||
} | |||||
if (idx != 1) | |||||
{ | |||||
vec3 axis = vec3(.0f); | |||||
axis[2 - idx] = 1; | |||||
NewRotation = quat::rotate(90.f, axis); | |||||
} | |||||
NewPhyobj->SetTransform(NewPosition, NewRotation); | |||||
Ticker::Ref(NewPhyobj); | |||||
m_ground_list << NewPhyobj; | |||||
NewPhyobj->SetTransform(NewPosition, NewRotation); | |||||
Ticker::Ref(NewPhyobj); | |||||
m_ground_list << NewPhyobj; | |||||
} | |||||
} | } | ||||
if (USE_PLATFORM) | |||||
{ | { | ||||
quat NewRotation = quat(1.f); | |||||
quat NewRotation = quat::fromeuler_xyz(5.f, 0.f, 0.f); | |||||
vec3 NewPosition = pos_offset + vec3(5.0f, -20.0f, -15.0f); | vec3 NewPosition = pos_offset + vec3(5.0f, -20.0f, -15.0f); | ||||
PhysicsObject* NewPhyobj = new PhysicsObject(m_simulation, NewPosition, NewRotation, 0); | |||||
PhysicsObject* NewPhyobj = new PhysicsObject(m_simulation, NewPosition, NewRotation, 1); | |||||
m_platform_list << NewPhyobj; | m_platform_list << NewPhyobj; | ||||
Ticker::Ref(NewPhyobj); | Ticker::Ref(NewPhyobj); | ||||
NewPosition = pos_offset + vec3(-20.0f, -25.0f, 5.0f); | NewPosition = pos_offset + vec3(-20.0f, -25.0f, 5.0f); | ||||
NewPhyobj = new PhysicsObject(m_simulation, NewPosition, NewRotation, 0); | |||||
NewPhyobj = new PhysicsObject(m_simulation, NewPosition, NewRotation, 1); | |||||
m_platform_list << NewPhyobj; | m_platform_list << NewPhyobj; | ||||
Ticker::Ref(NewPhyobj); | Ticker::Ref(NewPhyobj); | ||||
} | } | ||||
if (1) | |||||
if (USE_CHARACTER) | |||||
{ | |||||
quat NewRotation = quat::fromeuler_xyz(0.f, 0.f, 0.f); | |||||
vec3 NewPosition = pos_offset + vec3(.0f, 40.0f, .0f); | |||||
PhysicsObject* NewPhyobj = new PhysicsObject(m_simulation, NewPosition, NewRotation, 2); | |||||
m_character_list << NewPhyobj; | |||||
Ticker::Ref(NewPhyobj); | |||||
} | |||||
if (USE_BODIES) | |||||
{ | { | ||||
for (int x=0; x < 6; x++) | for (int x=0; x < 6; x++) | ||||
{ | { | ||||
@@ -131,7 +153,7 @@ BtPhysTest::BtPhysTest(bool editor) | |||||
} | } | ||||
} | } | ||||
if (1) | |||||
if (USE_ROPE) | |||||
{ | { | ||||
Array<PhysicsObject*> RopeElements; | Array<PhysicsObject*> RopeElements; | ||||
for (int i = 0; i < 14; i++) | for (int i = 0; i < 14; i++) | ||||
@@ -314,34 +336,37 @@ void BtPhysTest::TickGame(float seconds) | |||||
vec3 GroundBarycenter = vec3(.0f); | vec3 GroundBarycenter = vec3(.0f); | ||||
vec3 PhysObjBarycenter = vec3(.0f); | vec3 PhysObjBarycenter = vec3(.0f); | ||||
float factor = .0f; | float factor = .0f; | ||||
for (int i = 0; i < m_ground_list.Count(); i++) | |||||
if (USE_WALL) | |||||
{ | { | ||||
PhysicsObject* PhysObj = m_ground_list[i]; | |||||
mat4 GroundMat = PhysObj->GetTransform(); | |||||
for (int i = 0; i < m_ground_list.Count(); i++) | |||||
{ | |||||
PhysicsObject* PhysObj = m_ground_list[i]; | |||||
mat4 GroundMat = PhysObj->GetTransform(); | |||||
GroundBarycenter += GroundMat.v3.xyz; | |||||
factor += 1.f; | |||||
} | |||||
GroundBarycenter += GroundMat.v3.xyz; | |||||
factor += 1.f; | |||||
} | |||||
GroundBarycenter /= factor; | |||||
GroundBarycenter /= factor; | |||||
for (int i = 0; i < m_ground_list.Count(); i++) | |||||
{ | |||||
PhysicsObject* PhysObj = m_ground_list[i]; | |||||
for (int i = 0; i < m_ground_list.Count(); i++) | |||||
{ | |||||
PhysicsObject* PhysObj = m_ground_list[i]; | |||||
mat4 GroundMat = PhysObj->GetTransform(); | |||||
vec3 CenterToGround = GroundMat.v3.xyz - GroundBarycenter; | |||||
vec3 CenterToCam = m_camera->m_position - GroundBarycenter; | |||||
mat4 GroundMat = PhysObj->GetTransform(); | |||||
vec3 CenterToGround = GroundMat.v3.xyz - GroundBarycenter; | |||||
vec3 CenterToCam = m_camera->m_position - GroundBarycenter; | |||||
if (dot(normalize(CenterToCam - CenterToGround), | |||||
normalize(CenterToGround)) > 0.f) | |||||
PhysObj->SetRender(false); | |||||
else | |||||
PhysObj->SetRender(true); | |||||
if (dot(normalize(CenterToCam - CenterToGround), | |||||
normalize(CenterToGround)) > 0.f) | |||||
PhysObj->SetRender(false); | |||||
else | |||||
PhysObj->SetRender(true); | |||||
} | |||||
} | } | ||||
if (0) | |||||
if (USE_ROTATION) | |||||
{ | { | ||||
for (int i = 0; i < m_ground_list.Count(); i++) | for (int i = 0; i < m_ground_list.Count(); i++) | ||||
{ | { | ||||
@@ -357,6 +382,7 @@ void BtPhysTest::TickGame(float seconds) | |||||
} | } | ||||
} | } | ||||
if (USE_PLATFORM) | |||||
{ | { | ||||
for (int i = 0; i < m_platform_list.Count(); i++) | for (int i = 0; i < m_platform_list.Count(); i++) | ||||
{ | { | ||||
@@ -378,20 +404,39 @@ void BtPhysTest::TickGame(float seconds) | |||||
} | } | ||||
} | } | ||||
PhysObjBarycenter = vec3(.0f); | |||||
for (int i = 0; i < m_physobj_list.Count(); i++) | |||||
if (USE_CHARACTER) | |||||
{ | { | ||||
PhysicsObject* PhysObj = m_physobj_list[i]; | |||||
mat4 GroundMat = PhysObj->GetTransform(); | |||||
for (int i = 0; i < m_character_list.Count(); i++) | |||||
{ | |||||
PhysicsObject* PhysObj = m_character_list[i]; | |||||
mat4 GroundMat = PhysObj->GetTransform(); | |||||
PhysObjBarycenter += GroundMat.v3.xyz; | |||||
factor += 1.f; | |||||
} | |||||
PhysObjBarycenter /= factor; | |||||
PhysObjBarycenter += GroundMat.v3.xyz; | |||||
factor += 1.f; | |||||
m_camera->SetTarget(PhysObjBarycenter); | |||||
m_camera->SetPosition(PhysObjBarycenter + vec3(-80.0f, 80.0f, .0f)); | |||||
} | } | ||||
else | |||||
{ | |||||
PhysObjBarycenter = vec3(.0f); | |||||
for (int i = 0; i < m_physobj_list.Count(); i++) | |||||
{ | |||||
PhysicsObject* PhysObj = m_physobj_list[i]; | |||||
mat4 GroundMat = PhysObj->GetTransform(); | |||||
PhysObjBarycenter /= factor; | |||||
PhysObjBarycenter += GroundMat.v3.xyz; | |||||
factor += 1.f; | |||||
} | |||||
m_camera->SetTarget(PhysObjBarycenter); | |||||
m_camera->SetPosition(GroundBarycenter + normalize(GroundBarycenter - PhysObjBarycenter) * 60.0f); | |||||
PhysObjBarycenter /= factor; | |||||
m_camera->SetTarget(PhysObjBarycenter); | |||||
m_camera->SetPosition(GroundBarycenter + normalize(GroundBarycenter - PhysObjBarycenter) * 60.0f); | |||||
} | |||||
#if 0 | #if 0 | ||||
///step the simulation | ///step the simulation | ||||
@@ -425,7 +470,7 @@ void BtPhysTest::TickDraw(float seconds) | |||||
m_ready = true; | m_ready = true; | ||||
} | } | ||||
Video::SetClearColor(vec4(0.0f, 0.0f, 0.12f, 1.0f)); | |||||
//Video::SetClearColor(vec4(0.0f, 0.0f, 0.12f, 1.0f)); | |||||
#if 0 | #if 0 | ||||
vec3 BarycenterLocation = vec3(.0f); | vec3 BarycenterLocation = vec3(.0f); | ||||
@@ -485,6 +530,13 @@ BtPhysTest::~BtPhysTest() | |||||
CurPop->GetPhysic()->RemoveFromSimulation(m_simulation); | CurPop->GetPhysic()->RemoveFromSimulation(m_simulation); | ||||
Ticker::Unref(CurPop); | Ticker::Unref(CurPop); | ||||
} | } | ||||
while (m_character_list.Count()) | |||||
{ | |||||
PhysicsObject* CurPop = m_character_list.Last(); | |||||
m_character_list.Pop(); | |||||
CurPop->GetCharacter()->RemoveFromSimulation(m_simulation); | |||||
Ticker::Unref(CurPop); | |||||
} | |||||
while (m_platform_list.Count()) | while (m_platform_list.Count()) | ||||
{ | { | ||||
PhysicsObject* CurPop = m_platform_list.Last(); | PhysicsObject* CurPop = m_platform_list.Last(); | ||||
@@ -29,6 +29,7 @@ private: | |||||
Array<PhysicsObject*> m_physobj_list; | Array<PhysicsObject*> m_physobj_list; | ||||
Array<PhysicsObject*> m_ground_list; | Array<PhysicsObject*> m_ground_list; | ||||
Array<PhysicsObject*> m_platform_list; | Array<PhysicsObject*> m_platform_list; | ||||
Array<PhysicsObject*> m_character_list; | |||||
#if 0 | #if 0 | ||||
EasyMesh m_ground_mesh; | EasyMesh m_ground_mesh; | ||||
@@ -22,7 +22,7 @@ class PhysicsObject : public WorldEntity | |||||
{ | { | ||||
public: | public: | ||||
PhysicsObject(Simulation* new_sim, const vec3 &base_location, const quat &base_rotation) | PhysicsObject(Simulation* new_sim, const vec3 &base_location, const quat &base_rotation) | ||||
: m_ready(false), m_should_render(true) | |||||
: m_ready(false), m_should_render(true), m_is_character(false) | |||||
{ | { | ||||
m_mesh.Compile("[sc#ddd afcb60 1 60 -.1]"); | m_mesh.Compile("[sc#ddd afcb60 1 60 -.1]"); | ||||
vec3 BoxSize = vec3(60.f, 1.f, 60.f); | vec3 BoxSize = vec3(60.f, 1.f, 60.f); | ||||
@@ -35,20 +35,38 @@ public: | |||||
} | } | ||||
PhysicsObject(Simulation* new_sim, const vec3 &base_location, const quat &base_rotation, int dummy) | PhysicsObject(Simulation* new_sim, const vec3 &base_location, const quat &base_rotation, int dummy) | ||||
: m_ready(false), m_should_render(true) | |||||
: m_ready(false), m_should_render(true), m_is_character(false) | |||||
{ | { | ||||
m_mesh.Compile("[sc#ddd afcb20 1 20 -.1]"); | |||||
vec3 BoxSize = vec3(20.f, 1.f, 20.f); | |||||
m_physics.SetCollisionChannel(0, 0xFF); | |||||
m_physics.SetShapeToBox(BoxSize); | |||||
m_physics.SetMass(.0f); | |||||
m_physics.SetTransform(base_location, base_rotation); | |||||
m_physics.InitBodyToRigid(true); | |||||
m_physics.AddToSimulation(new_sim); | |||||
if (dummy == 1) //for Rope purpose | |||||
{ | |||||
m_mesh.Compile("[sc#ddd afcb20 1 20 -.1]"); | |||||
//m_mesh.Compile("[sc#f00 afcb10 10 10 -.1]"); | |||||
vec3 BoxSize = vec3(20.f, 1.f, 20.f); | |||||
m_physics.SetCollisionChannel(0, 0xFF); | |||||
m_physics.SetShapeToBox(BoxSize); | |||||
m_physics.SetMass(.0f); | |||||
m_physics.SetTransform(base_location, base_rotation); | |||||
m_physics.InitBodyToRigid(true); | |||||
m_physics.AddToSimulation(new_sim); | |||||
} | |||||
else if (dummy == 2) //for character purpose | |||||
{ | |||||
m_is_character = true; | |||||
m_mesh.Compile("[sc#f00 afcb10 10 10 -.1]"); | |||||
//m_mesh.Compile("[sc#fff scb#fff ac1 2 2 2 0 0]"); | |||||
vec3 BoxSize = vec3(2.f, 2.f, 2.f); | |||||
m_character.SetCollisionChannel(0, 0xFF); | |||||
m_character.SetShapeToCapsule(BoxSize.x, BoxSize.y); | |||||
m_character.SetMass(.0f); | |||||
m_character.SetTransform(base_location, base_rotation); | |||||
m_character.InitBodyToGhost(); | |||||
m_character.AddToSimulation(new_sim); | |||||
} | |||||
} | } | ||||
PhysicsObject(Simulation* new_sim, float base_mass, const vec3 &base_location, int RandValue = -1) | PhysicsObject(Simulation* new_sim, float base_mass, const vec3 &base_location, int RandValue = -1) | ||||
: m_ready(false), m_should_render(true) | |||||
: m_ready(false), m_should_render(true), m_is_character(false) | |||||
{ | { | ||||
Array<char const *> MeshRand; | Array<char const *> MeshRand; | ||||
@@ -61,39 +79,39 @@ public: | |||||
int SphereLimit = MeshRand.Count(); | int SphereLimit = MeshRand.Count(); | ||||
MeshRand << "[sc#add asph15 2 2 2]"; | |||||
MeshRand << "[sc#dad asph15 2 2 2]"; | |||||
MeshRand << "[sc#dda asph15 2 2 2]"; | |||||
MeshRand << "[sc#daa asph15 2 2 2]"; | |||||
MeshRand << "[sc#ada asph15 2 2 2]"; | |||||
MeshRand << "[sc#aad asph15 2 2 2]"; | |||||
MeshRand << "[sc#add asph1 2 2 2]"; | |||||
MeshRand << "[sc#dad asph1 2 2 2]"; | |||||
MeshRand << "[sc#dda asph1 2 2 2]"; | |||||
MeshRand << "[sc#daa asph1 2 2 2]"; | |||||
MeshRand << "[sc#ada asph1 2 2 2]"; | |||||
MeshRand << "[sc#aad asph1 2 2 2]"; | |||||
int ConeLimit = MeshRand.Count(); | int ConeLimit = MeshRand.Count(); | ||||
MeshRand << "[sc#add scb#add ad48 2 0 rx180 ty-1 ac48 2 2 0 0 0]"; | |||||
MeshRand << "[sc#dad scb#dad ad48 2 0 rx180 ty-1 ac48 2 2 0 0 0]"; | |||||
MeshRand << "[sc#dda scb#dda ad48 2 0 rx180 ty-1 ac48 2 2 0 0 0]"; | |||||
MeshRand << "[sc#daa scb#daa ad48 2 0 rx180 ty-1 ac48 2 2 0 0 0]"; | |||||
MeshRand << "[sc#ada scb#ada ad48 2 0 rx180 ty-1 ac48 2 2 0 0 0]"; | |||||
MeshRand << "[sc#aad scb#aad ad48 2 0 rx180 ty-1 ac48 2 2 0 0 0]"; | |||||
MeshRand << "[sc#add scb#add ad1 2 0 rx180 ty-1 ac1 2 2 0 0 0]"; | |||||
MeshRand << "[sc#dad scb#dad ad1 2 0 rx180 ty-1 ac1 2 2 0 0 0]"; | |||||
MeshRand << "[sc#dda scb#dda ad1 2 0 rx180 ty-1 ac1 2 2 0 0 0]"; | |||||
MeshRand << "[sc#daa scb#daa ad1 2 0 rx180 ty-1 ac1 2 2 0 0 0]"; | |||||
MeshRand << "[sc#ada scb#ada ad1 2 0 rx180 ty-1 ac1 2 2 0 0 0]"; | |||||
MeshRand << "[sc#aad scb#aad ad1 2 0 rx180 ty-1 ac1 2 2 0 0 0]"; | |||||
int CylLimit = MeshRand.Count(); | int CylLimit = MeshRand.Count(); | ||||
MeshRand << "[sc#add scb#add ad96 2 0 rx180 ty-1 my ac96 2 2 2 0 0]"; | |||||
MeshRand << "[sc#dad scb#dad ad96 2 0 rx180 ty-1 my ac96 2 2 2 0 0]"; | |||||
MeshRand << "[sc#dda scb#dda ad96 2 0 rx180 ty-1 my ac96 2 2 2 0 0]"; | |||||
MeshRand << "[sc#daa scb#daa ad96 2 0 rx180 ty-1 my ac96 2 2 2 0 0]"; | |||||
MeshRand << "[sc#ada scb#ada ad96 2 0 rx180 ty-1 my ac96 2 2 2 0 0]"; | |||||
MeshRand << "[sc#aad scb#aad ad96 2 0 rx180 ty-1 my ac96 2 2 2 0 0]"; | |||||
MeshRand << "[sc#add scb#add ad1 2 0 rx180 ty-1 my ac1 2 2 2 0 0]"; | |||||
MeshRand << "[sc#dad scb#dad ad1 2 0 rx180 ty-1 my ac1 2 2 2 0 0]"; | |||||
MeshRand << "[sc#dda scb#dda ad1 2 0 rx180 ty-1 my ac1 2 2 2 0 0]"; | |||||
MeshRand << "[sc#daa scb#daa ad1 2 0 rx180 ty-1 my ac1 2 2 2 0 0]"; | |||||
MeshRand << "[sc#ada scb#ada ad1 2 0 rx180 ty-1 my ac1 2 2 2 0 0]"; | |||||
MeshRand << "[sc#aad scb#aad ad1 2 0 rx180 ty-1 my ac1 2 2 2 0 0]"; | |||||
int CapsLimit = MeshRand.Count(); | int CapsLimit = MeshRand.Count(); | ||||
MeshRand << "[sc#add scb#add acap21 2 1]"; | |||||
MeshRand << "[sc#dad scb#dad acap21 2 1]"; | |||||
MeshRand << "[sc#dda scb#dda acap21 2 1]"; | |||||
MeshRand << "[sc#daa scb#daa acap21 2 1]"; | |||||
MeshRand << "[sc#ada scb#ada acap21 2 1]"; | |||||
MeshRand << "[sc#aad scb#aad acap21 2 1]"; | |||||
MeshRand << "[sc#add scb#add acap1 2 1]"; | |||||
MeshRand << "[sc#dad scb#dad acap1 2 1]"; | |||||
MeshRand << "[sc#dda scb#dda acap1 2 1]"; | |||||
MeshRand << "[sc#daa scb#daa acap1 2 1]"; | |||||
MeshRand << "[sc#ada scb#ada acap1 2 1]"; | |||||
MeshRand << "[sc#aad scb#aad acap1 2 1]"; | |||||
switch (RandValue) | switch (RandValue) | ||||
{ | { | ||||
@@ -167,12 +185,18 @@ public: | |||||
void SetTransform(const lol::vec3& base_location, const lol::quat& base_rotation=lol::quat(lol::mat4(1.0f))) | void SetTransform(const lol::vec3& base_location, const lol::quat& base_rotation=lol::quat(lol::mat4(1.0f))) | ||||
{ | { | ||||
m_physics.SetTransform(base_location, base_rotation); | |||||
if (m_is_character) | |||||
m_character.SetTransform(base_location, base_rotation); | |||||
else | |||||
m_physics.SetTransform(base_location, base_rotation); | |||||
} | } | ||||
lol::mat4 GetTransform() | lol::mat4 GetTransform() | ||||
{ | { | ||||
return m_physics.GetTransform(); | |||||
if (m_is_character) | |||||
return m_character.GetTransform(); | |||||
else | |||||
return m_physics.GetTransform(); | |||||
} | } | ||||
void SetRender(bool should_render) | void SetRender(bool should_render) | ||||
@@ -182,6 +206,7 @@ public: | |||||
EasyMesh *GetMesh() { return &m_mesh; } | EasyMesh *GetMesh() { return &m_mesh; } | ||||
EasyPhysic *GetPhysic() { return &m_physics; } | EasyPhysic *GetPhysic() { return &m_physics; } | ||||
EasyPhysic *GetCharacter() { return &m_character; } | |||||
~PhysicsObject() | ~PhysicsObject() | ||||
{ | { | ||||
@@ -206,16 +231,23 @@ protected: | |||||
} | } | ||||
if (m_should_render) | if (m_should_render) | ||||
m_mesh.Render(m_physics.GetTransform()); | |||||
{ | |||||
if (m_is_character) | |||||
m_mesh.Render(m_character.GetTransform()); | |||||
else | |||||
m_mesh.Render(m_physics.GetTransform()); | |||||
} | |||||
} | } | ||||
private: | private: | ||||
//Base datas | //Base datas | ||||
EasyMesh m_mesh; | |||||
EasyPhysic m_physics; | |||||
EasyMesh m_mesh; | |||||
EasyPhysic m_physics; | |||||
EasyCharacterController m_character; | |||||
bool m_ready; | bool m_ready; | ||||
bool m_should_render; | bool m_should_render; | ||||
bool m_is_character; | |||||
}; | }; | ||||
#endif /* __PHYSICOBJECT_H__ */ | #endif /* __PHYSICOBJECT_H__ */ | ||||
@@ -75,31 +75,36 @@ void EasyPhysic::SetShapeTo(btCollisionShape* collision_shape) | |||||
void EasyPhysic::SetShapeToBox(lol::vec3& box_size) | void EasyPhysic::SetShapeToBox(lol::vec3& box_size) | ||||
{ | { | ||||
vec3 new_box_size = box_size * LOL2BT_UNIT * LOL2BT_SIZE; | vec3 new_box_size = box_size * LOL2BT_UNIT * LOL2BT_SIZE; | ||||
SetShapeTo(new btBoxShape(LOL2BT_VEC3(new_box_size))); | |||||
m_convex_shape = new btBoxShape(LOL2BT_VEC3(new_box_size)); | |||||
SetShapeTo(m_convex_shape); | |||||
} | } | ||||
void EasyPhysic::SetShapeToSphere(float radius) | void EasyPhysic::SetShapeToSphere(float radius) | ||||
{ | { | ||||
SetShapeTo(new btSphereShape(radius * LOL2BT_UNIT * LOL2BT_SIZE)); | |||||
m_convex_shape = new btSphereShape(radius * LOL2BT_UNIT * LOL2BT_SIZE); | |||||
SetShapeTo(m_convex_shape); | |||||
} | } | ||||
void EasyPhysic::SetShapeToCone(float radius, float height) | void EasyPhysic::SetShapeToCone(float radius, float height) | ||||
{ | { | ||||
SetShapeTo(new btConeShape( radius * LOL2BT_UNIT, | |||||
height * LOL2BT_UNIT)); | |||||
m_convex_shape = new btConeShape( radius * LOL2BT_UNIT, | |||||
height * LOL2BT_UNIT); | |||||
SetShapeTo(m_convex_shape); | |||||
} | } | ||||
void EasyPhysic::SetShapeToCylinder(lol::vec3& cyl_size) | void EasyPhysic::SetShapeToCylinder(lol::vec3& cyl_size) | ||||
{ | { | ||||
vec3 new_cyl_size = cyl_size * LOL2BT_UNIT; | vec3 new_cyl_size = cyl_size * LOL2BT_UNIT; | ||||
new_cyl_size.y *= LOL2BT_SIZE; | new_cyl_size.y *= LOL2BT_SIZE; | ||||
SetShapeTo(new btCylinderShape(LOL2BT_VEC3(new_cyl_size))); | |||||
m_convex_shape = new btCylinderShape(LOL2BT_VEC3(new_cyl_size)); | |||||
SetShapeTo(m_convex_shape); | |||||
} | } | ||||
void EasyPhysic::SetShapeToCapsule(float radius, float height) | void EasyPhysic::SetShapeToCapsule(float radius, float height) | ||||
{ | { | ||||
SetShapeTo(new btCapsuleShape( radius * LOL2BT_UNIT * LOL2BT_SIZE, | |||||
height * LOL2BT_UNIT * LOL2BT_SIZE)); | |||||
m_convex_shape = new btCapsuleShape(radius * LOL2BT_UNIT * LOL2BT_SIZE, | |||||
height * LOL2BT_UNIT * LOL2BT_SIZE); | |||||
SetShapeTo(m_convex_shape); | |||||
} | } | ||||
//------------------------------------------------------------------------- | //------------------------------------------------------------------------- | ||||
@@ -107,6 +112,8 @@ void EasyPhysic::SetShapeToCapsule(float radius, float height) | |||||
//-- | //-- | ||||
void EasyPhysic::SetTransform(const lol::vec3& base_location, const lol::quat& base_rotation) | void EasyPhysic::SetTransform(const lol::vec3& base_location, const lol::quat& base_rotation) | ||||
{ | { | ||||
m_local_to_world = lol::mat4::translate(base_location) * mat4(base_rotation); | |||||
if (m_ghost_object) | if (m_ghost_object) | ||||
m_ghost_object->setWorldTransform(btTransform(LOL2BT_QUAT(base_rotation), LOL2BT_VEC3(base_location * LOL2BT_UNIT))); | m_ghost_object->setWorldTransform(btTransform(LOL2BT_QUAT(base_rotation), LOL2BT_VEC3(base_location * LOL2BT_UNIT))); | ||||
else | else | ||||
@@ -150,11 +157,22 @@ void EasyPhysic::InitBodyToRigid(bool SetToKinematic) | |||||
m_rigid_body = new btRigidBody(NewInfos); | m_rigid_body = new btRigidBody(NewInfos); | ||||
m_collision_object = m_rigid_body; | m_collision_object = m_rigid_body; | ||||
if (m_mass == .0f && SetToKinematic) | |||||
if (m_mass == .0f) | |||||
{ | { | ||||
m_rigid_body->setActivationState(DISABLE_DEACTIVATION); | |||||
m_rigid_body->setCollisionFlags(m_rigid_body->getCollisionFlags() | btCollisionObject::CF_KINEMATIC_OBJECT); | |||||
if (SetToKinematic) | |||||
{ | |||||
m_rigid_body->setActivationState(DISABLE_DEACTIVATION); | |||||
m_rigid_body->setCollisionFlags(m_rigid_body->getCollisionFlags() | btCollisionObject::CF_KINEMATIC_OBJECT); | |||||
} | |||||
} | } | ||||
else | |||||
SetMass(m_mass); | |||||
} | |||||
//Return correct Ghost Object | |||||
btGhostObject* EasyPhysic::GetGhostObject() | |||||
{ | |||||
return new btGhostObject(); | |||||
} | } | ||||
//Init to Ghost object, for Overlap/Sweep Test/Touching logic | //Init to Ghost object, for Overlap/Sweep Test/Touching logic | ||||
@@ -163,14 +181,13 @@ void EasyPhysic::InitBodyToGhost() | |||||
if (m_collision_object) | if (m_collision_object) | ||||
delete m_collision_object; | delete m_collision_object; | ||||
m_ghost_object = new btGhostObject(); | |||||
m_ghost_object = GetGhostObject(); | |||||
m_ghost_object->setCollisionShape(m_collision_shape); | m_ghost_object->setCollisionShape(m_collision_shape); | ||||
m_collision_object = m_ghost_object; | m_collision_object = m_ghost_object; | ||||
SetTransform(vec3(.0f)); | |||||
SetTransform(m_local_to_world.v3.xyz, lol::quat(m_local_to_world)); | |||||
m_ghost_object->setCollisionFlags(m_ghost_object->getCollisionFlags()); | m_ghost_object->setCollisionFlags(m_ghost_object->getCollisionFlags()); | ||||
//btCollisionObject::CF_CHARACTER_OBJECT | |||||
} | } | ||||
//Add Physic object to the simulation | //Add Physic object to the simulation | ||||
@@ -205,7 +222,7 @@ void EasyPhysic::RemoveFromSimulation(class Simulation* current_simulation) | |||||
{ | { | ||||
if (m_rigid_body) | if (m_rigid_body) | ||||
dynamics_world->removeRigidBody(m_rigid_body); | dynamics_world->removeRigidBody(m_rigid_body); | ||||
else | |||||
else if (m_collision_object) | |||||
dynamics_world->removeCollisionObject(m_collision_object); | dynamics_world->removeCollisionObject(m_collision_object); | ||||
} | } | ||||
} | } | ||||
@@ -241,33 +258,53 @@ void EasyPhysic::SetLocalInertia(float mass) | |||||
//EASY_CHARACTER_CONTROLLER | //EASY_CHARACTER_CONTROLLER | ||||
//-- | //-- | ||||
void EasyCharacterController::SetTransform(const lol::vec3& base_location, const lol::quat& base_rotation) | |||||
//Deactivated for Character controller | |||||
void EasyCharacterController::InitBodyToRigid(bool ZeroMassIsKinematic) | |||||
{ | { | ||||
} | } | ||||
void EasyCharacterController::SetMass(float mass) | |||||
{ | |||||
} | |||||
void EasyCharacterController::InitBodyToRigid(bool ZeroMassIsKinematic) | |||||
//Return correct Ghost Object | |||||
btGhostObject* EasyCharacterController::GetGhostObject() | |||||
{ | { | ||||
return new btPairCachingGhostObject(); | |||||
} | } | ||||
//Init to Pair caching ghost object, since Character uses that one. | |||||
void EasyCharacterController::InitBodyToGhost() | void EasyCharacterController::InitBodyToGhost() | ||||
{ | { | ||||
//btCollisionObject::CF_CHARACTER_OBJECT | |||||
EasyPhysic::InitBodyToGhost(); | |||||
m_pair_caching_object = (btPairCachingGhostObject*)m_ghost_object; | |||||
m_ghost_object->setCollisionFlags(btCollisionObject::CF_CHARACTER_OBJECT | m_ghost_object->getCollisionFlags()); | |||||
} | } | ||||
//Add Physic object to the simulation | |||||
void EasyCharacterController::AddToSimulation(class Simulation* current_simulation) | void EasyCharacterController::AddToSimulation(class Simulation* current_simulation) | ||||
{ | { | ||||
EasyPhysic::AddToSimulation(current_simulation); | |||||
btDiscreteDynamicsWorld* dynamics_world = current_simulation->GetWorld(); | |||||
if (dynamics_world) | |||||
{ | |||||
if (m_character) | |||||
delete m_character; | |||||
m_character = new btKinematicCharacterController(m_pair_caching_object, m_convex_shape, m_step_height, m_up_axis); | |||||
dynamics_world->addAction(m_character); | |||||
} | |||||
} | } | ||||
//Remove Physic object to the simulation | |||||
void EasyCharacterController::RemoveFromSimulation(class Simulation* current_simulation) | void EasyCharacterController::RemoveFromSimulation(class Simulation* current_simulation) | ||||
{ | { | ||||
EasyPhysic::RemoveFromSimulation(current_simulation); | |||||
} | |||||
mat4 EasyCharacterController::GetTransform() | |||||
{ | |||||
return mat4(1.f); | |||||
btDiscreteDynamicsWorld* dynamics_world = current_simulation->GetWorld(); | |||||
if (dynamics_world) | |||||
{ | |||||
if (m_character) | |||||
dynamics_world->removeAction(m_character); | |||||
} | |||||
} | } | ||||
//------------------------------------------------------------------------- | //------------------------------------------------------------------------- | ||||
@@ -22,6 +22,7 @@ | |||||
#include <bullet/btBulletDynamicsCommon.h> | #include <bullet/btBulletDynamicsCommon.h> | ||||
#include <bullet/btBulletCollisionCommon.h> | #include <bullet/btBulletCollisionCommon.h> | ||||
#include <bullet/BulletCollision/CollisionDispatch/btGhostObject.h> | #include <bullet/BulletCollision/CollisionDispatch/btGhostObject.h> | ||||
#include <BulletDynamics/Character/btKinematicCharacterController.h> | |||||
#endif | #endif | ||||
namespace lol | namespace lol | ||||
@@ -60,6 +61,8 @@ protected: | |||||
virtual void SetLocalInertia(float mass); | virtual void SetLocalInertia(float mass); | ||||
virtual void SetShapeTo(btCollisionShape* collision_shape); | virtual void SetShapeTo(btCollisionShape* collision_shape); | ||||
virtual btGhostObject* GetGhostObject(); | |||||
btCollisionObject* m_collision_object; | btCollisionObject* m_collision_object; | ||||
btGhostObject* m_ghost_object; | btGhostObject* m_ghost_object; | ||||
@@ -68,6 +71,7 @@ protected: | |||||
btVector3 m_local_inertia; | btVector3 m_local_inertia; | ||||
btCollisionShape* m_collision_shape; | btCollisionShape* m_collision_shape; | ||||
btConvexShape* m_convex_shape; | |||||
btMotionState* m_motion_state; | btMotionState* m_motion_state; | ||||
#else // NO PHYSIC IMPLEMENTATION | #else // NO PHYSIC IMPLEMENTATION | ||||
@@ -90,6 +94,8 @@ public: | |||||
virtual void RemoveFromSimulation(class Simulation* current_simulation) { } | virtual void RemoveFromSimulation(class Simulation* current_simulation) { } | ||||
virtual mat4 GetTransform() { return mat4(1.0f); } | virtual mat4 GetTransform() { return mat4(1.0f); } | ||||
virtual void InitBodyToGhost() { } | |||||
#endif // PHYSIC IMPLEMENTATION | #endif // PHYSIC IMPLEMENTATION | ||||
public: | public: | ||||
@@ -121,20 +127,31 @@ class EasyCharacterController : public EasyPhysic | |||||
#ifdef HAVE_PHYS_USE_BULLET | #ifdef HAVE_PHYS_USE_BULLET | ||||
public: | public: | ||||
EasyCharacterController(); | |||||
~EasyCharacterController(); | |||||
EasyCharacterController() : | |||||
EasyPhysic(), | |||||
m_character(NULL) | |||||
{ | |||||
m_up_axis = 1; | |||||
} | |||||
~EasyCharacterController() | |||||
{ | |||||
delete m_character; | |||||
} | |||||
virtual void SetTransform(const lol::vec3& base_location, const lol::quat& base_rotation=lol::quat(lol::mat4(1.0f))); | |||||
virtual void SetMass(float mass); | |||||
virtual void InitBodyToRigid(bool ZeroMassIsKinematic=false); | virtual void InitBodyToRigid(bool ZeroMassIsKinematic=false); | ||||
virtual void InitBodyToGhost(); | virtual void InitBodyToGhost(); | ||||
virtual void AddToSimulation(class Simulation* current_simulation); | virtual void AddToSimulation(class Simulation* current_simulation); | ||||
virtual void RemoveFromSimulation(class Simulation* current_simulation); | virtual void RemoveFromSimulation(class Simulation* current_simulation); | ||||
virtual mat4 GetTransform(); | |||||
protected: | protected: | ||||
virtual btGhostObject* GetGhostObject(); | |||||
btPairCachingGhostObject* m_pair_caching_object; | btPairCachingGhostObject* m_pair_caching_object; | ||||
btKinematicCharacterController* m_character; | |||||
float m_step_height; | |||||
int m_up_axis; | |||||
#else // NO PHYSIC IMPLEMENTATION | #else // NO PHYSIC IMPLEMENTATION | ||||