2011-02-25 13:35:59 +00:00
|
|
|
/** \file gameengine/Ketsji/KX_BulletPhysicsController.cpp
|
|
|
|
* \ingroup ketsji
|
|
|
|
*/
|
2005-07-18 05:41:00 +00:00
|
|
|
//under visual studio the #define in KX_ConvertPhysicsObject.h is quicker for recompilation
|
|
|
|
#include "KX_ConvertPhysicsObject.h"
|
|
|
|
|
2013-01-23 07:26:39 +00:00
|
|
|
#ifdef WITH_BULLET
|
2005-07-16 22:13:20 +00:00
|
|
|
|
2005-07-16 21:47:54 +00:00
|
|
|
#include "KX_BulletPhysicsController.h"
|
|
|
|
|
2006-11-21 00:53:40 +00:00
|
|
|
#include "btBulletDynamicsCommon.h"
|
2005-12-31 07:20:08 +00:00
|
|
|
#include "SG_Spatial.h"
|
|
|
|
|
|
|
|
#include "KX_GameObject.h"
|
|
|
|
#include "KX_MotionState.h"
|
|
|
|
#include "KX_ClientObjectInfo.h"
|
|
|
|
|
|
|
|
#include "PHY_IPhysicsEnvironment.h"
|
2008-07-19 10:27:52 +00:00
|
|
|
#include "CcdPhysicsEnvironment.h"
|
2008-09-25 17:53:15 +00:00
|
|
|
#include "BulletSoftBody/btSoftBody.h"
|
2005-12-31 07:20:08 +00:00
|
|
|
|
2006-06-18 22:10:00 +00:00
|
|
|
|
2012-05-28 21:36:29 +00:00
|
|
|
KX_BulletPhysicsController::KX_BulletPhysicsController (const CcdConstructionInfo& ci, bool dyna, bool sensor, bool character, bool compound)
|
|
|
|
: KX_IPhysicsController(dyna,sensor,character,compound,(PHY_IPhysicsController*)this),
|
2008-07-18 14:40:24 +00:00
|
|
|
CcdPhysicsController(ci),
|
2009-01-13 22:59:18 +00:00
|
|
|
m_savedCollisionFlags(0),
|
2009-04-30 19:00:17 +00:00
|
|
|
m_savedCollisionFilterGroup(0),
|
|
|
|
m_savedCollisionFilterMask(0),
|
|
|
|
m_savedMass(0.0),
|
|
|
|
m_savedDyna(false),
|
|
|
|
m_suspended(false),
|
2009-01-13 22:59:18 +00:00
|
|
|
m_bulletChildShape(NULL)
|
2005-07-16 21:47:54 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
KX_BulletPhysicsController::~KX_BulletPhysicsController ()
|
|
|
|
{
|
2008-03-09 21:51:38 +00:00
|
|
|
// The game object has a direct link to
|
|
|
|
if (m_pObject)
|
|
|
|
{
|
|
|
|
// If we cheat in SetObject, we must also cheat here otherwise the
|
|
|
|
// object will still things it has a physical controller
|
|
|
|
// Note that it requires that m_pObject is reset in case the object is deleted
|
|
|
|
// before the controller (usual case, see KX_Scene::RemoveNodeDestructObjec)
|
|
|
|
// The non usual case is when the object is not deleted because its reference is hanging
|
|
|
|
// in a AddObject actuator but the node is deleted. This case is covered here.
|
|
|
|
KX_GameObject* gameobj = (KX_GameObject*) m_pObject->GetSGClientObject();
|
|
|
|
gameobj->SetPhysicsController(NULL,false);
|
|
|
|
}
|
2005-07-16 21:47:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void KX_BulletPhysicsController::resolveCombinedVelocities(float linvelX,float linvelY,float linvelZ,float angVelX,float angVelY,float angVelZ)
|
|
|
|
{
|
|
|
|
CcdPhysicsController::resolveCombinedVelocities(linvelX,linvelY,linvelZ,angVelX,angVelY,angVelZ);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////
|
|
|
|
// KX_IPhysicsController interface
|
|
|
|
////////////////////////////////////
|
|
|
|
|
|
|
|
void KX_BulletPhysicsController::applyImpulse(const MT_Point3& attach, const MT_Vector3& impulse)
|
|
|
|
{
|
2005-08-05 17:00:32 +00:00
|
|
|
CcdPhysicsController::applyImpulse(attach[0],attach[1],attach[2],impulse[0],impulse[1],impulse[2]);
|
|
|
|
|
2005-07-16 21:47:54 +00:00
|
|
|
}
|
|
|
|
|
2009-04-14 12:34:39 +00:00
|
|
|
float KX_BulletPhysicsController::GetLinVelocityMin()
|
|
|
|
{
|
|
|
|
return (float)CcdPhysicsController::GetLinVelocityMin();
|
|
|
|
}
|
|
|
|
void KX_BulletPhysicsController::SetLinVelocityMin(float val)
|
|
|
|
{
|
|
|
|
CcdPhysicsController::SetLinVelocityMin(val);
|
|
|
|
}
|
|
|
|
|
2013-01-30 05:55:17 +00:00
|
|
|
void KX_BulletPhysicsController::Jump()
|
|
|
|
{
|
|
|
|
CcdPhysicsController::Jump();
|
|
|
|
}
|
|
|
|
|
2009-04-14 12:34:39 +00:00
|
|
|
float KX_BulletPhysicsController::GetLinVelocityMax()
|
|
|
|
{
|
|
|
|
return (float)CcdPhysicsController::GetLinVelocityMax();
|
|
|
|
}
|
|
|
|
void KX_BulletPhysicsController::SetLinVelocityMax(float val)
|
|
|
|
{
|
|
|
|
CcdPhysicsController::SetLinVelocityMax(val);
|
|
|
|
}
|
|
|
|
|
2005-07-16 21:47:54 +00:00
|
|
|
void KX_BulletPhysicsController::SetObject (SG_IObject* object)
|
|
|
|
{
|
2005-12-31 07:20:08 +00:00
|
|
|
SG_Controller::SetObject(object);
|
|
|
|
|
|
|
|
// cheating here...
|
|
|
|
//should not be necessary, is it for duplicates ?
|
|
|
|
|
|
|
|
KX_GameObject* gameobj = (KX_GameObject*) object->GetSGClientObject();
|
|
|
|
gameobj->SetPhysicsController(this,gameobj->IsDynamic());
|
2006-04-24 02:03:55 +00:00
|
|
|
CcdPhysicsController::setNewClientInfo(gameobj->getClientInfo());
|
2005-12-31 07:20:08 +00:00
|
|
|
|
2009-06-01 09:44:41 +00:00
|
|
|
if (m_bSensor)
|
|
|
|
{
|
|
|
|
// use a different callback function for sensor object,
|
2011-10-22 01:53:35 +00:00
|
|
|
// bullet will not synchronize, we must do it explicitly
|
2009-06-01 09:44:41 +00:00
|
|
|
SG_Callbacks& callbacks = gameobj->GetSGNode()->GetCallBackFunctions();
|
|
|
|
callbacks.m_updatefunc = KX_GameObject::SynchronizeTransformFunc;
|
|
|
|
}
|
2005-07-16 21:47:54 +00:00
|
|
|
}
|
|
|
|
|
2009-04-14 12:34:39 +00:00
|
|
|
MT_Scalar KX_BulletPhysicsController::GetRadius()
|
|
|
|
{
|
|
|
|
return MT_Scalar(CcdPhysicsController::GetRadius());
|
|
|
|
}
|
2008-03-09 21:51:38 +00:00
|
|
|
|
2006-11-21 00:53:40 +00:00
|
|
|
void KX_BulletPhysicsController::setMargin (float collisionMargin)
|
2006-06-18 22:10:00 +00:00
|
|
|
{
|
|
|
|
CcdPhysicsController::SetMargin(collisionMargin);
|
|
|
|
}
|
2005-07-16 21:47:54 +00:00
|
|
|
void KX_BulletPhysicsController::RelativeTranslate(const MT_Vector3& dloc,bool local)
|
|
|
|
{
|
|
|
|
CcdPhysicsController::RelativeTranslate(dloc[0],dloc[1],dloc[2],local);
|
|
|
|
|
|
|
|
}
|
2005-08-08 17:08:42 +00:00
|
|
|
|
2013-01-30 05:55:17 +00:00
|
|
|
void KX_BulletPhysicsController::SetWalkDirection(const MT_Vector3& dloc,bool local)
|
|
|
|
{
|
|
|
|
CcdPhysicsController::SetWalkDirection(dloc[0],dloc[1],dloc[2],local);
|
|
|
|
}
|
|
|
|
|
2005-07-16 21:47:54 +00:00
|
|
|
void KX_BulletPhysicsController::RelativeRotate(const MT_Matrix3x3& drot,bool local)
|
|
|
|
{
|
2012-04-29 17:23:19 +00:00
|
|
|
float rotval[9];
|
|
|
|
drot.getValue3x3(rotval);
|
2005-08-08 17:08:42 +00:00
|
|
|
CcdPhysicsController::RelativeRotate(rotval,local);
|
2005-07-16 21:47:54 +00:00
|
|
|
}
|
2005-08-08 17:08:42 +00:00
|
|
|
|
2005-07-16 21:47:54 +00:00
|
|
|
void KX_BulletPhysicsController::ApplyTorque(const MT_Vector3& torque,bool local)
|
|
|
|
{
|
2005-12-31 07:20:08 +00:00
|
|
|
CcdPhysicsController::ApplyTorque(torque.x(),torque.y(),torque.z(),local);
|
2005-07-16 21:47:54 +00:00
|
|
|
}
|
|
|
|
void KX_BulletPhysicsController::ApplyForce(const MT_Vector3& force,bool local)
|
|
|
|
{
|
2005-12-31 07:20:08 +00:00
|
|
|
CcdPhysicsController::ApplyForce(force.x(),force.y(),force.z(),local);
|
2005-07-16 21:47:54 +00:00
|
|
|
}
|
|
|
|
MT_Vector3 KX_BulletPhysicsController::GetLinearVelocity()
|
|
|
|
{
|
2005-12-31 07:20:08 +00:00
|
|
|
float angVel[3];
|
2006-12-16 05:50:38 +00:00
|
|
|
//CcdPhysicsController::GetAngularVelocity(angVel[0],angVel[1],angVel[2]);
|
|
|
|
CcdPhysicsController::GetLinearVelocity(angVel[0],angVel[1],angVel[2]);//rcruiz
|
2005-12-31 07:20:08 +00:00
|
|
|
return MT_Vector3(angVel[0],angVel[1],angVel[2]);
|
2005-07-16 21:47:54 +00:00
|
|
|
}
|
2008-06-24 19:37:43 +00:00
|
|
|
MT_Vector3 KX_BulletPhysicsController::GetAngularVelocity()
|
|
|
|
{
|
|
|
|
float angVel[3];
|
|
|
|
//CcdPhysicsController::GetAngularVelocity(angVel[0],angVel[1],angVel[2]);
|
|
|
|
CcdPhysicsController::GetAngularVelocity(angVel[0],angVel[1],angVel[2]);//rcruiz
|
|
|
|
return MT_Vector3(angVel[0],angVel[1],angVel[2]);
|
|
|
|
}
|
2005-07-16 21:47:54 +00:00
|
|
|
MT_Vector3 KX_BulletPhysicsController::GetVelocity(const MT_Point3& pos)
|
|
|
|
{
|
2005-12-31 07:20:08 +00:00
|
|
|
float linVel[3];
|
2008-10-01 07:55:02 +00:00
|
|
|
CcdPhysicsController::GetVelocity(pos[0], pos[1], pos[2], linVel[0],linVel[1],linVel[2]);
|
2005-12-31 07:20:08 +00:00
|
|
|
return MT_Vector3(linVel[0],linVel[1],linVel[2]);
|
2005-07-16 21:47:54 +00:00
|
|
|
}
|
2005-12-31 07:20:08 +00:00
|
|
|
|
2013-01-30 05:55:17 +00:00
|
|
|
MT_Vector3 KX_BulletPhysicsController::GetWalkDirection()
|
|
|
|
{
|
|
|
|
float dir[3];
|
|
|
|
CcdPhysicsController::GetWalkDirection(dir[0], dir[1], dir[2]);
|
|
|
|
return MT_Vector3(dir[0], dir[1], dir[2]);
|
|
|
|
}
|
|
|
|
|
2005-07-16 21:47:54 +00:00
|
|
|
void KX_BulletPhysicsController::SetAngularVelocity(const MT_Vector3& ang_vel,bool local)
|
|
|
|
{
|
|
|
|
CcdPhysicsController::SetAngularVelocity(ang_vel.x(),ang_vel.y(),ang_vel.z(),local);
|
|
|
|
|
|
|
|
}
|
|
|
|
void KX_BulletPhysicsController::SetLinearVelocity(const MT_Vector3& lin_vel,bool local)
|
|
|
|
{
|
|
|
|
CcdPhysicsController::SetLinearVelocity(lin_vel.x(),lin_vel.y(),lin_vel.z(),local);
|
|
|
|
}
|
|
|
|
void KX_BulletPhysicsController::getOrientation(MT_Quaternion& orn)
|
|
|
|
{
|
2005-12-31 07:20:08 +00:00
|
|
|
float myorn[4];
|
|
|
|
CcdPhysicsController::getOrientation(myorn[0],myorn[1],myorn[2],myorn[3]);
|
|
|
|
orn = MT_Quaternion(myorn[0],myorn[1],myorn[2],myorn[3]);
|
2005-07-16 21:47:54 +00:00
|
|
|
}
|
2008-07-21 12:37:27 +00:00
|
|
|
void KX_BulletPhysicsController::setOrientation(const MT_Matrix3x3& orn)
|
2005-07-16 21:47:54 +00:00
|
|
|
{
|
2008-07-22 09:53:25 +00:00
|
|
|
btMatrix3x3 btmat(orn[0][0], orn[0][1], orn[0][2], orn[1][0], orn[1][1], orn[1][2], orn[2][0], orn[2][1], orn[2][2]);
|
2008-07-21 12:37:27 +00:00
|
|
|
CcdPhysicsController::setWorldOrientation(btmat);
|
2005-07-16 21:47:54 +00:00
|
|
|
}
|
|
|
|
void KX_BulletPhysicsController::setPosition(const MT_Point3& pos)
|
|
|
|
{
|
2005-12-31 07:20:08 +00:00
|
|
|
CcdPhysicsController::setPosition(pos.x(),pos.y(),pos.z());
|
2005-07-16 21:47:54 +00:00
|
|
|
}
|
|
|
|
void KX_BulletPhysicsController::setScaling(const MT_Vector3& scaling)
|
|
|
|
{
|
2005-12-31 07:20:08 +00:00
|
|
|
CcdPhysicsController::setScaling(scaling.x(),scaling.y(),scaling.z());
|
2005-07-16 21:47:54 +00:00
|
|
|
}
|
BGE: new sensor object to generalize Near and Radar sensor, static-static collision capbility.
A new type of "Sensor" physics object is available in the GE for advanced
collision management. It's called Sensor for its similarities with the
physics objects that underlie the Near and Radar sensors.
Like the Near and Radar object it is:
- static and ghost
- invisible by default
- always active to ensure correct collision detection
- capable of detecting both static and dynamic objects
- ignoring collision with their parent
- capable of broadphase filtering based on:
* Actor option: the collisioning object must have the Actor flag set to be detected
* property/material: as specified in the collision sensors attached to it
Broadphase filtering is important for performance reason: the collision points
will be computed only for the objects that pass the broahphase filter.
- automatically removed from the simulation when no collision sensor is active on it
Unlike the Near and Radar object it can:
- take any shape, including triangle mesh
- be made visible for debugging (just use the Visible actuator)
- have multiple collision sensors using it
Other than that, the sensor objects are ordinary objects. You can move them
freely or parent them. When parented to a dynamic object, they can provide
advanced collision control to this object.
The type of collision capability depends on the shape:
- box, sphere, cylinder, cone, convex hull provide volume detection.
- triangle mesh provides surface detection but you can give some volume
to the suface by increasing the margin in the Advanced Settings panel.
The margin applies on both sides of the surface.
Performance tip:
- Sensor objects perform better than Near and Radar: they do less synchronizations
because of the Scenegraph optimizations and they can have multiple collision sensors
on them (with different property filtering for example).
- Always prefer simple shape (box, sphere) to complex shape whenever possible.
- Always use broadphase filtering (avoid collision sensor with empty propery/material)
- Use collision sensor only when you need them. When no collision sensor is active
on the sensor object, it is removed from the simulation and consume no CPU.
Known limitations:
- When running Blender in debug mode, you will see one warning line of the console:
"warning btCollisionDispatcher::needsCollision: static-static collision!"
In release mode this message is not printed.
- Collision margin has no effect on sphere, cone and cylinder shape.
Other performance improvements:
- Remove unnecessary interpolation for Near and Radar objects and by extension
sensor objects.
- Use direct matrix copy instead of quaternion to synchronize orientation.
Other bug fix:
- Fix Near/Radar position error on newly activated objects. This was causing
several detection problems in YoFrankie
- Fix margin not passed correctly to gImpact shape.
- Disable force/velocity actions on static objects
2009-05-17 12:51:51 +00:00
|
|
|
void KX_BulletPhysicsController::SetTransform()
|
|
|
|
{
|
|
|
|
btVector3 pos;
|
|
|
|
btVector3 scale;
|
|
|
|
float ori[12];
|
|
|
|
m_MotionState->getWorldPosition(pos.m_floats[0],pos.m_floats[1],pos.m_floats[2]);
|
|
|
|
m_MotionState->getWorldScaling(scale.m_floats[0],scale.m_floats[1],scale.m_floats[2]);
|
|
|
|
m_MotionState->getWorldOrientation(ori);
|
|
|
|
btMatrix3x3 rot(ori[0], ori[4], ori[8],
|
|
|
|
ori[1], ori[5], ori[9],
|
|
|
|
ori[2], ori[6], ori[10]);
|
|
|
|
CcdPhysicsController::forceWorldTransform(rot, pos);
|
|
|
|
}
|
|
|
|
|
2005-07-16 21:47:54 +00:00
|
|
|
MT_Scalar KX_BulletPhysicsController::GetMass()
|
|
|
|
{
|
2008-09-25 17:53:15 +00:00
|
|
|
if (GetSoftBody())
|
|
|
|
return GetSoftBody()->getTotalMass();
|
|
|
|
|
|
|
|
MT_Scalar invmass = 0.f;
|
|
|
|
if (GetRigidBody())
|
|
|
|
invmass = GetRigidBody()->getInvMass();
|
2005-07-16 21:47:54 +00:00
|
|
|
if (invmass)
|
|
|
|
return 1.f/invmass;
|
|
|
|
return 0.f;
|
|
|
|
|
|
|
|
}
|
2008-10-01 07:55:02 +00:00
|
|
|
|
2009-04-08 16:25:00 +00:00
|
|
|
MT_Vector3 KX_BulletPhysicsController::GetLocalInertia()
|
|
|
|
{
|
2011-09-01 02:12:53 +00:00
|
|
|
MT_Vector3 inertia(0.f, 0.f, 0.f);
|
|
|
|
btVector3 inv_inertia;
|
|
|
|
if (GetRigidBody()) {
|
|
|
|
inv_inertia = GetRigidBody()->getInvInertiaDiagLocal();
|
|
|
|
if (!btFuzzyZero(inv_inertia.getX()) &&
|
|
|
|
!btFuzzyZero(inv_inertia.getY()) &&
|
|
|
|
!btFuzzyZero(inv_inertia.getZ()))
|
2009-04-08 16:25:00 +00:00
|
|
|
inertia = MT_Vector3(1.f/inv_inertia.getX(), 1.f/inv_inertia.getY(), 1.f/inv_inertia.getZ());
|
2011-09-01 02:12:53 +00:00
|
|
|
}
|
|
|
|
return inertia;
|
2009-04-08 16:25:00 +00:00
|
|
|
}
|
|
|
|
|
2005-07-16 21:47:54 +00:00
|
|
|
MT_Vector3 KX_BulletPhysicsController::getReactionForce()
|
|
|
|
{
|
|
|
|
assert(0);
|
|
|
|
return MT_Vector3(0.f,0.f,0.f);
|
|
|
|
}
|
|
|
|
void KX_BulletPhysicsController::setRigidBody(bool rigid)
|
|
|
|
{
|
2012-07-09 04:57:21 +00:00
|
|
|
CcdPhysicsController::setRigidBody(rigid);
|
2005-07-16 21:47:54 +00:00
|
|
|
}
|
|
|
|
|
2009-01-13 22:59:18 +00:00
|
|
|
/* This function dynamically adds the collision shape of another controller to
|
2012-03-09 18:28:30 +00:00
|
|
|
* the current controller shape provided it is a compound shape.
|
|
|
|
* The idea is that dynamic parenting on a compound object will dynamically extend the shape
|
|
|
|
*/
|
2009-01-13 22:59:18 +00:00
|
|
|
void KX_BulletPhysicsController::AddCompoundChild(KX_IPhysicsController* child)
|
|
|
|
{
|
|
|
|
if (child == NULL || !IsCompound())
|
|
|
|
return;
|
|
|
|
// other controller must be a bullet controller too
|
|
|
|
// verify that body and shape exist and match
|
|
|
|
KX_BulletPhysicsController* childCtrl = dynamic_cast<KX_BulletPhysicsController*>(child);
|
|
|
|
btRigidBody* rootBody = GetRigidBody();
|
|
|
|
btRigidBody* childBody = childCtrl->GetRigidBody();
|
|
|
|
if (!rootBody || !childBody)
|
|
|
|
return;
|
|
|
|
const btCollisionShape* rootShape = rootBody->getCollisionShape();
|
|
|
|
const btCollisionShape* childShape = childBody->getCollisionShape();
|
|
|
|
if (!rootShape ||
|
|
|
|
!childShape ||
|
|
|
|
rootShape->getShapeType() != COMPOUND_SHAPE_PROXYTYPE ||
|
|
|
|
childShape->getShapeType() == COMPOUND_SHAPE_PROXYTYPE)
|
|
|
|
return;
|
|
|
|
btCompoundShape* compoundShape = (btCompoundShape*)rootShape;
|
|
|
|
// compute relative transformation between parent and child
|
|
|
|
btTransform rootTrans;
|
|
|
|
btTransform childTrans;
|
|
|
|
rootBody->getMotionState()->getWorldTransform(rootTrans);
|
|
|
|
childBody->getMotionState()->getWorldTransform(childTrans);
|
|
|
|
btVector3 rootScale = rootShape->getLocalScaling();
|
|
|
|
rootScale[0] = 1.0/rootScale[0];
|
|
|
|
rootScale[1] = 1.0/rootScale[1];
|
|
|
|
rootScale[2] = 1.0/rootScale[2];
|
|
|
|
// relative scale = child_scale/parent_scale
|
|
|
|
btVector3 relativeScale = childShape->getLocalScaling()*rootScale;
|
2012-09-16 04:58:18 +00:00
|
|
|
btMatrix3x3 rootRotInverse = rootTrans.getBasis().transpose();
|
2009-01-13 22:59:18 +00:00
|
|
|
// relative pos = parent_rot^-1 * ((parent_pos-child_pos)/parent_scale)
|
|
|
|
btVector3 relativePos = rootRotInverse*((childTrans.getOrigin()-rootTrans.getOrigin())*rootScale);
|
|
|
|
// relative rot = parent_rot^-1 * child_rot
|
|
|
|
btMatrix3x3 relativeRot = rootRotInverse*childTrans.getBasis();
|
|
|
|
// create a proxy shape info to store the transformation
|
|
|
|
CcdShapeConstructionInfo* proxyShapeInfo = new CcdShapeConstructionInfo();
|
|
|
|
// store the transformation to this object shapeinfo
|
|
|
|
proxyShapeInfo->m_childTrans.setOrigin(relativePos);
|
|
|
|
proxyShapeInfo->m_childTrans.setBasis(relativeRot);
|
|
|
|
proxyShapeInfo->m_childScale.setValue(relativeScale[0], relativeScale[1], relativeScale[2]);
|
|
|
|
// we will need this to make sure that we remove the right proxy later when unparenting
|
|
|
|
proxyShapeInfo->m_userData = childCtrl;
|
|
|
|
proxyShapeInfo->SetProxy(childCtrl->GetShapeInfo()->AddRef());
|
2010-02-03 21:41:03 +00:00
|
|
|
// add to parent compound shapeinfo (increments ref count)
|
2009-01-13 22:59:18 +00:00
|
|
|
GetShapeInfo()->AddShape(proxyShapeInfo);
|
|
|
|
// create new bullet collision shape from the object shapeinfo and set scaling
|
2010-02-03 21:41:03 +00:00
|
|
|
btCollisionShape* newChildShape = proxyShapeInfo->CreateBulletShape(childCtrl->GetMargin(), childCtrl->getConstructionInfo().m_bGimpact, true);
|
2009-01-13 22:59:18 +00:00
|
|
|
newChildShape->setLocalScaling(relativeScale);
|
|
|
|
// add bullet collision shape to parent compound collision shape
|
|
|
|
compoundShape->addChildShape(proxyShapeInfo->m_childTrans,newChildShape);
|
2010-02-03 21:41:03 +00:00
|
|
|
// proxyShapeInfo is not needed anymore, release it
|
|
|
|
proxyShapeInfo->Release();
|
2009-01-13 22:59:18 +00:00
|
|
|
// remember we created this shape
|
|
|
|
childCtrl->m_bulletChildShape = newChildShape;
|
|
|
|
// recompute inertia of parent
|
|
|
|
if (!rootBody->isStaticOrKinematicObject())
|
|
|
|
{
|
|
|
|
btVector3 localInertia;
|
|
|
|
float mass = 1.f/rootBody->getInvMass();
|
|
|
|
compoundShape->calculateLocalInertia(mass,localInertia);
|
|
|
|
rootBody->setMassProps(mass,localInertia);
|
|
|
|
}
|
|
|
|
// must update the broadphase cache,
|
|
|
|
GetPhysicsEnvironment()->refreshCcdPhysicsController(this);
|
|
|
|
// remove the children
|
|
|
|
GetPhysicsEnvironment()->disableCcdPhysicsController(childCtrl);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Reverse function of the above, it will remove a shape from a compound shape
|
2012-03-09 18:28:30 +00:00
|
|
|
* provided that the former was added to the later using AddCompoundChild()
|
|
|
|
*/
|
2009-01-13 22:59:18 +00:00
|
|
|
void KX_BulletPhysicsController::RemoveCompoundChild(KX_IPhysicsController* child)
|
|
|
|
{
|
|
|
|
if (child == NULL || !IsCompound())
|
|
|
|
return;
|
|
|
|
// other controller must be a bullet controller too
|
|
|
|
// verify that body and shape exist and match
|
|
|
|
KX_BulletPhysicsController* childCtrl = dynamic_cast<KX_BulletPhysicsController*>(child);
|
|
|
|
btRigidBody* rootBody = GetRigidBody();
|
|
|
|
btRigidBody* childBody = childCtrl->GetRigidBody();
|
|
|
|
if (!rootBody || !childBody)
|
|
|
|
return;
|
|
|
|
const btCollisionShape* rootShape = rootBody->getCollisionShape();
|
|
|
|
if (!rootShape ||
|
|
|
|
rootShape->getShapeType() != COMPOUND_SHAPE_PROXYTYPE)
|
|
|
|
return;
|
|
|
|
btCompoundShape* compoundShape = (btCompoundShape*)rootShape;
|
|
|
|
// retrieve the shapeInfo
|
|
|
|
CcdShapeConstructionInfo* childShapeInfo = childCtrl->GetShapeInfo();
|
|
|
|
CcdShapeConstructionInfo* rootShapeInfo = GetShapeInfo();
|
|
|
|
// and verify that the child is part of the parent
|
|
|
|
int i = rootShapeInfo->FindChildShape(childShapeInfo, childCtrl);
|
|
|
|
if (i < 0)
|
|
|
|
return;
|
|
|
|
rootShapeInfo->RemoveChildShape(i);
|
|
|
|
if (childCtrl->m_bulletChildShape)
|
|
|
|
{
|
|
|
|
int numChildren = compoundShape->getNumChildShapes();
|
|
|
|
for (i=0; i<numChildren; i++)
|
|
|
|
{
|
|
|
|
if (compoundShape->getChildShape(i) == childCtrl->m_bulletChildShape)
|
|
|
|
{
|
|
|
|
compoundShape->removeChildShapeByIndex(i);
|
|
|
|
compoundShape->recalculateLocalAabb();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
delete childCtrl->m_bulletChildShape;
|
|
|
|
childCtrl->m_bulletChildShape = NULL;
|
|
|
|
}
|
|
|
|
// recompute inertia of parent
|
|
|
|
if (!rootBody->isStaticOrKinematicObject())
|
|
|
|
{
|
|
|
|
btVector3 localInertia;
|
|
|
|
float mass = 1.f/rootBody->getInvMass();
|
|
|
|
compoundShape->calculateLocalInertia(mass,localInertia);
|
|
|
|
rootBody->setMassProps(mass,localInertia);
|
|
|
|
}
|
|
|
|
// must update the broadphase cache,
|
|
|
|
GetPhysicsEnvironment()->refreshCcdPhysicsController(this);
|
|
|
|
// reactivate the children
|
|
|
|
GetPhysicsEnvironment()->enableCcdPhysicsController(childCtrl);
|
|
|
|
}
|
|
|
|
|
2009-01-14 22:33:39 +00:00
|
|
|
void KX_BulletPhysicsController::SetMass(MT_Scalar newmass)
|
|
|
|
{
|
|
|
|
btRigidBody *body = GetRigidBody();
|
2009-04-30 19:00:17 +00:00
|
|
|
if (body && !m_suspended && newmass>MT_EPSILON && GetMass()>MT_EPSILON)
|
2009-01-14 22:33:39 +00:00
|
|
|
{
|
|
|
|
btVector3 grav = body->getGravity();
|
|
|
|
btVector3 accel = grav / GetMass();
|
|
|
|
|
|
|
|
btBroadphaseProxy* handle = body->getBroadphaseHandle();
|
|
|
|
GetPhysicsEnvironment()->updateCcdPhysicsController(this,
|
|
|
|
newmass,
|
|
|
|
body->getCollisionFlags(),
|
|
|
|
handle->m_collisionFilterGroup,
|
|
|
|
handle->m_collisionFilterMask);
|
|
|
|
body->setGravity(accel);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-07-18 14:40:24 +00:00
|
|
|
void KX_BulletPhysicsController::SuspendDynamics(bool ghost)
|
2005-07-16 21:47:54 +00:00
|
|
|
{
|
2008-07-18 14:40:24 +00:00
|
|
|
btRigidBody *body = GetRigidBody();
|
BGE: new sensor object to generalize Near and Radar sensor, static-static collision capbility.
A new type of "Sensor" physics object is available in the GE for advanced
collision management. It's called Sensor for its similarities with the
physics objects that underlie the Near and Radar sensors.
Like the Near and Radar object it is:
- static and ghost
- invisible by default
- always active to ensure correct collision detection
- capable of detecting both static and dynamic objects
- ignoring collision with their parent
- capable of broadphase filtering based on:
* Actor option: the collisioning object must have the Actor flag set to be detected
* property/material: as specified in the collision sensors attached to it
Broadphase filtering is important for performance reason: the collision points
will be computed only for the objects that pass the broahphase filter.
- automatically removed from the simulation when no collision sensor is active on it
Unlike the Near and Radar object it can:
- take any shape, including triangle mesh
- be made visible for debugging (just use the Visible actuator)
- have multiple collision sensors using it
Other than that, the sensor objects are ordinary objects. You can move them
freely or parent them. When parented to a dynamic object, they can provide
advanced collision control to this object.
The type of collision capability depends on the shape:
- box, sphere, cylinder, cone, convex hull provide volume detection.
- triangle mesh provides surface detection but you can give some volume
to the suface by increasing the margin in the Advanced Settings panel.
The margin applies on both sides of the surface.
Performance tip:
- Sensor objects perform better than Near and Radar: they do less synchronizations
because of the Scenegraph optimizations and they can have multiple collision sensors
on them (with different property filtering for example).
- Always prefer simple shape (box, sphere) to complex shape whenever possible.
- Always use broadphase filtering (avoid collision sensor with empty propery/material)
- Use collision sensor only when you need them. When no collision sensor is active
on the sensor object, it is removed from the simulation and consume no CPU.
Known limitations:
- When running Blender in debug mode, you will see one warning line of the console:
"warning btCollisionDispatcher::needsCollision: static-static collision!"
In release mode this message is not printed.
- Collision margin has no effect on sphere, cone and cylinder shape.
Other performance improvements:
- Remove unnecessary interpolation for Near and Radar objects and by extension
sensor objects.
- Use direct matrix copy instead of quaternion to synchronize orientation.
Other bug fix:
- Fix Near/Radar position error on newly activated objects. This was causing
several detection problems in YoFrankie
- Fix margin not passed correctly to gImpact shape.
- Disable force/velocity actions on static objects
2009-05-17 12:51:51 +00:00
|
|
|
if (body && !m_suspended && !IsSensor())
|
2008-07-18 14:40:24 +00:00
|
|
|
{
|
2008-07-19 10:27:52 +00:00
|
|
|
btBroadphaseProxy* handle = body->getBroadphaseHandle();
|
2008-07-18 14:40:24 +00:00
|
|
|
m_savedCollisionFlags = body->getCollisionFlags();
|
2008-07-20 15:40:03 +00:00
|
|
|
m_savedMass = GetMass();
|
2009-04-27 16:40:26 +00:00
|
|
|
m_savedDyna = m_bDyna;
|
2008-07-19 10:27:52 +00:00
|
|
|
m_savedCollisionFilterGroup = handle->m_collisionFilterGroup;
|
|
|
|
m_savedCollisionFilterMask = handle->m_collisionFilterMask;
|
2009-04-30 19:00:17 +00:00
|
|
|
m_suspended = true;
|
2008-07-19 10:27:52 +00:00
|
|
|
GetPhysicsEnvironment()->updateCcdPhysicsController(this,
|
2008-07-20 15:40:03 +00:00
|
|
|
0.0,
|
2008-07-19 10:27:52 +00:00
|
|
|
btCollisionObject::CF_STATIC_OBJECT|((ghost)?btCollisionObject::CF_NO_CONTACT_RESPONSE:(m_savedCollisionFlags&btCollisionObject::CF_NO_CONTACT_RESPONSE)),
|
|
|
|
btBroadphaseProxy::StaticFilter,
|
|
|
|
btBroadphaseProxy::AllFilter ^ btBroadphaseProxy::StaticFilter);
|
2009-04-27 16:40:26 +00:00
|
|
|
m_bDyna = false;
|
2008-07-18 14:40:24 +00:00
|
|
|
}
|
2005-07-16 21:47:54 +00:00
|
|
|
}
|
2008-07-18 14:40:24 +00:00
|
|
|
|
2005-07-16 21:47:54 +00:00
|
|
|
void KX_BulletPhysicsController::RestoreDynamics()
|
|
|
|
{
|
2008-07-18 14:40:24 +00:00
|
|
|
btRigidBody *body = GetRigidBody();
|
2009-04-30 19:00:17 +00:00
|
|
|
if (body && m_suspended)
|
2008-07-18 14:40:24 +00:00
|
|
|
{
|
2009-06-17 06:54:35 +00:00
|
|
|
// before make sure any position change that was done in this logic frame are accounted for
|
|
|
|
SetTransform();
|
2008-07-19 10:27:52 +00:00
|
|
|
GetPhysicsEnvironment()->updateCcdPhysicsController(this,
|
2008-07-20 15:40:03 +00:00
|
|
|
m_savedMass,
|
2008-07-19 10:27:52 +00:00
|
|
|
m_savedCollisionFlags,
|
|
|
|
m_savedCollisionFilterGroup,
|
|
|
|
m_savedCollisionFilterMask);
|
2009-04-30 19:00:17 +00:00
|
|
|
body->activate();
|
2009-04-27 16:40:26 +00:00
|
|
|
m_bDyna = m_savedDyna;
|
2009-04-30 19:00:17 +00:00
|
|
|
m_suspended = false;
|
2008-07-18 14:40:24 +00:00
|
|
|
}
|
2005-07-16 21:47:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SG_Controller* KX_BulletPhysicsController::GetReplica(class SG_Node* destnode)
|
|
|
|
{
|
2005-12-31 07:20:08 +00:00
|
|
|
PHY_IMotionState* motionstate = new KX_MotionState(destnode);
|
|
|
|
|
|
|
|
KX_BulletPhysicsController* physicsreplica = new KX_BulletPhysicsController(*this);
|
|
|
|
|
|
|
|
//parentcontroller is here be able to avoid collisions between parent/child
|
|
|
|
|
|
|
|
PHY_IPhysicsController* parentctrl = NULL;
|
2008-09-29 06:58:49 +00:00
|
|
|
KX_BulletPhysicsController* parentKxCtrl = NULL;
|
|
|
|
CcdPhysicsController* ccdParent = NULL;
|
|
|
|
|
2005-12-31 07:20:08 +00:00
|
|
|
|
|
|
|
if (destnode != destnode->GetRootSGParent())
|
|
|
|
{
|
|
|
|
KX_GameObject* clientgameobj = (KX_GameObject*) destnode->GetRootSGParent()->GetSGClientObject();
|
|
|
|
if (clientgameobj)
|
|
|
|
{
|
|
|
|
parentctrl = (KX_BulletPhysicsController*)clientgameobj->GetPhysicsController();
|
|
|
|
} else
|
|
|
|
{
|
|
|
|
// it could be a false node, try the children
|
|
|
|
NodeList::const_iterator childit;
|
|
|
|
for (
|
|
|
|
childit = destnode->GetSGChildren().begin();
|
|
|
|
childit!= destnode->GetSGChildren().end();
|
|
|
|
++childit
|
|
|
|
) {
|
2009-02-25 03:26:02 +00:00
|
|
|
KX_GameObject *clientgameobj_child = static_cast<KX_GameObject*>( (*childit)->GetSGClientObject());
|
|
|
|
if (clientgameobj_child)
|
2005-12-31 07:20:08 +00:00
|
|
|
{
|
2009-02-25 03:26:02 +00:00
|
|
|
parentKxCtrl = (KX_BulletPhysicsController*)clientgameobj_child->GetPhysicsController();
|
2008-09-29 06:58:49 +00:00
|
|
|
parentctrl = parentKxCtrl;
|
|
|
|
ccdParent = parentKxCtrl;
|
2005-12-31 07:20:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-09-29 06:58:49 +00:00
|
|
|
physicsreplica->setParentCtrl(ccdParent);
|
2005-12-31 07:20:08 +00:00
|
|
|
physicsreplica->PostProcessReplica(motionstate,parentctrl);
|
2006-06-22 02:20:36 +00:00
|
|
|
physicsreplica->m_userdata = (PHY_IPhysicsController*)physicsreplica;
|
2009-01-13 22:59:18 +00:00
|
|
|
physicsreplica->m_bulletChildShape = NULL;
|
2005-12-31 07:20:08 +00:00
|
|
|
return physicsreplica;
|
|
|
|
|
2005-07-16 21:47:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void KX_BulletPhysicsController::SetSumoTransform(bool nondynaonly)
|
|
|
|
{
|
2006-12-01 01:04:27 +00:00
|
|
|
|
2012-05-28 21:36:29 +00:00
|
|
|
if (!m_bDyna && !m_bSensor && !m_bCharacter)
|
2006-12-01 01:04:27 +00:00
|
|
|
{
|
2009-04-28 18:56:48 +00:00
|
|
|
btCollisionObject* object = GetRigidBody();
|
|
|
|
object->setActivationState(ACTIVE_TAG);
|
|
|
|
object->setCollisionFlags(object->getCollisionFlags() | btCollisionObject::CF_KINEMATIC_OBJECT);
|
2006-12-01 01:04:27 +00:00
|
|
|
} else
|
|
|
|
{
|
|
|
|
if (!nondynaonly)
|
|
|
|
{
|
2009-04-07 22:14:06 +00:00
|
|
|
/*
|
2006-12-01 01:04:27 +00:00
|
|
|
btTransform worldTrans;
|
2008-09-25 17:53:15 +00:00
|
|
|
if (GetRigidBody())
|
|
|
|
{
|
|
|
|
GetRigidBody()->getMotionState()->getWorldTransform(worldTrans);
|
|
|
|
GetRigidBody()->setCenterOfMassTransform(worldTrans);
|
|
|
|
}
|
2009-04-07 22:14:06 +00:00
|
|
|
*/
|
2006-12-01 01:04:27 +00:00
|
|
|
/*
|
|
|
|
scaling?
|
|
|
|
if (m_bDyna)
|
|
|
|
{
|
|
|
|
m_sumoObj->setScaling(MT_Vector3(1,1,1));
|
|
|
|
} else
|
|
|
|
{
|
|
|
|
MT_Vector3 scale;
|
|
|
|
GetWorldScaling(scale);
|
|
|
|
m_sumoObj->setScaling(scale);
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
2005-07-16 21:47:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// todo: remove next line !
|
|
|
|
void KX_BulletPhysicsController::SetSimulatedTime(double time)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
// call from scene graph to update
|
|
|
|
bool KX_BulletPhysicsController::Update(double time)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// todo: check this code
|
|
|
|
//if (GetMass())
|
|
|
|
//{
|
|
|
|
// return false;//true;
|
|
|
|
// }
|
|
|
|
// return false;
|
|
|
|
}
|
|
|
|
|
2011-03-16 21:20:24 +00:00
|
|
|
|
|
|
|
const char* KX_BulletPhysicsController::getName()
|
|
|
|
{
|
|
|
|
if (m_pObject)
|
|
|
|
{
|
|
|
|
KX_GameObject* gameobj = (KX_GameObject*) m_pObject->GetSGClientObject();
|
|
|
|
return gameobj->GetName();
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-01-23 07:26:39 +00:00
|
|
|
#endif // WITH_BULLET
|