first commit

This commit is contained in:
2026-06-22 11:49:35 +02:00
commit d805f2ba86
619 changed files with 126873 additions and 0 deletions

View File

@ -0,0 +1,189 @@
#ifndef CC_PHYSICS_H
#define CC_PHYSICS_H
#include "BulletCollision/CollisionDispatch/btGhostObject.h"
#include "BulletCollision/CollisionShapes/btMultiSphereShape.h"
#include "BulletCollision/CollisionShapes/btCapsuleShape.h"
#include "BulletCollision/BroadphaseCollision/btOverlappingPairCache.h"
#include "BulletCollision/BroadphaseCollision/btCollisionAlgorithm.h"
#include "BulletCollision/CollisionDispatch/btCollisionWorld.h"
#include "LinearMath/btDefaultMotionState.h"
#include "BulletDynamics/Character/btCharacterControllerInterface.h"
//
class btCustomCharacterController : public btCharacterControllerInterface
{
btScalar mHalfHeight;
btPairCachingGhostObject *mGhostObject;
btConvexShape *mConvexShape;
btConvexShape *mStandingConvexShape;
btConvexShape *mDuckingConvexShape;
btCollisionWorld *mCollisionWorld;
btVector3 mStepVelocity;
btScalar mVerticalVelocity;
btScalar mVerticalOffset;
btScalar mFallSpeed;
btScalar mJumpSpeed;
btScalar mMaxJumpHeight;
btScalar mMaxSlopeRadians;
btScalar mMaxSlopeCosine;
btScalar mGravity;
btScalar mTurnAngle;
btScalar mStepHeight;
btScalar mAddedMargin;
btVector3 mWalkDirection;
btVector3 mNormalizedDirection;
btVector3 mCurrentPosition;
btManifoldArray mManifoldArray;
bool mGroundContact;
btVector3 mGroundNormal;
bool mTouchingContact;
bool dbg_step_high;
bool dbg_down_sweep_hit;
void performStep(btScalar dt);
bool SweepAndSlide(btVector3 &from, btVector3 &to, int);
bool mUseWalkDirection;
btScalar mVelocityTimeInterval;
int mUpAxis;
btVector3 mLinearVelocity;
btScalar mMass;
class ClosestNotMeRayResultCallback : public btCollisionWorld::ClosestRayResultCallback
{
btCollisionObject *mMe;
public:
ClosestNotMeRayResultCallback(btCollisionObject * me) : btCollisionWorld::ClosestRayResultCallback(btVector3(0, 0, 0), btVector3(0, 0, 0)), mMe(me) {}
btScalar addSingleResult(btCollisionWorld::LocalRayResult &rayResult, bool normalInWorldSpace)
{
if (rayResult.m_collisionObject == mMe)
return 1.0;
return btCollisionWorld::ClosestRayResultCallback::addSingleResult(rayResult, normalInWorldSpace);
}
};
class ClosestNotMeConvexResultCallback : public btCollisionWorld::ClosestConvexResultCallback
{
btCollisionObject *mMe;
const btVector3 mUp;
btScalar mMinSlopeDot;
public:
ClosestNotMeConvexResultCallback(btCollisionObject *me, const btVector3 &up, btScalar minSlopeDot) : btCollisionWorld::ClosestConvexResultCallback(btVector3(0, 0, 0), btVector3(0, 0, 0)), mMe(me), mUp(up), mMinSlopeDot(minSlopeDot) {}
btScalar addSingleResult(btCollisionWorld::LocalConvexResult &convexResult, bool normalInWorldSpace)
{
if (convexResult.m_hitCollisionObject == mMe)
return 1.0;
btVector3 hitNormalWorld;
if (normalInWorldSpace)
hitNormalWorld = convexResult.m_hitNormalLocal;
else
hitNormalWorld = convexResult.m_hitCollisionObject->getWorldTransform().getBasis() * convexResult.m_hitNormalLocal;
btScalar dotUp = mUp.dot(hitNormalWorld);
if (dotUp < mMinSlopeDot)
return 1.0;
return btCollisionWorld::ClosestConvexResultCallback::addSingleResult(convexResult, normalInWorldSpace);
}
};
static btVector3 *getUpAxisDirections()
{
static btVector3 sUpAxisDirection[3] = { btVector3(1, 0, 0), btVector3(0, 1, 0), btVector3(0, 0, 1) };
return sUpAxisDirection;
}
static btVector3 getNormalizedVector(const btVector3& v)
{
btVector3 n = v.normalized();
if (n.length() < SIMD_EPSILON)
n.setValue(0, 0, 0);
return n;
}
bool SweepTest(const btVector3 &, const btVector3 &, btScalar &fraction, btVector3 *normal = 0, btVector3 *hit = 0);
btVector3 computeReflectionDirection(const btVector3 & direction, const btVector3 & normal);
void setPlayerMode();
public:
btCustomCharacterController(btPairCachingGhostObject *ghostObject, btConvexShape *convexShape, btScalar stepHeight, btCollisionWorld *collisionWorld, int upAxis = 1);
btVector3 getUpAxisDirection() const { return getUpAxisDirections()[mUpAxis]; }
void setDuckingConvexShape(btConvexShape * shape);
bool recoverFromPenetration(const btVector3 &step_direction);
void stepUp(btCollisionWorld * collisionWorld);
void setRBForceImpulseBasedOnCollision();
void updateTargetPositionBasedOnCollision(const btVector3 & hitNormal, btScalar tangentMag = 0, btScalar normalMag = 1);
void stepForwardAndStrafe(btCollisionWorld * collisionWorld, const btVector3 & walkMove);
void stepDown(btCollisionWorld * collisionWorld, btScalar dt);
void setVelocityForTimeInterval(const btVector3 & velocity, btScalar timeInterval);
void reset() {}
void warp(const btVector3 & origin);
void preStep(btCollisionWorld * collisionWorld);
void playerStep(btCollisionWorld * collisionWorld, btScalar dt);
void setFallSpeed(btScalar fallSpeed);
void setJumpSpeed(btScalar jumpSpeed);
void setMaxJumpHeight(btScalar maxJumpHeight);
bool canJump() const;
void jump();
void duck();
void stand();
bool canStand();
void setGravity(const btScalar gravity);
btScalar getGravity() const;
void setMaxSlope(btScalar slopeRadians);
btScalar getMaxSlope() const;
bool onGround() const;
void setWalkDirection(const btVector3 & walkDirection);
void setWalkDirection(const btScalar x, const btScalar y, const btScalar z);
void setOrientation(const btQuaternion & orientation);
btVector3 getWalkDirection() const;
btVector3 getPosition() const;
void debugDraw(btIDebugDraw * debugDrawer);
void updateAction(btCollisionWorld * collisionWorld, btScalar dt);
};
#endif // CC_PHYSICS_H

