diff options
author | Erwin Coumans <blender@erwincoumans.com> | 2006-05-23 01:03:43 +0400 |
---|---|---|
committer | Erwin Coumans <blender@erwincoumans.com> | 2006-05-23 01:03:43 +0400 |
commit | ab71e2a9b55e0982d1134792d10af55464c7ac9e (patch) | |
tree | 4a53791a67d3234360107a33c5365d4de4844bb7 /source/gameengine/Physics | |
parent | 677cf7f1333c634a52227a15d6dcb3351b39febc (diff) |
-added basic support for GameActuator 'load game', relative paths were broken, just load file into memory and load blend from memory.
-near sensor support
- python binding for PhysicsConstraints.setNumTimeSubSteps (dividing the physics timestep to tradeoff quality for performance)
Diffstat (limited to 'source/gameengine/Physics')
5 files changed, 241 insertions, 19 deletions
diff --git a/source/gameengine/Physics/Bullet/CcdPhysicsController.cpp b/source/gameengine/Physics/Bullet/CcdPhysicsController.cpp index b8ce09729c2..cddc780aff4 100644 --- a/source/gameengine/Physics/Bullet/CcdPhysicsController.cpp +++ b/source/gameengine/Physics/Bullet/CcdPhysicsController.cpp @@ -23,6 +23,9 @@ subject to the following restrictions: #include "CcdPhysicsEnvironment.h" #include "SimdTransformUtil.h" +#include "CollisionShapes/SphereShape.h" +#include "CollisionShapes/ConeShape.h" + class BP_Proxy; ///todo: fill all the empty CcdPhysicsController methods, hook them up to the RigidBody class @@ -486,3 +489,96 @@ bool CcdPhysicsController::wantsSleeping() return false; } +PHY_IPhysicsController* CcdPhysicsController::GetReplica() +{ + //very experimental, shape sharing is not implemented yet. + //just support SphereShape/ConeShape for now + + CcdConstructionInfo cinfo = m_cci; + if (cinfo.m_collisionShape) + { + switch (cinfo.m_collisionShape->GetShapeType()) + { + case SPHERE_SHAPE_PROXYTYPE: + { + SphereShape* orgShape = (SphereShape*)cinfo.m_collisionShape; + cinfo.m_collisionShape = new SphereShape(*orgShape); + break; + } + + case CONE_SHAPE_PROXYTYPE: + { + ConeShape* orgShape = (ConeShape*)cinfo.m_collisionShape; + cinfo.m_collisionShape = new ConeShape(*orgShape); + break; + } + + + default: + { + return 0; + } + } + } + + cinfo.m_MotionState = new DefaultMotionState(); + + CcdPhysicsController* replica = new CcdPhysicsController(cinfo); + return replica; +} + +/////////////////////////////////////////////////////////// +///A small utility class, DefaultMotionState +/// +/////////////////////////////////////////////////////////// + +DefaultMotionState::DefaultMotionState() +{ + m_worldTransform.setIdentity(); +} + + +DefaultMotionState::~DefaultMotionState() +{ + +} + +void DefaultMotionState::getWorldPosition(float& posX,float& posY,float& posZ) +{ + posX = m_worldTransform.getOrigin().x(); + posY = m_worldTransform.getOrigin().y(); + posZ = m_worldTransform.getOrigin().z(); +} + +void DefaultMotionState::getWorldScaling(float& scaleX,float& scaleY,float& scaleZ) +{ + scaleX = 1.; + scaleY = 1.; + scaleZ = 1.; +} + +void DefaultMotionState::getWorldOrientation(float& quatIma0,float& quatIma1,float& quatIma2,float& quatReal) +{ + quatIma0 = m_worldTransform.getRotation().x(); + quatIma1 = m_worldTransform.getRotation().y(); + quatIma2 = m_worldTransform.getRotation().z(); + quatReal = m_worldTransform.getRotation()[3]; +} + +void DefaultMotionState::setWorldPosition(float posX,float posY,float posZ) +{ + SimdPoint3 pos(posX,posY,posZ); + m_worldTransform.setOrigin( pos ); +} + +void DefaultMotionState::setWorldOrientation(float quatIma0,float quatIma1,float quatIma2,float quatReal) +{ + SimdQuaternion orn(quatIma0,quatIma1,quatIma2,quatReal); + m_worldTransform.setRotation( orn ); +} + +void DefaultMotionState::calculateWorldTransformations() +{ + +} + diff --git a/source/gameengine/Physics/Bullet/CcdPhysicsController.h b/source/gameengine/Physics/Bullet/CcdPhysicsController.h index 646b21f285e..056feba652c 100644 --- a/source/gameengine/Physics/Bullet/CcdPhysicsController.h +++ b/source/gameengine/Physics/Bullet/CcdPhysicsController.h @@ -27,6 +27,7 @@ subject to the following restrictions: #include "SimdTransform.h" #include "Dynamics/RigidBody.h" +#include "PHY_IMotionState.h" #include "BroadphaseCollision/BroadphaseProxy.h" //for CollisionShape access class CollisionShape; @@ -155,7 +156,8 @@ class CcdPhysicsController : public PHY_IPhysicsController // clientinfo for raycasts for example virtual void* getNewClientInfo(); virtual void setNewClientInfo(void* clientinfo); - virtual PHY_IPhysicsController* GetReplica() {return 0;} + virtual PHY_IPhysicsController* GetReplica(); + virtual void calcXform() {} ; virtual void SetMargin(float margin) {}; @@ -184,4 +186,30 @@ class CcdPhysicsController : public PHY_IPhysicsController }; + + + +///DefaultMotionState implements standard motionstate, using SimdTransform +class DefaultMotionState : public PHY_IMotionState + +{ + public: + DefaultMotionState(); + + virtual ~DefaultMotionState(); + + virtual void getWorldPosition(float& posX,float& posY,float& posZ); + virtual void getWorldScaling(float& scaleX,float& scaleY,float& scaleZ); + virtual void getWorldOrientation(float& quatIma0,float& quatIma1,float& quatIma2,float& quatReal); + + virtual void setWorldPosition(float posX,float posY,float posZ); + virtual void setWorldOrientation(float quatIma0,float quatIma1,float quatIma2,float quatReal); + + virtual void calculateWorldTransformations(); + + SimdTransform m_worldTransform; + +}; + + #endif //BULLET2_PHYSICSCONTROLLER_H diff --git a/source/gameengine/Physics/Bullet/CcdPhysicsEnvironment.cpp b/source/gameengine/Physics/Bullet/CcdPhysicsEnvironment.cpp index e24bc1653af..4b4d3f15dae 100644 --- a/source/gameengine/Physics/Bullet/CcdPhysicsEnvironment.cpp +++ b/source/gameengine/Physics/Bullet/CcdPhysicsEnvironment.cpp @@ -29,6 +29,10 @@ subject to the following restrictions: #include "CollisionDispatch/CollisionWorld.h" #include "CollisionShapes/ConvexShape.h" +#include "CollisionShapes/ConeShape.h" + + + #include "BroadphaseCollision/Dispatcher.h" #include "NarrowPhaseCollision/PersistentManifold.h" #include "CollisionShapes/TriangleMeshShape.h" @@ -321,6 +325,7 @@ static void DrawAabb(IDebugDraw* debugDrawer,const SimdVector3& from,const SimdV CcdPhysicsEnvironment::CcdPhysicsEnvironment(CollisionDispatcher* dispatcher,BroadphaseInterface* broadphase) :m_scalingPropagated(false), m_numIterations(10), +m_numTimeSubSteps(1), m_ccdMode(0), m_solverType(-1), m_profileTimings(0), @@ -342,9 +347,9 @@ m_enableSatCollisionDetection(false) SimdVector3 worldMin(-10000,-10000,-10000); SimdVector3 worldMax(10000,10000,10000); - //broadphase = new AxisSweep3(worldMin,worldMax); + broadphase = new AxisSweep3(worldMin,worldMax); - broadphase = new SimpleBroadphase(); + //broadphase = new SimpleBroadphase(); } @@ -498,6 +503,8 @@ void CcdPhysicsEnvironment::beginFrame() bool CcdPhysicsEnvironment::proceedDeltaTime(double curTime,float timeStep) { + //printf("proceedDeltaTime\n"); + #ifdef USE_QUICKPROF //toggle Profiler @@ -529,16 +536,37 @@ bool CcdPhysicsEnvironment::proceedDeltaTime(double curTime,float timeStep) if (!SimdFuzzyZero(timeStep)) { + { + //do the kinematic calculation here, over the full timestep + std::vector<CcdPhysicsController*>::iterator i; + for (i=m_controllers.begin(); + !(i==m_controllers.end()); i++) + { - // define this in blender, the stepsize is 30 hertz, 60 hertz works much better -//#define SPLIT_TIMESTEP 1 + CcdPhysicsController* ctrl = *i; -#ifdef SPLIT_TIMESTEP - proceedDeltaTimeOneStep(0.5f*timeStep); - proceedDeltaTimeOneStep(0.5f*timeStep); -#else - proceedDeltaTimeOneStep(timeStep); -#endif + SimdTransform predictedTrans; + RigidBody* body = ctrl->GetRigidBody(); + if (body->GetActivationState() != ISLAND_SLEEPING) + { + + if (body->IsStatic()) + { + //to calculate velocities next frame + body->saveKinematicState(timeStep); + } + } + } + } + + + int i; + float subTimeStep = timeStep / float(m_numTimeSubSteps); + + for (i=0;i<this->m_numTimeSubSteps;i++) + { + proceedDeltaTimeOneStep(subTimeStep); + } } else { //todo: interpolate @@ -564,7 +592,7 @@ bool CcdPhysicsEnvironment::proceedDeltaTimeOneStep(float timeStep) { - // printf("CcdPhysicsEnvironment::proceedDeltaTime\n"); + //printf("CcdPhysicsEnvironment::proceedDeltaTime\n"); if (SimdFuzzyZero(timeStep)) return true; @@ -812,11 +840,7 @@ bool CcdPhysicsEnvironment::proceedDeltaTimeOneStep(float timeStep) if (body->GetActivationState() != ISLAND_SLEEPING) { - if (body->IsStatic()) - { - //to calculate velocities next frame - body->saveKinematicState(timeStep); - } else + if (!body->IsStatic()) { body->predictIntegratedTransform(timeStep* toi, predictedTrans); body->proceedToTransform( predictedTrans); @@ -1393,10 +1417,34 @@ TypedConstraint* CcdPhysicsEnvironment::getConstraintById(int constraintId) void CcdPhysicsEnvironment::addSensor(PHY_IPhysicsController* ctrl) { + + CcdPhysicsController* ctrl1 = (CcdPhysicsController* )ctrl; + std::vector<CcdPhysicsController*>::iterator i = + std::find(m_controllers.begin(), m_controllers.end(), ctrl); + if ((i == m_controllers.end())) + { + addCcdPhysicsController(ctrl1); + } + + requestCollisionCallback(ctrl); //printf("addSensor\n"); } + +void CcdPhysicsEnvironment::removeCollisionCallback(PHY_IPhysicsController* ctrl) +{ + std::vector<CcdPhysicsController*>::iterator i = + std::find(m_triggerControllers.begin(), m_triggerControllers.end(), ctrl); + if (!(i == m_triggerControllers.end())) + { + std::swap(*i, m_triggerControllers.back()); + m_triggerControllers.pop_back(); + } +} + + void CcdPhysicsEnvironment::removeSensor(PHY_IPhysicsController* ctrl) { + removeCollisionCallback(ctrl); //printf("removeSensor\n"); } void CcdPhysicsEnvironment::addTouchCallback(int response_class, PHY_ResponseCallback callback, void *user) @@ -1585,3 +1633,41 @@ void CcdPhysicsEnvironment::UpdateAabbs(float timeStep) } } } + +PHY_IPhysicsController* CcdPhysicsEnvironment::CreateSphereController(float radius,const PHY__Vector3& position) +{ + + CcdConstructionInfo cinfo; + cinfo.m_collisionShape = new SphereShape(radius); + cinfo.m_MotionState = 0; + cinfo.m_physicsEnv = this; + cinfo.m_collisionFlags |= CollisionObject::noContactResponse; + DefaultMotionState* motionState = new DefaultMotionState(); + cinfo.m_MotionState = motionState; + motionState->m_worldTransform.setIdentity(); + motionState->m_worldTransform.setOrigin(SimdVector3(position[0],position[1],position[2])); + + CcdPhysicsController* sphereController = new CcdPhysicsController(cinfo); + + + return sphereController; +} + + +PHY_IPhysicsController* CcdPhysicsEnvironment::CreateConeController(float coneradius,float coneheight) +{ + CcdConstructionInfo cinfo; + cinfo.m_collisionShape = new ConeShape(coneradius,coneheight); + cinfo.m_MotionState = 0; + cinfo.m_physicsEnv = this; + DefaultMotionState* motionState = new DefaultMotionState(); + cinfo.m_MotionState = motionState; + motionState->m_worldTransform.setIdentity(); +// motionState->m_worldTransform.setOrigin(SimdVector3(position[0],position[1],position[2])); + + CcdPhysicsController* sphereController = new CcdPhysicsController(cinfo); + + + return sphereController; +} + diff --git a/source/gameengine/Physics/Bullet/CcdPhysicsEnvironment.h b/source/gameengine/Physics/Bullet/CcdPhysicsEnvironment.h index 08dff62e236..9de7053d271 100644 --- a/source/gameengine/Physics/Bullet/CcdPhysicsEnvironment.h +++ b/source/gameengine/Physics/Bullet/CcdPhysicsEnvironment.h @@ -48,7 +48,11 @@ class CcdPhysicsEnvironment : public PHY_IPhysicsEnvironment SimdVector3 m_gravity; IDebugDraw* m_debugDrawer; + //solver iterations int m_numIterations; + + //timestep subdivisions + int m_numTimeSubSteps; int m_ccdMode; int m_solverType; int m_profileTimings; @@ -74,6 +78,10 @@ class CcdPhysicsEnvironment : public PHY_IPhysicsEnvironment } virtual void setNumIterations(int numIter); + virtual void setNumTimeSubSteps(int numTimeSubSteps) + { + m_numTimeSubSteps = numTimeSubSteps; + } virtual void setDeactivationTime(float dTime); virtual void setDeactivationLinearTreshold(float linTresh) ; virtual void setDeactivationAngularTreshold(float angTresh) ; @@ -131,8 +139,10 @@ class CcdPhysicsEnvironment : public PHY_IPhysicsEnvironment 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 void removeCollisionCallback(PHY_IPhysicsController* ctrl); + + virtual PHY_IPhysicsController* CreateSphereController(float radius,const PHY__Vector3& position); + virtual PHY_IPhysicsController* CreateConeController(float coneradius,float coneheight); virtual int getNumContactPoints(); diff --git a/source/gameengine/Physics/common/PHY_IPhysicsEnvironment.h b/source/gameengine/Physics/common/PHY_IPhysicsEnvironment.h index 6b40b51eba7..cdd67257de2 100644 --- a/source/gameengine/Physics/common/PHY_IPhysicsEnvironment.h +++ b/source/gameengine/Physics/common/PHY_IPhysicsEnvironment.h @@ -57,6 +57,8 @@ class PHY_IPhysicsEnvironment virtual void setDebugMode(int debugMode) {} ///setNumIterations set the number of iterations for iterative solvers virtual void setNumIterations(int numIter) {} + ///setNumTimeSubSteps set the number of divisions of the timestep. Tradeoff quality versus performance. + virtual void setNumTimeSubSteps(int numTimeSubSteps){} ///setDeactivationTime sets the minimum time that an objects has to stay within the velocity tresholds until it gets fully deactivated virtual void setDeactivationTime(float dTime) {} ///setDeactivationLinearTreshold sets the linear velocity treshold, see setDeactivationTime |