ソースを参照

Small name refactor & correct Kinematic integration.

+ Constraint hollow class.
legacy
Benjamin ‘Touky’ Huet touky 12年前
コミット
38f0fd5fc3
5個のファイルの変更89行の追加58行の削除
  1. +2
    -2
      test/PhysicObject.h
  2. +24
    -33
      test/Physics/EasyPhysics.cpp
  3. +44
    -14
      test/Physics/EasyPhysics.h
  4. +4
    -0
      test/Physics/LolBtPhysicsIntegration.h
  5. +15
    -9
      test/Physics/LolPhysics.h

+ 2
- 2
test/PhysicObject.h ファイルの表示

@@ -30,7 +30,7 @@ public:
m_physics.SetShapeToBox(BoxSize);
m_physics.SetMass(.0f);
m_physics.SetTransform(base_location, base_rotation);
m_physics.InitBodyToRigid();
m_physics.InitBodyToRigid(true);
m_physics.AddToSimulation(new_sim);
}

@@ -164,7 +164,7 @@ protected:
private:
//Base datas
EasyMesh m_mesh;
EasyPhysics m_physics;
EasyPhysic m_physics;

bool m_ready;
bool m_should_render;


+ 24
- 33
test/Physics/EasyPhysics.cpp ファイルの表示

@@ -30,7 +30,7 @@ namespace phys

#ifdef HAVE_PHYS_USE_BULLET

EasyPhysics::EasyPhysics() :
EasyPhysic::EasyPhysic() :
m_collision_object(NULL),
m_rigid_body(NULL),
m_collision_shape(NULL),
@@ -42,7 +42,7 @@ EasyPhysics::EasyPhysics() :
{
}

EasyPhysics::~EasyPhysics()
EasyPhysic::~EasyPhysic()
{
m_rigid_body = NULL;
delete m_collision_object;
@@ -54,7 +54,7 @@ EasyPhysics::~EasyPhysics()
//Set Shape functions
//--

void EasyPhysics::SetShapeTo(btCollisionShape* collision_shape)
void EasyPhysic::SetShapeTo(btCollisionShape* collision_shape)
{
bool bReinitToRigidBody = false;
if (m_rigid_body)
@@ -72,60 +72,43 @@ void EasyPhysics::SetShapeTo(btCollisionShape* collision_shape)
}

//Box Shape support
void EasyPhysics::SetShapeToBox(lol::vec3& box_size)
void EasyPhysic::SetShapeToBox(lol::vec3& box_size)
{
vec3 new_box_size = box_size * LOL2BT_UNIT * LOL2BT_SIZE;
SetShapeTo(new btBoxShape(LOL2BT_VEC3(new_box_size)));
}

void EasyPhysics::SetShapeToSphere(float radius)
void EasyPhysic::SetShapeToSphere(float radius)
{
SetShapeTo(new btSphereShape(radius * LOL2BT_UNIT * LOL2BT_SIZE));
}

void EasyPhysics::SetShapeToCone(float radius, float height)
void EasyPhysic::SetShapeToCone(float radius, float height)
{
SetShapeTo(new btConeShape( radius * LOL2BT_UNIT,
height * LOL2BT_UNIT));
}

void EasyPhysics::SetShapeToCylinder(lol::vec3& cyl_size)
void EasyPhysic::SetShapeToCylinder(lol::vec3& cyl_size)
{
vec3 new_cyl_size = cyl_size * LOL2BT_UNIT;
new_cyl_size.y *= LOL2BT_SIZE;
SetShapeTo(new btCylinderShape(LOL2BT_VEC3(new_cyl_size)));
}

void EasyPhysics::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));
}

//-------------------------------------------------------------------------
//Bullet collision channel setup
//--
void EasyPhysics::CustomSetCollisionChannel(int NewGroup, int NewMask)
{
if (m_rigid_body)
m_rigid_body->setCollisionFlags(m_collision_mask);
}