View File

@ -0,0 +1,51 @@
/* -----------------------------------------------------------------------------
GSFramework
Copyright 2001-2013 Emmanuel Julien. All Rights Reserved.
----------------------------------------------------------------------------- */
#ifndef __NBULLET_CONSTRAINT__
#define __NBULLET_CONSTRAINT__
#include "physic/physic_constraint.h"
class btDiscreteDynamicsWorld;
class btTypedConstraint;
namespace GS {
namespace S3D {
/*
@short Bullet physic constraint.
@author Emmanuel Julien (ejulien@gsworks.fr)
*/
class BulletConstraint : public PhysicConstraint
{
friend class BulletWorld;
btDiscreteDynamicsWorld *world;
btTypedConstraint *constraint;
public:
void setLimitHinge(float low, float high, float _softness = 0.9f, float _biasFactor = 0.3f, float _relaxationFactor = 1.0f);
void SetPivotA(const Matrix4 &);
void SetPivotB(const Matrix4 &);
bool SetupConstraint(const PhysicConstraintDesc &);
void DeleteConstraint();
void Enable(bool = true);
BulletConstraint(btDiscreteDynamicsWorld *);
virtual ~BulletConstraint();
};
} // S3D
} // GS
#endif // __NBULLET_CONSTRAINT__

