Browse Source

Small refactor on the BulletCc.

legacy
Benjamin ‘Touky’ Huet touky 12 years ago
parent
commit
b887ac5355
4 changed files with 58 additions and 62 deletions
  1. +46
    -59
      test/Physics/Include/BulletCharacterController.h
  2. +6
    -0
      test/Physics/Include/EasyCharacterController.h
  3. +0
    -2
      test/Physics/Src/BulletCharacterController.cpp
  4. +6
    -1
      test/Physics/Src/EasyCharacterController.cpp

+ 46
- 59
test/Physics/Include/BulletCharacterController.h View File

@@ -35,44 +35,48 @@ namespace lol
#ifdef USE_LOL_CTRLR_CHARAC
#ifdef HAVE_PHYS_USE_BULLET

//SweepCallback used for Swweep Tests.
class ClosestNotMeConvexResultCallback : public btCollisionWorld::ClosestConvexResultCallback
{
public:
ClosestNotMeConvexResultCallback(btCollisionObject* NewMe, const vec3& NewUp, float MinSlopeDot) :
btCollisionWorld::ClosestConvexResultCallback(LOL2BTU_VEC3(vec3(.0f)), LOL2BTU_VEC3(vec3(.0f))),
m_me(NewMe),
m_up(NewUp),
m_min_slope_dot(MinSlopeDot) { }

virtual btScalar addSingleResult(btCollisionWorld::LocalConvexResult& ConvexResult, bool NormalInWorld)
//SweepCallback used for Swweep Tests.
class ClosestNotMeConvexResultCallback : public btCollisionWorld::ClosestConvexResultCallback
{
//We hit ourselves, FAIL
if (ConvexResult.m_hitCollisionObject == m_me)
return btScalar(1.f);

vec3 WorldHitNomal(.0f);
if (NormalInWorld)
WorldHitNomal = BT2LOL_VEC3(ConvexResult.m_hitNormalLocal);
else //need to transform Normal into worldspace
public:
ClosestNotMeConvexResultCallback(btCollisionObject* NewMe, const vec3& NewUp, float MinSlopeDot) :
btCollisionWorld::ClosestConvexResultCallback(LOL2BTU_VEC3(vec3(.0f)), LOL2BTU_VEC3(vec3(.0f))),
m_me(NewMe),
m_up(NewUp),
m_min_slope_dot(MinSlopeDot)
{
btVector3 TmpWorldHitNormal = ConvexResult.m_hitCollisionObject->getWorldTransform().getBasis() * ConvexResult.m_hitNormalLocal;
WorldHitNomal = BT2LOL_VEC3(TmpWorldHitNormal);
m_collisionFilterGroup = NewMe->getBroadphaseHandle()->m_collisionFilterGroup;
m_collisionFilterMask = NewMe->getBroadphaseHandle()->m_collisionFilterMask;
}

float DotUp = dot(m_up, WorldHitNomal);
//We hit below the accepted slope_dot, FAIL
if (DotUp < m_min_slope_dot)
return btScalar(1.f);

//Continue to next.
return ClosestConvexResultCallback::addSingleResult(ConvexResult, NormalInWorld);
}
protected:
btCollisionObject* m_me;
const vec3 m_up;
float m_min_slope_dot;
};
virtual btScalar addSingleResult(btCollisionWorld::LocalConvexResult& ConvexResult, bool NormalInWorld)
{
//We hit ourselves, FAIL
if (ConvexResult.m_hitCollisionObject == m_me)
return btScalar(1.f);

vec3 WorldHitNomal(.0f);
if (NormalInWorld)
WorldHitNomal = BT2LOL_VEC3(ConvexResult.m_hitNormalLocal);
else //need to transform Normal into worldspace
{
btVector3 TmpWorldHitNormal = ConvexResult.m_hitCollisionObject->getWorldTransform().getBasis() * ConvexResult.m_hitNormalLocal;
WorldHitNomal = BT2LOL_VEC3(TmpWorldHitNormal);
}

float DotUp = dot(m_up, WorldHitNomal);
//We hit below the accepted slope_dot, FAIL
if (DotUp < m_min_slope_dot)
return btScalar(1.f);

//Continue to next.
return ClosestConvexResultCallback::addSingleResult(ConvexResult, NormalInWorld);
}
protected:
btCollisionObject* m_me;
const vec3 m_up;
float m_min_slope_dot;
};