//-------------------------------------------------------------------------
//Base Location/Rotation setup
//--
void EasyPhysics::SetTransform(const lol::vec3& base_location, const lol::quat& base_rotation)
void EasyPhysic::SetTransform(const lol::vec3& base_location, const lol::quat& base_rotation)
{
if (m_motion_state)
{
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(btTransform(LOL2BT_QUAT(base_rotation), LOL2BT_VEC3(base_location)));
}
}
m_motion_state->setWorldTransform(btTransform(LOL2BT_QUAT(base_rotation), LOL2BT_VEC3(base_location)));
else
m_motion_state = new btDefaultMotionState(btTransform(LOL2BT_QUAT(base_rotation), LOL2BT_VEC3(base_location)));
}
@@ -134,7 +117,7 @@ void EasyPhysics::SetTransform(const lol::vec3& base_location, const lol::quat&
//Mass related functions
//--
//Set Shape functions
void EasyPhysics::SetMass(float mass)
void EasyPhysic::SetMass(float mass)
{
m_mass = mass;

@@ -150,7 +133,7 @@ void EasyPhysics::SetMass(float mass)
//--

//Init to rigid body
void EasyPhysics::InitBodyToRigid()
void EasyPhysic::InitBodyToRigid(bool SetToKinematic)
{
if (m_collision_object)
delete m_collision_object;
@@ -158,12 +141,19 @@ void EasyPhysics::InitBodyToRigid()
SetLocalInertia(m_mass);
if (!m_motion_state)
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;

if (m_mass == .0f && SetToKinematic)
{
m_rigid_body->setActivationState(DISABLE_DEACTIVATION);
m_rigid_body->setCollisionFlags(m_rigid_body->getCollisionFlags() | btCollisionObject::CF_KINEMATIC_OBJECT);
}
}

void EasyPhysics::AddToSimulation(class Simulation* current_simulation)
void EasyPhysic::AddToSimulation(class Simulation* current_simulation)
{
btDiscreteDynamicsWorld* dynamics_world = current_simulation->GetWorld();
if (m_rigid_body)
@@ -182,7 +172,7 @@ void EasyPhysics::AddToSimulation(class Simulation* current_simulation)
//Getter functons
//--

mat4 EasyPhysics::GetTransform()
mat4 EasyPhysic::GetTransform()
{
m_local_to_world = lol::mat4(1.0f);
if (m_rigid_body && m_motion_state)
@@ -197,14 +187,15 @@ mat4 EasyPhysics::GetTransform()
}

//Set Local Inertia
void EasyPhysics::SetLocalInertia(float mass)
void EasyPhysic::SetLocalInertia(float mass)
{
if (mass != .0f)
m_collision_shape->calculateLocalInertia(mass, m_local_inertia);
else
m_local_inertia = btVector3(.0f, .0f, .0f);
}
#endif

#endif // HAVE_PHYS_USE_BULLET

} /* namespace phys */



+ 44
- 14
test/Physics/EasyPhysics.h ファイルの表示

@@ -10,7 +10,7 @@
//

//
// The EasyPhysics class
// The EasyPhysic class
// ------------------
//

@@ -29,12 +29,14 @@ namespace lol
namespace phys
{

class EasyPhysics
class EasyPhysic
{

#ifdef HAVE_PHYS_USE_BULLET

public:
EasyPhysics();
~EasyPhysics();
EasyPhysic();
~EasyPhysic();

void SetShapeToBox(lol::vec3& box_size);
void SetShapeToSphere(float radius);
@@ -42,10 +44,10 @@ public:
void SetShapeToCylinder(lol::vec3& cyl_size);
void SetShapeToCapsule(float radius, float height);

void CustomSetCollisionChannel(int NewGroup, int NewMask);
bool CanChangeCollisionChannel() { return (m_rigid_body == NULL); }
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 InitBodyToRigid(bool ZeroMassIsKinematic=false);
void AddToSimulation(class Simulation* current_simulation);
mat4 GetTransform();

@@ -60,9 +62,11 @@ protected:

btCollisionShape* m_collision_shape;
btMotionState* m_motion_state;
#else

#else // NO PHYSIC IMPLEMENTATION

public:
EasyPhysics() { }
EasyPhysic() { }

void SetShapeToBox(lol::vec3& BoxSize) { }
void SetShapeToSphere(float radius) { }
@@ -70,22 +74,27 @@ public:
void SetShapeToCylinder(lol::vec3& cyl_size) { }
void SetShapeToCapsule(float radius, float height) { }

void CustomSetCollisionChannel(int NewGroup, int NewMask) { }
bool CanChangeCollisionChannel() { return true; }
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) { }
mat4 GetTransform() { return mat4(1.0f); }
#endif

#endif // PHYSIC IMPLEMENTATION

public:
//Sets the collision Group & Mask.
//Mask can change at runtime, not group !
void SetCollisionChannel(int NewGroup, int NewMask)
bool SetCollisionChannel(int NewGroup, int NewMask)
{
m_collision_group = (1<<NewGroup);
m_collision_mask = NewMask;
CustomSetCollisionChannel(NewGroup, NewMask);
if (CanChangeCollisionChannel())
{
m_collision_group = (1<<NewGroup);
m_collision_mask = NewMask;
return true;
}
return false;
}
int GetCollisionGroup() { return m_collision_group; }
int GetCollisionMask() { return m_collision_mask; }
@@ -97,6 +106,27 @@ protected:
int m_collision_mask;
};

class EasyConstraint
{
EasyConstraint()
{
//btPoint2PointConstraint(bA, bB, PivotA, PivotB)
//btHingeConstraint(bA, bB, TransfA, TransfB, UseRefA)
//btSliderConstraint(bA, bB, TransfA, TransfB, UseRefA)
//btConeTwistConstraint(bA, bB, TransfA, TransfB)
//btGeneric6DofConstraint(bA, bB, TransfA, TransfB, UseRefA)
}

#ifdef HAVE_PHYS_USE_BULLET

btTypedConstraint* m_typed_constraint;

#else // NO PHYSIC IMPLEMENTATION

#endif // PHYSIC IMPLEMENTATION

};

} /* namespace phys */

} /* namespace lol */


