00001
00006
00007 #include "KX_ObjectActuator.h"
00008 #include "KX_GameObject.h"
00009
00010
00011
00012
00013
00014 class CForceAction : public CAction
00015 {
00016 MT_Vector3 m_force;
00017 bool m_local;
00018 KX_GameObject* m_gameobj;
00019 public:
00020 CForceAction(const MT_Vector3& force,bool local,KX_GameObject* gameobj)
00021 : m_local(local),
00022 m_gameobj(gameobj),
00023 m_force(force) { };
00024
00025 virtual ~CForceAction() { };
00026
00027 virtual void Execute() const {
00028 m_gameobj->ApplyForce(m_force,m_local);
00029 };
00030
00031 };
00032
00033 class CTorqueAction : public CAction
00034 {
00035 MT_Vector3 m_torque;
00036 bool m_local;
00037 KX_GameObject* m_gameobj;
00038 public:
00039 CTorqueAction(const MT_Vector3& torque,bool local,KX_GameObject* gameobj)
00040 : m_local(local),
00041 m_gameobj(gameobj),
00042 m_torque(torque) { };
00043
00044 virtual ~CTorqueAction() { };
00045 virtual void Execute() const {
00046 m_gameobj->ApplyTorque(m_torque,m_local);
00047 };
00048
00049 };
00050
00051
00052 class CMoveAction : public CAction
00053 {
00054 MT_Vector3 m_dloc;
00055 bool m_local;
00056 KX_GameObject* m_gameobj;
00057 public:
00058 CMoveAction(const MT_Vector3& dloc,bool local,KX_GameObject* gameobj)
00059 : m_local(local),
00060 m_dloc(dloc),
00061 m_gameobj(gameobj) { };
00062
00063 virtual ~CMoveAction() { };
00064 virtual void Execute() const {
00065 m_gameobj->ApplyMovement(m_dloc,m_local);
00066 };
00067
00068 };
00069
00070 class CRotateAction : public CAction
00071 {
00072 MT_Vector3 m_drot;
00073 bool m_local;
00074 KX_GameObject* m_gameobj;
00075 public:
00076 CRotateAction(const MT_Vector3& drot,bool local,KX_GameObject* gameobj)
00077 : m_local(local),
00078 m_drot(drot),
00079 m_gameobj(gameobj) { };
00080
00081 virtual ~CRotateAction() { };
00082 virtual void Execute() const {
00083 m_gameobj->ApplyRotation(m_drot,m_local);
00084 };
00085
00086 };
00087
00088
00089 class CLinearVelocityAction : public CAction
00090 {
00091 MT_Vector3 m_linear_velocity;
00092 bool m_local;
00093 KX_GameObject* m_gameobj;
00094 public:
00095 CLinearVelocityAction(const MT_Vector3& linV, bool local, KX_GameObject* gameobj)
00096 : m_local(local),
00097 m_gameobj(gameobj),
00098 m_linear_velocity(linV) { };
00099
00100 virtual ~CLinearVelocityAction() { };
00101
00102 virtual void Execute() const {
00103 m_gameobj->setLinearVelocity(m_linear_velocity,m_local);
00104 };
00105
00106 };
00107
00108 class CAngularVelocityAction : public CAction
00109 {
00110 MT_Vector3 m_angular_velocity;
00111 bool m_local;
00112 KX_GameObject* m_gameobj;
00113 public:
00114 CAngularVelocityAction(const MT_Vector3& angV,
00115 bool local,
00116 KX_GameObject* gameobj)
00117 : m_local(local),
00118 m_gameobj(gameobj),
00119 m_angular_velocity(angV) { };
00120
00121 virtual ~CAngularVelocityAction() { };
00122
00123 virtual void Execute() const {
00124 m_gameobj->setAngularVelocity(m_angular_velocity,m_local);
00125 };
00126
00127 };
00128
00129
00130
00131
00132
00133
00134 bool KX_ObjectActuator::Update(double curtime,double deltatime)
00135 {
00136
00137 bool bNegativeEvent = IsNegativeEvent();
00138
00139 if (bNegativeEvent)
00140 return false;
00141
00142 if (GetParent())
00143 {
00144
00145
00146 if (!MT_fuzzyZero(m_force)) {
00147 GetParent()->Execute(CForceAction(m_force,(m_bitLocalFlag.Force) != 0,
00148 (KX_GameObject*)GetParent()));
00149 }
00150 if (!MT_fuzzyZero(m_torque)) {
00151 GetParent()->Execute(CTorqueAction(m_torque,(m_bitLocalFlag.Torque) != 0,
00152 (KX_GameObject*)GetParent()));
00153 }
00154 if (!MT_fuzzyZero(m_dloc)) {
00155 GetParent()->Execute(CMoveAction(m_dloc,(m_bitLocalFlag.DLoc) != 0,
00156 (KX_GameObject*)GetParent()));
00157 }
00158 if (!MT_fuzzyZero(m_drot)) {
00159 GetParent()->Execute(CRotateAction(m_drot,(m_bitLocalFlag.DRot) != 0,
00160 (KX_GameObject*)GetParent()));
00161 }
00162 if (!MT_fuzzyZero(m_linear_velocity)) {
00163 GetParent()->Execute(CLinearVelocityAction(m_linear_velocity,(m_bitLocalFlag.LinearVelocity) != 0,
00164 (KX_GameObject*)GetParent()));
00165 }
00166 if (!MT_fuzzyZero(m_angular_velocity)) {
00167 GetParent()->Execute(CAngularVelocityAction(m_angular_velocity,(m_bitLocalFlag.AngularVelocity) != 0,
00168 (KX_GameObject*)GetParent()));
00169 }
00170
00171 }
00172 return true;
00173 }
00174
00175
00176
00177 bool KX_ObjectActuator::isValid(
00178 KX_ObjectActuator::KX_OBJECT_ACT_VEC_TYPE type)
00179 {
00180 bool res = false;
00181 res = (type > KX_OBJECT_ACT_NODEF) && (type < KX_OBJECT_ACT_MAX);
00182 return res;
00183 }
00184
00185
00186
00187
00188
00189
00190 PyTypeObject KX_ObjectActuator::Type = {
00191 PyObject_HEAD_INIT(&PyType_Type)
00192 0,
00193 "KX_ObjectActuator",
00194 sizeof(KX_ObjectActuator),
00195 0,
00196 PyDestructor,
00197 0,
00198 __getattr,
00199 __setattr,
00200 0,
00201 __repr,
00202 0,
00203 0,
00204 0,
00205 0,
00206 0
00207 };
00208
00209 PyParentObject KX_ObjectActuator::Parents[] = {
00210 &KX_ObjectActuator::Type,
00211 &SCA_IActuator::Type,
00212 &SCA_ILogicBrick::Type,
00213 &CValue::Type,
00214 NULL
00215 };
00216
00217 PyMethodDef KX_ObjectActuator::Methods[] = {
00218 {"getForce", (PyCFunction) KX_ObjectActuator::sPyGetForce, METH_VARARGS},
00219 {"setForce", (PyCFunction) KX_ObjectActuator::sPySetForce, METH_VARARGS},
00220 {"getTorque", (PyCFunction) KX_ObjectActuator::sPyGetTorque, METH_VARARGS},
00221 {"setTorque", (PyCFunction) KX_ObjectActuator::sPySetTorque, METH_VARARGS},
00222 {"getDLoc", (PyCFunction) KX_ObjectActuator::sPyGetDLoc, METH_VARARGS},
00223 {"setDLoc", (PyCFunction) KX_ObjectActuator::sPySetDLoc, METH_VARARGS},
00224 {"getDRot", (PyCFunction) KX_ObjectActuator::sPyGetDRot, METH_VARARGS},
00225 {"setDRot", (PyCFunction) KX_ObjectActuator::sPySetDRot, METH_VARARGS},
00226 {"getLinearVelocity", (PyCFunction) KX_ObjectActuator::sPyGetLinearVelocity, METH_VARARGS},
00227 {"setLinearVelocity", (PyCFunction) KX_ObjectActuator::sPySetLinearVelocity, METH_VARARGS},
00228 {"getAngularVelocity", (PyCFunction) KX_ObjectActuator::sPyGetAngularVelocity, METH_VARARGS},
00229 {"setAngularVelocity", (PyCFunction) KX_ObjectActuator::sPySetAngularVelocity, METH_VARARGS},
00230 {NULL,NULL}
00231 };
00232
00233 PyObject* KX_ObjectActuator::_getattr(char* attr) {
00234 _getattr_up(SCA_IActuator);
00235 };
00236
00237
00238
00239
00240
00241 PyObject* KX_ObjectActuator::PyGetForce(PyObject* self,
00242 PyObject* args,
00243 PyObject* kwds)
00244 {
00245 PyObject *retVal = PyList_New(4);
00246
00247 PyList_SetItem(retVal, 0, PyFloat_FromDouble(m_force[0]));
00248 PyList_SetItem(retVal, 1, PyFloat_FromDouble(m_force[1]));
00249 PyList_SetItem(retVal, 2, PyFloat_FromDouble(m_force[2]));
00250 PyList_SetItem(retVal, 3, PyInt_FromLong(m_bitLocalFlag.Force));
00251
00252 return retVal;
00253 }
00254
00255 PyObject* KX_ObjectActuator::PySetForce(PyObject* self,
00256 PyObject* args,
00257 PyObject* kwds)
00258 {
00259 float vecArg[3];
00260 int bToggle = 0;
00261 if (!PyArg_ParseTuple(args, "fffi", &vecArg[0], &vecArg[1],
00262 &vecArg[2], &bToggle)) {
00263 return NULL;
00264 }
00265 m_force.setValue(vecArg);
00266 m_bitLocalFlag.Force = (bToggle == KX_TRUE);
00267 Py_Return;
00268 }
00269
00270
00271 PyObject* KX_ObjectActuator::PyGetTorque(PyObject* self,
00272 PyObject* args,
00273 PyObject* kwds)
00274 {
00275 PyObject *retVal = PyList_New(4);
00276
00277 PyList_SetItem(retVal, 0, PyFloat_FromDouble(m_torque[0]));
00278 PyList_SetItem(retVal, 1, PyFloat_FromDouble(m_torque[1]));
00279 PyList_SetItem(retVal, 2, PyFloat_FromDouble(m_torque[2]));
00280 PyList_SetItem(retVal, 3, PyInt_FromLong(m_bitLocalFlag.Torque));
00281
00282 return retVal;
00283 }
00284
00285 PyObject* KX_ObjectActuator::PySetTorque(PyObject* self,
00286 PyObject* args,
00287 PyObject* kwds)
00288 {
00289 float vecArg[3];
00290 int bToggle = 0;
00291 if (!PyArg_ParseTuple(args, "fffi", &vecArg[0], &vecArg[1],
00292 &vecArg[2], &bToggle)) {
00293 return NULL;
00294 }
00295 m_torque.setValue(vecArg);
00296 m_bitLocalFlag.Torque = (bToggle == KX_TRUE);
00297 Py_Return;
00298 }
00299
00300
00301 PyObject* KX_ObjectActuator::PyGetDLoc(PyObject* self,
00302 PyObject* args,
00303 PyObject* kwds)
00304 {
00305 PyObject *retVal = PyList_New(4);
00306
00307 PyList_SetItem(retVal, 0, PyFloat_FromDouble(m_dloc[0]));
00308 PyList_SetItem(retVal, 1, PyFloat_FromDouble(m_dloc[1]));
00309 PyList_SetItem(retVal, 2, PyFloat_FromDouble(m_dloc[2]));
00310 PyList_SetItem(retVal, 3, PyInt_FromLong(m_bitLocalFlag.DLoc));
00311
00312 return retVal;
00313 }
00314
00315 PyObject* KX_ObjectActuator::PySetDLoc(PyObject* self,
00316 PyObject* args,
00317 PyObject* kwds)
00318 {
00319 float vecArg[3];
00320 int bToggle = 0;
00321 if(!PyArg_ParseTuple(args, "fffi", &vecArg[0], &vecArg[1],
00322 &vecArg[2], &bToggle)) {
00323 return NULL;
00324 }
00325 m_dloc.setValue(vecArg);
00326 m_bitLocalFlag.DLoc = (bToggle == KX_TRUE);
00327 Py_Return;
00328 }
00329
00330
00331 PyObject* KX_ObjectActuator::PyGetDRot(PyObject* self,
00332 PyObject* args,
00333 PyObject* kwds)
00334 {
00335 PyObject *retVal = PyList_New(4);
00336
00337 PyList_SetItem(retVal, 0, PyFloat_FromDouble(m_drot[0]));
00338 PyList_SetItem(retVal, 1, PyFloat_FromDouble(m_drot[1]));
00339 PyList_SetItem(retVal, 2, PyFloat_FromDouble(m_drot[2]));
00340 PyList_SetItem(retVal, 3, PyInt_FromLong(m_bitLocalFlag.DRot));
00341
00342 return retVal;
00343 }
00344
00345 PyObject* KX_ObjectActuator::PySetDRot(PyObject* self,
00346 PyObject* args,
00347 PyObject* kwds)
00348 {
00349 float vecArg[3];
00350 int bToggle = 0;
00351 if (!PyArg_ParseTuple(args, "fffi", &vecArg[0], &vecArg[1],
00352 &vecArg[2], &bToggle)) {
00353 return NULL;
00354 }
00355 m_drot.setValue(vecArg);
00356 m_bitLocalFlag.DRot = (bToggle == KX_TRUE);
00357 Py_Return;
00358 }
00359
00360
00361 PyObject* KX_ObjectActuator::PyGetLinearVelocity(PyObject* self,
00362 PyObject* args,
00363 PyObject* kwds) {
00364 PyObject *retVal = PyList_New(4);
00365
00366 PyList_SetItem(retVal, 0, PyFloat_FromDouble(m_linear_velocity[0]));
00367 PyList_SetItem(retVal, 1, PyFloat_FromDouble(m_linear_velocity[1]));
00368 PyList_SetItem(retVal, 2, PyFloat_FromDouble(m_linear_velocity[2]));
00369 PyList_SetItem(retVal, 3, PyInt_FromLong(m_bitLocalFlag.LinearVelocity));
00370
00371 return retVal;
00372 }
00373
00374
00375 PyObject* KX_ObjectActuator::PySetLinearVelocity(PyObject* self,
00376 PyObject* args,
00377 PyObject* kwds) {
00378 float vecArg[3];
00379 int bToggle = 0;
00380 if (!PyArg_ParseTuple(args, "fffi", &vecArg[0], &vecArg[1],
00381 &vecArg[2], &bToggle)) {
00382 return NULL;
00383 }
00384 m_linear_velocity.setValue(vecArg);
00385 m_bitLocalFlag.LinearVelocity = (bToggle == KX_TRUE);
00386 Py_Return;
00387 }
00388
00389
00390 PyObject* KX_ObjectActuator::PyGetAngularVelocity(PyObject* self,
00391 PyObject* args,
00392 PyObject* kwds) {
00393 PyObject *retVal = PyList_New(4);
00394
00395 PyList_SetItem(retVal, 0, PyFloat_FromDouble(m_angular_velocity[0]));
00396 PyList_SetItem(retVal, 1, PyFloat_FromDouble(m_angular_velocity[1]));
00397 PyList_SetItem(retVal, 2, PyFloat_FromDouble(m_angular_velocity[2]));
00398 PyList_SetItem(retVal, 3, PyInt_FromLong(m_bitLocalFlag.AngularVelocity));
00399
00400 return retVal;
00401 }
00402
00403 PyObject* KX_ObjectActuator::PySetAngularVelocity(PyObject* self,
00404 PyObject* args,
00405 PyObject* kwds) {
00406 float vecArg[3];
00407 int bToggle = 0;
00408 if (!PyArg_ParseTuple(args, "fffi", &vecArg[0], &vecArg[1],
00409 &vecArg[2], &bToggle)) {
00410 return NULL;
00411 }
00412 m_angular_velocity.setValue(vecArg);
00413 m_bitLocalFlag.AngularVelocity = (bToggle == KX_TRUE);
00414 Py_Return;
00415 }
00416
00417
00418