View File

@ -0,0 +1,75 @@
/* -----------------------------------------------------------------------------
GSFramework
Copyright 2001-2013 Emmanuel Julien. All Rights Reserved.
----------------------------------------------------------------------------- */
#ifndef __NBULLETDEBUG__
#define __NBULLETDEBUG__
#include "btBulletDynamicsCommon.h"
#include "color/color.h"
#include "math/vector.h"
#include "container/narray.h"
namespace GS {
namespace Core { class Camera; }
namespace Render {
class Renderer;
class RasterFont;
}
namespace S3D {
using Render::Renderer;
using Render::RasterFont;
using Core::Camera;
/*!
@short Bullet debug interface.
*/
class BulletDebugDraw : public btIDebugDraw
{
friend class BulletWorld;
protected:
Renderer &renderer;
Camera *camera;
RasterFont *raster_font;
int debug_mode;
Array <Vector4> vtx_cache;
Array <Color> col_cache;
uint line_count;
bool xray_first_pass;
public:
float GetXRayAlpha() const;
void SetXRayFirstPass(bool pass);
void SetDebugMode(int mode) { debug_mode = mode; }
void drawLine(const btVector3 &from, const btVector3 &to, const btVector3 &color);
void drawContactPoint(const btVector3 &PointOnB, const btVector3 &normalOnB, btScalar distance, int lifeTime, const btVector3 &color);
void reportErrorWarning(const char *warningString);
void draw3dText(const btVector3 &location, const char *textString);
void setDebugMode(int mode) { debug_mode = mode; }
int getDebugMode() const { return debug_mode; }
void Flush();
BulletDebugDraw(Renderer &);
};
} // S3D
} // GS
#endif // __NBULLETDEBUG__

View File

