浏览代码

Added walls and better context to the physics.

legacy
Benjamin ‘Touky’ Huet touky 13 年前
父节点
当前提交
e82c65aa18
共有 5 个文件被更改,包括 112 次插入24 次删除
  1. +73
    -10
      test/BtPhysTest.cpp
  2. +1
    -1
      test/BtPhysTest.h
  3. +25
    -7
      test/PhysicObject.h
  4. +11
    -4
      test/Physics/EasyPhysics.cpp
  5. +2
    -2
      test/Physics/EasyPhysics.h

+ 73
- 10
test/BtPhysTest.cpp 查看文件

@@ -74,16 +74,42 @@ BtPhysTest::BtPhysTest(bool editor)
vec3 NewGravity = vec3(.0f, -10.0f, .0f); vec3 NewGravity = vec3(.0f, -10.0f, .0f);
m_simulation->SetGravity(NewGravity); 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_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 #if 0
///step the simulation ///step the simulation
@@ -322,7 +380,12 @@ void BtPhysTest::TickDraw(float seconds)
BtPhysTest::~BtPhysTest() BtPhysTest::~BtPhysTest()
{ {
Ticker::Unref(m_camera); 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()) while (m_physobj_list.Count())
{ {
PhysicsObject* CurPop = m_physobj_list.Last(); PhysicsObject* CurPop = m_physobj_list.Last();


+ 1
- 1
test/BtPhysTest.h 查看文件

@@ -26,7 +26,7 @@ private:


lol::phys::Simulation* m_simulation; lol::phys::Simulation* m_simulation;
Array<PhysicsObject*> m_physobj_list; Array<PhysicsObject*> m_physobj_list;
PhysicsObject* m_ground_object;
Array<PhysicsObject*> m_ground_list;


#if 0 #if 0
EasyMesh m_ground_mesh; EasyMesh m_ground_mesh;


+ 25
- 7
test/PhysicObject.h 查看文件

@@ -21,19 +21,20 @@ using namespace lol::phys;
class PhysicsObject : public WorldEntity class PhysicsObject : public WorldEntity
{ {
public: 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.SetShapeToBox(BoxSize);
m_physics.SetMass(.0f); m_physics.SetMass(.0f);
m_physics.SetTransform(base_location, base_rotation);
m_physics.InitBodyToRigid(); m_physics.InitBodyToRigid();
m_physics.AddToSimulation(new_sim); m_physics.AddToSimulation(new_sim);
} }


PhysicsObject(Simulation* new_sim, float base_mass, const vec3 &base_location) PhysicsObject(Simulation* new_sim, float base_mass, const vec3 &base_location)
: m_ready(false)
: m_ready(false), m_should_render(true)
{ {
Array<char *> MeshRand; Array<char *> MeshRand;


@@ -96,11 +97,26 @@ public:
m_physics.SetShapeToCapsule(BoxSize.x, BoxSize.y); m_physics.SetShapeToCapsule(BoxSize.x, BoxSize.y);


m_physics.SetMass(base_mass); m_physics.SetMass(base_mass);
m_physics.SetBaseTransform(base_location);
m_physics.SetTransform(base_location);
m_physics.InitBodyToRigid(); m_physics.InitBodyToRigid();
m_physics.AddToSimulation(new_sim); 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() ~PhysicsObject()
{ {
} }
@@ -123,7 +139,8 @@ protected:
m_ready = true; m_ready = true;
} }


m_mesh.Render(m_physics.GetTransform());
if (m_should_render)
m_mesh.Render(m_physics.GetTransform());
} }


private: private:
@@ -132,6 +149,7 @@ private:
EasyPhysics m_physics; EasyPhysics m_physics;


bool m_ready; bool m_ready;
bool m_should_render;
}; };


#endif /* __PHYSICOBJECT_H__ */ #endif /* __PHYSICOBJECT_H__ */


+ 11
- 4
test/Physics/EasyPhysics.cpp 查看文件

@@ -102,10 +102,18 @@ void EasyPhysics::SetShapeToCapsule(float radius, float height)
//------------------------------------------------------------------------- //-------------------------------------------------------------------------
//Base Location/Rotation setup //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) 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 else
m_motion_state = new btDefaultMotionState(btTransform(LOL2BT_QUAT(base_rotation), LOL2BT_VEC3(base_location))); 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 //Mass related functions
//-- //--

//Set Shape functions //Set Shape functions
void EasyPhysics::SetMass(float mass) void EasyPhysics::SetMass(float mass)
{ {
@@ -138,7 +145,7 @@ void EasyPhysics::InitBodyToRigid()


SetLocalInertia(m_mass); SetLocalInertia(m_mass);
if (!m_motion_state) if (!m_motion_state)
SetBaseTransform(vec3(.0f));
SetTransform(vec3(.0f));
btRigidBody::btRigidBodyConstructionInfo NewInfos(m_mass, m_motion_state, m_collision_shape, m_local_inertia); btRigidBody::btRigidBodyConstructionInfo NewInfos(m_mass, m_motion_state, m_collision_shape, m_local_inertia);
m_rigid_body = new btRigidBody(NewInfos); m_rigid_body = new btRigidBody(NewInfos);
m_collision_object = m_rigid_body; m_collision_object = m_rigid_body;


+ 2
- 2
test/Physics/EasyPhysics.h 查看文件

@@ -42,7 +42,7 @@ public:
void SetShapeToCylinder(lol::vec3& cyl_size); void SetShapeToCylinder(lol::vec3& cyl_size);
void SetShapeToCapsule(float radius, float height); 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 SetMass(float mass);
void InitBodyToRigid(); void InitBodyToRigid();
void AddToSimulation(class Simulation* current_simulation); void AddToSimulation(class Simulation* current_simulation);
@@ -72,7 +72,7 @@ public:
void SetShapeToCylinder(lol::vec3& cyl_size) { } void SetShapeToCylinder(lol::vec3& cyl_size) { }
void SetShapeToCapsule(float radius, float height) { } 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 SetMass(float mass) { }
void InitBodyToRigid() { } void InitBodyToRigid() { }
void AddToSimulation(class Simulation* current_simulation) { } void AddToSimulation(class Simulation* current_simulation) { }


正在加载...
取消
保存