+ 4
- 0
test/Physics/LolBtPhysicsIntegration.h ファイルの表示

@@ -20,6 +20,8 @@
namespace lol
{

#ifdef HAVE_PHYS_USE_BULLET

#define LOL2BT_UNIT 1.0f
#define BT2LOL_UNIT 1.0f

@@ -32,6 +34,8 @@ namespace lol
#define LOL2BT_QUAT(ELEMENT) btQuaternion((ELEMENT).x, (ELEMENT).y, (ELEMENT).z, (ELEMENT).w)
#define BT2LOL_QUAT(ELEMENT) lol::quat((ELEMENT).getW(), BT2LOL_VEC3((ELEMENT).getAxis())

#endif // HAVE_PHYS_USE_BULLET

} /* namespace lol */

#endif /* __LOLBTPHYSICSINTEGRATION_H__ */


+ 15
- 9
test/Physics/LolPhysics.h ファイルの表示

@@ -23,8 +23,6 @@ namespace phys

class Simulation : public Entity
{
friend class EasyPhysics;

public:
Simulation() :
m_broadphase(0),
@@ -109,7 +107,9 @@ private:
btSequentialImpulseConstraintSolver* m_solver;
// The world.
btDiscreteDynamicsWorld* m_dynamics_world;
#else

#else // NO PHYSIC IMPLEMENTATION

public:
void Init() { }
void TickGame(float seconds) { }
@@ -118,7 +118,8 @@ private:
void CustomSetContinuousDetection(bool ShouldUseCCD) { }
void CustomSetGravity(vec3 &NewGravity) { }
void CustomSetTimestep(float NewTimestep) { }
#endif //HAVE_PHYS_USE_BULLET

#endif // PHYSIC IMPLEMENTATION

public:
//Main logic :
@@ -150,13 +151,18 @@ public:
}

private:
//Adds the given EasyPhysics to the correct list.
void AddToDynamic(EasyPhysics* dynamic_EP) { m_dynamic_list << dynamic_EP; }
void AddToStatic(EasyPhysics* static_EP) { m_static_list << static_EP; }
friend class EasyPhysic;
friend class EasyConstraint;

//Adds the given EasyPhysic to the correct list.
void AddToDynamic(EasyPhysic* NewEPDynamic) { m_dynamic_list << NewEPDynamic; }
void AddToStatic(EasyPhysic* NewEPStatic) { m_static_list << NewEPStatic; }
void AddToConstraint(EasyConstraint* NewEC) { m_constraint_list << NewEC; }

//Easy Physics body List
Array<EasyPhysics*> m_dynamic_list;
Array<EasyPhysics*> m_static_list;
Array<EasyPhysic*> m_dynamic_list;
Array<EasyPhysic*> m_static_list;
Array<EasyConstraint*> m_constraint_list;

//Easy Physics data storage
float m_timestep;


読み込み中…
キャンセル
保存