@ -0,0 +1,175 @@
/* -----------------------------------------------------------------------------
GSFramework
Copyright 2001-2013 Emmanuel Julien. All Rights Reserved.
----------------------------------------------------------------------------- */
#ifndef __NBULLET_ITEM__
#define __NBULLET_ITEM__
#include "physic_bullet/bullet_world.h"
#include "physic/physic_item.h"
#include "math/matrix4.h"
#include "container/narray.h"
#include "memory/nauto_ptr.h"
#include "BulletDynamics/Character/btKinematicCharacterController.h"
namespace GS {
namespace S3D {
class BulletPhysicItem;
//
class BulletMotionState : public btDefaultMotionState
{
BulletPhysicItem *item;
Matrix4 bullet_matrix, engine_matrix;
public:
/// Return the last matrix Bullet sent.
const Matrix4 &GetGraphicMatrix() const { return bullet_matrix; }
void SetEngineMatrix(const Matrix4 &m) { engine_matrix = m; }
/// Transform to Bullet.
virtual void getWorldTransform(btTransform &centerOfMassWorldTrans) const;
/// Transform from Bullet.
virtual void setWorldTransform(const btTransform &centerOfMassWorldTrans);
BulletMotionState(BulletPhysicItem *);
};
/*
@short Bullet physic item.
@author Emmanuel Julien (ejulien@gsworks.fr)
*/
class BulletPhysicItem : public PhysicItem
{
friend class BulletWorld;
btDiscreteDynamicsWorld *btworld;
struct ShapeData
{
AutoPtr <btCollisionShape> shape;
SharedPtr <BulletConvex> convex;
SharedPtr <BulletMesh> mesh;
};
Array <ShapeData> shapes;
uint collision_mask, self_mask; // cached copies to properly handle activation/deactivation.
public:
AutoPtr <BulletMotionState> motion_state;
AutoPtr <btCompoundShape> compound;
AutoPtr <btRigidBody> rigid_body;
AutoPtr <btDefaultVehicleRaycaster> vehicle_raycaster;
AutoPtr <btRaycastVehicle> vehicle;
AutoPtr <btPairCachingGhostObject> ghost_object;
AutoPtr <btConvexShape> convex_shape;
AutoPtr <btCharacterControllerInterface> character_controller;
Vector4 center;
Vector4 scale;
bool SetupCharacterController(const PhysicItemDesc &);
bool SetupKinematicDynamicBody(const PhysicItemDesc &, PhysicWorld *);
float SetupCollisionShapes(const PhysicItemDesc &, Array <btScalar> &, PhysicWorld *);
void ForceUpdateMassShapePhysic(const PhysicItemDesc &, PhysicWorld *);
/// Create a Bullet transformation from a 4x4 matrix.
static void TransformFromMatrix4(const Matrix4 &, btTransform &);
/// Create a 4x4 matrix to a Bullet transformation.
static void TransformToMatrix4(const btTransform &, Matrix4 &);
/*
@short Return the center of mass offset.
Bullet does not handle offset center of mass directly.
*/
const Vector4 &GetCenter() const { return center; }
void SetScale(const Vector4 &);
Vector4 GetScale() const;
//----------------------------------------------------------------------
void SetSelfMask(uint m);
uint GetSelfMask() const;
void SetCollisionMask(uint m);
uint GetCollisionMask() const;
//----------------------------------------------------------------------
//----------------------------------------------------------------------
void SetLinearFactor(const Vector4 &);
void SetAngularFactor(const Vector4 &);
void SetLinearDamping(float = 0.999f);
float GetLinearDamping() const;
void SetAngularDamping(float = 0.99f);
float GetAngularDamping() const;
void SetGravity(const Vector4 &);
Vector4 GetGravity() const;
void ApplyImpulse(const Vector4 &I, const Vector4 *p = 0);
void ApplyForce(const Vector4 &F, const Vector4 *p = 0);
void ApplyTorque(const Vector4 &T);
void SetAngularVelocity(const Vector4 &);
Vector4 GetAngularVelocity() const;
void SetLinearVelocity(const Vector4 &);
Vector4 GetLinearVelocity() const;
Vector4 GetLocalPointVelocity(const Vector4 &) const;
Vector4 GetWorldPointVelocity(const Vector4 &) const;
Vector4 GetCenterOfMass() const;
//----------------------------------------------------------------------
//----------------------------------------------------------------------
void GetGraphicMatrix(Matrix4 &);
void SetEngineMatrix(const Matrix4 &);
void GetMatrix(Matrix4 &);
void SetMatrix(const Matrix4 &);
//----------------------------------------------------------------------
//----------------------------------------------------------------------
virtual void VehicleSetForce(float F, uint wheel_index);
virtual void VehicleSetBrake(float F, uint wheel_index);
virtual void VehicleSetSteering(float v, uint wheel_index);
virtual void VehicleSetFriction(float f, uint wheel_index);
virtual Matrix4 VehicleGetWheelMatrix(uint wheel_index);
//----------------------------------------------------------------------
//----------------------------------------------------------------------
virtual void CharacterSetRotationMatrix(const Matrix3 &);
virtual void CharacterSetVelocity(const Vector4 &);
//----------------------------------------------------------------------
virtual void SetSleeping(bool sleep = false);
virtual bool IsSleeping() const;
virtual void SetActive(bool active);
virtual bool GetActive() const;
virtual void ResetBody();
virtual bool SetupBody(const PhysicItemDesc &, PhysicWorld *);
virtual void DeleteBody();
BulletPhysicItem(btDiscreteDynamicsWorld * = 0);
virtual ~BulletPhysicItem();
};
} // S3D
} // GS
#endif // __NBULLET_ITEM__

View File

