diff --git a/test/BtPhysTest.cpp b/test/BtPhysTest.cpp index 70149761..41d24252 100644 --- a/test/BtPhysTest.cpp +++ b/test/BtPhysTest.cpp @@ -74,16 +74,42 @@ BtPhysTest::BtPhysTest(bool editor) vec3 NewGravity = vec3(.0f, -10.0f, .0f); m_simulation->SetGravity(NewGravity); - m_ground_object = new PhysicsObject(m_simulation); - Ticker::Ref(m_ground_object); + float offset = 30.f; + vec3 pos_offset = vec3(.0f, 30.f, .0f); + for (int i=0; i < 6; i++) + { + int idx = i/2; + vec3 NewPosition = pos_offset; + NewPosition[idx] += offset; + offset *= -1.f; + + quat NewRotation = quat(mat4(1.f)); + PhysicsObject* NewPhyobj = new PhysicsObject(m_simulation, NewPosition, NewRotation); + + if (idx != 1) + { + vec3 axis = vec3(.0f); + axis[idx] = 1; + NewRotation = quat::rotate(90.f, axis); + } - for (int x=0; x < 10; x++) + NewPhyobj->SetTransform(NewPosition, NewRotation); + Ticker::Ref(NewPhyobj); + m_ground_list << NewPhyobj; + } + + for (int x=0; x < 5; x++) { - for (int y=0; y < 10; y++) + for (int y=0; y < 5; y++) { - PhysicsObject* new_physobj = new PhysicsObject(m_simulation, 10.f, vec3(0.f, 20.f, -20.0f) + vec3(.0f, 4.f * (float)y, 4.f * (float)x)); - m_physobj_list << new_physobj; - Ticker::Ref(new_physobj); + for (int z=0; z < 4; z++) + { + PhysicsObject* new_physobj = new PhysicsObject(m_simulation, 10.f, + vec3(-20.f, 40.f, -20.f) + + vec3(4.f * (float)x, 4.f * (float)y, 4.f * (float)z)); + m_physobj_list << new_physobj; + Ticker::Ref(new_physobj); + } } } @@ -242,8 +268,40 @@ void BtPhysTest::TickGame(float seconds) m_simulation->TickContext(seconds); - m_camera->SetTarget(vec3(.0f)); - m_camera->SetPosition(vec3(-30.0f, 20.0f, .0f)); + vec3 barycenter = vec3(.0f); + float factor = .0f; + 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 - vec3(.0f, 50.f, .0f); + vec3 CenterToCam = m_camera->m_position - vec3(.0f, 50.f, .0f); + if (dot(CenterToCam, CenterToGround) > .0f) + PhysObj->SetRender(false); + else + PhysObj->SetRender(true); + + barycenter += GroundMat.v3.xyz; + factor += 1.f; + } + + barycenter /= factor; + + for (int i = 0; i < m_ground_list.Count(); i++) + { + PhysicsObject* PhysObj = m_ground_list[i]; + + mat4 GroundMat = PhysObj->GetTransform(); + mat4 CenterMx = mat4::translate(barycenter); + //GroundMat = inverse(CenterMx) * GroundMat; + //GroundMat = CenterMx * GroundMat; + //mat4(quat::rotate(seconds * 10.0f, vec3(0, 1, 0))) * CenterMx; + //PhysObj->SetTransform(GroundMat.v3.xyz, quat(GroundMat)); + } + + m_camera->SetTarget(barycenter); + m_camera->SetPosition(vec3(-40.0f, 60.0f, -40.0f)); #if 0 ///step the simulation @@ -322,7 +380,12 @@ void BtPhysTest::TickDraw(float seconds) BtPhysTest::~BtPhysTest() { Ticker::Unref(m_camera); - Ticker::Unref(m_ground_object); + while (m_ground_list.Count()) + { + PhysicsObject* CurPop = m_ground_list.Last(); + m_ground_list.Pop(); + Ticker::Unref(CurPop); + } while (m_physobj_list.Count()) { PhysicsObject* CurPop = m_physobj_list.Last(); diff --git a/test/BtPhysTest.h b/test/BtPhysTest.h index b4f421be..ce57610f 100644 --- a/test/BtPhysTest.h +++ b/test/BtPhysTest.h @@ -26,7 +26,7 @@ private: lol::phys::Simulation* m_simulation; Array m_physobj_list; - PhysicsObject* m_ground_object; + Array m_ground_list; #if 0 EasyMesh m_ground_mesh; diff --git a/test/PhysicObject.h b/test/PhysicObject.h index 3cce304b..07ba19d5 100644 --- a/test/PhysicObject.h +++ b/test/PhysicObject.h @@ -21,19 +21,20 @@ using namespace lol::phys; class PhysicsObject : public WorldEntity { public: - PhysicsObject(Simulation* new_sim) - : m_ready(false) + PhysicsObject(Simulation* new_sim, const vec3 &base_location, const quat &base_rotation) + : m_ready(false), m_should_render(true) { - m_mesh.Compile("[sc#add afcb110 1 110 -.1]"); - vec3 BoxSize = vec3(110.f, 1.f, 110.f); + m_mesh.Compile("[sc#add afcb60 1 60 -.1]"); + vec3 BoxSize = vec3(60.f, 1.f, 60.f); m_physics.SetShapeToBox(BoxSize); m_physics.SetMass(.0f); + m_physics.SetTransform(base_location, base_rotation); m_physics.InitBodyToRigid(); m_physics.AddToSimulation(new_sim); } PhysicsObject(Simulation* new_sim, float base_mass, const vec3 &base_location) - : m_ready(false) + : m_ready(false), m_should_render(true) { Array MeshRand; @@ -96,11 +97,26 @@ public: m_physics.SetShapeToCapsule(BoxSize.x, BoxSize.y); m_physics.SetMass(base_mass); - m_physics.SetBaseTransform(base_location); + m_physics.SetTransform(base_location); m_physics.InitBodyToRigid(); m_physics.AddToSimulation(new_sim); } + 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); + } + + lol::mat4 GetTransform() + { + return m_physics.GetTransform(); + } + + void SetRender(bool should_render) + { + m_should_render = should_render; + } + ~PhysicsObject() { } @@ -123,7 +139,8 @@ protected: m_ready = true; } - m_mesh.Render(m_physics.GetTransform()); + if (m_should_render) + m_mesh.Render(m_physics.GetTransform()); } private: @@ -132,6 +149,7 @@ private: EasyPhysics m_physics; bool m_ready; + bool m_should_render; }; #endif /* __PHYSICOBJECT_H__ */ diff --git a/test/Physics/EasyPhysics.cpp b/test/Physics/EasyPhysics.cpp index 50756db3..074ef85c 100644 --- a/test/Physics/EasyPhysics.cpp +++ b/test/Physics/EasyPhysics.cpp @@ -102,10 +102,18 @@ void EasyPhysics::SetShapeToCapsule(float radius, float height) //------------------------------------------------------------------------- //Base Location/Rotation setup //-- -void EasyPhysics::SetBaseTransform(const lol::vec3& base_location, const lol::quat& base_rotation) +void EasyPhysics::SetTransform(const lol::vec3& base_location, const lol::quat& base_rotation) { if (m_motion_state) - m_motion_state->setWorldTransform(btTransform(LOL2BT_QUAT(base_rotation), LOL2BT_VEC3(base_location))); + { + if (m_mass != .0f) + m_motion_state->setWorldTransform(btTransform(LOL2BT_QUAT(base_rotation), LOL2BT_VEC3(base_location))); + else + { + m_rigid_body->setWorldTransform(btTransform(LOL2BT_QUAT(base_rotation), LOL2BT_VEC3(base_location))); + m_motion_state->setWorldTransform(m_rigid_body->getWorldTransform()); + } + } else m_motion_state = new btDefaultMotionState(btTransform(LOL2BT_QUAT(base_rotation), LOL2BT_VEC3(base_location))); } @@ -113,7 +121,6 @@ void EasyPhysics::SetBaseTransform(const lol::vec3& base_location, const lol::qu //------------------------------------------------------------------------- //Mass related functions //-- - //Set Shape functions void EasyPhysics::SetMass(float mass) { @@ -138,7 +145,7 @@ void EasyPhysics::InitBodyToRigid() SetLocalInertia(m_mass); if (!m_motion_state) - SetBaseTransform(vec3(.0f)); + SetTransform(vec3(.0f)); btRigidBody::btRigidBodyConstructionInfo NewInfos(m_mass, m_motion_state, m_collision_shape, m_local_inertia); m_rigid_body = new btRigidBody(NewInfos); m_collision_object = m_rigid_body; diff --git a/test/Physics/EasyPhysics.h b/test/Physics/EasyPhysics.h index 3f8b3984..f0a62bfb 100644 --- a/test/Physics/EasyPhysics.h +++ b/test/Physics/EasyPhysics.h @@ -42,7 +42,7 @@ public: void SetShapeToCylinder(lol::vec3& cyl_size); void SetShapeToCapsule(float radius, float height); - void SetBaseTransform(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))); void SetMass(float mass); void InitBodyToRigid(); void AddToSimulation(class Simulation* current_simulation); @@ -72,7 +72,7 @@ public: void SetShapeToCylinder(lol::vec3& cyl_size) { } void SetShapeToCapsule(float radius, float height) { } - void SetBaseTransform(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))) { } void SetMass(float mass) { } void InitBodyToRigid() { } void AddToSimulation(class Simulation* current_simulation) { }