///BulletKinematicCharacterController is an object that supports a sliding motion in a world.
///It uses a ghost object and convex sweep test to test for upcoming collisions. This is combined with discrete collision detection to recover from penetrations.
@@ -80,8 +84,9 @@ namespace lol
class BulletKinematicCharacterController : public btActionInterface
{
public:
BulletKinematicCharacterController(btPairCachingGhostObject* NewGhostObject, btConvexShape* NewConvexShape, float NewStepHeight, int NewUpAxis=1)
BulletKinematicCharacterController(EasyCharacterController* NewCharacter, btPairCachingGhostObject* NewGhostObject, btConvexShape* NewConvexShape, float NewStepHeight, int NewUpAxis=1)
{
m_character = NewCharacter;
m_convex_shape = NewConvexShape;
m_i_up_axis = NewUpAxis;
m_ghost_object = NewGhostObject;
@@ -118,25 +123,13 @@ namespace lol
//--

//Returns the reflection Direction of a ray going 'Direction' hitting a surface with Normal 'Normal' from: http://www-cs-students.stanford.edu/~adityagp/final/node3.html
vec3 GetReflectedDir(const vec3& Direction, const vec3& Normal)
{
return Direction - (2.f * dot(Direction, Normal) * Normal);
}
vec3 GetReflectedDir(const vec3& Direction, const vec3& Normal) { return Direction - (2.f * dot(Direction, Normal) * Normal); }
//Returns the portion of 'direction' that is parallel to 'normal'
vec3 ProjectDirOnNorm(const vec3& Direction, const vec3& Normal)
{
return Normal * dot(Direction, Normal);
}
vec3 ProjectDirOnNorm(const vec3& Direction, const vec3& Normal) { return Normal * dot(Direction, Normal); }
//Returns the portion of 'Direction' that is perpindicular to 'Normal'
vec3 ProjectDirOnNormPerpindicular(const vec3& Direction, const vec3& Normal)
{
return Direction - ProjectDirOnNorm(Direction, Normal);
}
vec3 ProjectDirOnNormPerpindicular(const vec3& Direction, const vec3& Normal) { return Direction - ProjectDirOnNorm(Direction, Normal); }
//Returns Ghost Object. -duh-
btPairCachingGhostObject* GetGhostObject()
{
return m_ghost_object;
}
btPairCachingGhostObject* GetGhostObject() { return m_ghost_object; }

//"Real" war functions
bool RecoverFromPenetration(btCollisionWorld* CollisionWorld);
@@ -158,14 +151,7 @@ namespace lol
///btActionInterface interface : KEEP IN camelCase
void debugDraw(btIDebugDraw* debugDrawer) { }
void SetUpAxis(int NewAxis)
{
if (NewAxis < 0)
NewAxis = 0;
if (NewAxis > 2)
NewAxis = 2;
m_i_up_axis = NewAxis;
}
void SetUpAxis(int NewAxis) { m_i_up_axis = abs(NewAxis) % 3; }

//!!!!!! SHOULD DITCH THAT !!!!!!
//This should probably be called setPositionIncrementPerSimulatorStep.
@@ -229,6 +215,7 @@ namespace lol

private:

EasyCharacterController* m_character;
btPairCachingGhostObject* m_ghost_object;
btConvexShape* m_convex_shape; //is also in m_ghost_object, but it needs to be convex, so we store it here to avoid upcast



+ 6
- 0
test/Physics/Include/EasyCharacterController.h View File

@@ -42,6 +42,8 @@ class EasyCharacterController : public EasyPhysic,

#ifdef HAVE_PHYS_USE_BULLET

friend class BulletKinematicCharacterController;

public:
EasyCharacterController(WorldEntity* NewOwnerEntity) :
EasyPhysic(NewOwnerEntity),
@@ -102,6 +104,10 @@ protected:
vec3 m_walk_velocity;
vec3 m_current_velocity;

//Feedback from Bullet collision :
//The idea is : we tell what to do to the BCC at each Hit
int MoveDidImpact(int HitType, const ClosestNotMeConvexResultCallback& SweepCallback);

#else // NO PHYSIC IMPLEMENTATION

virtual void InitBodyToRigid(bool ZeroMassIsKinematic=false) { }


+ 0
- 2
test/Physics/Src/BulletCharacterController.cpp View File

@@ -138,8 +138,6 @@ void BulletKinematicCharacterController::DoMove(btCollisionWorld* CollisionWorld
vec3 SweepDirNeg(m_current_position - m_target_position);

ClosestNotMeConvexResultCallback SweepCallback(m_ghost_object, SweepDirNeg, .0f);
SweepCallback.m_collisionFilterGroup = GetGhostObject()->getBroadphaseHandle()->m_collisionFilterGroup;
SweepCallback.m_collisionFilterMask = GetGhostObject()->getBroadphaseHandle()->m_collisionFilterMask;

//The sweep test is done with an added margin, so we use it and then discard it
float SavedMargin = m_convex_shape->getMargin();


+ 6
- 1
test/Physics/Src/EasyCharacterController.cpp View File

@@ -62,7 +62,7 @@ void EasyCharacterController::AddToSimulation(class Simulation* current_simulati
delete m_character;

//m_character = new btKinematicCharacterController(m_pair_caching_object, m_convex_shape, m_step_height, m_up_axis);
m_character = new BulletKinematicCharacterController(m_pair_caching_object, m_convex_shape, m_step_height, m_up_axis);
m_character = new BulletKinematicCharacterController(this, m_pair_caching_object, m_convex_shape, m_step_height, m_up_axis);

//Deactivate Character controller basic behaviour.
//m_character->setGravity(.0f);
@@ -146,6 +146,11 @@ void EasyCharacterController::TickGame(float seconds)
}
}

int EasyCharacterController::MoveDidImpact(int HitType, const ClosestNotMeConvexResultCallback& SweepCallback)
{
return 0;
}

#endif // HAVE_PHYS_USE_BULLET

} /* namespace phys */


Loading…
Cancel
Save