@ -0,0 +1,127 @@
/* -----------------------------------------------------------------------------
GSFramework
Copyright 2001-2013 Emmanuel Julien. All Rights Reserved.
------------------------------------------------------------------------------*/
#ifndef __NBULLETPHYSICWORLD__
#define __NBULLETPHYSICWORLD__
#include "nstring/nstring.h"
/*
@short Enable multi-threading support in Bullet Dynamics.
Not working at the moment (need to let the API stabilize a bit first).
*/
#define __ENABLE_BULLET_MULTITHREAD__ 0
/*
@short Number of thread used by Bullet.
*/
#define __BULLET_THREAD_COUNT__ 3
#include "memory/nauto_ptr.h"
#include "memory/nshared_ptr.h"
#include "physic/physic_world.h"
#include "btBulletDynamicsCommon.h"
#include "BulletCollision/CollisionShapes/btHeightfieldTerrainShape.h"
#if __ENABLE_BULLET_MULTITHREAD__
#include "BulletMultiThreaded/SpuGatheringCollisionDispatcher.h"
#include "BulletMultiThreaded/PlatformDefinitions.h"
#if __PLATFORM_WINDOWS__
#include "BulletMultiThreaded/Win32ThreadSupport.h"
#include "BulletMultiThreaded/SpuNarrowPhaseCollisionTask/SpuGatheringCollisionTask.h"
#elif __PLATFORM_LINUX__
#include "BulletMultiThreaded/PosixThreadSupport.h"
#include "BulletMultiThreaded/SpuNarrowPhaseCollisionTask/SpuGatheringCollisionTask.h"
#endif
#endif
namespace GS {
namespace S3D {
class Scene;
class BulletDebugDraw;
/// Bullet convex hull.
struct BulletConvex : public SharedObject
{
String name;
Vector4 center;
AutoPtr <btConvexHullShape> convex;
};
/// Bullet mesh.
struct BulletMesh : public SharedObject
{
String name;
String suffix;
Vector4 center;
Array <String> bt_mat;
Array <ushort> bt_id_mat;
Array <btScalar> bt_vtx;
Array <int> bt_idx;
AutoPtr <btTriangleIndexVertexArray> mesh_interface;
AutoPtr <btBvhTriangleMeshShape> mesh;
};
/*!
@short Bullet physic world.
@author Emmanuel Julien (ejulien@gsworks.fr)
*/
class BulletWorld : public PhysicWorld
{
protected:
SharedList <BulletMesh *> mesh_cache;
SharedList <BulletConvex *> convex_cache;
AutoPtr <btDiscreteDynamicsWorld> world;
AutoPtr <btBroadphaseInterface> broadphase;
AutoPtr <btCollisionDispatcher> dispatcher;
AutoPtr <btDefaultCollisionConfiguration> collision_config;
AutoPtr <btSequentialImpulseConstraintSolver> solver;
AutoPtr <btOverlappingPairCallback> pair_callback;
#if __ENABLE_BULLET_MULTITHREAD__
AutoPtr <btThreadSupportInterface> thread_support_collision;
#endif
AutoPtr <BulletDebugDraw> debug_draw;
public:
PhysicItem *NewItem();
PhysicConstraint *NewConstraint();
BulletConvex *LoadConvex(const char *);
BulletMesh *LoadMesh(const char *, const char *suffix);
void ClearConvexMeshCache();
bool HasDebugger() const;
void CreateDebugger(Renderer * = 0);
void DrawDebug(Renderer &, Camera *, RasterFont *, bool xray_first_pass);
/// Raytrace world, the callback object Process() method is called on each hit.
bool Raytrace(const Vector4 &s, const Vector4 &d, PhysicTrace &, int collision_mask = ~0, int shape_mask = ~0, float max_distance = -1.f);
uint GetCollisionPairCount();
bool GetCollisionPair(uint, CollisionPair &);
void Step(const Time &dt);
bool Create();
void Delete();
BulletWorld();
~BulletWorld();
};
} // S3D
} // GS
#endif // __NBULLETPHYSICWORLD__