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 ,
* Inc . , 59 Temple Place - Suite 330 , Boston , MA 02111 - 1307 , USA .
*
* 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
* Game object wrapper
*/
2002-11-25 15:29:57 +00:00
# ifdef HAVE_CONFIG_H
# include <config.h>
# endif
2005-11-28 06:51:54 +00:00
# if defined(_WIN64)
typedef unsigned __int64 uint_ptr ;
# else
typedef unsigned long uint_ptr ;
# endif
2002-10-12 11:37:38 +00:00
# ifdef WIN32
// This warning tells us about truncation of __long__ stl-generated names.
// It can occasionally cause DevStudio to have internal compiler warnings.
# pragma warning( disable : 4786 )
# endif
# define KX_INERTIA_INFINITE 10000
# include "RAS_IPolygonMaterial.h"
2006-01-06 03:46:54 +00:00
# include "KX_BlenderMaterial.h"
2002-10-12 11:37:38 +00:00
# include "KX_GameObject.h"
# include "RAS_MeshObject.h"
# include "KX_MeshProxy.h"
# include <stdio.h> // printf
# include "SG_Controller.h"
# include "KX_IPhysicsController.h"
# include "SG_Node.h"
# include "SG_Controller.h"
# include "KX_ClientObjectInfo.h"
# include "RAS_BucketManager.h"
2008-03-15 17:08:58 +00:00
# include "KX_RayCast.h"
2008-04-06 18:30:52 +00:00
# include "KX_PythonInit.h"
2004-05-26 12:06:41 +00:00
# include "KX_PyMath.h"
2008-07-19 07:45:19 +00:00
# include "SCA_IActuator.h"
# include "SCA_ISensor.h"
2004-05-26 12:06:41 +00:00
2002-10-12 11:37:38 +00:00
// This file defines relationships between parents and children
// in the game engine.
# include "KX_SG_NodeRelationships.h"
KX_GameObject : : KX_GameObject (
void * sgReplicationInfo ,
SG_Callbacks callbacks ,
PyTypeObject * T
) :
SCA_IObject ( T ) ,
m_bDyna ( false ) ,
2008-04-30 19:58:44 +00:00
m_layer ( 0 ) ,
2002-10-12 11:37:38 +00:00
m_bSuspendDynamics ( false ) ,
2004-03-22 22:02:18 +00:00
m_bUseObjectColor ( false ) ,
2008-05-25 14:37:39 +00:00
m_bIsNegativeScaling ( false ) ,
2008-06-18 06:46:49 +00:00
m_pBlenderObject ( NULL ) ,
2004-03-22 22:02:18 +00:00
m_bVisible ( true ) ,
Patch: [ #2439 ] Makes objects react properly to deformations after a mesh replacement call.
from brian hayward (bthayward)
Detailed description:
Currently, when an armature deformed object's mesh is replaced by the ReplaceMesh actuator, the new mesh fails to deform to the armature's movement.
My patch fixes this by properly replacing the deform controller along with the mesh (when appropriete).
For instance, if one had an animated character using any of the standard deformation techniques (armature, ipo, RVK, or AVK), that character's mesh would currently be prevented from changing mid-game. It could be replaced, but the new mesh would lack the controller which tells it how to deform. If one wanted to dynamiclly add a hat on top of the character's head, it would require storing a secondary prebuilt character (mesh, armature, logic, ect...) on another layer FOR EACH HAT the character could possibly wear, then swapping out the whole character when the hat change was desired. So if you had 4 possible hat/character combos, you would have 4 character meshes, 4 armatures, 4 sets of logic, and so on. I find this lack of flexibility to be unresonable.
With my patch, one could accomplish the same thing mearly by making one version of the character in the main layer, and adding an invisible object atop the character's head (which is parented to the head bone). Then whenever it becomes desirable, one can replace the invisible object's mesh with the desirable hat's mesh, then make it visible. With my patch, the hat object would then continue to deform to the character's head regardless of which hat was currently being worn.
*note 1*
for armature/mesh deformations, the new mesh must have properly assigned vertex groups which match one or more of the bones of the target armature before the replaceMesh call is made. Otherwise the vertices won't react to the armature because they won't know how. (not sure if vertices can be scripted to change groups after the game has started)
*note 2*
The added processing time involved with replacing the object's deform controller is negligible.
2005-04-18 11:44:21 +00:00
m_pPhysicsController1 ( NULL ) ,
2008-03-15 17:08:58 +00:00
m_pPhysicsEnvironment ( NULL ) ,
Merge of apricot branch game engine changes into trunk, excluding GLSL.
GLEW
====
Added the GLEW opengl extension library into extern/, always compiled
into Blender now. This is much nicer than doing this kind of extension
management manually, and will be used in the game engine, for GLSL, and
other opengl extensions.
* According to the GLEW website it works on Windows, Linux, Mac OS X,
FreeBSD, Irix, and Solaris. There might still be platform specific
issues due to this commit, so let me know and I'll look into it.
* This means also that all extensions will now always be compiled in,
regardless of the glext.h on the platform where compilation happens.
Game Engine
===========
Refactoring of the use of opengl extensions and other drawing code
in the game engine, and cleaning up some hacks related to GLSL
integration. These changes will be merged into trunk too after this.
The game engine graphics demos & apricot level survived my tests,
but this could use some good testing of course.
For users: please test with the options "Generate Display Lists" and
"Vertex Arrays" enabled, these should be the fastest and are supposed
to be "unreliable", but if that's the case that's probably due to bugs
that can be fixed.
* The game engine now also uses GLEW for extensions, replacing the
custom opengl extensions code that was there. Removes a lot of
#ifdef's, but the runtime checks stay of course.
* Removed the WITHOUT_GLEXT environment variable. This was added to
work around a specific bug and only disabled multitexturing anyway.
It might also have caused a slowdown since it was retrieving the
environment variable for every vertex in immediate mode (bug #13680).
* Refactored the code to allow drawing skinned meshes with vertex
arrays too, removing some specific immediate mode drawing functions
for this that only did extra normal calculation. Now it always splits
vertices of flat faces instead.
* Refactored normal recalculation with some minor optimizations,
required for the above change.
* Removed some outdated code behind the __NLA_OLDDEFORM #ifdef.
* Fixed various bugs in setting of multitexture coordinates and vertex
attributes for vertex arrays. These were not being enabled/disabled
correct according to the opengl spec, leading to crashes. Also tangent
attributes used an immediate mode call for vertex arrays, which can't
work.
* Fixed use of uninitialized variable in RAS_TexVert.
* Exporting skinned meshes was doing O(n^2) lookups for vertices and
deform weights, now uses same trick as regular meshes.
2008-06-17 10:27:34 +00:00
m_pHitObject ( NULL ) ,
m_isDeformable ( false )
2002-10-12 11:37:38 +00:00
{
m_ignore_activity_culling = false ;
2004-04-11 02:50:02 +00:00
m_pClient_info = new KX_ClientObjectInfo ( this , KX_ClientObjectInfo : : ACTOR ) ;
2002-10-12 11:37:38 +00:00
m_pSGNode = new SG_Node ( this , sgReplicationInfo , callbacks ) ;
2008-04-06 18:30:52 +00:00
2002-10-12 11:37:38 +00:00
// define the relationship between this node and it's parent.
2004-05-26 12:06:41 +00:00
2002-10-12 11:37:38 +00:00
KX_NormalParentRelation * parent_relation =
KX_NormalParentRelation : : New ( ) ;
m_pSGNode - > SetParentRelation ( parent_relation ) ;
2004-05-26 12:06:41 +00:00
2002-10-12 11:37:38 +00:00
} ;
KX_GameObject : : ~ KX_GameObject ( )
{
// is this delete somewhere ?
//if (m_sumoObj)
// delete m_sumoObj;
delete m_pClient_info ;
//if (m_pSGNode)
// delete m_pSGNode;
2008-03-09 21:51:38 +00:00
if ( m_pSGNode )
{
// must go through controllers and make sure they will not use us anymore
// This is important for KX_BulletPhysicsControllers that unregister themselves
// from the object when they are deleted.
SGControllerList : : iterator contit ;
SGControllerList & controllers = m_pSGNode - > GetSGControllerList ( ) ;
for ( contit = controllers . begin ( ) ; contit ! = controllers . end ( ) ; + + contit )
{
( * contit ) - > ClearObject ( ) ;
}
m_pSGNode - > SetSGClientObject ( NULL ) ;
}
2002-10-12 11:37:38 +00:00
}
CValue * KX_GameObject : : Calc ( VALUE_OPERATOR op , CValue * val )
{
return NULL ;
}
CValue * KX_GameObject : : CalcFinal ( VALUE_DATA_TYPE dtype , VALUE_OPERATOR op , CValue * val )
{
return NULL ;
}
const STR_String & KX_GameObject : : GetText ( )
{
return m_text ;
}
float KX_GameObject : : GetNumber ( )
{
return 0 ;
}
STR_String KX_GameObject : : GetName ( )
{
return m_name ;
}
void KX_GameObject : : SetName ( STR_String name )
{
m_name = name ;
} ; // Set the name of the value
void KX_GameObject : : ReplicaSetName ( STR_String name )
{
}
KX_IPhysicsController * KX_GameObject : : GetPhysicsController ( )
{
return m_pPhysicsController1 ;
}
KX_GameObject * KX_GameObject : : GetParent ( )
{
KX_GameObject * result = NULL ;
SG_Node * node = m_pSGNode ;
while ( node & & ! result )
{
node = node - > GetSGParent ( ) ;
if ( node )
result = ( KX_GameObject * ) node - > GetSGClientObject ( ) ;
}
if ( result )
result - > AddRef ( ) ;
return result ;
}
2008-04-06 18:30:52 +00:00
void KX_GameObject : : SetParent ( KX_Scene * scene , KX_GameObject * obj )
{
if ( obj & & GetSGNode ( ) - > GetSGParent ( ) ! = obj - > GetSGNode ( ) )
{
// Make sure the objects have some scale
MT_Vector3 scale1 = NodeGetWorldScaling ( ) ;
MT_Vector3 scale2 = obj - > NodeGetWorldScaling ( ) ;
if ( fabs ( scale2 [ 0 ] ) < FLT_EPSILON | |
fabs ( scale2 [ 1 ] ) < FLT_EPSILON | |
fabs ( scale2 [ 2 ] ) < FLT_EPSILON | |
fabs ( scale1 [ 0 ] ) < FLT_EPSILON | |
fabs ( scale1 [ 1 ] ) < FLT_EPSILON | |
fabs ( scale1 [ 2 ] ) < FLT_EPSILON ) { return ; }
// Remove us from our old parent and set our new parent
RemoveParent ( scene ) ;
obj - > GetSGNode ( ) - > AddChild ( GetSGNode ( ) ) ;
2008-07-20 15:40:03 +00:00
if ( m_pPhysicsController1 )
{
m_pPhysicsController1 - > SuspendDynamics ( true ) ;
}
2008-04-06 18:30:52 +00:00
// Set us to our new scale, position, and orientation
scale1 [ 0 ] = scale1 [ 0 ] / scale2 [ 0 ] ;
scale1 [ 1 ] = scale1 [ 1 ] / scale2 [ 1 ] ;
scale1 [ 2 ] = scale1 [ 2 ] / scale2 [ 2 ] ;
MT_Matrix3x3 invori = obj - > NodeGetWorldOrientation ( ) . inverse ( ) ;
MT_Vector3 newpos = invori * ( NodeGetWorldPosition ( ) - obj - > NodeGetWorldPosition ( ) ) * scale1 ;
NodeSetLocalScale ( scale1 ) ;
NodeSetLocalPosition ( MT_Point3 ( newpos [ 0 ] , newpos [ 1 ] , newpos [ 2 ] ) ) ;
2008-05-02 17:22:28 +00:00
NodeSetLocalOrientation ( invori * NodeGetWorldOrientation ( ) ) ;
2008-04-06 18:30:52 +00:00
NodeUpdateGS ( 0.f , true ) ;
// object will now be a child, it must be removed from the parent list
CListValue * rootlist = scene - > GetRootParentList ( ) ;
if ( rootlist - > RemoveValue ( this ) )
// the object was in parent list, decrement ref count as it's now removed
Release ( ) ;
}
}
void KX_GameObject : : RemoveParent ( KX_Scene * scene )
{
if ( GetSGNode ( ) - > GetSGParent ( ) )
{
// Set us to the right spot
GetSGNode ( ) - > SetLocalScale ( GetSGNode ( ) - > GetWorldScaling ( ) ) ;
GetSGNode ( ) - > SetLocalOrientation ( GetSGNode ( ) - > GetWorldOrientation ( ) ) ;
GetSGNode ( ) - > SetLocalPosition ( GetSGNode ( ) - > GetWorldPosition ( ) ) ;
2002-10-12 11:37:38 +00:00
2008-04-06 18:30:52 +00:00
// Remove us from our parent
GetSGNode ( ) - > DisconnectFromParent ( ) ;
NodeUpdateGS ( 0.f , true ) ;
// the object is now a root object, add it to the parentlist
CListValue * rootlist = scene - > GetRootParentList ( ) ;
if ( ! rootlist - > SearchValue ( this ) )
// object was not in root list, add it now and increment ref count
rootlist - > Add ( AddRef ( ) ) ;
2008-07-18 14:40:24 +00:00
if ( m_pPhysicsController1 )
{
m_pPhysicsController1 - > RestoreDynamics ( ) ;
}
2008-04-06 18:30:52 +00:00
}
}
2002-10-12 11:37:38 +00:00
void KX_GameObject : : ProcessReplica ( KX_GameObject * replica )
{
replica - > m_pPhysicsController1 = NULL ;
replica - > m_pSGNode = NULL ;
replica - > m_pClient_info = new KX_ClientObjectInfo ( * m_pClient_info ) ;
2004-11-06 04:58:10 +00:00
replica - > m_pClient_info - > m_gameobject = replica ;
2002-10-12 11:37:38 +00:00
}
CValue * KX_GameObject : : GetReplica ( )
{
KX_GameObject * replica = new KX_GameObject ( * this ) ;
// this will copy properties and so on...
CValue : : AddDataToReplica ( replica ) ;
ProcessReplica ( replica ) ;
2004-03-22 22:02:18 +00:00
2002-10-12 11:37:38 +00:00
return replica ;
}
void KX_GameObject : : ApplyForce ( const MT_Vector3 & force , bool local )
{
if ( m_pPhysicsController1 )
m_pPhysicsController1 - > ApplyForce ( force , local ) ;
}
void KX_GameObject : : ApplyTorque ( const MT_Vector3 & torque , bool local )
{
if ( m_pPhysicsController1 )
m_pPhysicsController1 - > ApplyTorque ( torque , local ) ;
}
void KX_GameObject : : ApplyMovement ( const MT_Vector3 & dloc , bool local )
{
2004-03-22 22:02:18 +00:00
if ( m_pPhysicsController1 ) // (IsDynamic())
2002-10-12 11:37:38 +00:00
{
m_pPhysicsController1 - > RelativeTranslate ( dloc , local ) ;
}
2004-03-22 22:02:18 +00:00
GetSGNode ( ) - > RelativeTranslate ( dloc , GetSGNode ( ) - > GetSGParent ( ) , local ) ;
2002-10-12 11:37:38 +00:00
}
void KX_GameObject : : ApplyRotation ( const MT_Vector3 & drot , bool local )
{
MT_Matrix3x3 rotmat ( drot ) ;
2008-04-11 21:19:00 +00:00
2004-03-22 22:02:18 +00:00
GetSGNode ( ) - > RelativeRotate ( rotmat , local ) ;
2008-04-11 21:19:00 +00:00
if ( m_pPhysicsController1 ) { // (IsDynamic())
m_pPhysicsController1 - > RelativeRotate ( rotmat , local ) ;
}
2002-10-12 11:37:38 +00:00
}
/**
GetOpenGL Matrix , returns an OpenGL ' compatible ' matrix
*/
double * KX_GameObject : : GetOpenGLMatrix ( )
{
// todo: optimize and only update if necessary
double * fl = m_OpenGL_4x4Matrix . getPointer ( ) ;
MT_Transform trans ;
trans . setOrigin ( GetSGNode ( ) - > GetWorldPosition ( ) ) ;
trans . setBasis ( GetSGNode ( ) - > GetWorldOrientation ( ) ) ;
MT_Vector3 scaling = GetSGNode ( ) - > GetWorldScaling ( ) ;
2008-05-25 14:37:39 +00:00
m_bIsNegativeScaling = ( ( scaling [ 0 ] < 0.0 ) ^ ( scaling [ 1 ] < 0.0 ) ^ ( scaling [ 2 ] < 0.0 ) ) ? true : false ;
2002-10-12 11:37:38 +00:00
trans . scale ( scaling [ 0 ] , scaling [ 1 ] , scaling [ 2 ] ) ;
trans . getValue ( fl ) ;
return fl ;
}
void KX_GameObject : : Bucketize ( )
{
double * fl = GetOpenGLMatrix ( ) ;
2004-03-22 22:02:18 +00:00
for ( size_t i = 0 ; i < m_meshes . size ( ) ; i + + )
2002-10-12 11:37:38 +00:00
m_meshes [ i ] - > Bucketize ( fl , this , m_bUseObjectColor , m_objectColor ) ;
}
void KX_GameObject : : RemoveMeshes ( )
{
double * fl = GetOpenGLMatrix ( ) ;
2004-03-22 22:02:18 +00:00
for ( size_t i = 0 ; i < m_meshes . size ( ) ; i + + )
2002-10-12 11:37:38 +00:00
m_meshes [ i ] - > RemoveFromBuckets ( fl , this ) ;
//note: meshes can be shared, and are deleted by KX_BlenderSceneConverter
m_meshes . clear ( ) ;
}
void KX_GameObject : : UpdateNonDynas ( )
{
if ( m_pPhysicsController1 )
{
m_pPhysicsController1 - > SetSumoTransform ( true ) ;
}
}
void KX_GameObject : : UpdateTransform ( )
{
if ( m_pPhysicsController1 )
m_pPhysicsController1 - > SetSumoTransform ( false ) ;
}
2004-05-26 12:06:41 +00:00
void KX_GameObject : : UpdateTransformFunc ( SG_IObject * node , void * gameobj , void * scene )
{
( ( KX_GameObject * ) gameobj ) - > UpdateTransform ( ) ;
}
2002-10-12 11:37:38 +00:00
void KX_GameObject : : SetDebugColor ( unsigned int bgra )
{
2004-03-22 22:02:18 +00:00
for ( size_t i = 0 ; i < m_meshes . size ( ) ; i + + )
2002-10-12 11:37:38 +00:00
m_meshes [ i ] - > DebugColor ( bgra ) ;
}
void KX_GameObject : : ResetDebugColor ( )
{
SetDebugColor ( 0xff000000 ) ;
}
BGE logic patch: new "Add" mode for Ipo actuator, several corrections in state system.
New Add mode for Ipo actuator
=============================
A new Add button, mutually exclusive with Force button, is available in
the Ipo actuator. When selected, it activates the Add mode that consists
in adding the Ipo curve to the current object situation in world
coordinates, or parent coordinates if the object has a parent. Scale Ipo
curves are multiplied instead of added to the object current scale.
If the local flag is selected, the Ipo curve is added (multiplied) in
the object's local coordinates.
Delta Ipo curves are handled identically to normal Ipo curve and there
is no need to work with Delta Ipo curves provided that you make sure
that the Ipo curve starts from origin. Origin means location 0 for
Location Ipo curve, rotation 0 for Rotation Ipo curve and scale 1 for
Scale Ipo curve.
The "current object situation" means the object's location, rotation
and scale at the start of the Ipo curve. For Loop Stop and Loop End Ipo
actuators, this means at the start of each loop. This initial state is
used as a base during the execution of the Ipo Curve but when the Ipo
curve is restarted (later or immediately in case of Loop mode), the
object current situation at that time is used as the new base.
For reference, here is the exact operation of the Add mode for each
type of Ipo curve (oLoc, oRot, oScale, oMat: object's loc/rot/scale
and orientation matrix at the start of the curve; iLoc, iRot, iScale,
iMat: Ipo curve loc/rot/scale and orientation matrix resulting from
the rotation).
Location
Local=false: newLoc = oLoc+iLoc
Local=true : newLoc = oLoc+oScale*(oMat*iLoc)
Rotation
Local=false: newMat = iMat*oMat
Local=true : newMat = oMat*iMat
Scale
Local=false: newScale = oScale*iScale
Local=true : newScale = oScale*iScale
Add+Local mode is very useful to have dynamic object executing complex
movement relative to their current location/orientation. Of cource,
dynamics should be disabled during the execution of the curve.
Several corrections in state system
===================================
- Object initial state is taken into account when adding object
dynamically
- Fix bug with link count when adding object dynamically
- Fix false on-off detection for Actuator sensor when actuator is
trigged on negative event.
- Fix Parent actuator false activation on negative event
- Loop Ipo curve not restarting at correct frame when start frame is
different from one.
2008-07-08 12:18:43 +00:00
void KX_GameObject : : InitIPO ( bool ipo_as_force ,
bool ipo_add ,
bool ipo_local )
{
SGControllerList : : iterator it = GetSGNode ( ) - > GetSGControllerList ( ) . begin ( ) ;
while ( it ! = GetSGNode ( ) - > GetSGControllerList ( ) . end ( ) ) {
( * it ) - > SetOption ( SG_Controller : : SG_CONTR_IPO_RESET , true ) ;
( * it ) - > SetOption ( SG_Controller : : SG_CONTR_IPO_IPO_AS_FORCE , ipo_as_force ) ;
( * it ) - > SetOption ( SG_Controller : : SG_CONTR_IPO_IPO_ADD , ipo_add ) ;
( * it ) - > SetOption ( SG_Controller : : SG_CONTR_IPO_LOCAL , ipo_local ) ;
it + + ;
}
}
2002-10-12 11:37:38 +00:00
void KX_GameObject : : UpdateIPO ( float curframetime ,
BGE logic patch: new "Add" mode for Ipo actuator, several corrections in state system.
New Add mode for Ipo actuator
=============================
A new Add button, mutually exclusive with Force button, is available in
the Ipo actuator. When selected, it activates the Add mode that consists
in adding the Ipo curve to the current object situation in world
coordinates, or parent coordinates if the object has a parent. Scale Ipo
curves are multiplied instead of added to the object current scale.
If the local flag is selected, the Ipo curve is added (multiplied) in
the object's local coordinates.
Delta Ipo curves are handled identically to normal Ipo curve and there
is no need to work with Delta Ipo curves provided that you make sure
that the Ipo curve starts from origin. Origin means location 0 for
Location Ipo curve, rotation 0 for Rotation Ipo curve and scale 1 for
Scale Ipo curve.
The "current object situation" means the object's location, rotation
and scale at the start of the Ipo curve. For Loop Stop and Loop End Ipo
actuators, this means at the start of each loop. This initial state is
used as a base during the execution of the Ipo Curve but when the Ipo
curve is restarted (later or immediately in case of Loop mode), the
object current situation at that time is used as the new base.
For reference, here is the exact operation of the Add mode for each
type of Ipo curve (oLoc, oRot, oScale, oMat: object's loc/rot/scale
and orientation matrix at the start of the curve; iLoc, iRot, iScale,
iMat: Ipo curve loc/rot/scale and orientation matrix resulting from
the rotation).
Location
Local=false: newLoc = oLoc+iLoc
Local=true : newLoc = oLoc+oScale*(oMat*iLoc)
Rotation
Local=false: newMat = iMat*oMat
Local=true : newMat = oMat*iMat
Scale
Local=false: newScale = oScale*iScale
Local=true : newScale = oScale*iScale
Add+Local mode is very useful to have dynamic object executing complex
movement relative to their current location/orientation. Of cource,
dynamics should be disabled during the execution of the curve.
Several corrections in state system
===================================
- Object initial state is taken into account when adding object
dynamically
- Fix bug with link count when adding object dynamically
- Fix false on-off detection for Actuator sensor when actuator is
trigged on negative event.
- Fix Parent actuator false activation on negative event
- Loop Ipo curve not restarting at correct frame when start frame is
different from one.
2008-07-08 12:18:43 +00:00
bool recurse )
2002-10-12 11:37:38 +00:00
{
BGE logic patch: new "Add" mode for Ipo actuator, several corrections in state system.
New Add mode for Ipo actuator
=============================
A new Add button, mutually exclusive with Force button, is available in
the Ipo actuator. When selected, it activates the Add mode that consists
in adding the Ipo curve to the current object situation in world
coordinates, or parent coordinates if the object has a parent. Scale Ipo
curves are multiplied instead of added to the object current scale.
If the local flag is selected, the Ipo curve is added (multiplied) in
the object's local coordinates.
Delta Ipo curves are handled identically to normal Ipo curve and there
is no need to work with Delta Ipo curves provided that you make sure
that the Ipo curve starts from origin. Origin means location 0 for
Location Ipo curve, rotation 0 for Rotation Ipo curve and scale 1 for
Scale Ipo curve.
The "current object situation" means the object's location, rotation
and scale at the start of the Ipo curve. For Loop Stop and Loop End Ipo
actuators, this means at the start of each loop. This initial state is
used as a base during the execution of the Ipo Curve but when the Ipo
curve is restarted (later or immediately in case of Loop mode), the
object current situation at that time is used as the new base.
For reference, here is the exact operation of the Add mode for each
type of Ipo curve (oLoc, oRot, oScale, oMat: object's loc/rot/scale
and orientation matrix at the start of the curve; iLoc, iRot, iScale,
iMat: Ipo curve loc/rot/scale and orientation matrix resulting from
the rotation).
Location
Local=false: newLoc = oLoc+iLoc
Local=true : newLoc = oLoc+oScale*(oMat*iLoc)
Rotation
Local=false: newMat = iMat*oMat
Local=true : newMat = oMat*iMat
Scale
Local=false: newScale = oScale*iScale
Local=true : newScale = oScale*iScale
Add+Local mode is very useful to have dynamic object executing complex
movement relative to their current location/orientation. Of cource,
dynamics should be disabled during the execution of the curve.
Several corrections in state system
===================================
- Object initial state is taken into account when adding object
dynamically
- Fix bug with link count when adding object dynamically
- Fix false on-off detection for Actuator sensor when actuator is
trigged on negative event.
- Fix Parent actuator false activation on negative event
- Loop Ipo curve not restarting at correct frame when start frame is
different from one.
2008-07-08 12:18:43 +00:00
// just the 'normal' update procedure.
2002-10-12 11:37:38 +00:00
GetSGNode ( ) - > SetSimulatedTime ( curframetime , recurse ) ;
GetSGNode ( ) - > UpdateWorldData ( curframetime ) ;
UpdateTransform ( ) ;
}
2006-01-06 03:46:54 +00:00
// IPO update
void
KX_GameObject : : UpdateMaterialData (
MT_Vector4 rgba ,
MT_Vector3 specrgb ,
MT_Scalar hard ,
MT_Scalar spec ,
MT_Scalar ref ,
MT_Scalar emit ,
MT_Scalar alpha
)
{
int mesh = 0 ;
if ( ( ( unsigned int ) mesh < m_meshes . size ( ) ) & & mesh > = 0 ) {
RAS_MaterialBucket : : Set : : iterator mit = m_meshes [ mesh ] - > GetFirstMaterial ( ) ;
for ( ; mit ! = m_meshes [ mesh ] - > GetLastMaterial ( ) ; + + mit )
{
RAS_IPolyMaterial * poly = ( * mit ) - > GetPolyMaterial ( ) ;
if ( poly - > GetFlag ( ) & RAS_BLENDERMAT )
{
SetObjectColor ( rgba ) ;
KX_BlenderMaterial * m = static_cast < KX_BlenderMaterial * > ( poly ) ;
m - > UpdateIPO ( rgba , specrgb , hard , spec , ref , emit , alpha ) ;
}
}
}
}
2002-10-12 11:37:38 +00:00
bool
KX_GameObject : : GetVisible (
void
)
{
return m_bVisible ;
}
void
KX_GameObject : : SetVisible (
bool v
)
{
m_bVisible = v ;
}
2008-04-30 19:58:44 +00:00
void
KX_GameObject : : SetLayer (
int l
)
{
m_layer = l ;
}
int
KX_GameObject : : GetLayer (
void
)
{
return m_layer ;
}
2002-10-12 11:37:38 +00:00
// used by Python, and the actuatorshould _not_ be misused by the
// scene!
void
KX_GameObject : : MarkVisible (
bool visible
)
{
/* If explicit visibility settings are used, this is
* determined on this level . Maybe change this to mesh level
* later on ? */
2004-05-26 12:06:41 +00:00
double * fl = GetOpenGLMatrixPtr ( ) - > getPointer ( ) ;
2004-03-22 22:02:18 +00:00
for ( size_t i = 0 ; i < m_meshes . size ( ) ; i + + )
2002-10-12 11:37:38 +00:00
{
m_meshes [ i ] - > MarkVisible ( fl , this , visible , m_bUseObjectColor , m_objectColor ) ;
}
}
// Always use the flag?
void
KX_GameObject : : MarkVisible (
void
)
{
2004-05-26 12:06:41 +00:00
double * fl = GetOpenGLMatrixPtr ( ) - > getPointer ( ) ;
2004-03-22 22:02:18 +00:00
for ( size_t i = 0 ; i < m_meshes . size ( ) ; i + + )
2002-10-12 11:37:38 +00:00
{
m_meshes [ i ] - > MarkVisible ( fl ,
this ,
m_bVisible ,
m_bUseObjectColor ,
m_objectColor
) ;
}
}
2006-02-13 05:45:32 +00:00
2002-10-12 11:37:38 +00:00
void KX_GameObject : : addLinearVelocity ( const MT_Vector3 & lin_vel , bool local )
{
2004-03-22 22:02:18 +00:00
if ( m_pPhysicsController1 )
m_pPhysicsController1 - > SetLinearVelocity ( lin_vel + m_pPhysicsController1 - > GetLinearVelocity ( ) , local ) ;
2002-10-12 11:37:38 +00:00
}
void KX_GameObject : : setLinearVelocity ( const MT_Vector3 & lin_vel , bool local )
{
if ( m_pPhysicsController1 )
m_pPhysicsController1 - > SetLinearVelocity ( lin_vel , local ) ;
}
void KX_GameObject : : setAngularVelocity ( const MT_Vector3 & ang_vel , bool local )
{
if ( m_pPhysicsController1 )
m_pPhysicsController1 - > SetAngularVelocity ( ang_vel , local ) ;
}
2004-04-08 11:34:50 +00:00
void KX_GameObject : : ResolveCombinedVelocities (
const MT_Vector3 & lin_vel ,
const MT_Vector3 & ang_vel ,
bool lin_vel_local ,
bool ang_vel_local
) {
if ( m_pPhysicsController1 )
{
2005-03-25 10:33:39 +00:00
MT_Vector3 lv = lin_vel_local ? NodeGetWorldOrientation ( ) * lin_vel : lin_vel ;
MT_Vector3 av = ang_vel_local ? NodeGetWorldOrientation ( ) * ang_vel : ang_vel ;
2004-04-08 11:34:50 +00:00
m_pPhysicsController1 - > resolveCombinedVelocities (
2005-03-25 10:33:39 +00:00
lv . x ( ) , lv . y ( ) , lv . z ( ) , av . x ( ) , av . y ( ) , av . z ( ) ) ;
2004-04-08 11:34:50 +00:00
}
}
2002-10-12 11:37:38 +00:00
void KX_GameObject : : SetObjectColor ( const MT_Vector4 & rgbavec )
{
m_bUseObjectColor = true ;
m_objectColor = rgbavec ;
}
added a factor argument for aligning to vector, this isn't correct since it does linear interpolation of the vector and renormalizes.
(can be improved to rotate correctly but for our use ist ok for now, would also be useful to have an argument to clamp the maximum rotation angle to get a constant rotation speed),
This will used to make franky upright when falling from an angle, to track to a surface when hanging onto a ledge and setting the glide pitch.
Without this rotation is instant and jerky.
currently this is done with Mathutils which isnt available in Blender Player.
def do_rotate_up(own):
own.alignAxisToVect([0,0,1], 2, 0.1)
replaces...
def do_rotate_up(own):
up_nor = Vector(0,0,1)
own_mat = Matrix(*own.getOrientation()).transpose()
own_up = up_nor * own_mat
ang = AngleBetweenVecs(own_up, up_nor)
if ang > 0.005:
# Set orientation
cross = CrossVecs(own_up, up_nor)
new_mat = own_mat * RotationMatrix(ang*0.1, 3, 'r', cross)
own.setOrientation(new_mat.transpose())
M source/gameengine/Ketsji/KX_GameObject.cpp
M source/gameengine/Ketsji/KX_GameObject.h
2008-07-09 09:21:52 +00:00
void KX_GameObject : : AlignAxisToVect ( const MT_Vector3 & dir , int axis , float fac )
2008-06-02 17:31:05 +00:00
{
MT_Matrix3x3 orimat ;
MT_Vector3 vect , ori , z , x , y ;
MT_Scalar len ;
2002-10-12 11:37:38 +00:00
2008-06-02 17:31:05 +00:00
vect = dir ;
len = vect . length ( ) ;
if ( MT_fuzzyZero ( len ) )
{
cout < < " alignAxisToVect() Error: Null vector! \n " ;
return ;
}
added a factor argument for aligning to vector, this isn't correct since it does linear interpolation of the vector and renormalizes.
(can be improved to rotate correctly but for our use ist ok for now, would also be useful to have an argument to clamp the maximum rotation angle to get a constant rotation speed),
This will used to make franky upright when falling from an angle, to track to a surface when hanging onto a ledge and setting the glide pitch.
Without this rotation is instant and jerky.
currently this is done with Mathutils which isnt available in Blender Player.
def do_rotate_up(own):
own.alignAxisToVect([0,0,1], 2, 0.1)
replaces...
def do_rotate_up(own):
up_nor = Vector(0,0,1)
own_mat = Matrix(*own.getOrientation()).transpose()
own_up = up_nor * own_mat
ang = AngleBetweenVecs(own_up, up_nor)
if ang > 0.005:
# Set orientation
cross = CrossVecs(own_up, up_nor)
new_mat = own_mat * RotationMatrix(ang*0.1, 3, 'r', cross)
own.setOrientation(new_mat.transpose())
M source/gameengine/Ketsji/KX_GameObject.cpp
M source/gameengine/Ketsji/KX_GameObject.h
2008-07-09 09:21:52 +00:00
if ( fac < = 0.0 ) {
return ;
}
2008-06-02 17:31:05 +00:00
// normalize
vect / = len ;
orimat = GetSGNode ( ) - > GetWorldOrientation ( ) ;
switch ( axis )
{
case 0 : //x axis
ori = MT_Vector3 ( orimat [ 0 ] [ 2 ] , orimat [ 1 ] [ 2 ] , orimat [ 2 ] [ 2 ] ) ; //pivot axis
if ( MT_abs ( vect . dot ( ori ) ) > 1.0 - 3.0 * MT_EPSILON ) //is the vector paralell to the pivot?
ori = MT_Vector3 ( orimat [ 0 ] [ 1 ] , orimat [ 1 ] [ 1 ] , orimat [ 2 ] [ 1 ] ) ; //change the pivot!
added a factor argument for aligning to vector, this isn't correct since it does linear interpolation of the vector and renormalizes.
(can be improved to rotate correctly but for our use ist ok for now, would also be useful to have an argument to clamp the maximum rotation angle to get a constant rotation speed),
This will used to make franky upright when falling from an angle, to track to a surface when hanging onto a ledge and setting the glide pitch.
Without this rotation is instant and jerky.
currently this is done with Mathutils which isnt available in Blender Player.
def do_rotate_up(own):
own.alignAxisToVect([0,0,1], 2, 0.1)
replaces...
def do_rotate_up(own):
up_nor = Vector(0,0,1)
own_mat = Matrix(*own.getOrientation()).transpose()
own_up = up_nor * own_mat
ang = AngleBetweenVecs(own_up, up_nor)
if ang > 0.005:
# Set orientation
cross = CrossVecs(own_up, up_nor)
new_mat = own_mat * RotationMatrix(ang*0.1, 3, 'r', cross)
own.setOrientation(new_mat.transpose())
M source/gameengine/Ketsji/KX_GameObject.cpp
M source/gameengine/Ketsji/KX_GameObject.h
2008-07-09 09:21:52 +00:00
if ( fac = = 1.0 ) {
x = vect ;
} else {
x = ( vect * fac ) + ( ( orimat * MT_Vector3 ( 1.0 , 0.0 , 0.0 ) ) * ( 1 - fac ) ) ;
len = x . length ( ) ;
if ( MT_fuzzyZero ( len ) ) x = vect ;
else x / = len ;
}
2008-06-02 17:31:05 +00:00
y = ori . cross ( x ) ;
z = x . cross ( y ) ;
break ;
case 1 : //y axis
ori = MT_Vector3 ( orimat [ 0 ] [ 0 ] , orimat [ 1 ] [ 0 ] , orimat [ 2 ] [ 0 ] ) ;
if ( MT_abs ( vect . dot ( ori ) ) > 1.0 - 3.0 * MT_EPSILON )
ori = MT_Vector3 ( orimat [ 0 ] [ 2 ] , orimat [ 1 ] [ 2 ] , orimat [ 2 ] [ 2 ] ) ;
added a factor argument for aligning to vector, this isn't correct since it does linear interpolation of the vector and renormalizes.
(can be improved to rotate correctly but for our use ist ok for now, would also be useful to have an argument to clamp the maximum rotation angle to get a constant rotation speed),
This will used to make franky upright when falling from an angle, to track to a surface when hanging onto a ledge and setting the glide pitch.
Without this rotation is instant and jerky.
currently this is done with Mathutils which isnt available in Blender Player.
def do_rotate_up(own):
own.alignAxisToVect([0,0,1], 2, 0.1)
replaces...
def do_rotate_up(own):
up_nor = Vector(0,0,1)
own_mat = Matrix(*own.getOrientation()).transpose()
own_up = up_nor * own_mat
ang = AngleBetweenVecs(own_up, up_nor)
if ang > 0.005:
# Set orientation
cross = CrossVecs(own_up, up_nor)
new_mat = own_mat * RotationMatrix(ang*0.1, 3, 'r', cross)
own.setOrientation(new_mat.transpose())
M source/gameengine/Ketsji/KX_GameObject.cpp
M source/gameengine/Ketsji/KX_GameObject.h
2008-07-09 09:21:52 +00:00
if ( fac = = 1.0 ) {
y = vect ;
} else {
y = ( vect * fac ) + ( ( orimat * MT_Vector3 ( 0.0 , 1.0 , 0.0 ) ) * ( 1 - fac ) ) ;
len = y . length ( ) ;
if ( MT_fuzzyZero ( len ) ) y = vect ;
else y / = len ;
}
2008-06-02 17:31:05 +00:00
z = ori . cross ( y ) ;
x = y . cross ( z ) ;
break ;
case 2 : //z axis
ori = MT_Vector3 ( orimat [ 0 ] [ 1 ] , orimat [ 1 ] [ 1 ] , orimat [ 2 ] [ 1 ] ) ;
if ( MT_abs ( vect . dot ( ori ) ) > 1.0 - 3.0 * MT_EPSILON )
ori = MT_Vector3 ( orimat [ 0 ] [ 0 ] , orimat [ 1 ] [ 0 ] , orimat [ 2 ] [ 0 ] ) ;
added a factor argument for aligning to vector, this isn't correct since it does linear interpolation of the vector and renormalizes.
(can be improved to rotate correctly but for our use ist ok for now, would also be useful to have an argument to clamp the maximum rotation angle to get a constant rotation speed),
This will used to make franky upright when falling from an angle, to track to a surface when hanging onto a ledge and setting the glide pitch.
Without this rotation is instant and jerky.
currently this is done with Mathutils which isnt available in Blender Player.
def do_rotate_up(own):
own.alignAxisToVect([0,0,1], 2, 0.1)
replaces...
def do_rotate_up(own):
up_nor = Vector(0,0,1)
own_mat = Matrix(*own.getOrientation()).transpose()
own_up = up_nor * own_mat
ang = AngleBetweenVecs(own_up, up_nor)
if ang > 0.005:
# Set orientation
cross = CrossVecs(own_up, up_nor)
new_mat = own_mat * RotationMatrix(ang*0.1, 3, 'r', cross)
own.setOrientation(new_mat.transpose())
M source/gameengine/Ketsji/KX_GameObject.cpp
M source/gameengine/Ketsji/KX_GameObject.h
2008-07-09 09:21:52 +00:00
if ( fac = = 1.0 ) {
z = vect ;
} else {
z = ( vect * fac ) + ( ( orimat * MT_Vector3 ( 0.0 , 0.0 , 1.0 ) ) * ( 1 - fac ) ) ;
len = z . length ( ) ;
if ( MT_fuzzyZero ( len ) ) z = vect ;
else z / = len ;
}
2008-06-02 17:31:05 +00:00
x = ori . cross ( z ) ;
y = z . cross ( x ) ;
break ;
default : //wrong input?
cout < < " alignAxisToVect(): Wrong axis ' " < < axis < < " ' \n " ;
return ;
}
x . normalize ( ) ; //normalize the vectors
y . normalize ( ) ;
z . normalize ( ) ;
orimat = MT_Matrix3x3 ( x [ 0 ] , y [ 0 ] , z [ 0 ] ,
x [ 1 ] , y [ 1 ] , z [ 1 ] ,
x [ 2 ] , y [ 2 ] , z [ 2 ] ) ;
if ( GetSGNode ( ) - > GetSGParent ( ) ! = NULL )
{
// the object is a child, adapt its local orientation so that
// the global orientation is aligned as we want.
MT_Matrix3x3 invori = GetSGNode ( ) - > GetSGParent ( ) - > GetWorldOrientation ( ) . inverse ( ) ;
NodeSetLocalOrientation ( invori * orimat ) ;
}
else
NodeSetLocalOrientation ( orimat ) ;
}
2002-10-12 11:37:38 +00:00
BGE logic update: new servo control motion actuator, new distance constraint actuator, new orientation constraint actuator, new actuator sensor.
General
=======
- Removal of Damp option in motion actuator (replaced by
Servo control motion).
- No PyDoc at present, will be added soon.
Generalization of the Lvl option
================================
A sensor with the Lvl option selected will always produce an
event at the start of the game or when entering a state or at
object creation. The event will be positive or negative
depending of the sensor condition. A negative pulse makes
sense when used with a NAND controller: it will be converted
into an actuator activation.
Servo control motion
====================
A new variant of the motion actuator allows to control speed
with force. The control if of type "PID" (Propotional, Integral,
Derivate): the force is automatically adapted to achieve the
target speed. All the parameters of the servo controller are
configurable. The result is a great variety of motion style:
anysotropic friction, flying, sliding, pseudo Dloc...
This actuator should be used in preference to Dloc and LinV
as it produces more fluid movements and avoids the collision
problem with Dloc.
LinV : target speed as (X,Y,Z) vector in local or world
coordinates (mostly useful in local coordinates).
Limit: the force can be limited along each axis (in the same
coordinates of LinV). No limitation means that the force
will grow as large as necessary to achieve the target
speed along that axis. Set a max value to limit the
accelaration along an axis (slow start) and set a min
value (negative) to limit the brake force.
P: Proportional coefficient of servo controller, don't set
directly unless you know what you're doing.
I: Integral coefficient of servo controller. Use low value
(<0.1) for slow reaction (sliding), high values (>0.5)
for hard control. The P coefficient will be automatically
set to 60 times the I coefficient (a reasonable value).
D: Derivate coefficient. Leave to 0 unless you know what
you're doing. High values create instability.
Notes: - This actuator works perfectly in zero friction
environment: the PID controller will simulate friction
by applying force as needed.
- This actuator is compatible with simple Drot motion
actuator but not with LinV and Dloc motion.
- (0,0,0) is a valid target speed.
- All parameters are accessible through Python.
Distance constraint actuator
============================
A new variant of the constraint actuator allows to set the
distance and orientation relative to a surface. The controller
uses a ray to detect the surface (or any object) and adapt the
distance and orientation parallel to the surface.
Damp: Time constant (in nb of frames) of distance and
orientation control.
Dist: Select to enable distance control and set target
distance. The object will be position at the given
distance of surface along the ray direction.
Direction: chose a local axis as the ray direction.
Range: length of ray. Objecgt within this distance will be
detected.
N : Select to enable orientation control. The actuator will
change the orientation and the location of the object
so that it is parallel to the surface at the vertical
of the point of contact of the ray.
M/P : Select to enable material detection. Default is property
detection.
Property/Material: name of property/material that the target of
ray must have to be detected. If not set, property/
material filter is disabled and any collisioning object
within range will be detected.
PER : Select to enable persistent operation. Normally the
actuator disables itself automatically if the ray does
not reach a valid target.
time : Maximum activation time of actuator.
0 : unlimited.
>0: number of frames before automatic deactivation.
rotDamp: Time constant (in nb of frame) of orientation control.
0 : use Damp parameter.
>0: use a different time constant for orientation.
Notes: - If neither N nor Dist options are set, the actuator
does not change the position and orientation of the
object; it works as a ray sensor.
- The ray has no "X-ray" capability: if the first object
hit does not have the required property/material, it
returns no hit and the actuator disables itself unless
PER option is enabled.
- This actuator changes the position and orientation but
not the speed of the object. This has an important
implication in a gravity environment: the gravity will
cause the speed to increase although the object seems
to stay still (it is repositioned at each frame).
The gravity must be compensated in one way or another.
the new servo control motion actuator is the simplest
way: set the target speed along the ray axis to 0
and the servo control will automatically compensate
the gravity.
- This actuator changes the orientation of the object
and will conflict with Drot motion unless it is
placed BEFORE the Drot motion actuator (the order of
actuator is important)
- All parameters are accessible through Python.
Orientation constraint
======================
A new variant of the constraint actuator allows to align an
object axis along a global direction.
Damp : Time constant (in nb of frames) of orientation control.
X,Y,Z: Global coordinates of reference direction.
time : Maximum activation time of actuator.
0 : unlimited.
>0: number of frames before automatic deactivation.
Notes: - (X,Y,Z) = (0,0,0) is not a valid direction
- This actuator changes the orientation of the object
and will conflict with Drot motion unless it is placed
BEFORE the Drot motion actuator (the order of
actuator is important).
- This actuator doesn't change the location and speed.
It is compatible with gravity.
- All parameters are accessible through Python.
Actuator sensor
===============
This sensor detects the activation and deactivation of actuators
of the same object. The sensor generates a positive pulse when
the corresponding sensor is activated and a negative pulse when
it is deactivated (the contrary if the Inv option is selected).
This is mostly useful to chain actions and to detect the loss of
contact of the distance motion actuator.
Notes: - Actuators are disabled at the start of the game; if you
want to detect the On-Off transition of an actuator
after it has been activated at least once, unselect the
Lvl and Inv options and use a NAND controller.
- Some actuators deactivates themselves immediately after
being activated. The sensor detects this situation as
an On-Off transition.
- The actuator name can be set through Python.
2008-07-04 08:14:50 +00:00
MT_Scalar KX_GameObject : : GetMass ( )
{
if ( m_pPhysicsController1 )
{
return m_pPhysicsController1 - > GetMass ( ) ;
}
return 0.0 ;
}
2008-05-06 20:55:55 +00:00
MT_Vector3 KX_GameObject : : GetLinearVelocity ( bool local )
2002-10-12 11:37:38 +00:00
{
2008-06-18 21:22:17 +00:00
MT_Vector3 velocity ( 0.0 , 0.0 , 0.0 ) , locvel ;
MT_Matrix3x3 ori ;
2002-10-12 11:37:38 +00:00
if ( m_pPhysicsController1 )
{
velocity = m_pPhysicsController1 - > GetLinearVelocity ( ) ;
2008-05-06 20:55:55 +00:00
if ( local )
{
ori = GetSGNode ( ) - > GetWorldOrientation ( ) ;
2008-06-18 21:22:17 +00:00
locvel = velocity * ori ;
return locvel ;
2008-05-06 20:55:55 +00:00
}
2002-10-12 11:37:38 +00:00
}
2008-05-06 20:55:55 +00:00
return velocity ;
2002-10-12 11:37:38 +00:00
}
2008-06-24 19:37:43 +00:00
MT_Vector3 KX_GameObject : : GetAngularVelocity ( bool local )
{
MT_Vector3 velocity ( 0.0 , 0.0 , 0.0 ) , locvel ;
MT_Matrix3x3 ori ;
if ( m_pPhysicsController1 )
{
velocity = m_pPhysicsController1 - > GetAngularVelocity ( ) ;
if ( local )
{
ori = GetSGNode ( ) - > GetWorldOrientation ( ) ;
locvel = velocity * ori ;
return locvel ;
}
}
return velocity ;
}
2002-10-12 11:37:38 +00:00
// scenegraph node stuff
void KX_GameObject : : NodeSetLocalPosition ( const MT_Point3 & trans )
{
2008-07-20 15:40:03 +00:00
if ( m_pPhysicsController1 & & ( ! GetSGNode ( ) | | ! GetSGNode ( ) - > GetSGParent ( ) ) )
2002-10-12 11:37:38 +00:00
{
2008-07-20 15:40:03 +00:00
// don't update physic controller if the object is a child:
// 1) the transformation will not be right
// 2) in this case, the physic controller is necessarily a static object
// that is updated from the normal kinematic synchronization
2002-10-12 11:37:38 +00:00
m_pPhysicsController1 - > setPosition ( trans ) ;
}
2006-12-25 04:11:39 +00:00
if ( GetSGNode ( ) )
GetSGNode ( ) - > SetLocalPosition ( trans ) ;
2002-10-12 11:37:38 +00:00
}
void KX_GameObject : : NodeSetLocalOrientation ( const MT_Matrix3x3 & rot )
{
2008-07-20 15:40:03 +00:00
if ( m_pPhysicsController1 & & ( ! GetSGNode ( ) | | ! GetSGNode ( ) - > GetSGParent ( ) ) )
2002-10-12 11:37:38 +00:00
{
2008-07-20 15:40:03 +00:00
// see note above
2008-07-21 12:37:27 +00:00
m_pPhysicsController1 - > setOrientation ( rot ) ;
2002-10-12 11:37:38 +00:00
}
2006-12-25 04:11:39 +00:00
if ( GetSGNode ( ) )
GetSGNode ( ) - > SetLocalOrientation ( rot ) ;
2002-10-12 11:37:38 +00:00
}
void KX_GameObject : : NodeSetLocalScale ( const MT_Vector3 & scale )
{
2008-07-20 15:40:03 +00:00
if ( m_pPhysicsController1 & & ( ! GetSGNode ( ) | | ! GetSGNode ( ) - > GetSGParent ( ) ) )
2002-10-12 11:37:38 +00:00
{
2008-07-20 15:40:03 +00:00
// see note above
2002-10-12 11:37:38 +00:00
m_pPhysicsController1 - > setScaling ( scale ) ;
}
2006-12-25 04:11:39 +00:00
if ( GetSGNode ( ) )
GetSGNode ( ) - > SetLocalScale ( scale ) ;
2002-10-12 11:37:38 +00:00
}
void KX_GameObject : : NodeSetRelativeScale ( const MT_Vector3 & scale )
{
2006-12-25 04:11:39 +00:00
if ( GetSGNode ( ) )
GetSGNode ( ) - > RelativeScale ( scale ) ;
2002-10-12 11:37:38 +00:00
}
BGE logic update: new servo control motion actuator, new distance constraint actuator, new orientation constraint actuator, new actuator sensor.
General
=======
- Removal of Damp option in motion actuator (replaced by
Servo control motion).
- No PyDoc at present, will be added soon.
Generalization of the Lvl option
================================
A sensor with the Lvl option selected will always produce an
event at the start of the game or when entering a state or at
object creation. The event will be positive or negative
depending of the sensor condition. A negative pulse makes
sense when used with a NAND controller: it will be converted
into an actuator activation.
Servo control motion
====================
A new variant of the motion actuator allows to control speed
with force. The control if of type "PID" (Propotional, Integral,
Derivate): the force is automatically adapted to achieve the
target speed. All the parameters of the servo controller are
configurable. The result is a great variety of motion style:
anysotropic friction, flying, sliding, pseudo Dloc...
This actuator should be used in preference to Dloc and LinV
as it produces more fluid movements and avoids the collision
problem with Dloc.
LinV : target speed as (X,Y,Z) vector in local or world
coordinates (mostly useful in local coordinates).
Limit: the force can be limited along each axis (in the same
coordinates of LinV). No limitation means that the force
will grow as large as necessary to achieve the target
speed along that axis. Set a max value to limit the
accelaration along an axis (slow start) and set a min
value (negative) to limit the brake force.
P: Proportional coefficient of servo controller, don't set
directly unless you know what you're doing.
I: Integral coefficient of servo controller. Use low value
(<0.1) for slow reaction (sliding), high values (>0.5)
for hard control. The P coefficient will be automatically
set to 60 times the I coefficient (a reasonable value).
D: Derivate coefficient. Leave to 0 unless you know what
you're doing. High values create instability.
Notes: - This actuator works perfectly in zero friction
environment: the PID controller will simulate friction
by applying force as needed.
- This actuator is compatible with simple Drot motion
actuator but not with LinV and Dloc motion.
- (0,0,0) is a valid target speed.
- All parameters are accessible through Python.
Distance constraint actuator
============================
A new variant of the constraint actuator allows to set the
distance and orientation relative to a surface. The controller
uses a ray to detect the surface (or any object) and adapt the
distance and orientation parallel to the surface.
Damp: Time constant (in nb of frames) of distance and
orientation control.
Dist: Select to enable distance control and set target
distance. The object will be position at the given
distance of surface along the ray direction.
Direction: chose a local axis as the ray direction.
Range: length of ray. Objecgt within this distance will be
detected.
N : Select to enable orientation control. The actuator will
change the orientation and the location of the object
so that it is parallel to the surface at the vertical
of the point of contact of the ray.
M/P : Select to enable material detection. Default is property
detection.
Property/Material: name of property/material that the target of
ray must have to be detected. If not set, property/
material filter is disabled and any collisioning object
within range will be detected.
PER : Select to enable persistent operation. Normally the
actuator disables itself automatically if the ray does
not reach a valid target.
time : Maximum activation time of actuator.
0 : unlimited.
>0: number of frames before automatic deactivation.
rotDamp: Time constant (in nb of frame) of orientation control.
0 : use Damp parameter.
>0: use a different time constant for orientation.
Notes: - If neither N nor Dist options are set, the actuator
does not change the position and orientation of the
object; it works as a ray sensor.
- The ray has no "X-ray" capability: if the first object
hit does not have the required property/material, it
returns no hit and the actuator disables itself unless
PER option is enabled.
- This actuator changes the position and orientation but
not the speed of the object. This has an important
implication in a gravity environment: the gravity will
cause the speed to increase although the object seems
to stay still (it is repositioned at each frame).
The gravity must be compensated in one way or another.
the new servo control motion actuator is the simplest
way: set the target speed along the ray axis to 0
and the servo control will automatically compensate
the gravity.
- This actuator changes the orientation of the object
and will conflict with Drot motion unless it is
placed BEFORE the Drot motion actuator (the order of
actuator is important)
- All parameters are accessible through Python.
Orientation constraint
======================
A new variant of the constraint actuator allows to align an
object axis along a global direction.
Damp : Time constant (in nb of frames) of orientation control.
X,Y,Z: Global coordinates of reference direction.
time : Maximum activation time of actuator.
0 : unlimited.
>0: number of frames before automatic deactivation.
Notes: - (X,Y,Z) = (0,0,0) is not a valid direction
- This actuator changes the orientation of the object
and will conflict with Drot motion unless it is placed
BEFORE the Drot motion actuator (the order of
actuator is important).
- This actuator doesn't change the location and speed.
It is compatible with gravity.
- All parameters are accessible through Python.
Actuator sensor
===============
This sensor detects the activation and deactivation of actuators
of the same object. The sensor generates a positive pulse when
the corresponding sensor is activated and a negative pulse when
it is deactivated (the contrary if the Inv option is selected).
This is mostly useful to chain actions and to detect the loss of
contact of the distance motion actuator.
Notes: - Actuators are disabled at the start of the game; if you
want to detect the On-Off transition of an actuator
after it has been activated at least once, unselect the
Lvl and Inv options and use a NAND controller.
- Some actuators deactivates themselves immediately after
being activated. The sensor detects this situation as
an On-Off transition.
- The actuator name can be set through Python.
2008-07-04 08:14:50 +00:00
void KX_GameObject : : NodeSetWorldPosition ( const MT_Point3 & trans )
{
SG_Node * parent = m_pSGNode - > GetSGParent ( ) ;
if ( parent ! = NULL )
{
// Make sure the objects have some scale
MT_Vector3 scale = parent - > GetWorldScaling ( ) ;
if ( fabs ( scale [ 0 ] ) < FLT_EPSILON | |
fabs ( scale [ 1 ] ) < FLT_EPSILON | |
fabs ( scale [ 2 ] ) < FLT_EPSILON )
{
return ;
}
scale [ 0 ] = 1.0 / scale [ 0 ] ;
scale [ 1 ] = 1.0 / scale [ 1 ] ;
scale [ 2 ] = 1.0 / scale [ 2 ] ;
MT_Matrix3x3 invori = parent - > GetWorldOrientation ( ) . inverse ( ) ;
MT_Vector3 newpos = invori * ( trans - parent - > GetWorldPosition ( ) ) * scale ;
NodeSetLocalPosition ( MT_Point3 ( newpos [ 0 ] , newpos [ 1 ] , newpos [ 2 ] ) ) ;
}
else
{
NodeSetLocalPosition ( trans ) ;
}
}
2002-10-12 11:37:38 +00:00
void KX_GameObject : : NodeUpdateGS ( double time , bool bInitiator )
{
2006-12-25 04:11:39 +00:00
if ( GetSGNode ( ) )
GetSGNode ( ) - > UpdateWorldData ( time ) ;
2002-10-12 11:37:38 +00:00
}
const MT_Matrix3x3 & KX_GameObject : : NodeGetWorldOrientation ( ) const
{
return GetSGNode ( ) - > GetWorldOrientation ( ) ;
}
const MT_Vector3 & KX_GameObject : : NodeGetWorldScaling ( ) const
{
return GetSGNode ( ) - > GetWorldScaling ( ) ;
}
const MT_Point3 & KX_GameObject : : NodeGetWorldPosition ( ) const
{
return GetSGNode ( ) - > GetWorldPosition ( ) ;
}
/* Suspend/ resume: for the dynamic behaviour, there is a simple
* method . For the residual motion , there is not . I wonder what the
* correct solution is for Sumo . Remove from the motion - update tree ?
*
* So far , only switch the physics and logic .
* */
void KX_GameObject : : Resume ( void )
{
if ( m_suspended ) {
SCA_IObject : : Resume ( ) ;
GetPhysicsController ( ) - > RestoreDynamics ( ) ;
m_suspended = false ;
}
}
2008-07-18 14:40:24 +00:00
void KX_GameObject : : Suspend ( )
2002-10-12 11:37:38 +00:00
{
if ( ( ! m_ignore_activity_culling )
& & ( ! m_suspended ) ) {
SCA_IObject : : Suspend ( ) ;
GetPhysicsController ( ) - > SuspendDynamics ( ) ;
m_suspended = true ;
}
}
/* ------- python stuff ---------------------------------------------------*/
PyMethodDef KX_GameObject : : Methods [ ] = {
2008-07-04 00:05:50 +00:00
{ " getPosition " , ( PyCFunction ) KX_GameObject : : sPyGetPosition , METH_NOARGS } ,
{ " setPosition " , ( PyCFunction ) KX_GameObject : : sPySetPosition , METH_O } ,
2004-05-26 12:06:41 +00:00
{ " getLinearVelocity " , ( PyCFunction ) KX_GameObject : : sPyGetLinearVelocity , METH_VARARGS } ,
2008-07-03 01:34:50 +00:00
{ " setLinearVelocity " , ( PyCFunction ) KX_GameObject : : sPySetLinearVelocity , METH_VARARGS } ,
2004-05-26 12:06:41 +00:00
{ " getVelocity " , ( PyCFunction ) KX_GameObject : : sPyGetVelocity , METH_VARARGS } ,
2008-07-04 00:05:50 +00:00
{ " getMass " , ( PyCFunction ) KX_GameObject : : sPyGetMass , METH_NOARGS } ,
{ " getReactionForce " , ( PyCFunction ) KX_GameObject : : sPyGetReactionForce , METH_NOARGS } ,
{ " getOrientation " , ( PyCFunction ) KX_GameObject : : sPyGetOrientation , METH_NOARGS } ,
{ " setOrientation " , ( PyCFunction ) KX_GameObject : : sPySetOrientation , METH_O } ,
{ " getVisible " , ( PyCFunction ) KX_GameObject : : sPyGetVisible , METH_NOARGS } ,
{ " setVisible " , ( PyCFunction ) KX_GameObject : : sPySetVisible , METH_O } ,
{ " getState " , ( PyCFunction ) KX_GameObject : : sPyGetState , METH_NOARGS } ,
{ " setState " , ( PyCFunction ) KX_GameObject : : sPySetState , METH_O } ,
{ " alignAxisToVect " , ( PyCFunction ) KX_GameObject : : sPyAlignAxisToVect , METH_VARARGS } ,
{ " getAxisVect " , ( PyCFunction ) KX_GameObject : : sPyGetAxisVect , METH_O } ,
{ " suspendDynamics " , ( PyCFunction ) KX_GameObject : : sPySuspendDynamics , METH_NOARGS } ,
{ " restoreDynamics " , ( PyCFunction ) KX_GameObject : : sPyRestoreDynamics , METH_NOARGS } ,
{ " enableRigidBody " , ( PyCFunction ) KX_GameObject : : sPyEnableRigidBody , METH_NOARGS } ,
{ " disableRigidBody " , ( PyCFunction ) KX_GameObject : : sPyDisableRigidBody , METH_NOARGS } ,
2002-10-12 11:37:38 +00:00
{ " applyImpulse " , ( PyCFunction ) KX_GameObject : : sPyApplyImpulse , METH_VARARGS } ,
2008-07-04 00:05:50 +00:00
{ " setCollisionMargin " , ( PyCFunction ) KX_GameObject : : sPySetCollisionMargin , METH_O } ,
{ " getParent " , ( PyCFunction ) KX_GameObject : : sPyGetParent , METH_NOARGS } ,
{ " setParent " , ( PyCFunction ) KX_GameObject : : sPySetParent , METH_O } ,
{ " removeParent " , ( PyCFunction ) KX_GameObject : : sPyRemoveParent , METH_NOARGS } ,
2008-07-20 17:18:46 +00:00
{ " getChildren " , ( PyCFunction ) KX_GameObject : : sPyGetChildren , METH_NOARGS } ,
{ " getChildrenRecursive " , ( PyCFunction ) KX_GameObject : : sPyGetChildrenRecursive , METH_NOARGS } ,
2002-10-12 11:37:38 +00:00
{ " getMesh " , ( PyCFunction ) KX_GameObject : : sPyGetMesh , METH_VARARGS } ,
2008-07-04 00:05:50 +00:00
{ " getPhysicsId " , ( PyCFunction ) KX_GameObject : : sPyGetPhysicsId , METH_NOARGS } ,
{ " getPropertyNames " , ( PyCFunction ) KX_GameObject : : sPyGetPropertyNames , METH_NOARGS } ,
{ " endObject " , ( PyCFunction ) KX_GameObject : : sPyEndObject , METH_NOARGS } ,
2008-03-15 17:08:58 +00:00
KX_PYMETHODTABLE ( KX_GameObject , rayCastTo ) ,
2008-05-24 22:50:31 +00:00
KX_PYMETHODTABLE ( KX_GameObject , rayCast ) ,
2008-07-04 00:05:50 +00:00
KX_PYMETHODTABLE ( KX_GameObject , getDistanceTo ) ,
2002-10-12 11:37:38 +00:00
{ NULL , NULL } //Sentinel
} ;
2004-07-17 05:28:23 +00:00
/*
2002-10-12 11:37:38 +00:00
bool KX_GameObject : : ConvertPythonVectorArgs ( PyObject * args ,
MT_Vector3 & pos ,
MT_Vector3 & pos2 )
{
PyObject * pylist ;
PyObject * pylist2 ;
bool error = ( PyArg_ParseTuple ( args , " OO " , & pylist , & pylist2 ) ) ! = 0 ;
pos = ConvertPythonPylist ( pylist ) ;
pos2 = ConvertPythonPylist ( pylist2 ) ;
return error ;
}
2004-07-17 05:28:23 +00:00
*/
2002-10-12 11:37:38 +00:00
2008-07-04 00:05:50 +00:00
PyObject * KX_GameObject : : PyEndObject ( PyObject * self )
2008-06-27 11:35:55 +00:00
{
KX_Scene * scene = PHY_GetActiveScene ( ) ;
scene - > DelayedRemoveObject ( this ) ;
return Py_None ;
}
2002-10-12 11:37:38 +00:00
2008-07-04 00:05:50 +00:00
PyObject * KX_GameObject : : PyGetPosition ( PyObject * self )
2002-10-12 11:37:38 +00:00
{
2004-07-17 05:28:23 +00:00
return PyObjectFrom ( NodeGetWorldPosition ( ) ) ;
2002-10-12 11:37:38 +00:00
}
PyTypeObject KX_GameObject : : Type = {
PyObject_HEAD_INIT ( & PyType_Type )
0 ,
" KX_GameObject " ,
sizeof ( KX_GameObject ) ,
0 ,
PyDestructor ,
0 ,
__getattr ,
__setattr ,
0 , //&MyPyCompare,
__repr ,
0 , //&cvalue_as_number,
0 ,
0 ,
0 ,
0
} ;
PyParentObject KX_GameObject : : Parents [ ] = {
& KX_GameObject : : Type ,
& SCA_IObject : : Type ,
& CValue : : Type ,
NULL
} ;
2004-05-16 13:05:15 +00:00
PyObject * KX_GameObject : : _getattr ( const STR_String & attr )
2002-10-12 11:37:38 +00:00
{
2004-05-26 12:06:41 +00:00
if ( m_pPhysicsController1 )
{
if ( attr = = " mass " )
return PyFloat_FromDouble ( GetPhysicsController ( ) - > GetMass ( ) ) ;
}
if ( attr = = " parent " )
{
KX_GameObject * parent = GetParent ( ) ;
if ( parent )
2004-07-17 05:28:23 +00:00
{
parent - > AddRef ( ) ;
2004-05-26 12:06:41 +00:00
return parent ;
2004-07-17 05:28:23 +00:00
}
2008-07-04 00:05:50 +00:00
Py_RETURN_NONE ;
2004-05-26 12:06:41 +00:00
}
2004-07-17 05:28:23 +00:00
2004-05-26 12:06:41 +00:00
if ( attr = = " visible " )
return PyInt_FromLong ( m_bVisible ) ;
if ( attr = = " position " )
2004-07-17 05:28:23 +00:00
return PyObjectFrom ( NodeGetWorldPosition ( ) ) ;
2004-05-26 12:06:41 +00:00
if ( attr = = " orientation " )
2004-07-17 05:28:23 +00:00
return PyObjectFrom ( NodeGetWorldOrientation ( ) ) ;
2004-05-26 12:06:41 +00:00
if ( attr = = " scaling " )
2004-07-17 05:28:23 +00:00
return PyObjectFrom ( NodeGetWorldScaling ( ) ) ;
if ( attr = = " name " )
return PyString_FromString ( m_name . ReadPtr ( ) ) ;
2008-06-14 17:12:49 +00:00
if ( attr = = " timeOffset " ) {
if ( m_pSGNode - > GetSGParent ( ) - > IsSlowParent ( ) ) {
return PyFloat_FromDouble ( static_cast < KX_SlowParentRelation * > ( m_pSGNode - > GetSGParent ( ) - > GetParentRelation ( ) ) - > GetTimeOffset ( ) ) ;
} else {
return PyFloat_FromDouble ( 0.0 ) ;
}
}
2004-05-26 12:06:41 +00:00
2002-10-12 11:37:38 +00:00
_getattr_up ( SCA_IObject ) ;
}
2004-05-26 12:06:41 +00:00
int KX_GameObject : : _setattr ( const STR_String & attr , PyObject * value ) // _setattr method
{
if ( attr = = " mass " )
return 1 ;
if ( attr = = " parent " )
return 1 ;
if ( PyInt_Check ( value ) )
{
int val = PyInt_AsLong ( value ) ;
if ( attr = = " visible " )
{
SetVisible ( val ! = 0 ) ;
return 0 ;
}
}
2008-06-14 17:12:49 +00:00
if ( PyFloat_Check ( value ) )
{
MT_Scalar val = PyFloat_AsDouble ( value ) ;
if ( attr = = " timeOffset " ) {
2008-07-04 00:05:50 +00:00
if ( m_pSGNode - > GetSGParent ( ) & & m_pSGNode - > GetSGParent ( ) - > IsSlowParent ( ) ) {
2008-06-14 17:12:49 +00:00
static_cast < KX_SlowParentRelation * > ( m_pSGNode - > GetSGParent ( ) - > GetParentRelation ( ) ) - > SetTimeOffset ( val ) ;
return 0 ;
} else {
return 0 ;
}
}
}
2004-05-26 12:06:41 +00:00
if ( PySequence_Check ( value ) )
{
if ( attr = = " orientation " )
{
MT_Matrix3x3 rot ;
if ( PyObject_IsMT_Matrix ( value , 3 ) )
{
2004-07-17 05:28:23 +00:00
if ( PyMatTo ( value , rot ) )
{
NodeSetLocalOrientation ( rot ) ;
2008-04-06 18:30:52 +00:00
NodeUpdateGS ( 0.f , true ) ;
2004-07-17 05:28:23 +00:00
return 0 ;
}
return 1 ;
2004-05-26 12:06:41 +00:00
}
if ( PySequence_Size ( value ) = = 4 )
{
2004-07-17 05:28:23 +00:00
MT_Quaternion qrot ;
if ( PyVecTo ( value , qrot ) )
{
rot . setRotation ( qrot ) ;
NodeSetLocalOrientation ( rot ) ;
2008-04-06 18:30:52 +00:00
NodeUpdateGS ( 0.f , true ) ;
2004-07-17 05:28:23 +00:00
return 0 ;
}
return 1 ;
2004-05-26 12:06:41 +00:00
}
if ( PySequence_Size ( value ) = = 3 )
{
2004-07-17 05:28:23 +00:00
MT_Vector3 erot ;
if ( PyVecTo ( value , erot ) )
{
rot . setEuler ( erot ) ;
NodeSetLocalOrientation ( rot ) ;
2008-04-06 18:30:52 +00:00
NodeUpdateGS ( 0.f , true ) ;
2004-07-17 05:28:23 +00:00
return 0 ;
}
return 1 ;
2004-05-26 12:06:41 +00:00
}
return 1 ;
}
if ( attr = = " position " )
{
2004-07-17 05:28:23 +00:00
MT_Point3 pos ;
if ( PyVecTo ( value , pos ) )
{
NodeSetLocalPosition ( pos ) ;
2008-04-06 18:30:52 +00:00
NodeUpdateGS ( 0.f , true ) ;
2004-07-17 05:28:23 +00:00
return 0 ;
}
return 1 ;
2004-05-26 12:06:41 +00:00
}
if ( attr = = " scaling " )
{
2004-07-17 05:28:23 +00:00
MT_Vector3 scale ;
if ( PyVecTo ( value , scale ) )
{
NodeSetLocalScale ( scale ) ;
2008-04-06 18:30:52 +00:00
NodeUpdateGS ( 0.f , true ) ;
2004-07-17 05:28:23 +00:00
return 0 ;
}
return 1 ;
}
}
if ( PyString_Check ( value ) )
{
if ( attr = = " name " )
{
m_name = PyString_AsString ( value ) ;
2004-05-26 12:06:41 +00:00
return 0 ;
}
}
2008-04-06 18:30:52 +00:00
/* Need to have parent settable here too */
2004-05-26 12:06:41 +00:00
return SCA_IObject : : _setattr ( attr , value ) ;
}
2002-10-12 11:37:38 +00:00
PyObject * KX_GameObject : : PyGetLinearVelocity ( PyObject * self ,
PyObject * args ,
PyObject * kwds )
{
// only can get the velocity if we have a physics object connected to us...
2008-05-06 20:55:55 +00:00
int local = 0 ;
if ( PyArg_ParseTuple ( args , " |i " , & local ) )
{
return PyObjectFrom ( GetLinearVelocity ( ( local ! = 0 ) ) ) ;
}
else
{
return NULL ;
}
2002-10-12 11:37:38 +00:00
}
2008-07-03 01:34:50 +00:00
PyObject * KX_GameObject : : PySetLinearVelocity ( PyObject * self ,
PyObject * args ,
PyObject * kwds )
{
int local = 0 ;
PyObject * pyvect ;
if ( PyArg_ParseTuple ( args , " O|i " , & pyvect , & local ) ) {
MT_Vector3 velocity ;
if ( PyVecTo ( pyvect , velocity ) ) {
setLinearVelocity ( velocity , ( local ! = 0 ) ) ;
2008-07-04 00:05:50 +00:00
Py_RETURN_NONE ;
2008-07-03 01:34:50 +00:00
}
}
return NULL ;
}
2002-10-12 11:37:38 +00:00
2008-07-04 00:05:50 +00:00
PyObject * KX_GameObject : : PySetVisible ( PyObject * self , PyObject * value )
2002-10-12 11:37:38 +00:00
{
2008-07-04 00:05:50 +00:00
int visible = PyInt_AsLong ( value ) ;
2002-10-12 11:37:38 +00:00
2008-07-04 00:05:50 +00:00
if ( visible = = - 1 & & PyErr_Occurred ( ) ) {
PyErr_SetString ( PyExc_TypeError , " expected 0 or 1 " ) ;
return NULL ;
2002-10-12 11:37:38 +00:00
}
2008-07-04 00:05:50 +00:00
MarkVisible ( visible ! = 0 ) ;
m_bVisible = ( visible ! = 0 ) ;
Py_RETURN_NONE ;
2002-10-12 11:37:38 +00:00
}
2008-07-04 00:05:50 +00:00
PyObject * KX_GameObject : : PyGetVisible ( PyObject * self )
2008-06-26 12:39:06 +00:00
{
return PyInt_FromLong ( m_bVisible ) ;
}
2002-10-12 11:37:38 +00:00
2008-07-04 00:05:50 +00:00
PyObject * KX_GameObject : : PyGetState ( PyObject * self )
2008-06-29 21:52:23 +00:00
{
int state = 0 ;
state | = GetState ( ) ;
return PyInt_FromLong ( state ) ;
}
2008-07-04 00:05:50 +00:00
PyObject * KX_GameObject : : PySetState ( PyObject * self , PyObject * value )
2008-06-29 21:52:23 +00:00
{
2008-07-04 00:05:50 +00:00
int state_i = PyInt_AsLong ( value ) ;
2008-06-29 21:52:23 +00:00
unsigned int state = 0 ;
2008-07-04 00:05:50 +00:00
if ( state_i = = - 1 & & PyErr_Occurred ( ) ) {
PyErr_SetString ( PyExc_TypeError , " expected an int bit field " ) ;
return NULL ;
2008-06-29 21:52:23 +00:00
}
2008-07-04 00:05:50 +00:00
state | = state_i ;
if ( ( state & ( ( 1 < < 30 ) - 1 ) ) = = 0 ) {
PyErr_SetString ( PyExc_AttributeError , " The state bitfield was not between 0 and 30 (1<<0 and 1<<29) " ) ;
return NULL ;
2008-06-29 21:52:23 +00:00
}
2008-07-04 00:05:50 +00:00
SetState ( state ) ;
Py_RETURN_NONE ;
2008-06-29 21:52:23 +00:00
}
2002-10-12 11:37:38 +00:00
PyObject * KX_GameObject : : PyGetVelocity ( PyObject * self ,
PyObject * args ,
PyObject * kwds )
{
// only can get the velocity if we have a physics object connected to us...
MT_Vector3 velocity ( 0.0 , 0.0 , 0.0 ) ;
MT_Point3 point ( 0.0 , 0.0 , 0.0 ) ;
2004-05-26 12:06:41 +00:00
PyObject * pypos = NULL ;
if ( PyArg_ParseTuple ( args , " |O " , & pypos ) )
2002-10-12 11:37:38 +00:00
{
2004-05-26 12:06:41 +00:00
if ( pypos )
2004-07-17 05:28:23 +00:00
PyVecTo ( pypos , point ) ;
2002-10-12 11:37:38 +00:00
}
2008-07-01 16:43:46 +00:00
else {
return NULL ;
}
2002-10-12 11:37:38 +00:00
if ( m_pPhysicsController1 )
{
velocity = m_pPhysicsController1 - > GetVelocity ( point ) ;
}
2004-07-17 05:28:23 +00:00
return PyObjectFrom ( velocity ) ;
2002-10-12 11:37:38 +00:00
}
2008-07-04 00:05:50 +00:00
PyObject * KX_GameObject : : PyGetMass ( PyObject * self )
2002-10-12 11:37:38 +00:00
{
2008-07-04 00:05:50 +00:00
return PyFloat_FromDouble ( GetPhysicsController ( ) - > GetMass ( ) ) ;
2002-10-12 11:37:38 +00:00
}
2008-07-04 00:05:50 +00:00
PyObject * KX_GameObject : : PyGetReactionForce ( PyObject * self )
2002-10-12 11:37:38 +00:00
{
// only can get the velocity if we have a physics object connected to us...
2004-07-17 05:28:23 +00:00
return PyObjectFrom ( GetPhysicsController ( ) - > getReactionForce ( ) ) ;
2002-10-12 11:37:38 +00:00
}
2008-07-04 00:05:50 +00:00
PyObject * KX_GameObject : : PyEnableRigidBody ( PyObject * self )
2002-10-12 11:37:38 +00:00
{
GetPhysicsController ( ) - > setRigidBody ( true ) ;
2008-07-04 00:05:50 +00:00
Py_RETURN_NONE ;
2002-10-12 11:37:38 +00:00
}
2008-07-04 00:05:50 +00:00
PyObject * KX_GameObject : : PyDisableRigidBody ( PyObject * self )
2002-10-12 11:37:38 +00:00
{
GetPhysicsController ( ) - > setRigidBody ( false ) ;
2008-07-04 00:05:50 +00:00
Py_RETURN_NONE ;
2002-10-12 11:37:38 +00:00
}
2008-07-04 00:05:50 +00:00
PyObject * KX_GameObject : : PyGetParent ( PyObject * self )
2002-10-12 11:37:38 +00:00
{
KX_GameObject * parent = this - > GetParent ( ) ;
if ( parent )
2004-07-17 05:28:23 +00:00
{
parent - > AddRef ( ) ;
2002-10-12 11:37:38 +00:00
return parent ;
2004-07-17 05:28:23 +00:00
}
2008-07-04 00:05:50 +00:00
Py_RETURN_NONE ;
2002-10-12 11:37:38 +00:00
}
2008-07-04 00:05:50 +00:00
PyObject * KX_GameObject : : PySetParent ( PyObject * self , PyObject * value )
2008-04-06 18:30:52 +00:00
{
2008-07-04 00:05:50 +00:00
if ( ! PyObject_TypeCheck ( value , & KX_GameObject : : Type ) ) {
PyErr_SetString ( PyExc_TypeError , " expected a KX_GameObject type " ) ;
2008-07-01 16:43:46 +00:00
return NULL ;
}
2008-07-04 00:05:50 +00:00
// The object we want to set as parent
CValue * m_ob = ( CValue * ) value ;
KX_GameObject * obj = ( ( KX_GameObject * ) m_ob ) ;
KX_Scene * scene = PHY_GetActiveScene ( ) ;
this - > SetParent ( scene , obj ) ;
Py_RETURN_NONE ;
2008-04-06 18:30:52 +00:00
}
2002-10-12 11:37:38 +00:00
2008-07-04 00:05:50 +00:00
PyObject * KX_GameObject : : PyRemoveParent ( PyObject * self )
2008-04-06 18:30:52 +00:00
{
KX_Scene * scene = PHY_GetActiveScene ( ) ;
this - > RemoveParent ( scene ) ;
2008-07-04 00:05:50 +00:00
Py_RETURN_NONE ;
2008-04-06 18:30:52 +00:00
}
2002-10-12 11:37:38 +00:00
2008-07-20 17:18:46 +00:00
2008-07-22 16:44:35 +00:00
static void walk_children ( SG_Node * node , CListValue * list , bool recursive )
2008-07-20 17:18:46 +00:00
{
NodeList & children = node - > GetSGChildren ( ) ;
for ( NodeList : : iterator childit = children . begin ( ) ; ! ( childit = = children . end ( ) ) ; + + childit )
{
SG_Node * childnode = ( * childit ) ;
2008-07-22 16:44:35 +00:00
CValue * childobj = ( CValue * ) childnode - > GetSGClientObject ( ) ;
2008-07-20 17:18:46 +00:00
if ( childobj ! = NULL ) // This is a GameObject
{
// add to the list
2008-07-22 16:44:35 +00:00
list - > Add ( childobj - > AddRef ( ) ) ;
2008-07-20 17:18:46 +00:00
}
// if the childobj is NULL then this may be an inverse parent link
// so a non recursive search should still look down this node.
if ( recursive | | childobj = = NULL ) {
walk_children ( childnode , list , recursive ) ;
}
}
}
PyObject * KX_GameObject : : PyGetChildren ( PyObject * self )
{
2008-07-22 16:44:35 +00:00
CListValue * list = new CListValue ( ) ;
2008-07-20 17:18:46 +00:00
walk_children ( m_pSGNode , list , 0 ) ;
return list ;
}
PyObject * KX_GameObject : : PyGetChildrenRecursive ( PyObject * self )
{
2008-07-22 16:44:35 +00:00
CListValue * list = new CListValue ( ) ;
2008-07-20 17:18:46 +00:00
walk_children ( m_pSGNode , list , 1 ) ;
return list ;
}
2002-10-12 11:37:38 +00:00
PyObject * KX_GameObject : : PyGetMesh ( PyObject * self ,
PyObject * args ,
PyObject * kwds )
{
2004-05-26 12:06:41 +00:00
int mesh = 0 ;
2006-01-06 03:46:54 +00:00
2004-05-26 12:06:41 +00:00
if ( PyArg_ParseTuple ( args , " |i " , & mesh ) )
2002-10-12 11:37:38 +00:00
{
2005-03-09 19:45:59 +00:00
if ( ( ( unsigned int ) mesh < m_meshes . size ( ) ) & & mesh > = 0 )
2004-05-26 12:06:41 +00:00
{
KX_MeshProxy * meshproxy = new KX_MeshProxy ( m_meshes [ mesh ] ) ;
return meshproxy ;
}
2002-10-12 11:37:38 +00:00
}
2008-07-10 12:47:20 +00:00
Py_RETURN_NONE ;
2002-10-12 11:37:38 +00:00
}
2006-06-18 22:10:00 +00:00
2008-07-04 00:05:50 +00:00
PyObject * KX_GameObject : : PySetCollisionMargin ( PyObject * self , PyObject * value )
2006-06-18 22:10:00 +00:00
{
2008-07-04 00:05:50 +00:00
float collisionMargin = PyFloat_AsDouble ( value ) ;
if ( collisionMargin = = - 1 & & PyErr_Occurred ( ) ) {
PyErr_SetString ( PyExc_TypeError , " expected a float " ) ;
return NULL ;
2006-06-18 22:10:00 +00:00
}
2008-07-04 00:05:50 +00:00
if ( m_pPhysicsController1 )
{
m_pPhysicsController1 - > setMargin ( collisionMargin ) ;
Py_RETURN_NONE ;
}
PyErr_SetString ( PyExc_RuntimeError , " This object has no physics controller " ) ;
2006-06-18 22:10:00 +00:00
return NULL ;
}
2002-10-12 11:37:38 +00:00
PyObject * KX_GameObject : : PyApplyImpulse ( PyObject * self ,
PyObject * args ,
PyObject * kwds )
{
2004-05-26 12:06:41 +00:00
PyObject * pyattach ;
PyObject * pyimpulse ;
2008-07-04 00:05:50 +00:00
if ( ! m_pPhysicsController1 ) {
PyErr_SetString ( PyExc_RuntimeError , " This object has no physics controller " ) ;
return NULL ;
}
2004-05-26 12:06:41 +00:00
if ( PyArg_ParseTuple ( args , " OO " , & pyattach , & pyimpulse ) )
2002-10-12 11:37:38 +00:00
{
2004-07-17 05:28:23 +00:00
MT_Point3 attach ;
MT_Vector3 impulse ;
2008-07-04 00:05:50 +00:00
if ( PyVecTo ( pyattach , attach ) & & PyVecTo ( pyimpulse , impulse ) )
2002-10-12 11:37:38 +00:00
{
2008-07-04 00:05:50 +00:00
m_pPhysicsController1 - > applyImpulse ( attach , impulse ) ;
Py_RETURN_NONE ;
2002-10-12 11:37:38 +00:00
}
2004-05-26 12:06:41 +00:00
2002-10-12 11:37:38 +00:00
}
2004-07-17 05:28:23 +00:00
return NULL ;
2002-10-12 11:37:38 +00:00
}
2008-07-04 00:05:50 +00:00
PyObject * KX_GameObject : : PySuspendDynamics ( PyObject * self )
2002-10-12 11:37:38 +00:00
{
2008-06-25 14:09:15 +00:00
SuspendDynamics ( ) ;
2008-07-04 00:05:50 +00:00
Py_RETURN_NONE ;
2002-10-12 11:37:38 +00:00
}
2008-07-04 00:05:50 +00:00
PyObject * KX_GameObject : : PyRestoreDynamics ( PyObject * self )
2002-10-12 11:37:38 +00:00
{
2008-06-25 14:09:15 +00:00
RestoreDynamics ( ) ;
2008-07-04 00:05:50 +00:00
Py_RETURN_NONE ;
2002-10-12 11:37:38 +00:00
}
2008-07-04 00:05:50 +00:00
PyObject * KX_GameObject : : PyGetOrientation ( PyObject * self ) //keywords
2002-10-12 11:37:38 +00:00
{
2004-07-17 05:28:23 +00:00
return PyObjectFrom ( NodeGetWorldOrientation ( ) ) ;
2002-10-12 11:37:38 +00:00
}
2008-07-04 00:05:50 +00:00
PyObject * KX_GameObject : : PySetOrientation ( PyObject * self , PyObject * value )
2002-10-12 11:37:38 +00:00
{
2008-07-04 00:05:50 +00:00
MT_Matrix3x3 matrix ;
if ( PyObject_IsMT_Matrix ( value , 3 ) & & PyMatTo ( value , matrix ) )
2002-10-12 11:37:38 +00:00
{
2008-07-04 00:05:50 +00:00
NodeSetLocalOrientation ( matrix ) ;
NodeUpdateGS ( 0.f , true ) ;
Py_RETURN_NONE ;
}
MT_Quaternion quat ;
if ( PyVecTo ( value , quat ) )
{
matrix . setRotation ( quat ) ;
NodeSetLocalOrientation ( matrix ) ;
NodeUpdateGS ( 0.f , true ) ;
Py_RETURN_NONE ;
2002-10-12 11:37:38 +00:00
}
2004-07-17 05:28:23 +00:00
return NULL ;
2002-10-12 11:37:38 +00:00
}
2008-06-02 17:31:05 +00:00
PyObject * KX_GameObject : : PyAlignAxisToVect ( PyObject * self ,
PyObject * args ,
PyObject * kwds )
{
PyObject * pyvect ;
int axis = 2 ; //z axis is the default
added a factor argument for aligning to vector, this isn't correct since it does linear interpolation of the vector and renormalizes.
(can be improved to rotate correctly but for our use ist ok for now, would also be useful to have an argument to clamp the maximum rotation angle to get a constant rotation speed),
This will used to make franky upright when falling from an angle, to track to a surface when hanging onto a ledge and setting the glide pitch.
Without this rotation is instant and jerky.
currently this is done with Mathutils which isnt available in Blender Player.
def do_rotate_up(own):
own.alignAxisToVect([0,0,1], 2, 0.1)
replaces...
def do_rotate_up(own):
up_nor = Vector(0,0,1)
own_mat = Matrix(*own.getOrientation()).transpose()
own_up = up_nor * own_mat
ang = AngleBetweenVecs(own_up, up_nor)
if ang > 0.005:
# Set orientation
cross = CrossVecs(own_up, up_nor)
new_mat = own_mat * RotationMatrix(ang*0.1, 3, 'r', cross)
own.setOrientation(new_mat.transpose())
M source/gameengine/Ketsji/KX_GameObject.cpp
M source/gameengine/Ketsji/KX_GameObject.h
2008-07-09 09:21:52 +00:00
float fac = 1.0 ;
2008-06-02 17:31:05 +00:00
added a factor argument for aligning to vector, this isn't correct since it does linear interpolation of the vector and renormalizes.
(can be improved to rotate correctly but for our use ist ok for now, would also be useful to have an argument to clamp the maximum rotation angle to get a constant rotation speed),
This will used to make franky upright when falling from an angle, to track to a surface when hanging onto a ledge and setting the glide pitch.
Without this rotation is instant and jerky.
currently this is done with Mathutils which isnt available in Blender Player.
def do_rotate_up(own):
own.alignAxisToVect([0,0,1], 2, 0.1)
replaces...
def do_rotate_up(own):
up_nor = Vector(0,0,1)
own_mat = Matrix(*own.getOrientation()).transpose()
own_up = up_nor * own_mat
ang = AngleBetweenVecs(own_up, up_nor)
if ang > 0.005:
# Set orientation
cross = CrossVecs(own_up, up_nor)
new_mat = own_mat * RotationMatrix(ang*0.1, 3, 'r', cross)
own.setOrientation(new_mat.transpose())
M source/gameengine/Ketsji/KX_GameObject.cpp
M source/gameengine/Ketsji/KX_GameObject.h
2008-07-09 09:21:52 +00:00
if ( PyArg_ParseTuple ( args , " O|if " , & pyvect , & axis , & fac ) )
2008-06-02 17:31:05 +00:00
{
MT_Vector3 vect ;
if ( PyVecTo ( pyvect , vect ) )
{
added a factor argument for aligning to vector, this isn't correct since it does linear interpolation of the vector and renormalizes.
(can be improved to rotate correctly but for our use ist ok for now, would also be useful to have an argument to clamp the maximum rotation angle to get a constant rotation speed),
This will used to make franky upright when falling from an angle, to track to a surface when hanging onto a ledge and setting the glide pitch.
Without this rotation is instant and jerky.
currently this is done with Mathutils which isnt available in Blender Player.
def do_rotate_up(own):
own.alignAxisToVect([0,0,1], 2, 0.1)
replaces...
def do_rotate_up(own):
up_nor = Vector(0,0,1)
own_mat = Matrix(*own.getOrientation()).transpose()
own_up = up_nor * own_mat
ang = AngleBetweenVecs(own_up, up_nor)
if ang > 0.005:
# Set orientation
cross = CrossVecs(own_up, up_nor)
new_mat = own_mat * RotationMatrix(ang*0.1, 3, 'r', cross)
own.setOrientation(new_mat.transpose())
M source/gameengine/Ketsji/KX_GameObject.cpp
M source/gameengine/Ketsji/KX_GameObject.h
2008-07-09 09:21:52 +00:00
AlignAxisToVect ( vect , axis , fac ) ;
2008-07-04 00:05:50 +00:00
Py_RETURN_NONE ;
2008-06-02 17:31:05 +00:00
}
}
return NULL ;
}
2002-10-12 11:37:38 +00:00
2008-07-04 00:05:50 +00:00
PyObject * KX_GameObject : : PyGetAxisVect ( PyObject * self , PyObject * value )
{
MT_Vector3 vect ;
if ( PyVecTo ( value , vect ) )
{
2008-07-04 19:00:56 +00:00
return PyObjectFrom ( NodeGetWorldOrientation ( ) * vect ) ;
2008-07-04 00:05:50 +00:00
}
return NULL ;
}
PyObject * KX_GameObject : : PySetPosition ( PyObject * self , PyObject * value )
2002-10-12 11:37:38 +00:00
{
2004-07-17 05:28:23 +00:00
MT_Point3 pos ;
2008-07-04 00:05:50 +00:00
if ( PyVecTo ( value , pos ) )
2002-10-12 11:37:38 +00:00
{
2004-05-26 12:06:41 +00:00
NodeSetLocalPosition ( pos ) ;
2006-04-01 03:30:15 +00:00
NodeUpdateGS ( 0.f , true ) ;
2008-07-04 00:05:50 +00:00
Py_RETURN_NONE ;
2002-10-12 11:37:38 +00:00
}
2008-07-04 00:05:50 +00:00
2004-07-17 05:28:23 +00:00
return NULL ;
2002-10-12 11:37:38 +00:00
}
2008-07-04 00:05:50 +00:00
PyObject * KX_GameObject : : PyGetPhysicsId ( PyObject * self )
2002-10-12 11:37:38 +00:00
{
KX_IPhysicsController * ctrl = GetPhysicsController ( ) ;
2005-11-28 06:51:54 +00:00
uint_ptr physid = 0 ;
2002-10-12 11:37:38 +00:00
if ( ctrl )
{
2005-11-28 06:51:54 +00:00
physid = ( uint_ptr ) ctrl - > GetUserData ( ) ;
2002-10-12 11:37:38 +00:00
}
2005-11-28 06:51:54 +00:00
return PyInt_FromLong ( ( long ) physid ) ;
2002-10-12 11:37:38 +00:00
}
2008-07-04 00:05:50 +00:00
PyObject * KX_GameObject : : PyGetPropertyNames ( PyObject * self )
2008-06-26 12:39:06 +00:00
{
return ConvertKeysToPython ( ) ;
}
2005-01-16 05:55:04 +00:00
KX_PYMETHODDEF_DOC ( KX_GameObject , getDistanceTo ,
" getDistanceTo(other): get distance to another point/KX_GameObject " )
{
MT_Point3 b ;
if ( PyVecArgTo ( args , b ) )
{
return PyFloat_FromDouble ( NodeGetWorldPosition ( ) . distance ( b ) ) ;
}
PyErr_Clear ( ) ;
PyObject * pyother ;
if ( PyArg_ParseTuple ( args , " O! " , & KX_GameObject : : Type , & pyother ) )
{
KX_GameObject * other = static_cast < KX_GameObject * > ( pyother ) ;
return PyFloat_FromDouble ( NodeGetWorldPosition ( ) . distance ( other - > NodeGetWorldPosition ( ) ) ) ;
}
return NULL ;
}
2008-03-15 17:08:58 +00:00
bool KX_GameObject : : RayHit ( KX_ClientObjectInfo * client , MT_Point3 & hit_point , MT_Vector3 & hit_normal , void * const data )
{
KX_GameObject * hitKXObj = client - > m_gameobject ;
if ( client - > m_type > KX_ClientObjectInfo : : ACTOR )
{
// false hit
return false ;
}
if ( m_testPropName . Length ( ) = = 0 | | hitKXObj - > GetProperty ( m_testPropName ) ! = NULL )
{
m_pHitObject = hitKXObj ;
return true ;
}
return false ;
}
KX_PYMETHODDEF_DOC ( KX_GameObject , rayCastTo ,
2008-05-24 18:06:58 +00:00
" rayCastTo(other,dist,prop): look towards another point/KX_GameObject and return first object hit within dist that matches prop \n "
2008-03-15 17:08:58 +00:00
" prop = property name that object must have; can be omitted => detect any object \n "
" dist = max distance to look (can be negative => look behind); 0 or omitted => detect up to other \n "
" other = 3-tuple or object reference " )
{
MT_Point3 toPoint ;
PyObject * pyarg ;
float dist = 0.0f ;
char * propName = NULL ;
if ( ! PyArg_ParseTuple ( args , " O|fs " , & pyarg , & dist , & propName ) )
return NULL ;
if ( ! PyVecTo ( pyarg , toPoint ) )
{
KX_GameObject * other ;
PyErr_Clear ( ) ;
2008-07-06 14:11:30 +00:00
if ( ! PyType_IsSubtype ( pyarg - > ob_type , & KX_GameObject : : Type ) ) {
PyErr_SetString ( PyExc_TypeError , " the first argument to rayCastTo must be a vector or a KX_GameObject " ) ;
2008-03-15 17:08:58 +00:00
return NULL ;
2008-07-06 14:11:30 +00:00
}
2008-03-15 17:08:58 +00:00
other = static_cast < KX_GameObject * > ( pyarg ) ;
toPoint = other - > NodeGetWorldPosition ( ) ;
}
MT_Point3 fromPoint = NodeGetWorldPosition ( ) ;
if ( dist ! = 0.0f )
{
MT_Vector3 toDir = toPoint - fromPoint ;
toDir . normalize ( ) ;
toPoint = fromPoint + ( dist ) * toDir ;
}
MT_Point3 resultPoint ;
MT_Vector3 resultNormal ;
PHY_IPhysicsEnvironment * pe = GetPhysicsEnvironment ( ) ;
KX_IPhysicsController * spc = GetPhysicsController ( ) ;
KX_GameObject * parent = GetParent ( ) ;
if ( ! spc & & parent )
spc = parent - > GetPhysicsController ( ) ;
if ( parent )
parent - > Release ( ) ;
m_pHitObject = NULL ;
if ( propName )
m_testPropName = propName ;
else
m_testPropName . SetLength ( 0 ) ;
KX_RayCast : : RayTest ( spc , pe , fromPoint , toPoint , resultPoint , resultNormal , KX_RayCast : : Callback < KX_GameObject > ( this ) ) ;
if ( m_pHitObject )
{
m_pHitObject - > AddRef ( ) ;
return m_pHitObject ;
}
2008-07-04 00:05:50 +00:00
Py_RETURN_NONE ;
2008-03-15 17:08:58 +00:00
}
2008-05-24 22:50:31 +00:00
KX_PYMETHODDEF_DOC ( KX_GameObject , rayCast ,
" rayCast(to,from,dist,prop): cast a ray and return tuple (object,hit,normal) of contact point with object within dist that matches prop or None if no hit \n "
2008-05-24 18:06:58 +00:00
" prop = property name that object must have; can be omitted => detect any object \n "
" dist = max distance to look (can be negative => look behind); 0 or omitted => detect up to to \n "
" from = 3-tuple or object reference for origin of ray (if object, use center of object) \n "
" Can None or omitted => start from self object center \n "
" to = 3-tuple or object reference for destination of ray (if object, use center of object) \n "
" Note: the object on which you call this method matters: the ray will ignore it if it goes through it \n " )
{
MT_Point3 toPoint ;
MT_Point3 fromPoint ;
PyObject * pyto ;
PyObject * pyfrom = NULL ;
float dist = 0.0f ;
char * propName = NULL ;
KX_GameObject * other ;
if ( ! PyArg_ParseTuple ( args , " O|Ofs " , & pyto , & pyfrom , & dist , & propName ) )
return NULL ;
if ( ! PyVecTo ( pyto , toPoint ) )
{
PyErr_Clear ( ) ;
2008-07-06 14:11:30 +00:00
if ( ! PyType_IsSubtype ( pyto - > ob_type , & KX_GameObject : : Type ) ) {
PyErr_SetString ( PyExc_TypeError , " the first argument to rayCast must be a vector or a KX_GameObject " ) ;
2008-05-24 18:06:58 +00:00
return NULL ;
2008-07-06 14:11:30 +00:00
}
2008-05-24 18:06:58 +00:00
other = static_cast < KX_GameObject * > ( pyto ) ;
toPoint = other - > NodeGetWorldPosition ( ) ;
}
if ( ! pyfrom | | pyfrom = = Py_None )
{
fromPoint = NodeGetWorldPosition ( ) ;
}
else if ( ! PyVecTo ( pyfrom , fromPoint ) )
{
PyErr_Clear ( ) ;
2008-07-06 14:11:30 +00:00
if ( ! PyType_IsSubtype ( pyfrom - > ob_type , & KX_GameObject : : Type ) ) {
PyErr_SetString ( PyExc_TypeError , " the second optional argument to rayCast must be a vector or a KX_GameObject " ) ;
2008-05-24 18:06:58 +00:00
return NULL ;
2008-07-06 14:11:30 +00:00
}
2008-05-24 18:06:58 +00:00
other = static_cast < KX_GameObject * > ( pyfrom ) ;
fromPoint = other - > NodeGetWorldPosition ( ) ;
}
2008-07-09 15:30:15 +00:00
if ( dist ! = 0.0f ) {
2008-05-24 18:06:58 +00:00
MT_Vector3 toDir = toPoint - fromPoint ;
2008-07-09 15:30:15 +00:00
if ( MT_fuzzyZero ( toDir . length2 ( ) ) ) {
return Py_BuildValue ( " OOO " , Py_None , Py_None , Py_None ) ;
}
2008-05-24 18:06:58 +00:00
toDir . normalize ( ) ;
toPoint = fromPoint + ( dist ) * toDir ;
2008-07-09 15:30:15 +00:00
} else if ( MT_fuzzyZero ( ( toPoint - fromPoint ) . length2 ( ) ) ) {
return Py_BuildValue ( " OOO " , Py_None , Py_None , Py_None ) ;
2008-05-24 18:06:58 +00:00
}
2008-07-09 15:30:15 +00:00
2008-05-24 18:06:58 +00:00
MT_Point3 resultPoint ;
MT_Vector3 resultNormal ;
PHY_IPhysicsEnvironment * pe = GetPhysicsEnvironment ( ) ;
KX_IPhysicsController * spc = GetPhysicsController ( ) ;
KX_GameObject * parent = GetParent ( ) ;
if ( ! spc & & parent )
spc = parent - > GetPhysicsController ( ) ;
if ( parent )
parent - > Release ( ) ;
m_pHitObject = NULL ;
if ( propName )
m_testPropName = propName ;
else
m_testPropName . SetLength ( 0 ) ;
KX_RayCast : : RayTest ( spc , pe , fromPoint , toPoint , resultPoint , resultNormal , KX_RayCast : : Callback < KX_GameObject > ( this ) ) ;
if ( m_pHitObject )
{
PyObject * returnValue = PyTuple_New ( 3 ) ;
if ( ! returnValue )
return NULL ;
PyTuple_SET_ITEM ( returnValue , 0 , m_pHitObject - > AddRef ( ) ) ;
PyTuple_SET_ITEM ( returnValue , 1 , PyObjectFrom ( resultPoint ) ) ;
PyTuple_SET_ITEM ( returnValue , 2 , PyObjectFrom ( resultNormal ) ) ;
return returnValue ;
//return Py_BuildValue("(O,(fff),(fff))",
// m_pHitObject->AddRef(), // trick: KX_GameObject are not true Python object, they use a difference reference count system
// resultPoint[0], resultPoint[1], resultPoint[2],
// resultNormal[0], resultNormal[1], resultNormal[2]);
}
return Py_BuildValue ( " OOO " , Py_None , Py_None , Py_None ) ;
2008-07-04 00:05:50 +00:00
//Py_RETURN_NONE;
2008-05-24 18:06:58 +00:00
}
2002-10-12 11:37:38 +00:00
/* ---------------------------------------------------------------------
* Some stuff taken from the header
* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
void KX_GameObject : : Relink ( GEN_Map < GEN_HashedPtr , void * > * map_parameter )
{
2008-07-19 07:45:19 +00:00
// we will relink the sensors and actuators that use object references
// if the object is part of the replicated hierarchy, use the new
// object reference instead
SCA_SensorList & sensorlist = GetSensors ( ) ;
SCA_SensorList : : iterator sit ;
for ( sit = sensorlist . begin ( ) ; sit ! = sensorlist . end ( ) ; sit + + )
{
( * sit ) - > Relink ( map_parameter ) ;
}
SCA_ActuatorList & actuatorlist = GetActuators ( ) ;
SCA_ActuatorList : : iterator ait ;
for ( ait = actuatorlist . begin ( ) ; ait ! = actuatorlist . end ( ) ; ait + + )
{
( * ait ) - > Relink ( map_parameter ) ;
}
2002-10-12 11:37:38 +00:00
}