2005-07-16 10:15:31 +00:00
|
|
|
#include "CcdPhysicsController.h"
|
|
|
|
|
|
|
|
#include "Dynamics/RigidBody.h"
|
|
|
|
#include "PHY_IMotionState.h"
|
|
|
|
#include "BroadphaseCollision/BroadphaseProxy.h"
|
|
|
|
#include "CollisionShapes/ConvexShape.h"
|
2005-12-31 07:20:08 +00:00
|
|
|
#include "CcdPhysicsEnvironment.h"
|
|
|
|
|
2005-07-16 10:15:31 +00:00
|
|
|
|
|
|
|
class BP_Proxy;
|
|
|
|
|
2005-08-13 08:05:48 +00:00
|
|
|
///todo: fill all the empty CcdPhysicsController methods, hook them up to the RigidBody class
|
|
|
|
|
2005-08-03 18:22:30 +00:00
|
|
|
//'temporarily' global variables
|
2005-08-04 19:07:39 +00:00
|
|
|
float gDeactivationTime = 2.f;
|
2005-08-13 08:05:48 +00:00
|
|
|
bool gDisableDeactivation = false;
|
|
|
|
|
2005-08-03 18:22:30 +00:00
|
|
|
float gLinearSleepingTreshold = 0.8f;
|
|
|
|
float gAngularSleepingTreshold = 1.0f;
|
|
|
|
|
2005-07-16 10:15:31 +00:00
|
|
|
#include "Dynamics/MassProps.h"
|
|
|
|
|
|
|
|
SimdVector3 startVel(0,0,0);//-10000);
|
|
|
|
CcdPhysicsController::CcdPhysicsController (const CcdConstructionInfo& ci)
|
2005-12-31 07:20:08 +00:00
|
|
|
:m_cci(ci)
|
2005-07-16 10:15:31 +00:00
|
|
|
{
|
|
|
|
m_collisionDelay = 0;
|
2005-08-05 17:00:32 +00:00
|
|
|
m_newClientInfo = 0;
|
2005-08-04 19:07:39 +00:00
|
|
|
|
2005-07-16 10:15:31 +00:00
|
|
|
m_MotionState = ci.m_MotionState;
|
|
|
|
|
|
|
|
|
2005-12-31 07:20:08 +00:00
|
|
|
m_broadphaseHandle = ci.m_broadphaseHandle;
|
|
|
|
|
|
|
|
m_collisionShape = ci.m_collisionShape;
|
|
|
|
|
|
|
|
CreateRigidbody();
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef WIN32
|
|
|
|
if (m_body->getInvMass())
|
|
|
|
m_body->setLinearVelocity(startVel);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void CcdPhysicsController::CreateRigidbody()
|
|
|
|
{
|
2005-07-16 10:15:31 +00:00
|
|
|
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);
|
|
|
|
|
2005-12-31 07:20:08 +00:00
|
|
|
MassProps mp(m_cci.m_mass, m_cci.m_localInertiaTensor);
|
2005-07-16 10:15:31 +00:00
|
|
|
|
2005-12-31 07:20:08 +00:00
|
|
|
m_body = new RigidBody(mp,0,0,m_cci.m_friction,m_cci.m_restitution);
|
2005-07-16 10:15:31 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
// init the rigidbody properly
|
|
|
|
//
|
|
|
|
|
2005-12-31 21:59:56 +00:00
|
|
|
m_body->setMassProps(m_cci.m_mass, m_cci.m_localInertiaTensor * m_cci.m_inertiaFactor);
|
2005-12-31 07:20:08 +00:00
|
|
|
m_body->setGravity( m_cci.m_gravity);
|
|
|
|
m_body->setDamping(m_cci.m_linearDamping, m_cci.m_angularDamping);
|
2005-07-16 10:15:31 +00:00
|
|
|
m_body->setCenterOfMassTransform( trans );
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
CcdPhysicsController::~CcdPhysicsController()
|
|
|
|
{
|
|
|
|
//will be reference counted, due to sharing
|
|
|
|
//delete m_collisionShape;
|
2005-12-31 07:20:08 +00:00
|
|
|
m_cci.m_physicsEnv->removeCcdPhysicsController(this);
|
2005-07-16 10:15:31 +00:00
|
|
|
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)
|
|
|
|
{
|
2005-12-31 07:20:08 +00:00
|
|
|
|
|
|
|
m_MotionState = motionstate;
|
|
|
|
m_broadphaseHandle = 0;
|
|
|
|
m_body = 0;
|
|
|
|
CreateRigidbody();
|
|
|
|
|
|
|
|
m_cci.m_physicsEnv->addCcdPhysicsController(this);
|
|
|
|
|
|
|
|
|
|
|
|
/* SM_Object* dynaparent=0;
|
|
|
|
SumoPhysicsController* sumoparentctrl = (SumoPhysicsController* )parentctrl;
|
|
|
|
|
|
|
|
if (sumoparentctrl)
|
|
|
|
{
|
|
|
|
dynaparent = sumoparentctrl->GetSumoObject();
|
|
|
|
}
|
|
|
|
|
|
|
|
SM_Object* orgsumoobject = m_sumoObj;
|
|
|
|
|
|
|
|
|
|
|
|
m_sumoObj = new SM_Object(
|
|
|
|
orgsumoobject->getShapeHandle(),
|
|
|
|
orgsumoobject->getMaterialProps(),
|
|
|
|
orgsumoobject->getShapeProps(),
|
|
|
|
dynaparent);
|
|
|
|
|
|
|
|
m_sumoObj->setRigidBody(orgsumoobject->isRigidBody());
|
|
|
|
|
|
|
|
m_sumoObj->setMargin(orgsumoobject->getMargin());
|
|
|
|
m_sumoObj->setPosition(orgsumoobject->getPosition());
|
|
|
|
m_sumoObj->setOrientation(orgsumoobject->getOrientation());
|
|
|
|
//if it is a dyna, register for a callback
|
|
|
|
m_sumoObj->registerCallback(*this);
|
|
|
|
|
|
|
|
m_sumoScene->add(* (m_sumoObj));
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
2005-07-16 10:15:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// kinematic methods
|
|
|
|
void CcdPhysicsController::RelativeTranslate(float dlocX,float dlocY,float dlocZ,bool local)
|
|
|
|
{
|
2005-11-11 05:02:14 +00:00
|
|
|
SimdVector3 dloc(dlocX,dlocY,dlocZ);
|
2005-07-16 10:15:31 +00:00
|
|
|
SimdTransform xform = m_body->getCenterOfMassTransform();
|
2005-11-11 05:02:14 +00:00
|
|
|
|
|
|
|
if (local)
|
|
|
|
{
|
|
|
|
dloc = xform.getBasis()*dloc;
|
|
|
|
}
|
|
|
|
|
|
|
|
xform.setOrigin(xform.getOrigin() + dloc);
|
2005-07-16 10:15:31 +00:00
|
|
|
this->m_body->setCenterOfMassTransform(xform);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2005-08-17 19:52:56 +00:00
|
|
|
void CcdPhysicsController::RelativeRotate(const float rotval[9],bool local)
|
2005-08-08 17:08:42 +00:00
|
|
|
{
|
2005-08-17 19:52:56 +00:00
|
|
|
if (m_body )
|
|
|
|
{
|
|
|
|
SimdMatrix3x3 drotmat( rotval[0],rotval[1],rotval[2],
|
|
|
|
rotval[4],rotval[5],rotval[6],
|
|
|
|
rotval[8],rotval[9],rotval[10]);
|
|
|
|
|
|
|
|
|
|
|
|
SimdMatrix3x3 currentOrn;
|
|
|
|
GetWorldOrientation(currentOrn);
|
|
|
|
|
|
|
|
SimdTransform xform = m_body->getCenterOfMassTransform();
|
|
|
|
|
|
|
|
xform.setBasis(xform.getBasis()*(local ?
|
|
|
|
drotmat : (currentOrn.inverse() * drotmat * currentOrn)));
|
|
|
|
|
|
|
|
m_body->setCenterOfMassTransform(xform);
|
|
|
|
}
|
|
|
|
|
2005-08-08 17:08:42 +00:00
|
|
|
}
|
2005-08-17 19:52:56 +00:00
|
|
|
|
|
|
|
void CcdPhysicsController::GetWorldOrientation(SimdMatrix3x3& mat)
|
|
|
|
{
|
|
|
|
float orn[4];
|
|
|
|
m_MotionState->getWorldOrientation(orn[0],orn[1],orn[2],orn[3]);
|
|
|
|
SimdQuaternion quat(orn[0],orn[1],orn[2],orn[3]);
|
|
|
|
mat.setRotation(quat);
|
|
|
|
}
|
|
|
|
|
2005-07-16 10:15:31 +00:00
|
|
|
void CcdPhysicsController::getOrientation(float &quatImag0,float &quatImag1,float &quatImag2,float &quatReal)
|
|
|
|
{
|
2005-11-11 05:02:14 +00:00
|
|
|
SimdQuaternion q = m_body->getCenterOfMassTransform().getRotation();
|
|
|
|
quatImag0 = q[0];
|
|
|
|
quatImag1 = q[1];
|
|
|
|
quatImag2 = q[2];
|
|
|
|
quatReal = q[3];
|
2005-07-16 10:15:31 +00:00
|
|
|
}
|
|
|
|
void CcdPhysicsController::setOrientation(float quatImag0,float quatImag1,float quatImag2,float quatReal)
|
|
|
|
{
|
2005-08-13 08:05:48 +00:00
|
|
|
m_body->activate();
|
|
|
|
|
|
|
|
SimdTransform xform = m_body->getCenterOfMassTransform();
|
|
|
|
xform.setRotation(SimdQuaternion(quatImag0,quatImag1,quatImag2,quatReal));
|
|
|
|
m_body->setCenterOfMassTransform(xform);
|
|
|
|
|
2005-07-16 10:15:31 +00:00
|
|
|
}
|
2005-08-13 08:05:48 +00:00
|
|
|
|
2005-07-16 10:15:31 +00:00
|
|
|
void CcdPhysicsController::setPosition(float posX,float posY,float posZ)
|
|
|
|
{
|
2005-08-13 08:05:48 +00:00
|
|
|
m_body->activate();
|
|
|
|
|
|
|
|
SimdTransform xform = m_body->getCenterOfMassTransform();
|
|
|
|
xform.setOrigin(SimdVector3(posX,posY,posZ));
|
|
|
|
m_body->setCenterOfMassTransform(xform);
|
|
|
|
|
2005-07-16 10:15:31 +00:00
|
|
|
}
|
|
|
|
void CcdPhysicsController::resolveCombinedVelocities(float linvelX,float linvelY,float linvelZ,float angVelX,float angVelY,float angVelZ)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void CcdPhysicsController::getPosition(PHY__Vector3& pos) const
|
|
|
|
{
|
2005-12-31 21:59:56 +00:00
|
|
|
const SimdTransform& xform = m_body->getCenterOfMassTransform();
|
|
|
|
pos[0] = xform.getOrigin().x();
|
|
|
|
pos[1] = xform.getOrigin().y();
|
|
|
|
pos[2] = xform.getOrigin().z();
|
2005-07-16 10:15:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CcdPhysicsController::setScaling(float scaleX,float scaleY,float scaleZ)
|
|
|
|
{
|
2005-12-31 21:59:56 +00:00
|
|
|
if (!SimdFuzzyZero(m_cci.m_scaling.x()-scaleX) ||
|
|
|
|
!SimdFuzzyZero(m_cci.m_scaling.y()-scaleY) ||
|
|
|
|
!SimdFuzzyZero(m_cci.m_scaling.z()-scaleZ))
|
2005-11-11 05:02:14 +00:00
|
|
|
{
|
2005-12-31 21:59:56 +00:00
|
|
|
m_cci.m_scaling = SimdVector3(scaleX,scaleY,scaleZ);
|
|
|
|
|
|
|
|
if (m_body && m_body->GetCollisionShape())
|
|
|
|
{
|
|
|
|
m_body->GetCollisionShape()->setLocalScaling(m_cci.m_scaling);
|
|
|
|
m_body->GetCollisionShape()->CalculateLocalInertia(m_cci.m_mass, m_cci.m_localInertiaTensor);
|
|
|
|
m_body->setMassProps(m_cci.m_mass, m_cci.m_localInertiaTensor * m_cci.m_inertiaFactor);
|
|
|
|
}
|
2005-11-11 05:02:14 +00:00
|
|
|
}
|
2005-07-16 10:15:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// physics methods
|
|
|
|
void CcdPhysicsController::ApplyTorque(float torqueX,float torqueY,float torqueZ,bool local)
|
|
|
|
{
|
2005-11-11 05:02:14 +00:00
|
|
|
SimdVector3 torque(torqueX,torqueY,torqueZ);
|
|
|
|
SimdTransform xform = m_body->getCenterOfMassTransform();
|
|
|
|
if (local)
|
|
|
|
{
|
|
|
|
torque = xform.getBasis()*torque;
|
|
|
|
}
|
|
|
|
m_body->applyTorque(torque);
|
2005-07-16 10:15:31 +00:00
|
|
|
}
|
2005-11-11 05:02:14 +00:00
|
|
|
|
2005-07-16 10:15:31 +00:00
|
|
|
void CcdPhysicsController::ApplyForce(float forceX,float forceY,float forceZ,bool local)
|
|
|
|
{
|
2005-11-11 05:02:14 +00:00
|
|
|
SimdVector3 force(forceX,forceX,forceX);
|
|
|
|
SimdTransform xform = m_body->getCenterOfMassTransform();
|
|
|
|
if (local)
|
|
|
|
{
|
|
|
|
force = xform.getBasis()*force;
|
|
|
|
}
|
|
|
|
m_body->applyCentralForce(force);
|
2005-07-16 10:15:31 +00:00
|
|
|
}
|
|
|
|
void CcdPhysicsController::SetAngularVelocity(float ang_velX,float ang_velY,float ang_velZ,bool local)
|
|
|
|
{
|
|
|
|
SimdVector3 angvel(ang_velX,ang_velY,ang_velZ);
|
2005-11-11 05:02:14 +00:00
|
|
|
SimdTransform xform = m_body->getCenterOfMassTransform();
|
|
|
|
if (local)
|
|
|
|
{
|
|
|
|
angvel = xform.getBasis()*angvel;
|
|
|
|
}
|
2005-07-16 10:15:31 +00:00
|
|
|
|
|
|
|
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);
|
2005-11-11 05:02:14 +00:00
|
|
|
SimdTransform xform = m_body->getCenterOfMassTransform();
|
|
|
|
if (local)
|
|
|
|
{
|
|
|
|
linVel = xform.getBasis()*linVel;
|
|
|
|
}
|
2005-07-16 10:15:31 +00:00
|
|
|
m_body->setLinearVelocity(linVel);
|
|
|
|
}
|
|
|
|
void CcdPhysicsController::applyImpulse(float attachX,float attachY,float attachZ, float impulseX,float impulseY,float impulseZ)
|
|
|
|
{
|
2005-08-05 22:10:27 +00:00
|
|
|
|
2005-08-05 17:00:32 +00:00
|
|
|
SimdVector3 impulse(impulseX,impulseY,impulseZ);
|
|
|
|
SimdVector3 pos(attachX,attachY,attachZ);
|
|
|
|
|
2005-08-13 08:05:48 +00:00
|
|
|
m_body->activate();
|
2005-08-05 17:00:32 +00:00
|
|
|
|
|
|
|
m_body->applyImpulse(impulse,pos);
|
|
|
|
|
2005-07-16 10:15:31 +00:00
|
|
|
}
|
|
|
|
void CcdPhysicsController::SetActive(bool active)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
// reading out information from physics
|
|
|
|
void CcdPhysicsController::GetLinearVelocity(float& linvX,float& linvY,float& linvZ)
|
|
|
|
{
|
2005-11-11 05:02:14 +00:00
|
|
|
const SimdVector3& linvel = this->m_body->getLinearVelocity();
|
|
|
|
linvX = linvel.x();
|
|
|
|
linvY = linvel.y();
|
|
|
|
linvZ = linvel.z();
|
|
|
|
|
2005-07-16 10:15:31 +00:00
|
|
|
}
|
2005-12-31 07:20:08 +00:00
|
|
|
|
|
|
|
void CcdPhysicsController::GetAngularVelocity(float& angVelX,float& angVelY,float& angVelZ)
|
|
|
|
{
|
|
|
|
const SimdVector3& angvel= m_body->getAngularVelocity();
|
|
|
|
angVelX = angvel.x();
|
|
|
|
angVelY = angvel.y();
|
|
|
|
angVelZ = angvel.z();
|
|
|
|
}
|
|
|
|
|
2005-07-16 10:15:31 +00:00
|
|
|
void CcdPhysicsController::GetVelocity(const float posX,const float posY,const float posZ,float& linvX,float& linvY,float& linvZ)
|
|
|
|
{
|
2005-11-11 05:02:14 +00:00
|
|
|
SimdVector3 pos(posX,posY,posZ);
|
|
|
|
SimdVector3 rel_pos = pos-m_body->getCenterOfMassPosition();
|
|
|
|
SimdVector3 linvel = m_body->getVelocityInLocalPoint(rel_pos);
|
|
|
|
linvX = linvel.x();
|
|
|
|
linvY = linvel.y();
|
|
|
|
linvZ = linvel.z();
|
2005-07-16 10:15:31 +00:00
|
|
|
}
|
|
|
|
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()
|
|
|
|
{
|
2005-08-05 17:00:32 +00:00
|
|
|
return m_newClientInfo;
|
2005-07-16 10:15:31 +00:00
|
|
|
}
|
|
|
|
void CcdPhysicsController::setNewClientInfo(void* clientinfo)
|
|
|
|
{
|
2005-08-05 17:00:32 +00:00
|
|
|
m_newClientInfo = clientinfo;
|
2005-07-16 10:15:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-08-04 19:07:39 +00:00
|
|
|
void CcdPhysicsController::UpdateDeactivation(float timeStep)
|
2005-07-16 10:15:31 +00:00
|
|
|
{
|
2005-08-04 19:07:39 +00:00
|
|
|
if ( (m_body->GetActivationState() == 2))
|
|
|
|
return;
|
|
|
|
|
2005-07-16 10:15:31 +00:00
|
|
|
|
2005-08-03 18:22:30 +00:00
|
|
|
if ((m_body->getLinearVelocity().length2() < gLinearSleepingTreshold*gLinearSleepingTreshold) &&
|
2005-07-16 10:15:31 +00:00
|
|
|
(m_body->getAngularVelocity().length2() < gAngularSleepingTreshold*gAngularSleepingTreshold))
|
|
|
|
{
|
2005-08-04 19:07:39 +00:00
|
|
|
m_body->m_deactivationTime += timeStep;
|
2005-07-16 10:15:31 +00:00
|
|
|
} else
|
|
|
|
{
|
2005-08-04 19:07:39 +00:00
|
|
|
m_body->m_deactivationTime=0.f;
|
|
|
|
m_body->SetActivationState(0);
|
2005-07-16 10:15:31 +00:00
|
|
|
}
|
|
|
|
|
2005-08-04 19:07:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CcdPhysicsController::wantsSleeping()
|
|
|
|
{
|
|
|
|
|
|
|
|
//disable deactivation
|
2005-08-13 08:05:48 +00:00
|
|
|
if (gDisableDeactivation || (gDeactivationTime == 0.f))
|
2005-08-04 19:07:39 +00:00
|
|
|
return false;
|
|
|
|
//2 == ISLAND_SLEEPING, 3 == WANTS_DEACTIVATION
|
|
|
|
if ( (m_body->GetActivationState() == 2) || (m_body->GetActivationState() == 3))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if (m_body->m_deactivationTime> gDeactivationTime)
|
2005-07-16 10:15:31 +00:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|