|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285 |
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- #if !defined __BULLETCHARACTERCONTROLLER_BULLETCHARACTERCONTROLLER_H__
- #define __BULLETCHARACTERCONTROLLER_BULLETCHARACTERCONTROLLER_H__
-
- #include "core.h"
- #include "easyphysics.h"
-
-
- #define USE_LOL_CTRLR_CHARAC
-
- namespace lol
- {
-
- namespace phys
- {
-
- #ifdef USE_LOL_CTRLR_CHARAC
-
-
- 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)
- {
-
- if (ConvexResult.m_hitCollisionObject == m_me)
- return btScalar(1.f);
-
- vec3 WorldHitNomal(.0f);
- if (NormalInWorld)
- WorldHitNomal = BT2LOL_VEC3(ConvexResult.m_hitNormalLocal);
- else
- {
- btVector3 TmpWorldHitNormal = ConvexResult.m_hitCollisionObject->getWorldTransform().getBasis() * ConvexResult.m_hitNormalLocal;
- WorldHitNomal = BT2LOL_VEC3(TmpWorldHitNormal);
- }
-
- float DotUp = dot(m_up, WorldHitNomal);
-
- if (DotUp < m_min_slope_dot)
- return btScalar(1.f);
-
-
- return ClosestConvexResultCallback::addSingleResult(ConvexResult, NormalInWorld);
- }
- protected:
- btCollisionObject* m_me;
- const vec3 m_up;
- float m_min_slope_dot;
- };
-
-
-
-
- class BulletKinematicCharacterController : public btActionInterface
- {
- public:
- BulletKinematicCharacterController(btPairCachingGhostObject* NewGhostObject, btConvexShape* NewConvexShape, float NewStepHeight, int NewUpAxis=1)
- {
- m_convex_shape = NewConvexShape;
- m_i_up_axis = NewUpAxis;
- m_ghost_object = NewGhostObject;
- m_step_height = NewStepHeight;
-
- m_added_margin = 0.02f;
- m_walk_direction = vec3(.0f, .0f, .0f);
- m_do_gobject_sweep_test = true;
- m_turn_angle = .0f;
- m_use_walk_direction = false;
- m_velocity_time_interval = .0f;
- m_vertical_velocity = .0f;
- m_vertical_offset = .0f;
- m_f_gravity = 9.8f * 3.f;
- m_fall_speed = 55.f;
- m_jump_speed = 10.f;
- m_was_on_ground = false;
- m_was_jumping = false;
- SetMaxSlope(45.f);
- }
- ~BulletKinematicCharacterController() { }
-
- protected:
-
- static vec3* GetUpAxisDirections()
- {
- static vec3 sUpAxisDirection[3] = { vec3(1.0f, 0.0f, 0.0f), vec3(0.0f, 1.0f, 0.0f), vec3(0.0f, 0.0f, 1.0f) };
-
- return sUpAxisDirection;
- }
-
-
-
-
-
-
- vec3 GetReflectedDir(const vec3& Direction, const vec3& Normal)
- {
- return Direction - (2.f * dot(Direction, Normal) * Normal);
- }
-
- vec3 ProjectDirOnNorm(const vec3& Direction, const vec3& Normal)
- {
- return Normal * dot(Direction, Normal);
- }
-
- vec3 ProjectDirOnNormPerpindicular(const vec3& Direction, const vec3& Normal)
- {
- return Direction - ProjectDirOnNorm(Direction, Normal);
- }
-
- btPairCachingGhostObject* GetGhostObject()
- {
- return m_ghost_object;
- }
-
-
- bool RecoverFromPenetration(btCollisionWorld* CollisionWorld);
- void UpdateTargetOnHit(const vec3& hit_normal, float TangentMag = .0f, float NormalMag = 1.f);
- void DoMove(btCollisionWorld* CollisionWorld, const vec3& MoveStep, float DeltaTime);
-
- public:
-
- virtual void updateAction(btCollisionWorld* CollisionWorld, float deltaTime)
- {
- PreStep(CollisionWorld);
- PlayerStep(CollisionWorld, deltaTime);
- }
-
-
- void PreStep(btCollisionWorld* CollisionWorld);
- void PlayerStep(btCollisionWorld* CollisionWorld, float DeltaTime);
-
-
- void debugDraw(btIDebugDraw* debugDrawer) { }
-
- void SetUpAxis(int NewAxis)
- {
- if (NewAxis < 0)
- NewAxis = 0;
- if (NewAxis > 2)
- NewAxis = 2;
- m_i_up_axis = NewAxis;
- }
-
-
-
-
-
-
-
- virtual void SetWalkDirection(const vec3& walkDirection)
- {
- m_use_walk_direction = true;
- m_walk_direction = walkDirection;
- m_normalized_direction = normalize(m_walk_direction);
- }
-
-
-
-
-
-
- virtual void SetVelocityForTimeInterval(const vec3& velocity, float timeInterval)
- {
- m_use_walk_direction = false;
- m_walk_direction = velocity;
- m_normalized_direction = normalize(m_walk_direction);
- m_velocity_time_interval = timeInterval;
- }
-
-
- void Reset() { }
- void Warp(const vec3& NewOrigin)
- {
- btTransform NewTransform;
- NewTransform.setIdentity();
- NewTransform.setOrigin(LOL2BTU_VEC3(NewOrigin));
- m_ghost_object->setWorldTransform(NewTransform);
- }
-
-
-
-
- void SetFallSpeed(float NewFallSpeed) { m_fall_speed = NewFallSpeed; }
- void SetJumpSpeed(float NewJumpSpeed) { m_jump_speed = NewJumpSpeed; }
- void SetMaxJumpHeight(float NewMaxJumpHeight) { m_max_jump_height = NewMaxJumpHeight; }
-
-
- bool CanJump() const { return OnGround(); }
- void Jump();
-
-
- void SetGravity(float NewGravity) { m_f_gravity = NewGravity; }
- float GetGravity() const { return m_f_gravity; }
-
-
-
- void SetMaxSlope(float NewSlopeRadians) { m_max_slope_radians = NewSlopeRadians; m_max_slope_cosine = lol::cos(NewSlopeRadians); }
- float GetMaxSlope() const { return m_max_slope_radians; }
-
- void SetUseGhostSweepTest(bool UseGObjectSweepTest) { m_do_gobject_sweep_test = UseGObjectSweepTest; }
-
- bool OnGround() const { return m_vertical_velocity == .0f && m_vertical_offset == .0f; }
-
- private:
-
- btPairCachingGhostObject* m_ghost_object;
- btConvexShape* m_convex_shape;
-
-
- btManifoldArray m_manifold_array;
-
- float m_half_height;
- float m_velocity_time_interval;
- float m_vertical_velocity;
- float m_vertical_offset;
- float m_fall_speed;
- float m_jump_speed;
- float m_max_jump_height;
- float m_max_slope_radians;
- float m_max_slope_cosine;
- float m_f_gravity;
- float m_turn_angle;
- float m_step_height;
- float m_added_margin;
-
-
- vec3 m_walk_direction;
- vec3 m_normalized_direction;
-
-
- vec3 m_current_position;
- float m_current_step_offset;
- vec3 m_target_position;
-
- vec3 m_touching_normal;
- bool m_touching_contact;
-
- bool m_was_on_ground;
- bool m_was_jumping;
- bool m_do_gobject_sweep_test;
- bool m_use_walk_direction;
- int m_i_up_axis;
-
-
-
-
-
-
- vec3 m_gravity;
-
-
- vec3 m_velocity;
- };
-
- #endif
-
- }
-
- }
-
- #endif
-
|