added Bullet physics binding files
authorErwin Coumans <blender@erwincoumans.com>
Sat, 16 Jul 2005 10:15:31 +0000 (10:15 +0000)
committerErwin Coumans <blender@erwincoumans.com>
Sat, 16 Jul 2005 10:15:31 +0000 (10:15 +0000)
source/gameengine/Physics/Bullet/CcdPhysicsController.cpp [new file with mode: 0644]
source/gameengine/Physics/Bullet/CcdPhysicsController.h [new file with mode: 0644]
source/gameengine/Physics/Bullet/CcdPhysicsEnvironment.h [new file with mode: 0644]

diff --git a/source/gameengine/Physics/Bullet/CcdPhysicsController.cpp b/source/gameengine/Physics/Bullet/CcdPhysicsController.cpp
new file mode 100644 (file)
index 0000000..376a75f
--- /dev/null
@@ -0,0 +1,230 @@
+#include "CcdPhysicsController.h"
+
+#include "Dynamics/RigidBody.h"
+#include "PHY_IMotionState.h"
+#include "BroadphaseCollision/BroadphaseProxy.h"
+#include "CollisionShapes/ConvexShape.h"
+
+class BP_Proxy;
+
+bool gEnableSleeping = true;//false;//true;
+#include "Dynamics/MassProps.h"
+
+SimdVector3 startVel(0,0,0);//-10000);
+CcdPhysicsController::CcdPhysicsController (const CcdConstructionInfo& ci)
+{
+       m_collisionDelay = 0;
+
+       m_sleepingCounter = 0;
+
+       m_MotionState = ci.m_MotionState;
+
+
+       SimdTransform trans;
+       float tmp[3];
+       m_MotionState->getWorldPosition(tmp[0],tmp[1],tmp[2]);
+       trans.setOrigin(SimdVector3(tmp[0],tmp[1],tmp[2]));
+
+       SimdQuaternion orn;
+       m_MotionState->getWorldOrientation(orn[0],orn[1],orn[2],orn[3]);
+       trans.setRotation(orn);
+
+       MassProps mp(ci.m_mass, ci.m_localInertiaTensor);
+
+       m_body = new RigidBody(mp,0,0);
+       
+       m_broadphaseHandle = ci.m_broadphaseHandle;
+
+       m_collisionShape = ci.m_collisionShape;
+
+       //
+       // init the rigidbody properly
+       //
+       
+       m_body->setMassProps(ci.m_mass, ci.m_localInertiaTensor);
+       m_body->setGravity( ci.m_gravity);
+
+       m_friction = ci.m_friction;
+       m_restitution = ci.m_restitution;
+
+       m_body->setDamping(ci.m_linearDamping, ci.m_angularDamping);
+
+       
+       m_body->setCenterOfMassTransform( trans );
+
+       #ifdef WIN32
+       if (m_body->getInvMass())
+               m_body->setLinearVelocity(startVel);
+       #endif
+
+}
+
+CcdPhysicsController::~CcdPhysicsController()
+{
+       //will be reference counted, due to sharing
+       //delete m_collisionShape;
+       delete m_MotionState;
+       delete m_body;
+}
+
+               /**
+                       SynchronizeMotionStates ynchronizes dynas, kinematic and deformable entities (and do 'late binding')
+               */
+bool           CcdPhysicsController::SynchronizeMotionStates(float time)
+{
+       const SimdVector3& worldPos = m_body->getCenterOfMassPosition();
+       m_MotionState->setWorldPosition(worldPos[0],worldPos[1],worldPos[2]);
+       
+       const SimdQuaternion& worldquat = m_body->getOrientation();
+       m_MotionState->setWorldOrientation(worldquat[0],worldquat[1],worldquat[2],worldquat[3]);
+
+       m_MotionState->calculateWorldTransformations();
+
+       float scale[3];
+       m_MotionState->getWorldScaling(scale[0],scale[1],scale[2]);
+       
+       SimdVector3 scaling(scale[0],scale[1],scale[2]);
+       m_collisionShape->setLocalScaling(scaling);
+
+
+       return true;
+}
+
+               /**
+                       WriteMotionStateToDynamics synchronizes dynas, kinematic and deformable entities (and do 'late binding')
+               */
+               
+void           CcdPhysicsController::WriteMotionStateToDynamics(bool nondynaonly)
+{
+
+}
+void           CcdPhysicsController::WriteDynamicsToMotionState()
+{
+}
+               // controller replication
+void           CcdPhysicsController::PostProcessReplica(class PHY_IMotionState* motionstate,class PHY_IPhysicsController* parentctrl)
+{
+}
+
+               // kinematic methods
+void           CcdPhysicsController::RelativeTranslate(float dlocX,float dlocY,float dlocZ,bool local)
+{
+       SimdTransform xform = m_body->getCenterOfMassTransform();
+       xform.setOrigin(xform.getOrigin() + SimdVector3(dlocX,dlocY,dlocZ));
+       this->m_body->setCenterOfMassTransform(xform);
+
+}
+
+void           CcdPhysicsController::RelativeRotate(const float drot[9],bool local)
+{
+}
+void           CcdPhysicsController::getOrientation(float &quatImag0,float &quatImag1,float &quatImag2,float &quatReal)
+{
+}
+void           CcdPhysicsController::setOrientation(float quatImag0,float quatImag1,float quatImag2,float quatReal)
+{
+}
+void           CcdPhysicsController::setPosition(float posX,float posY,float posZ)
+{
+}
+void           CcdPhysicsController::resolveCombinedVelocities(float linvelX,float linvelY,float linvelZ,float angVelX,float angVelY,float angVelZ)
+{
+}
+
+void           CcdPhysicsController::getPosition(PHY__Vector3& pos) const
+{
+       assert(0);
+}
+
+void           CcdPhysicsController::setScaling(float scaleX,float scaleY,float scaleZ)
+{
+}
+               
+               // physics methods
+void           CcdPhysicsController::ApplyTorque(float torqueX,float torqueY,float torqueZ,bool local)
+{
+}
+void           CcdPhysicsController::ApplyForce(float forceX,float forceY,float forceZ,bool local)
+{
+}
+void           CcdPhysicsController::SetAngularVelocity(float ang_velX,float ang_velY,float ang_velZ,bool local)
+{
+       SimdVector3 angvel(ang_velX,ang_velY,ang_velZ);
+
+       m_body->setAngularVelocity(angvel);
+
+}
+void           CcdPhysicsController::SetLinearVelocity(float lin_velX,float lin_velY,float lin_velZ,bool local)
+{
+
+       SimdVector3 linVel(lin_velX,lin_velY,lin_velZ);
+       m_body->setLinearVelocity(linVel);
+}
+void           CcdPhysicsController::applyImpulse(float attachX,float attachY,float attachZ, float impulseX,float impulseY,float impulseZ)
+{
+}
+void           CcdPhysicsController::SetActive(bool active)
+{
+}
+               // reading out information from physics
+void           CcdPhysicsController::GetLinearVelocity(float& linvX,float& linvY,float& linvZ)
+{
+}
+void           CcdPhysicsController::GetVelocity(const float posX,const float posY,const float posZ,float& linvX,float& linvY,float& linvZ)
+{
+}
+void           CcdPhysicsController::getReactionForce(float& forceX,float& forceY,float& forceZ)
+{
+}
+
+               // dyna's that are rigidbody are free in orientation, dyna's with non-rigidbody are restricted 
+void           CcdPhysicsController::setRigidBody(bool rigid)
+{
+}
+
+               // clientinfo for raycasts for example
+void*          CcdPhysicsController::getNewClientInfo()
+{
+       return 0;
+}
+void           CcdPhysicsController::setNewClientInfo(void* clientinfo)
+{
+
+}
+
+#ifdef WIN32
+float gSleepingTreshold = 0.8f;
+float gAngularSleepingTreshold = 1.f;
+
+#else
+
+float gSleepingTreshold = 0.8f;
+float gAngularSleepingTreshold = 1.0f;
+#endif
+
+
+bool CcdPhysicsController::wantsSleeping()
+{
+
+       if (!gEnableSleeping)
+               return false;
+
+       if ( (m_body->GetActivationState() == 3) || (m_body->GetActivationState() == 2))
+               return true;
+
+       if ((m_body->getLinearVelocity().length2() < gSleepingTreshold*gSleepingTreshold) &&
+               (m_body->getAngularVelocity().length2() < gAngularSleepingTreshold*gAngularSleepingTreshold))
+       {
+               m_sleepingCounter++;
+       } else
+       {
+               m_sleepingCounter=0;
+       }
+
+       if (m_sleepingCounter> 150)
+       {
+               return true;
+       }
+       return false;
+}
+
diff --git a/source/gameengine/Physics/Bullet/CcdPhysicsController.h b/source/gameengine/Physics/Bullet/CcdPhysicsController.h
new file mode 100644 (file)
index 0000000..3a8590e
--- /dev/null
@@ -0,0 +1,133 @@
+
+#ifndef BULLET2_PHYSICSCONTROLLER_H
+#define BULLET2_PHYSICSCONTROLLER_H
+
+#include "PHY_IPhysicsController.h"
+
+///    PHY_IPhysicsController is the abstract simplified Interface to a physical object.
+///    It contains the IMotionState and IDeformableMesh Interfaces.
+#include "SimdVector3.h"
+#include "SimdScalar.h"        
+class CollisionShape;
+
+struct CcdConstructionInfo
+{
+       CcdConstructionInfo()
+               : m_gravity(0,0,0),
+               m_mass(0.f),
+               m_friction(0.1f),
+               m_restitution(0.1f),
+               m_linearDamping(0.1f),
+               m_angularDamping(0.1f),
+               m_MotionState(0),
+               m_collisionShape(0)
+
+       {
+       }
+       SimdVector3     m_localInertiaTensor;
+       SimdVector3     m_gravity;
+       SimdScalar      m_mass;
+       SimdScalar      m_friction;
+       SimdScalar      m_restitution;
+       
+       SimdScalar      m_linearDamping;
+       SimdScalar      m_angularDamping;
+       void*           m_broadphaseHandle;
+       class   PHY_IMotionState*                       m_MotionState;
+
+       CollisionShape*                 m_collisionShape;
+       
+};
+
+
+class RigidBody;
+
+///CcdPhysicsController is a physics object that supports continuous collision detection and time of impact based physics resolution.
+class CcdPhysicsController : public PHY_IPhysicsController     
+{
+       RigidBody* m_body;
+       class   PHY_IMotionState*                       m_MotionState;
+       CollisionShape*                 m_collisionShape;
+
+       int                             m_sleepingCounter;
+       public:
+       
+               int                             m_collisionDelay;
+       
+               SimdScalar      m_friction;
+               SimdScalar      m_restitution;
+               void*  m_broadphaseHandle;
+
+               CcdPhysicsController (const CcdConstructionInfo& ci);
+
+               virtual ~CcdPhysicsController();
+
+
+               RigidBody* GetRigidBody() { return m_body;}
+
+               CollisionShape* GetCollisionShape() { return m_collisionShape;}
+               ////////////////////////////////////
+               // PHY_IPhysicsController interface
+               ////////////////////////////////////
+
+
+               /**
+                       SynchronizeMotionStates ynchronizes dynas, kinematic and deformable entities (and do 'late binding')
+               */
+               virtual bool            SynchronizeMotionStates(float time);
+               /**
+                       WriteMotionStateToDynamics ynchronizes dynas, kinematic and deformable entities (and do 'late binding')
+               */
+               
+               virtual void            WriteMotionStateToDynamics(bool nondynaonly);
+               virtual void            WriteDynamicsToMotionState();
+               // controller replication
+               virtual void            PostProcessReplica(class PHY_IMotionState* motionstate,class PHY_IPhysicsController* parentctrl);
+
+               // kinematic methods
+               virtual void            RelativeTranslate(float dlocX,float dlocY,float dlocZ,bool local);
+               virtual void            RelativeRotate(const float drot[9],bool local);
+               virtual void            getOrientation(float &quatImag0,float &quatImag1,float &quatImag2,float &quatReal);
+               virtual void            setOrientation(float quatImag0,float quatImag1,float quatImag2,float quatReal);
+               virtual void            setPosition(float posX,float posY,float posZ);
+               virtual void            getPosition(PHY__Vector3&       pos) const;
+
+               virtual void            setScaling(float scaleX,float scaleY,float scaleZ);
+               
+               // physics methods
+               virtual void            ApplyTorque(float torqueX,float torqueY,float torqueZ,bool local);
+               virtual void            ApplyForce(float forceX,float forceY,float forceZ,bool local);
+               virtual void            SetAngularVelocity(float ang_velX,float ang_velY,float ang_velZ,bool local);
+               virtual void            SetLinearVelocity(float lin_velX,float lin_velY,float lin_velZ,bool local);
+               virtual void            applyImpulse(float attachX,float attachY,float attachZ, float impulseX,float impulseY,float impulseZ);
+               virtual void            SetActive(bool active);
+
+               // reading out information from physics
+               virtual void            GetLinearVelocity(float& linvX,float& linvY,float& linvZ);
+               virtual void            GetVelocity(const float posX,const float posY,const float posZ,float& linvX,float& linvY,float& linvZ); 
+               virtual void            getReactionForce(float& forceX,float& forceY,float& forceZ);
+
+               // dyna's that are rigidbody are free in orientation, dyna's with non-rigidbody are restricted 
+               virtual void            setRigidBody(bool rigid);
+
+               
+               virtual void            resolveCombinedVelocities(float linvelX,float linvelY,float linvelZ,float angVelX,float angVelY,float angVelZ);
+
+               // clientinfo for raycasts for example
+               virtual void*                           getNewClientInfo();
+               virtual void                            setNewClientInfo(void* clientinfo);
+               virtual PHY_IPhysicsController* GetReplica() {return 0;}
+
+               virtual void    calcXform() {} ;
+               virtual void SetMargin(float margin) {};
+               virtual float GetMargin() const {return 0.f;};
+
+
+               bool    wantsSleeping();
+
+               void    SetAabb(const SimdVector3& aabbMin,const SimdVector3& aabbMax);
+
+
+};
+
+#endif //BULLET2_PHYSICSCONTROLLER_H
diff --git a/source/gameengine/Physics/Bullet/CcdPhysicsEnvironment.h b/source/gameengine/Physics/Bullet/CcdPhysicsEnvironment.h
new file mode 100644 (file)
index 0000000..88cf4b4
--- /dev/null
@@ -0,0 +1,121 @@
+#ifndef CCDPHYSICSENVIRONMENT
+#define CCDPHYSICSENVIRONMENT
+
+#include "PHY_IPhysicsEnvironment.h"
+#include <vector>
+class CcdPhysicsController;
+#include "SimdVector3.h"
+
+struct PHY_IPhysicsDebugDraw
+{
+       virtual void    DrawLine(const SimdVector3& from,const SimdVector3& to,const SimdVector3& color)=0;
+};
+
+
+class Point2PointConstraint;
+class ToiContactDispatcher;
+class Dispatcher;
+//#include "BroadphaseInterface.h"
+
+class Vehicle;
+class PersistentManifold;
+class BroadphaseInterface;
+
+/// Physics Environment takes care of stepping the simulation and is a container for physics entities.
+/// It stores rigidbodies,constraints, materials etc.
+/// A derived class may be able to 'construct' entities by loading and/or converting
+class CcdPhysicsEnvironment : public PHY_IPhysicsEnvironment
+{
+       SimdVector3 m_gravity;
+       BroadphaseInterface*    m_broadphase;
+       PHY_IPhysicsDebugDraw*  m_debugDrawer;
+       
+       public:
+               CcdPhysicsEnvironment(ToiContactDispatcher* dispatcher=0, BroadphaseInterface* broadphase=0);
+
+               virtual         ~CcdPhysicsEnvironment();
+
+               /////////////////////////////////////
+               //PHY_IPhysicsEnvironment interface
+               /////////////////////////////////////
+
+               /// Perform an integration step of duration 'timeStep'.
+
+               virtual void setDebugDrawer(PHY_IPhysicsDebugDraw* debugDrawer)
+               {
+                       m_debugDrawer = debugDrawer;
+               }
+
+
+               virtual void            beginFrame() {};
+               virtual void            endFrame() {};
+               /// Perform an integration step of duration 'timeStep'.
+               virtual bool            proceedDeltaTime(double curTime,float timeStep);
+               virtual void            setFixedTimeStep(bool useFixedTimeStep,float fixedTimeStep){};
+               //returns 0.f if no fixed timestep is used
+               virtual float           getFixedTimeStep(){ return 0.f;};
+
+               virtual void            setGravity(float x,float y,float z);
+
+               virtual int                     createConstraint(class PHY_IPhysicsController* ctrl,class PHY_IPhysicsController* ctrl2,PHY_ConstraintType type,
+                       float pivotX,float pivotY,float pivotZ,
+                       float axisX,float axisY,float axisZ);
+               virtual void            removeConstraint(int constraintid);
+
+
+               virtual PHY_IPhysicsController* rayTest(PHY_IPhysicsController* ignoreClient, float fromX,float fromY,float fromZ, float toX,float toY,float toZ, 
+                                                                               float& hitX,float& hitY,float& hitZ,float& normalX,float& normalY,float& normalZ);
+
+
+               //Methods for gamelogic collision/physics callbacks
+               //todo:
+               virtual void addSensor(PHY_IPhysicsController* ctrl) {};
+               virtual void removeSensor(PHY_IPhysicsController* ctrl){};
+               virtual void addTouchCallback(int response_class, PHY_ResponseCallback callback, void *user){};
+               virtual void requestCollisionCallback(PHY_IPhysicsController* ctrl){};
+               virtual PHY_IPhysicsController* CreateSphereController(float radius,const PHY__Vector3& position) {return 0;};
+               virtual PHY_IPhysicsController* CreateConeController(float coneradius,float coneheight){ return 0;};
+       
+
+               virtual int     getNumContactPoints();
+
+               virtual void getContactPoint(int i,float& hitX,float& hitY,float& hitZ,float& normalX,float& normalY,float& normalZ);
+
+               //////////////////////
+               //CcdPhysicsEnvironment interface
+               ////////////////////////
+       
+               void    addCcdPhysicsController(CcdPhysicsController* ctrl);
+
+               void    removeCcdPhysicsController(CcdPhysicsController* ctrl);
+
+               BroadphaseInterface*    GetBroadphase() { return m_broadphase; }
+
+               Dispatcher* GetDispatcher();
+
+               int     GetNumControllers();
+
+               CcdPhysicsController* GetPhysicsController( int index);
+
+               int     GetNumManifolds() const;
+
+               const PersistentManifold*       GetManifold(int index) const;
+
+       private:
+               
+               void    UpdateActivationState();        
+               void    SyncMotionStates(float timeStep);
+               
+               std::vector<CcdPhysicsController*> m_controllers;
+
+               std::vector<Point2PointConstraint*> m_p2pConstraints;
+
+               std::vector<Vehicle*>   m_vehicles;
+
+               class ToiContactDispatcher* m_dispatcher;
+
+               bool    m_scalingPropagated;
+
+};
+
+#endif //CCDPHYSICSENVIRONMENT