4 * ***** BEGIN GPL LICENSE BLOCK *****
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
21 * All rights reserved.
23 * The Original Code is: all of this file.
25 * Contributor(s): none yet.
27 * ***** END GPL LICENSE BLOCK *****
28 * General KX game object.
31 #ifndef __KX_GAMEOBJECT
32 #define __KX_GAMEOBJECT
35 // get rid of this stupid "warning 'this' used in initialiser list", generated by VC when including Solid/Sumo
36 #pragma warning (disable : 4355)
40 #include "ListValue.h"
41 #include "SCA_IObject.h"
43 #include "MT_Transform.h"
44 #include "MT_CmMatrix4x4.h"
46 #include "GEN_HashedPtr.h"
48 #include "KX_KetsjiEngine.h" /* for m_anim_framerate */
49 #include "KX_IPhysicsController.h" /* for suspend/resume */
50 #include "DNA_object_types.h"
51 #include "SCA_LogicManager.h" /* for ConvertPythonToGameObject to search object names */
52 #define KX_OB_DYNAMIC 1
55 //Forward declarations.
56 struct KX_ClientObjectInfo;
59 class KX_IPhysicsController;
60 class PHY_IGraphicController;
61 class PHY_IPhysicsEnvironment;
65 * KX_GameObject is the main class for dynamic objects.
67 class KX_GameObject : public SCA_IObject
73 KX_ClientObjectInfo* m_pClient_info;
77 std::vector<RAS_MeshObject*> m_meshes;
78 struct Object* m_pBlenderObject;
79 struct Object* m_pBlenderGroupObject;
81 bool m_bSuspendDynamics;
82 bool m_bUseObjectColor;
83 bool m_bIsNegativeScaling;
84 MT_Vector4 m_objectColor;
86 // visible = user setting
87 // culled = while rendering, depending on camera
91 KX_IPhysicsController* m_pPhysicsController1;
92 PHY_IGraphicController* m_pGraphicController;
93 // used for ray casting
94 PHY_IPhysicsEnvironment* m_pPhysicsEnvironment;
95 STR_String m_testPropName;
97 KX_GameObject* m_pHitObject;
101 MT_CmMatrix4x4 m_OpenGL_4x4Matrix;
106 // Python attributes that wont convert into CValue
108 // there are 2 places attributes can be stored, in the CValue,
109 // where attributes are converted into BGE's CValue types
110 // these can be used with property actuators
112 // For the python API, For types that cannot be converted into CValues (lists, dicts, GameObjects)
113 // these will be put into "m_attrlist", logic bricks cannot access them.
115 // rules for setting attributes.
117 // * there should NEVER be a CValue and a m_attrlist attribute with matching names. get/sets make sure of this.
118 // * if CValue conversion fails, use a PyObject in "m_attrlist"
119 // * when assigning a value, first see if it can be a CValue, if it can remove the "m_attrlist" and set the CValue
122 PyObject* m_attrlist;
126 virtual void /* This function should be virtual - derived classed override it */
128 GEN_Map<GEN_HashedPtr, void*> *map
132 * Compute an OpenGl compatable 4x4 matrix. Has the
133 * side effect of storing the result internally. The
134 * memory for the matrix remains the property of this class.
141 * Return a pointer to a MT_CmMatrix4x4 storing the
142 * opengl transformation for this object. This is updated
143 * by a call to GetOpenGLMatrix(). This class owns the
144 * memory for the returned matrix.
150 return &m_OpenGL_4x4Matrix;
154 * Get a pointer to the game object that is the parent of
155 * this object. Or NULL if there is no parent. The returned
156 * object is part of a reference counting scheme. Calling
157 * this function ups the reference count on the returned
158 * object. It is the responsibility of the caller to decrement
159 * the reference count when you have finished with it.
166 * Sets the parent of this object to a game object
168 void SetParent(KX_Scene *scene, KX_GameObject *obj);
171 * Removes the parent of this object to a game object
173 void RemoveParent(KX_Scene *scene);
176 * Construct a game object. This class also inherits the
177 * default constructors - use those with care!
181 void* sgReplicationInfo,
182 SG_Callbacks callbacks,
183 PyTypeObject* T=&Type
192 /* temporarily to find memleaks */ return CValue::AddRef();
196 * @section Stuff which is here due to poor design.
197 * Inherited from CValue and needs an implementation.
198 * Do not expect these functions do to anything sensible.
202 * Inherited from CValue -- does nothing!
211 * Inherited from CValue -- does nothing!
215 VALUE_DATA_TYPE dtype,
221 * Inherited from CValue -- does nothing!
229 * Inherited from CValue -- does nothing!
236 * @section Inherited from CValue. These are the useful
237 * part of the CValue interface that this class implements.
241 * Inherited from CValue -- returns the name of this object.
248 * Inherited from CValue -- set the name of this object.
256 * Inherited from CValue -- does nothing.
264 * Inherited from CValue -- return a new copy of this
265 * instance allocated on the heap. Ownership of the new
266 * object belongs with the caller.
273 * Inherited from CValue -- Makes sure any internal
274 * data owned by this class is deep copied. Called internally
278 KX_GameObject* replica
282 * Return the linear velocity of the game object.
290 * Return the linear velocity of a given point in world coordinate
291 * but relative to center of object ([0,0,0]=center of object)
295 const MT_Point3& position
299 * Return the mass of the object
305 * Return the local inertia vector of the object
311 * Return the angular velocity of the game object.
319 * Align the object to a given normal.
323 const MT_Vector3& vect,
329 * Quick'n'dirty obcolor ipo stuff
334 const MT_Vector4& rgbavec
339 ResolveCombinedVelocities(
340 const MT_Vector3 & lin_vel,
341 const MT_Vector3 & ang_vel,
348 * @return a pointer to the physics environment in use during the game, for rayCasting
350 PHY_IPhysicsEnvironment* GetPhysicsEnvironment()
352 return m_pPhysicsEnvironment;
355 void SetPhysicsEnvironment(PHY_IPhysicsEnvironment* physicsEnvironment)
357 m_pPhysicsEnvironment = physicsEnvironment;
361 * @return a pointer to the physics controller owned by this class.
364 KX_IPhysicsController* GetPhysicsController() ;
366 void SetPhysicsController(KX_IPhysicsController* physicscontroller,bool isDynamic)
369 m_pPhysicsController1 = physicscontroller;
372 virtual class RAS_Deformer* GetDeformer()
376 virtual void SetDeformer(class RAS_Deformer* deformer)
382 * @return a pointer to the graphic controller owner by this class
384 PHY_IGraphicController* GetGraphicController()
386 return m_pGraphicController;
389 void SetGraphicController(PHY_IGraphicController* graphiccontroller)
391 m_pGraphicController = graphiccontroller;
395 * @section Coordinate system manipulation functions
398 void NodeSetLocalPosition(const MT_Point3& trans );
400 void NodeSetLocalOrientation(const MT_Matrix3x3& rot );
402 void NodeSetLocalScale( const MT_Vector3& scale );
404 void NodeSetRelativeScale( const MT_Vector3& scale );
406 // adapt local position so that world position is set to desired position
407 void NodeSetWorldPosition(const MT_Point3& trans);
416 NodeGetWorldOrientation(
426 NodeGetWorldPosition(
431 * @section scene graph node accessor functions.
434 SG_Node* GetSGNode( )
439 const SG_Node* GetSGNode( ) const
445 * @section blender object accessor functions.
448 struct Object* GetBlenderObject( )
450 return m_pBlenderObject;
453 void SetBlenderObject( struct Object* obj)
455 m_pBlenderObject = obj;
458 struct Object* GetBlenderGroupObject( )
460 return m_pBlenderGroupObject;
463 void SetBlenderGroupObject( struct Object* obj)
465 m_pBlenderGroupObject = obj;
470 return (m_pBlenderObject &&
471 (m_pBlenderObject->transflag & OB_DUPLIGROUP) &&
472 m_pBlenderObject->dup_group != NULL) ? true : false;
476 * Set the Scene graph node for this game object.
477 * warning - it is your responsibility to make sure
478 * all controllers look at this new node. You must
479 * also take care of the memory associated with the
480 * old node. This class takes ownership of the new
483 void SetSGNode(SG_Node* node )
488 //Is it a dynamic/physics object ?
489 bool IsDynamic() const
495 * Check if this object has a vertex parent relationship
497 bool IsVertexParent( )
499 return (m_pSGNode && m_pSGNode->GetSGParent() && m_pSGNode->GetSGParent()->IsVertexParent());
502 bool RayHit(KX_ClientObjectInfo* client, KX_RayCast* result, void * const data);
503 bool NeedRayCast(KX_ClientObjectInfo* client);
507 * @section Physics accessors for this node.
509 * All these calls get passed directly to the physics controller
510 * owned by this object.
511 * This is real interface bloat. Why not just use the physics controller
512 * directly? I think this is because the python interface is in the wrong
518 const MT_Vector3& force, bool local
523 const MT_Vector3& torque,
529 const MT_Vector3& drot,
535 const MT_Vector3& dloc,
541 const MT_Vector3& lin_vel,
547 const MT_Vector3& lin_vel,
553 const MT_Vector3& ang_vel,
558 * Update the physics object transform based upon the current SG_Node
565 static void UpdateTransformFunc(SG_IObject* node, void* gameobj, void* scene);
568 * Function to set IPO option at start of IPO
578 * Odd function to update an ipo. ???
586 * Updates Material Ipo data
601 * @section Mesh accessor functions.
605 * Update buckets to indicate that there is a new
606 * user of this object's meshes.
613 * Update buckets with data about the mesh after
614 * creating or duplicating the object, changing
615 * visibility, object color, .. .
623 * Clear the meshes associated with this class
624 * and remove from the bucketing system.
625 * Don't think this actually deletes any of the meshes.
632 * Add a mesh to the set of meshes associated with this
633 * node. Meshes added in this way are not deleted by this class.
634 * Make sure you call RemoveMeshes() before deleting the
641 m_meshes.push_back(mesh);
645 * Pick out a mesh associated with the integer 'num'.
651 return m_meshes[num];
655 * Return the number of meshes currently associated with this
661 return m_meshes.size();
665 * Set the debug color of the meshes associated with this
666 * class. Does this still work?
674 * Reset the debug color of meshes associated with this class.
681 * Was this object marked visible? (only for the explicit
682 * visibility system).
690 * Set visibility flag of this object
699 * Was this object culled?
707 * Set culled flag of this object
715 * Change the layer of the object (when it is added in another layer
716 * than the original layer)
724 * Get the object layer
732 * Get the negative scaling state
737 ) { return m_bIsNegativeScaling; }
748 * @section Logic bubbling methods.
752 * Stop making progress
757 * Resume making progress
761 void SuspendDynamics(void) {
762 if (m_bSuspendDynamics)
767 if (m_pPhysicsController1)
769 m_pPhysicsController1->SuspendDynamics();
771 m_bSuspendDynamics = true;
774 void RestoreDynamics(void) {
775 if (!m_bSuspendDynamics)
780 if (m_pPhysicsController1)
782 m_pPhysicsController1->RestoreDynamics();
784 m_bSuspendDynamics = false;
787 KX_ClientObjectInfo* getClientInfo() { return m_pClient_info; }
789 * @section Python interface functions.
792 virtual PyObject* py_getattro(PyObject *attr);
793 virtual int py_setattro(PyObject *attr, PyObject *value); // py_setattro method
794 virtual PyObject* py_repr(void) { return PyString_FromString(GetName().ReadPtr()); }
796 /* we need our own getattr and setattr types */
797 /* See m_attrlist definition for rules on how this works */
798 static PyObject *py_base_getattro_gameobject(PyObject * self, PyObject *attr)
800 PyObject *object= ((KX_GameObject *) self)->py_getattro(attr);
802 if (object==NULL && ((KX_GameObject *) self)->m_attrlist) {
803 /* backup the exception incase the attr doesnt exist in the dict either */
804 PyObject *err_type, *err_value, *err_tb;
805 PyErr_Fetch(&err_type, &err_value, &err_tb);
807 object= PyDict_GetItem(((KX_GameObject *) self)->m_attrlist, attr);
812 Py_XDECREF( err_type );
813 Py_XDECREF( err_value );
814 Py_XDECREF( err_tb );
817 PyErr_Restore(err_type, err_value, err_tb); /* use the error from the parent function */
823 static int py_base_setattro_gameobject(PyObject * self, PyObject *attr, PyObject *value)
827 /* Delete the item */
830 ret= ((PyObjectPlus*) self)->py_delattro(attr);
832 if (ret != 0) /* CValue attribute failed, try KX_GameObject m_attrlist dict */
834 if (((KX_GameObject *) self)->m_attrlist)
836 /* backup the exception incase the attr doesnt exist in the dict either */
837 PyObject *err_type, *err_value, *err_tb;
838 PyErr_Fetch(&err_type, &err_value, &err_tb);
840 if (PyDict_DelItem(((KX_GameObject *) self)->m_attrlist, attr) == 0)
844 Py_XDECREF( err_type );
845 Py_XDECREF( err_value );
846 Py_XDECREF( err_tb );
849 PyErr_Restore(err_type, err_value, err_tb); /* use the error from the parent function */
857 ret= ((PyObjectPlus*) self)->py_setattro(attr, value);
859 if (ret==PY_SET_ATTR_SUCCESS) {
860 /* remove attribute in our own dict to avoid double ups */
861 if (((KX_GameObject *) self)->m_attrlist) {
862 if (PyDict_DelItem(((KX_GameObject *) self)->m_attrlist, attr) != 0)
867 if (ret==PY_SET_ATTR_COERCE_FAIL) {
868 /* CValue attribute exists, remove and add dict value */
869 ((KX_GameObject *) self)->RemoveProperty(STR_String(PyString_AsString(attr)));
870 ret= PY_SET_ATTR_MISSING;
873 if (ret==PY_SET_ATTR_MISSING) {
874 /* Lazy initialization */
875 if (((KX_GameObject *) self)->m_attrlist==NULL)
876 ((KX_GameObject *) self)->m_attrlist = PyDict_New();
878 if (PyDict_SetItem(((KX_GameObject *) self)->m_attrlist, attr, value)==0) {
880 ret= PY_SET_ATTR_SUCCESS;
883 PyErr_Format(PyExc_AttributeError, "failed assigning value to KX_GameObject internal dictionary");
884 ret= PY_SET_ATTR_FAIL;
893 KX_PYMETHOD_NOARGS(KX_GameObject,GetPosition);
894 KX_PYMETHOD_O(KX_GameObject,SetPosition);
895 KX_PYMETHOD_O(KX_GameObject,SetWorldPosition);
896 KX_PYMETHOD_VARARGS(KX_GameObject, ApplyForce);
897 KX_PYMETHOD_VARARGS(KX_GameObject, ApplyTorque);
898 KX_PYMETHOD_VARARGS(KX_GameObject, ApplyRotation);
899 KX_PYMETHOD_VARARGS(KX_GameObject, ApplyMovement);
900 KX_PYMETHOD_VARARGS(KX_GameObject,GetLinearVelocity);
901 KX_PYMETHOD_VARARGS(KX_GameObject,SetLinearVelocity);
902 KX_PYMETHOD_VARARGS(KX_GameObject,GetAngularVelocity);
903 KX_PYMETHOD_VARARGS(KX_GameObject,SetAngularVelocity);
904 KX_PYMETHOD_VARARGS(KX_GameObject,GetVelocity);
905 KX_PYMETHOD_NOARGS(KX_GameObject,GetMass);
906 KX_PYMETHOD_NOARGS(KX_GameObject,GetReactionForce);
907 KX_PYMETHOD_NOARGS(KX_GameObject,GetOrientation);
908 KX_PYMETHOD_O(KX_GameObject,SetOrientation);
909 KX_PYMETHOD_NOARGS(KX_GameObject,GetVisible);
910 KX_PYMETHOD_VARARGS(KX_GameObject,SetVisible);
911 KX_PYMETHOD_NOARGS(KX_GameObject,GetState);
912 KX_PYMETHOD_O(KX_GameObject,SetState);
913 KX_PYMETHOD_VARARGS(KX_GameObject,AlignAxisToVect);
914 KX_PYMETHOD_O(KX_GameObject,GetAxisVect);
915 KX_PYMETHOD_NOARGS(KX_GameObject,SuspendDynamics);
916 KX_PYMETHOD_NOARGS(KX_GameObject,RestoreDynamics);
917 KX_PYMETHOD_NOARGS(KX_GameObject,EnableRigidBody);
918 KX_PYMETHOD_NOARGS(KX_GameObject,DisableRigidBody);
919 KX_PYMETHOD_VARARGS(KX_GameObject,ApplyImpulse);
920 KX_PYMETHOD_O(KX_GameObject,SetCollisionMargin);
921 KX_PYMETHOD_NOARGS(KX_GameObject,GetParent);
922 KX_PYMETHOD_O(KX_GameObject,SetParent);
923 KX_PYMETHOD_NOARGS(KX_GameObject,RemoveParent);
924 KX_PYMETHOD_NOARGS(KX_GameObject,GetChildren);
925 KX_PYMETHOD_NOARGS(KX_GameObject,GetChildrenRecursive);
926 KX_PYMETHOD_VARARGS(KX_GameObject,GetMesh);
927 KX_PYMETHOD_NOARGS(KX_GameObject,GetPhysicsId);
928 KX_PYMETHOD_NOARGS(KX_GameObject,GetPropertyNames);
929 KX_PYMETHOD_O(KX_GameObject,ReplaceMesh);
930 KX_PYMETHOD_NOARGS(KX_GameObject,EndObject);
931 KX_PYMETHOD_DOC(KX_GameObject,rayCastTo);
932 KX_PYMETHOD_DOC(KX_GameObject,rayCast);
933 KX_PYMETHOD_DOC_O(KX_GameObject,getDistanceTo);
934 KX_PYMETHOD_DOC_O(KX_GameObject,getVectTo);
935 KX_PYMETHOD_DOC_VARARGS(KX_GameObject, sendMessage);
937 static PyObject* pyattr_get_name(void* self_v, const KX_PYATTRIBUTE_DEF *attrdef);
938 static PyObject* pyattr_get_parent(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef);
940 static PyObject* pyattr_get_mass(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef);
941 static int pyattr_set_mass(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef, PyObject *value);
942 static PyObject* pyattr_get_visible(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef);
943 static int pyattr_set_visible(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef, PyObject *value);
944 static PyObject* pyattr_get_position(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef);
945 static int pyattr_set_position(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef, PyObject *value);
946 static PyObject* pyattr_get_localInertia(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef);
947 static int pyattr_set_localInertia(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef, PyObject *value);
948 static PyObject* pyattr_get_orientation(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef);
949 static int pyattr_set_orientation(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef, PyObject *value);
950 static PyObject* pyattr_get_scaling(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef);
951 static int pyattr_set_scaling(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef, PyObject *value);
952 static PyObject* pyattr_get_timeOffset(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef);
953 static int pyattr_set_timeOffset(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef, PyObject *value);
954 static PyObject* pyattr_get_state(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef);
955 static int pyattr_set_state(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef, PyObject *value);
956 static PyObject* pyattr_get_meshes(void* self_v, const KX_PYATTRIBUTE_DEF *attrdef);
958 /* for dir(), python3 uses __dir__() */
959 static PyObject* pyattr_get_dir_dict(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef);
962 static PyObject* pyattr_get_sensors(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef);
963 static PyObject* pyattr_get_controllers(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef);
964 static PyObject* pyattr_get_actuators(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef);
966 /* getitem/setitem */
967 static Py_ssize_t Map_Len(PyObject* self);
968 static PyMappingMethods Mapping;
969 static PyObject* Map_GetItem(PyObject *self_v, PyObject *item);
970 static int Map_SetItem(PyObject *self_v, PyObject *key, PyObject *val);
976 * Random internal function to convert python function arguments
978 * @return true if conversion was possible.
982 ConvertPythonVectorArgs(
990 /* utility conversion function */
991 bool ConvertPythonToGameObject(PyObject * value, KX_GameObject **object, bool py_none_ok);
993 #endif //__KX_GAMEOBJECT