game engine python api
[blender.git] / source / gameengine / Ketsji / KX_PhysicsObjectWrapper.cpp
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
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.
10  *
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.
15  *
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.
19  *
20  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
21  * All rights reserved.
22  *
23  * The Original Code is: all of this file.
24  *
25  * Contributor(s): none yet.
26  *
27  * ***** END GPL LICENSE BLOCK *****
28  */
29 #include <Python.h>
30 #include "KX_PhysicsObjectWrapper.h"
31 #include "PHY_IPhysicsEnvironment.h"
32 #include "PHY_IPhysicsController.h"
33
34 #ifdef HAVE_CONFIG_H
35 #include <config.h>
36 #endif
37
38 KX_PhysicsObjectWrapper::KX_PhysicsObjectWrapper(
39                                                 PHY_IPhysicsController* ctrl,
40                                                 PHY_IPhysicsEnvironment* physenv,PyTypeObject *T) :
41                                         PyObjectPlus(T),
42                                         m_ctrl(ctrl),
43                                         m_physenv(physenv)
44 {
45 }
46
47 KX_PhysicsObjectWrapper::~KX_PhysicsObjectWrapper()
48 {
49 }
50
51
52 PyObject* KX_PhysicsObjectWrapper::PySetPosition(PyObject* self, 
53                                                                                         PyObject* args, 
54                                                                                         PyObject* kwds)
55 {
56         float x,y,z;
57         if (PyArg_ParseTuple(args,"fff",&x,&y,&z))
58         {
59                 m_ctrl->setPosition(x,y,z);
60         }
61         else {
62                 return NULL;
63         }
64         Py_RETURN_NONE;
65 }
66
67
68 PyObject* KX_PhysicsObjectWrapper::PySetLinearVelocity(PyObject* self, 
69                                                                                         PyObject* args, 
70                                                                                         PyObject* kwds)
71 {
72         float x,y,z;
73         int local;
74         if (PyArg_ParseTuple(args,"fffi",&x,&y,&z,&local))
75         {
76                 m_ctrl->SetLinearVelocity(x,y,z,local != 0);
77         }
78         else {
79                 return NULL;
80         }
81         Py_RETURN_NONE;
82 }
83
84 PyObject* KX_PhysicsObjectWrapper::PySetAngularVelocity(PyObject* self, 
85                                                                                         PyObject* args, 
86                                                                                         PyObject* kwds)
87 {
88         float x,y,z;
89         int local;
90         if (PyArg_ParseTuple(args,"fffi",&x,&y,&z,&local))
91         {
92                 m_ctrl->SetAngularVelocity(x,y,z,local != 0);
93         }
94         else {
95                 return NULL;
96         }
97         Py_RETURN_NONE;
98 }
99
100 PyObject*       KX_PhysicsObjectWrapper::PySetActive(PyObject* self, 
101                                                                                         PyObject* args, 
102                                                                                         PyObject* kwds)
103 {
104         int active;
105         if (PyArg_ParseTuple(args,"i",&active))
106         {
107                 m_ctrl->SetActive(active!=0);
108         }
109         else {
110                 return NULL;
111         }
112         Py_RETURN_NONE;
113 }
114
115
116
117
118 //python specific stuff
119 PyTypeObject KX_PhysicsObjectWrapper::Type = {
120         PyObject_HEAD_INIT(&PyType_Type)
121                 0,
122                 "KX_PhysicsObjectWrapper",
123                 sizeof(KX_PhysicsObjectWrapper),
124                 0,
125                 PyDestructor,
126                 0,
127                 __getattr,
128                 __setattr,
129                 0, //&MyPyCompare,
130                 __repr,
131                 0, //&cvalue_as_number,
132                 0,
133                 0,
134                 0,
135                 0
136 };
137
138 PyParentObject KX_PhysicsObjectWrapper::Parents[] = {
139         &KX_PhysicsObjectWrapper::Type,
140         NULL
141 };
142
143 PyObject*       KX_PhysicsObjectWrapper::_getattr(const STR_String& attr)
144 {
145         _getattr_up(PyObjectPlus);
146 }
147
148
149 int     KX_PhysicsObjectWrapper::_setattr(const STR_String& attr,PyObject* pyobj)
150 {
151         PyTypeObject* type = pyobj->ob_type;
152         int result = 1;
153
154
155         if (type == &PyInt_Type)
156         {
157                 result = 0;
158         }
159         if (type == &PyString_Type)
160         {
161                 result = 0;
162         }
163         if (result)
164                 result = PyObjectPlus::_setattr(attr,pyobj);
165                 
166         return result;
167 };
168
169
170 PyMethodDef KX_PhysicsObjectWrapper::Methods[] = {
171         {"setPosition",(PyCFunction) KX_PhysicsObjectWrapper::sPySetPosition, METH_VARARGS},
172         {"setLinearVelocity",(PyCFunction) KX_PhysicsObjectWrapper::sPySetLinearVelocity, METH_VARARGS},
173         {"setAngularVelocity",(PyCFunction) KX_PhysicsObjectWrapper::sPySetAngularVelocity, METH_VARARGS},
174         {"setActive",(PyCFunction) KX_PhysicsObjectWrapper::sPySetActive, METH_VARARGS},
175         {NULL,NULL} //Sentinel
176 };