2011-02-23 10:52:22 +00:00
/*
2002-10-12 11:37:38 +00:00
* $ Id $
*
2008-04-16 22:40:48 +00:00
* * * * * * BEGIN GPL LICENSE BLOCK * * * * *
2002-10-12 11:37:38 +00:00
*
* This program is free software ; you can redistribute it and / or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation ; either version 2
2008-04-16 22:40:48 +00:00
* of the License , or ( at your option ) any later version .
2002-10-12 11:37:38 +00:00
*
* This program is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
* GNU General Public License for more details .
*
* You should have received a copy of the GNU General Public License
* along with this program ; if not , write to the Free Software Foundation ,
2010-02-12 13:34:04 +00:00
* Inc . , 51 Franklin Street , Fifth Floor , Boston , MA 02110 - 1301 , USA .
2002-10-12 11:37:38 +00:00
*
* The Original Code is Copyright ( C ) 2001 - 2002 by NaN Holding BV .
* All rights reserved .
*
* The Original Code is : all of this file .
*
* Contributor ( s ) : none yet .
*
2008-04-16 22:40:48 +00:00
* * * * * * END GPL LICENSE BLOCK * * * * *
2002-10-12 11:37:38 +00:00
*/
2010-09-15 16:13:32 +00:00
# if defined(WIN32) && !defined(FREE_WINDOWS)
2002-10-12 11:37:38 +00:00
# pragma warning (disable : 4786)
2004-03-22 22:02:18 +00:00
# endif
2002-10-12 11:37:38 +00:00
2005-03-25 10:33:39 +00:00
# include "MT_assert.h"
2009-11-24 22:44:29 +00:00
# include "KX_SoftBodyDeformer.h"
2002-10-12 11:37:38 +00:00
# include "KX_ConvertPhysicsObject.h"
2009-06-08 20:08:19 +00:00
# include "BL_DeformableGameObject.h"
2002-10-12 11:37:38 +00:00
# include "RAS_MeshObject.h"
# include "KX_Scene.h"
# include "SYS_System.h"
# include "PHY_Pro.h" //todo cleanup
# include "KX_ClientObjectInfo.h"
# include "GEN_Map.h"
# include "GEN_HashedPtr.h"
# include "KX_PhysicsEngineEnums.h"
# include "PHY_Pro.h"
# include "KX_MotionState.h" // bridge between motionstate and scenegraph node
2002-11-25 15:29:57 +00:00
2009-06-08 20:08:19 +00:00
extern " C " {
# include "BKE_DerivedMesh.h"
}
2005-07-16 21:47:54 +00:00
# ifdef USE_BULLET
2010-10-10 07:01:56 +00:00
# include "BulletSoftBody/btSoftBody.h"
2005-07-16 21:47:54 +00:00
# include "CcdPhysicsEnvironment.h"
# include "CcdPhysicsController.h"
2006-11-21 00:53:40 +00:00
# include "BulletCollision/BroadphaseCollision/btBroadphaseInterface.h"
2005-07-16 21:47:54 +00:00
# include "KX_BulletPhysicsController.h"
2006-11-21 00:53:40 +00:00
# include "btBulletDynamicsCommon.h"
2005-07-16 21:47:54 +00:00
2006-04-13 05:11:34 +00:00
# ifdef WIN32
# if _MSC_VER >= 1310
//only use SIMD Hull code under Win32
2006-04-26 03:20:28 +00:00
//#define TEST_HULL 1
2006-04-14 03:18:17 +00:00
# ifdef TEST_HULL
2006-04-13 05:11:34 +00:00
# define USE_HULL 1
2006-04-26 03:20:28 +00:00
//#define TEST_SIMD_HULL 1
2006-04-13 05:11:34 +00:00
# include "NarrowPhaseCollision/Hull.h"
2006-04-14 03:18:17 +00:00
# endif //#ifdef TEST_HULL
2006-04-13 05:11:34 +00:00
# endif //_MSC_VER
# endif //WIN32
2005-07-16 21:47:54 +00:00
2008-09-25 03:02:30 +00:00
2005-07-16 21:47:54 +00:00
// forward declarations
void KX_ConvertBulletObject ( class KX_GameObject * gameobj ,
class RAS_MeshObject * meshobj ,
2009-06-08 20:08:19 +00:00
struct DerivedMesh * dm ,
2005-07-16 21:47:54 +00:00
class KX_Scene * kxscene ,
struct PHY_ShapeProps * shapeprops ,
struct PHY_MaterialProps * smmaterial ,
struct KX_ObjectProperties * objprop )
{
2006-04-28 17:35:03 +00:00
CcdPhysicsEnvironment * env = ( CcdPhysicsEnvironment * ) kxscene - > GetPhysicsEnvironment ( ) ;
2005-07-16 21:47:54 +00:00
assert ( env ) ;
2006-12-01 03:29:20 +00:00
bool isbulletdyna = false ;
2009-06-08 20:08:19 +00:00
bool isbulletsensor = false ;
2010-02-03 21:41:03 +00:00
bool useGimpact = false ;
2005-07-16 21:47:54 +00:00
CcdConstructionInfo ci ;
2006-12-01 03:29:20 +00:00
class PHY_IMotionState * motionstate = new KX_MotionState ( gameobj - > GetSGNode ( ) ) ;
2008-08-21 15:19:54 +00:00
class CcdShapeConstructionInfo * shapeInfo = new CcdShapeConstructionInfo ( ) ;
2006-12-01 03:29:20 +00:00
if ( ! objprop - > m_dyna )
{
ci . m_collisionFlags | = btCollisionObject : : CF_STATIC_OBJECT ;
}
2008-03-15 20:52:03 +00:00
if ( objprop - > m_ghost )
{
ci . m_collisionFlags | = btCollisionObject : : CF_NO_CONTACT_RESPONSE ;
}
2006-12-01 03:29:20 +00:00
ci . m_MotionState = motionstate ;
ci . m_gravity = btVector3 ( 0 , 0 , 0 ) ;
2006-11-21 00:53:40 +00:00
ci . m_localInertiaTensor = btVector3 ( 0 , 0 , 0 ) ;
2005-07-16 21:47:54 +00:00
ci . m_mass = objprop - > m_dyna ? shapeprops - > m_mass : 0.f ;
2009-04-20 15:06:46 +00:00
ci . m_clamp_vel_min = shapeprops - > m_clamp_vel_min ;
ci . m_clamp_vel_max = shapeprops - > m_clamp_vel_max ;
BGE patch: new Physics button and margin parameter in Logic panel. Change subversion.
The Physics button controls the creation of a physics representation
of the object when starting the game. If the button is not selected,
the object is a pure graphical object with no physics representation
and all the other physics buttons are hidden.
Selecting this button gives access to the usual physics buttons.
The physics button is enabled by default to match previous Blender
behavior.
The margin parameter allows to control the collision margin from
the UI. Previously, this parameter was only accessible through
Python. By default, the collision margin is set to 0.0 on static
objects and 0.06 on dynamic objects.
To maintain compatibility with older games, the collision margin
is set to 0.06 on all objects when loading older blend file.
Note about the collision algorithms in Bullet 2.71
--------------------------------------------------
Bullet 2.71 handles the collision margin differently than Bullet 2.53
(the previous Bullet version in Blender). The collision margin is
now kept "inside" the object for box, sphere and cylinder bound
shapes. This means that two objects bound to any of these shape will
come in close contact when colliding.
The static mesh, convex hull and cone shapes still have their
collision margin "outside" the object, which leaves a space of 1
or 2 times the collision margin between objects.
The situation with Bullet 2.53 was more complicated, generally
leading to more space between objects, except for box-box collisions.
This means that running a old game under Bullet 2.71 may cause
visual problems, especially if the objects are small. You can fix
these problems by changing some visual aspect of the objects:
center, shape, size, position of children, etc.
2008-09-14 19:34:06 +00:00
ci . m_margin = objprop - > m_margin ;
2008-08-21 15:19:54 +00:00
shapeInfo - > m_radius = objprop - > m_radius ;
2006-12-01 03:29:20 +00:00
isbulletdyna = objprop - > m_dyna ;
2009-06-08 20:08:19 +00:00
isbulletsensor = objprop - > m_sensor ;
2010-02-03 21:41:03 +00:00
useGimpact = ( ( isbulletdyna | | isbulletsensor ) & & ! objprop - > m_softbody ) ;
2006-12-01 03:29:20 +00:00
ci . m_localInertiaTensor = btVector3 ( ci . m_mass / 3.f , ci . m_mass / 3.f , ci . m_mass / 3.f ) ;
2006-11-21 00:53:40 +00:00
btCollisionShape * bm = 0 ;
2005-07-16 21:47:54 +00:00
switch ( objprop - > m_boundclass )
{
case KX_BOUNDSPHERE :
{
2008-08-21 15:19:54 +00:00
//float radius = objprop->m_radius;
//btVector3 inertiaHalfExtents (
// radius,
// radius,
// radius);
2005-07-16 21:47:54 +00:00
//blender doesn't support multisphere, but for testing:
//bm = new MultiSphereShape(inertiaHalfExtents,,&trans.getOrigin(),&radius,1);
2008-08-21 15:19:54 +00:00
shapeInfo - > m_shapeType = PHY_SHAPE_SPHERE ;
2009-06-08 20:08:19 +00:00
bm = shapeInfo - > CreateBulletShape ( ci . m_margin ) ;
2005-07-16 21:47:54 +00:00
break ;
} ;
case KX_BOUNDBOX :
{
2008-08-21 15:19:54 +00:00
shapeInfo - > m_halfExtend . setValue (
2005-07-16 21:47:54 +00:00
objprop - > m_boundobject . box . m_extends [ 0 ] ,
2008-08-21 15:19:54 +00:00
objprop - > m_boundobject . box . m_extends [ 1 ] ,
objprop - > m_boundobject . box . m_extends [ 2 ] ) ;
2005-07-16 21:47:54 +00:00
2008-08-21 15:19:54 +00:00
shapeInfo - > m_halfExtend / = 2.0 ;
shapeInfo - > m_halfExtend = shapeInfo - > m_halfExtend . absolute ( ) ;
shapeInfo - > m_shapeType = PHY_SHAPE_BOX ;
2009-06-08 20:08:19 +00:00
bm = shapeInfo - > CreateBulletShape ( ci . m_margin ) ;
2005-07-16 21:47:54 +00:00
break ;
} ;
case KX_BOUNDCYLINDER :
{
2008-08-21 15:19:54 +00:00
shapeInfo - > m_halfExtend . setValue (
2005-07-16 21:47:54 +00:00
objprop - > m_boundobject . c . m_radius ,
objprop - > m_boundobject . c . m_radius ,
objprop - > m_boundobject . c . m_height * 0.5f
) ;
2008-08-21 15:19:54 +00:00
shapeInfo - > m_shapeType = PHY_SHAPE_CYLINDER ;
2009-06-08 20:08:19 +00:00
bm = shapeInfo - > CreateBulletShape ( ci . m_margin ) ;
2005-07-16 21:47:54 +00:00
break ;
}
2008-08-21 15:19:54 +00:00
case KX_BOUNDCONE :
2005-07-16 21:47:54 +00:00
{
2008-08-21 15:19:54 +00:00
shapeInfo - > m_radius = objprop - > m_boundobject . c . m_radius ;
shapeInfo - > m_height = objprop - > m_boundobject . c . m_height ;
shapeInfo - > m_shapeType = PHY_SHAPE_CONE ;
2009-06-08 20:08:19 +00:00
bm = shapeInfo - > CreateBulletShape ( ci . m_margin ) ;
2005-07-16 21:47:54 +00:00
break ;
}
2008-08-21 15:19:54 +00:00
case KX_BOUNDPOLYTOPE :
2005-07-16 21:47:54 +00:00
{
2010-02-03 21:41:03 +00:00
shapeInfo - > SetMesh ( meshobj , dm , true ) ;
2009-06-08 20:08:19 +00:00
bm = shapeInfo - > CreateBulletShape ( ci . m_margin ) ;
2008-08-21 15:19:54 +00:00
break ;
}
2010-08-28 20:56:54 +00:00
case KX_BOUNDCAPSULE :
{
shapeInfo - > m_radius = objprop - > m_boundobject . c . m_radius ;
shapeInfo - > m_height = objprop - > m_boundobject . c . m_height ;
shapeInfo - > m_shapeType = PHY_SHAPE_CAPSULE ;
bm = shapeInfo - > CreateBulletShape ( ci . m_margin ) ;
break ;
}
2008-08-21 15:19:54 +00:00
case KX_BOUNDMESH :
{
2009-06-08 20:08:19 +00:00
// mesh shapes can be shared, check first if we already have a shape on that mesh
2010-02-03 21:41:03 +00:00
class CcdShapeConstructionInfo * sharedShapeInfo = CcdShapeConstructionInfo : : FindMesh ( meshobj , dm , false ) ;
2009-06-08 20:08:19 +00:00
if ( sharedShapeInfo ! = NULL )
{
2010-02-03 21:41:03 +00:00
shapeInfo - > Release ( ) ;
2009-06-08 20:08:19 +00:00
shapeInfo = sharedShapeInfo ;
shapeInfo - > AddRef ( ) ;
2008-09-26 02:27:59 +00:00
} else
{
2010-02-03 21:41:03 +00:00
shapeInfo - > SetMesh ( meshobj , dm , false ) ;
2009-06-08 20:08:19 +00:00
}
// Soft bodies require welding. Only avoid remove doubles for non-soft bodies!
if ( objprop - > m_softbody )
{
shapeInfo - > setVertexWeldingThreshold1 ( objprop - > m_soft_welding ) ; //todo: expose this to the UI
2005-07-16 21:47:54 +00:00
}
2008-08-21 15:19:54 +00:00
2010-02-03 21:41:03 +00:00
bm = shapeInfo - > CreateBulletShape ( ci . m_margin , useGimpact , ! objprop - > m_softbody ) ;
2009-06-08 20:08:19 +00:00
//should we compute inertia for dynamic shape?
//bm->calculateLocalInertia(ci.m_mass,ci.m_localInertiaTensor);
2008-08-21 15:19:54 +00:00
break ;
2005-07-16 21:47:54 +00:00
}
2010-10-20 12:33:00 +00:00
case KX_BOUND_DYN_MESH :
/* do nothing */
break ;
2005-07-16 21:47:54 +00:00
}
// ci.m_localInertiaTensor.setValue(0.1f,0.1f,0.1f);
if ( ! bm )
2008-03-01 19:46:50 +00:00
{
delete motionstate ;
2010-02-03 21:41:03 +00:00
shapeInfo - > Release ( ) ;
2005-07-16 21:47:54 +00:00
return ;
2008-03-01 19:46:50 +00:00
}
2005-07-16 21:47:54 +00:00
2009-06-08 20:08:19 +00:00
//bm->setMargin(ci.m_margin);
2006-12-01 01:04:27 +00:00
2006-12-01 03:29:20 +00:00
if ( objprop - > m_isCompoundChild )
2006-12-01 01:04:27 +00:00
{
2006-12-01 03:29:20 +00:00
//find parent, compound shape and add to it
//take relative transform into account!
KX_BulletPhysicsController * parentCtrl = ( KX_BulletPhysicsController * ) objprop - > m_dynamic_parent - > GetPhysicsController ( ) ;
assert ( parentCtrl ) ;
2008-08-21 15:19:54 +00:00
CcdShapeConstructionInfo * parentShapeInfo = parentCtrl - > GetShapeInfo ( ) ;
2006-12-01 03:29:20 +00:00
btRigidBody * rigidbody = parentCtrl - > GetRigidBody ( ) ;
btCollisionShape * colShape = rigidbody - > getCollisionShape ( ) ;
assert ( colShape - > isCompound ( ) ) ;
btCompoundShape * compoundShape = ( btCompoundShape * ) colShape ;
2009-06-08 20:08:19 +00:00
// compute the local transform from parent, this may include several node in the chain
2008-09-13 11:46:07 +00:00
SG_Node * gameNode = gameobj - > GetSGNode ( ) ;
2009-06-08 20:08:19 +00:00
SG_Node * parentNode = objprop - > m_dynamic_parent - > GetSGNode ( ) ;
// relative transform
MT_Vector3 parentScale = parentNode - > GetWorldScaling ( ) ;
parentScale [ 0 ] = MT_Scalar ( 1.0 ) / parentScale [ 0 ] ;
parentScale [ 1 ] = MT_Scalar ( 1.0 ) / parentScale [ 1 ] ;
parentScale [ 2 ] = MT_Scalar ( 1.0 ) / parentScale [ 2 ] ;
MT_Vector3 relativeScale = gameNode - > GetWorldScaling ( ) * parentScale ;
MT_Matrix3x3 parentInvRot = parentNode - > GetWorldOrientation ( ) . transposed ( ) ;
MT_Vector3 relativePos = parentInvRot * ( ( gameNode - > GetWorldPosition ( ) - parentNode - > GetWorldPosition ( ) ) * parentScale ) ;
MT_Matrix3x3 relativeRot = parentInvRot * gameNode - > GetWorldOrientation ( ) ;
shapeInfo - > m_childScale . setValue ( relativeScale [ 0 ] , relativeScale [ 1 ] , relativeScale [ 2 ] ) ;
2008-09-13 11:46:07 +00:00
bm - > setLocalScaling ( shapeInfo - > m_childScale ) ;
2009-06-08 20:08:19 +00:00
shapeInfo - > m_childTrans . getOrigin ( ) . setValue ( relativePos [ 0 ] , relativePos [ 1 ] , relativePos [ 2 ] ) ;
float rot [ 12 ] ;
relativeRot . getValue ( rot ) ;
shapeInfo - > m_childTrans . getBasis ( ) . setFromOpenGLSubMatrix ( rot ) ;
2008-08-21 15:19:54 +00:00
parentShapeInfo - > AddShape ( shapeInfo ) ;
compoundShape - > addChildShape ( shapeInfo - > m_childTrans , bm ) ;
2006-12-01 03:29:20 +00:00
//do some recalc?
//recalc inertia for rigidbody
if ( ! rigidbody - > isStaticOrKinematicObject ( ) )
{
btVector3 localInertia ;
float mass = 1.f / rigidbody - > getInvMass ( ) ;
compoundShape - > calculateLocalInertia ( mass , localInertia ) ;
rigidbody - > setMassProps ( mass , localInertia ) ;
}
2010-02-03 21:41:03 +00:00
shapeInfo - > Release ( ) ;
2009-06-08 20:08:19 +00:00
// delete motionstate as it's not used
delete motionstate ;
2006-12-01 03:29:20 +00:00
return ;
2006-12-01 01:04:27 +00:00
}
2006-12-01 03:29:20 +00:00
if ( objprop - > m_hasCompoundChildren )
{
2008-08-21 15:19:54 +00:00
// create a compound shape info
CcdShapeConstructionInfo * compoundShapeInfo = new CcdShapeConstructionInfo ( ) ;
compoundShapeInfo - > m_shapeType = PHY_SHAPE_COMPOUND ;
compoundShapeInfo - > AddShape ( shapeInfo ) ;
// create the compound shape manually as we already have the child shape
2006-12-01 03:29:20 +00:00
btCompoundShape * compoundShape = new btCompoundShape ( ) ;
2008-08-21 15:19:54 +00:00
compoundShape - > addChildShape ( shapeInfo - > m_childTrans , bm ) ;
// now replace the shape
2006-12-01 03:29:20 +00:00
bm = compoundShape ;
2010-02-03 21:41:03 +00:00
shapeInfo - > Release ( ) ;
2008-08-21 15:19:54 +00:00
shapeInfo = compoundShapeInfo ;
2006-12-01 03:29:20 +00:00
}
2006-12-01 01:04:27 +00:00
2006-04-13 05:11:34 +00:00
# ifdef TEST_SIMD_HULL
if ( bm - > IsPolyhedral ( ) )
{
PolyhedralConvexShape * polyhedron = static_cast < PolyhedralConvexShape * > ( bm ) ;
if ( ! polyhedron - > m_optionalHull )
{
//first convert vertices in 'Point3' format
int numPoints = polyhedron - > GetNumVertices ( ) ;
Point3 * points = new Point3 [ numPoints + 1 ] ;
//first 4 points should not be co-planar, so add central point to satisfy MakeHull
points [ 0 ] = Point3 ( 0.f , 0.f , 0.f ) ;
2006-11-21 00:53:40 +00:00
btVector3 vertex ;
2006-04-13 05:11:34 +00:00
for ( int p = 0 ; p < numPoints ; p + + )
{
polyhedron - > GetVertex ( p , vertex ) ;
points [ p + 1 ] = Point3 ( vertex . getX ( ) , vertex . getY ( ) , vertex . getZ ( ) ) ;
}
Hull * hull = Hull : : MakeHull ( numPoints + 1 , points ) ;
polyhedron - > m_optionalHull = hull ;
}
}
# endif //TEST_SIMD_HULL
2005-07-16 21:47:54 +00:00
ci . m_collisionShape = bm ;
2008-08-21 15:19:54 +00:00
ci . m_shapeInfo = shapeInfo ;
2006-02-21 05:36:56 +00:00
ci . m_friction = smmaterial - > m_friction ; //tweak the friction a bit, so the default 0.5 works nice
2005-07-16 21:47:54 +00:00
ci . m_restitution = smmaterial - > m_restitution ;
2005-12-31 07:20:08 +00:00
ci . m_physicsEnv = env ;
2005-08-05 17:00:32 +00:00
// drag / damping is inverted
ci . m_linearDamping = 1.f - shapeprops - > m_lin_drag ;
ci . m_angularDamping = 1.f - shapeprops - > m_ang_drag ;
//need a bit of damping, else system doesn't behave well
2005-12-31 21:59:56 +00:00
ci . m_inertiaFactor = shapeprops - > m_inertia / 0.4f ; //defaults to 0.4, don't want to change behaviour
2008-09-28 03:07:13 +00:00
2008-09-29 03:09:03 +00:00
ci . m_do_anisotropic = shapeprops - > m_do_anisotropic ;
ci . m_anisotropicFriction . setValue ( shapeprops - > m_friction_scaling [ 0 ] , shapeprops - > m_friction_scaling [ 1 ] , shapeprops - > m_friction_scaling [ 2 ] ) ;
2008-09-29 06:58:49 +00:00
//////////
//do Fh, do Rot Fh
ci . m_do_fh = shapeprops - > m_do_fh ;
ci . m_do_rot_fh = shapeprops - > m_do_rot_fh ;
ci . m_fh_damping = smmaterial - > m_fh_damping ;
ci . m_fh_distance = smmaterial - > m_fh_distance ;
ci . m_fh_normal = smmaterial - > m_fh_normal ;
ci . m_fh_spring = smmaterial - > m_fh_spring ;
ci . m_radius = objprop - > m_radius ;
2008-09-29 03:09:03 +00:00
2008-09-28 03:07:13 +00:00
///////////////////
2008-09-25 03:02:30 +00:00
ci . m_gamesoftFlag = objprop - > m_gamesoftFlag ;
2008-09-28 03:07:13 +00:00
ci . m_soft_linStiff = objprop - > m_soft_linStiff ;
ci . m_soft_angStiff = objprop - > m_soft_angStiff ; /* angular stiffness 0..1 */
ci . m_soft_volume = objprop - > m_soft_volume ; /* volume preservation 0..1 */
ci . m_soft_viterations = objprop - > m_soft_viterations ; /* Velocities solver iterations */
ci . m_soft_piterations = objprop - > m_soft_piterations ; /* Positions solver iterations */
ci . m_soft_diterations = objprop - > m_soft_diterations ; /* Drift solver iterations */
ci . m_soft_citerations = objprop - > m_soft_citerations ; /* Cluster solver iterations */
ci . m_soft_kSRHR_CL = objprop - > m_soft_kSRHR_CL ; /* Soft vs rigid hardness [0,1] (cluster only) */
ci . m_soft_kSKHR_CL = objprop - > m_soft_kSKHR_CL ; /* Soft vs kinetic hardness [0,1] (cluster only) */
ci . m_soft_kSSHR_CL = objprop - > m_soft_kSSHR_CL ; /* Soft vs soft hardness [0,1] (cluster only) */
ci . m_soft_kSR_SPLT_CL = objprop - > m_soft_kSR_SPLT_CL ; /* Soft vs rigid impulse split [0,1] (cluster only) */
ci . m_soft_kSK_SPLT_CL = objprop - > m_soft_kSK_SPLT_CL ; /* Soft vs rigid impulse split [0,1] (cluster only) */
ci . m_soft_kSS_SPLT_CL = objprop - > m_soft_kSS_SPLT_CL ; /* Soft vs rigid impulse split [0,1] (cluster only) */
ci . m_soft_kVCF = objprop - > m_soft_kVCF ; /* Velocities correction factor (Baumgarte) */
ci . m_soft_kDP = objprop - > m_soft_kDP ; /* Damping coefficient [0,1] */
ci . m_soft_kDG = objprop - > m_soft_kDG ; /* Drag coefficient [0,+inf] */
ci . m_soft_kLF = objprop - > m_soft_kLF ; /* Lift coefficient [0,+inf] */
ci . m_soft_kPR = objprop - > m_soft_kPR ; /* Pressure coefficient [-inf,+inf] */
ci . m_soft_kVC = objprop - > m_soft_kVC ; /* Volume conversation coefficient [0,+inf] */
ci . m_soft_kDF = objprop - > m_soft_kDF ; /* Dynamic friction coefficient [0,1] */
ci . m_soft_kMT = objprop - > m_soft_kMT ; /* Pose matching coefficient [0,1] */
ci . m_soft_kCHR = objprop - > m_soft_kCHR ; /* Rigid contacts hardness [0,1] */
ci . m_soft_kKHR = objprop - > m_soft_kKHR ; /* Kinetic contacts hardness [0,1] */
ci . m_soft_kSHR = objprop - > m_soft_kSHR ; /* Soft contacts hardness [0,1] */
ci . m_soft_kAHR = objprop - > m_soft_kAHR ; /* Anchors hardness [0,1] */
ci . m_soft_collisionflags = objprop - > m_soft_collisionflags ; /* Vertex/Face or Signed Distance Field(SDF) or Clusters, Soft versus Soft or Rigid */
ci . m_soft_numclusteriterations = objprop - > m_soft_numclusteriterations ; /* number of iterations to refine collision clusters*/
////////////////////
2009-06-08 20:08:19 +00:00
ci . m_collisionFilterGroup =
( isbulletsensor ) ? short ( CcdConstructionInfo : : SensorFilter ) :
( isbulletdyna ) ? short ( CcdConstructionInfo : : DefaultFilter ) :
short ( CcdConstructionInfo : : StaticFilter ) ;
ci . m_collisionFilterMask =
( isbulletsensor ) ? short ( CcdConstructionInfo : : AllFilter ^ CcdConstructionInfo : : SensorFilter ) :
( isbulletdyna ) ? short ( CcdConstructionInfo : : AllFilter ) :
short ( CcdConstructionInfo : : AllFilter ^ CcdConstructionInfo : : StaticFilter ) ;
2008-05-01 16:00:59 +00:00
ci . m_bRigid = objprop - > m_dyna & & objprop - > m_angular_rigidbody ;
2009-06-08 20:08:19 +00:00
ci . m_contactProcessingThreshold = objprop - > m_contactProcessingThreshold ; //todo: expose this in advanced settings, just like margin, default to 10000 or so
2008-09-21 15:17:50 +00:00
ci . m_bSoft = objprop - > m_softbody ;
2009-06-08 20:08:19 +00:00
ci . m_bSensor = isbulletsensor ;
2010-02-03 21:41:03 +00:00
ci . m_bGimpact = useGimpact ;
2008-08-21 21:04:42 +00:00
MT_Vector3 scaling = gameobj - > NodeGetWorldScaling ( ) ;
ci . m_scaling . setValue ( scaling [ 0 ] , scaling [ 1 ] , scaling [ 2 ] ) ;
2009-06-08 20:08:19 +00:00
KX_BulletPhysicsController * physicscontroller = new KX_BulletPhysicsController ( ci , isbulletdyna , isbulletsensor , objprop - > m_hasCompoundChildren ) ;
2008-08-21 15:19:54 +00:00
// shapeInfo is reference counted, decrement now as we don't use it anymore
if ( shapeInfo )
shapeInfo - > Release ( ) ;
2005-12-31 07:20:08 +00:00
2009-06-08 20:08:19 +00:00
gameobj - > SetPhysicsController ( physicscontroller , isbulletdyna ) ;
// don't add automatically sensor object, they are added when a collision sensor is registered
if ( ! isbulletsensor & & objprop - > m_in_active_layer )
2005-12-31 07:20:08 +00:00
{
env - > addCcdPhysicsController ( physicscontroller ) ;
}
2005-08-05 17:00:32 +00:00
physicscontroller - > setNewClientInfo ( gameobj - > getClientInfo ( ) ) ;
2008-09-21 15:17:50 +00:00
{
btRigidBody * rbody = physicscontroller - > GetRigidBody ( ) ;
2006-02-22 06:58:05 +00:00
2009-03-09 07:12:16 +00:00
if ( rbody )
{
2009-04-20 15:06:46 +00:00
if ( objprop - > m_angular_rigidbody )
{
btVector3 linearFactor (
objprop - > m_lockXaxis ? 0 : 1 ,
objprop - > m_lockYaxis ? 0 : 1 ,
objprop - > m_lockZaxis ? 0 : 1 ) ;
btVector3 angularFactor (
objprop - > m_lockXRotaxis ? 0 : 1 ,
objprop - > m_lockYRotaxis ? 0 : 1 ,
objprop - > m_lockZRotaxis ? 0 : 1 ) ;
rbody - > setLinearFactor ( linearFactor ) ;
rbody - > setAngularFactor ( angularFactor ) ;
}
2009-03-09 07:12:16 +00:00
if ( rbody & & objprop - > m_disableSleeping )
{
rbody - > setActivationState ( DISABLE_DEACTIVATION ) ;
}
}
2008-09-21 15:17:50 +00:00
}
2008-09-29 06:58:49 +00:00
CcdPhysicsController * parentCtrl = objprop - > m_dynamic_parent ? ( KX_BulletPhysicsController * ) objprop - > m_dynamic_parent - > GetPhysicsController ( ) : 0 ;
physicscontroller - > setParentCtrl ( parentCtrl ) ;
2006-02-22 06:58:05 +00:00
2008-03-15 20:52:03 +00:00
//Now done directly in ci.m_collisionFlags so that it propagates to replica
//if (objprop->m_ghost)
//{
// rbody->setCollisionFlags(rbody->getCollisionFlags() | btCollisionObject::CF_NO_CONTACT_RESPONSE);
//}
2008-05-01 16:00:59 +00:00
2006-08-28 06:44:29 +00:00
if ( objprop - > m_dyna & & ! objprop - > m_angular_rigidbody )
2006-04-26 06:01:46 +00:00
{
/*
//setting the inertia could achieve similar results to constraint the up
//but it is prone to instability, so use special 'Angular' constraint
2006-11-21 00:53:40 +00:00
btVector3 inertia = physicscontroller - > GetRigidBody ( ) - > getInvInertiaDiagLocal ( ) ;
2006-04-26 06:01:46 +00:00
inertia . setX ( 0.f ) ;
inertia . setZ ( 0.f ) ;
physicscontroller - > GetRigidBody ( ) - > setInvInertiaDiagLocal ( inertia ) ;
physicscontroller - > GetRigidBody ( ) - > updateInertiaTensor ( ) ;
*/
2006-12-04 04:31:34 +00:00
//env->createConstraint(physicscontroller,0,PHY_ANGULAR_CONSTRAINT,0,0,0,0,0,1);
2008-05-01 16:00:59 +00:00
//Now done directly in ci.m_bRigid so that it propagates to replica
//physicscontroller->GetRigidBody()->setAngularFactor(0.f);
;
2006-04-26 06:01:46 +00:00
}
2005-08-05 17:00:32 +00:00
bool isActor = objprop - > m_isactor ;
2009-06-08 20:08:19 +00:00
gameobj - > getClientInfo ( ) - > m_type =
( isbulletsensor ) ? ( ( isActor ) ? KX_ClientObjectInfo : : OBACTORSENSOR : KX_ClientObjectInfo : : OBSENSOR ) :
( isActor ) ? KX_ClientObjectInfo : : ACTOR : KX_ClientObjectInfo : : STATIC ;
2005-08-05 17:00:32 +00:00
// store materialname in auxinfo, needed for touchsensors
2006-04-28 17:35:03 +00:00
if ( meshobj )
{
const STR_String & matname = meshobj - > GetMaterialName ( 0 ) ;
gameobj - > getClientInfo ( ) - > m_auxilary_info = ( matname . Length ( ) ? ( void * ) ( matname . ReadPtr ( ) + 2 ) : NULL ) ;
} else
{
gameobj - > getClientInfo ( ) - > m_auxilary_info = 0 ;
}
2005-08-05 17:00:32 +00:00
2005-07-16 21:47:54 +00:00
gameobj - > GetSGNode ( ) - > AddSGController ( physicscontroller ) ;
STR_String materialname ;
if ( meshobj )
materialname = meshobj - > GetMaterialName ( 0 ) ;
physicscontroller - > SetObject ( gameobj - > GetSGNode ( ) ) ;
2009-11-24 22:44:29 +00:00
#if 0
2008-09-24 03:12:10 +00:00
///test for soft bodies
if ( objprop - > m_softbody & & physicscontroller )
{
btSoftBody * softBody = physicscontroller - > GetSoftBody ( ) ;
2008-09-25 03:02:30 +00:00
if ( softBody & & gameobj - > GetMesh ( 0 ) ) //only the first mesh, if any
2008-09-24 03:12:10 +00:00
{
//should be a mesh then, so add a soft body deformer
2009-06-08 20:08:19 +00:00
KX_SoftBodyDeformer * softbodyDeformer = new KX_SoftBodyDeformer ( gameobj - > GetMesh ( 0 ) , ( BL_DeformableGameObject * ) gameobj ) ;
2008-09-24 03:12:10 +00:00
gameobj - > SetDeformer ( softbodyDeformer ) ;
}
}
2009-11-24 22:44:29 +00:00
# endif
2008-09-24 03:12:10 +00:00
2005-07-16 21:47:54 +00:00
}
2006-12-01 03:29:20 +00:00
2005-07-16 21:47:54 +00:00
void KX_ClearBulletSharedShapes ( )
{
}
2009-07-25 22:57:29 +00:00
/* Refresh the physics object from either an object or a mesh.
* gameobj must be valid
* from_gameobj and from_meshobj can be NULL
*
* when setting the mesh , the following vars get priority
* 1 ) from_meshobj - creates the phys mesh from RAS_MeshObject
* 2 ) from_gameobj - creates the phys mesh from the DerivedMesh where possible , else the RAS_MeshObject
* 3 ) gameobj - update the phys mesh from DerivedMesh or RAS_MeshObject
*
* Most of the logic behind this is in shapeInfo - > UpdateMesh ( . . . )
*/
bool KX_ReInstanceBulletShapeFromMesh ( KX_GameObject * gameobj , KX_GameObject * from_gameobj , RAS_MeshObject * from_meshobj )
{
KX_BulletPhysicsController * spc = static_cast < KX_BulletPhysicsController * > ( ( gameobj - > GetPhysicsController ( ) ) ) ;
CcdShapeConstructionInfo * shapeInfo ;
/* if this is the child of a compound shape this can happen
* dont support compound shapes for now */
if ( spc = = NULL )
return false ;
shapeInfo = spc - > GetShapeInfo ( ) ;
2009-11-24 22:44:29 +00:00
if ( shapeInfo - > m_shapeType ! = PHY_SHAPE_MESH /* || spc->GetSoftBody()*/ )
2009-07-25 22:57:29 +00:00
return false ;
spc - > DeleteControllerShape ( ) ;
if ( from_gameobj = = NULL & & from_meshobj = = NULL )
from_gameobj = gameobj ;
/* updates the arrays used for making the new bullet mesh */
shapeInfo - > UpdateMesh ( from_gameobj , from_meshobj ) ;
2006-01-05 23:10:54 +00:00
2009-07-25 22:57:29 +00:00
/* create the new bullet mesh */
2010-02-03 21:41:03 +00:00
CcdConstructionInfo & cci = spc - > getConstructionInfo ( ) ;
btCollisionShape * bm = shapeInfo - > CreateBulletShape ( cci . m_margin , cci . m_bGimpact , ! cci . m_bSoft ) ;
2009-07-25 22:57:29 +00:00
spc - > ReplaceControllerShape ( bm ) ;
return true ;
}
2010-10-10 07:01:56 +00:00
# endif // USE_BULLET