From ebbed2988abb8a8bd74606e64edc7543c099ff36 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benjamin=20=E2=80=98Touky=E2=80=99=20Huet?= Date: Mon, 9 Jul 2012 13:12:57 +0000 Subject: [PATCH] BtPhysTest : Container box now moves. --- test/BtPhysTest.cpp | 79 ++++++++++++++++++++++++------------ test/PhysicObject.h | 2 +- test/Physics/EasyPhysics.cpp | 2 +- test/Physics/LolPhysics.h | 6 +++ 4 files changed, 61 insertions(+), 28 deletions(-) diff --git a/test/BtPhysTest.cpp b/test/BtPhysTest.cpp index 41d24252..ff31da0c 100644 --- a/test/BtPhysTest.cpp +++ b/test/BtPhysTest.cpp @@ -73,23 +73,26 @@ BtPhysTest::BtPhysTest(bool editor) m_simulation->InitContext(); vec3 NewGravity = vec3(.0f, -10.0f, .0f); m_simulation->SetGravity(NewGravity); + m_simulation->SetContinuousDetection(true); - float offset = 30.f; + float offset = 29.5f; vec3 pos_offset = vec3(.0f, 30.f, .0f); for (int i=0; i < 6; i++) { + vec3 NewPosition = vec3(.0f); + quat NewRotation = quat(1.f); + + PhysicsObject* NewPhyobj = new PhysicsObject(m_simulation, NewPosition, NewRotation); + int idx = i/2; - vec3 NewPosition = pos_offset; + 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; + axis[2 - idx] = 1; NewRotation = quat::rotate(90.f, axis); } @@ -98,14 +101,14 @@ BtPhysTest::BtPhysTest(bool editor) m_ground_list << NewPhyobj; } - for (int x=0; x < 5; x++) + for (int x=0; x < 6; x++) { - for (int y=0; y < 5; y++) + for (int y=0; y < 6; y++) { - for (int z=0; z < 4; z++) + for (int z=0; z < 5; z++) { PhysicsObject* new_physobj = new PhysicsObject(m_simulation, 10.f, - vec3(-20.f, 40.f, -20.f) + + vec3(-20.f, 20.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); @@ -268,40 +271,64 @@ void BtPhysTest::TickGame(float seconds) m_simulation->TickContext(seconds); - vec3 barycenter = vec3(.0f); + vec3 GroundBarycenter = vec3(.0f); + vec3 PhysObjBarycenter = 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(); + + GroundBarycenter += GroundMat.v3.xyz; + factor += 1.f; + } + + GroundBarycenter /= factor; + 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) + vec3 CenterToGround = GroundMat.v3.xyz - GroundBarycenter; + vec3 CenterToCam = m_camera->m_position - GroundBarycenter; + vec3 CamDir = m_camera->m_position - m_camera->GetTarget(); + + if (dot(normalize(CenterToGround), normalize(CenterToCam)) > .0f && + dot(normalize(CenterToGround), normalize(CamDir)) > .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)); + mat4 CenterMx = mat4::translate(GroundBarycenter); + GroundMat = inverse(CenterMx) * GroundMat; + GroundMat = CenterMx * + mat4(quat::fromeuler_xyz(vec3(.0f, 20.f, 20.0f) * seconds)) + * GroundMat; + PhysObj->SetTransform(GroundMat.v3.xyz, quat(GroundMat)); } - m_camera->SetTarget(barycenter); - m_camera->SetPosition(vec3(-40.0f, 60.0f, -40.0f)); + PhysObjBarycenter = vec3(.0f); + for (int i = 0; i < m_physobj_list.Count(); i++) + { + PhysicsObject* PhysObj = m_physobj_list[i]; + mat4 GroundMat = PhysObj->GetTransform(); + + PhysObjBarycenter += GroundMat.v3.xyz; + factor += 1.f; + } + + PhysObjBarycenter /= factor; + + m_camera->SetTarget(PhysObjBarycenter); + m_camera->SetPosition(GroundBarycenter + normalize(GroundBarycenter - PhysObjBarycenter) * 60.0f); #if 0 ///step the simulation diff --git a/test/PhysicObject.h b/test/PhysicObject.h index 07ba19d5..94901d90 100644 --- a/test/PhysicObject.h +++ b/test/PhysicObject.h @@ -24,7 +24,7 @@ public: PhysicsObject(Simulation* new_sim, const vec3 &base_location, const quat &base_rotation) : m_ready(false), m_should_render(true) { - m_mesh.Compile("[sc#add afcb60 1 60 -.1]"); + m_mesh.Compile("[sc#ddd afcb60 1 60 -.1]"); vec3 BoxSize = vec3(60.f, 1.f, 60.f); m_physics.SetShapeToBox(BoxSize); m_physics.SetMass(.0f); diff --git a/test/Physics/EasyPhysics.cpp b/test/Physics/EasyPhysics.cpp index 074ef85c..20dd63fd 100644 --- a/test/Physics/EasyPhysics.cpp +++ b/test/Physics/EasyPhysics.cpp @@ -111,7 +111,7 @@ void EasyPhysics::SetTransform(const lol::vec3& base_location, const lol::quat& else { m_rigid_body->setWorldTransform(btTransform(LOL2BT_QUAT(base_rotation), LOL2BT_VEC3(base_location))); - m_motion_state->setWorldTransform(m_rigid_body->getWorldTransform()); + m_motion_state->setWorldTransform(btTransform(LOL2BT_QUAT(base_rotation), LOL2BT_VEC3(base_location))); } } else diff --git a/test/Physics/LolPhysics.h b/test/Physics/LolPhysics.h index 2c7d6346..cb6ca67a 100644 --- a/test/Physics/LolPhysics.h +++ b/test/Physics/LolPhysics.h @@ -56,6 +56,12 @@ public: m_dynamics_world = new btDiscreteDynamicsWorld(m_dispatcher, m_broadphase, m_solver, m_collision_configuration); } + void SetContinuousDetection(bool ShouldUseCCD) + { + if (m_dynamics_world) + m_dynamics_world->getDispatchInfo().m_useContinuous = ShouldUseCCD; + } + void SetGravity(vec3 &NewGravity) { if (m_dynamics_world)