Update MSVC project files
[blender.git] / source / gameengine / Ketsji / KX_ConstraintActuator.cpp
1 /**
2  * Apply a constraint to a position or rotation value
3  *
4  * $Id$
5  *
6  * ***** BEGIN GPL LICENSE BLOCK *****
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software Foundation,
20  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
21  *
22  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
23  * All rights reserved.
24  *
25  * The Original Code is: all of this file.
26  *
27  * Contributor(s): none yet.
28  *
29  * ***** END GPL LICENSE BLOCK *****
30  */
31
32 #include "SCA_IActuator.h"
33 #include "KX_ConstraintActuator.h"
34 #include "SCA_IObject.h"
35 #include "MT_Point3.h"
36 #include "MT_Matrix3x3.h"
37 #include "KX_GameObject.h"
38 #include "KX_RayCast.h"
39
40 #ifdef HAVE_CONFIG_H
41 #include <config.h>
42 #endif
43
44 /* ------------------------------------------------------------------------- */
45 /* Native functions                                                          */
46 /* ------------------------------------------------------------------------- */
47
48 KX_ConstraintActuator::KX_ConstraintActuator(SCA_IObject *gameobj, 
49                                                                                          int posDampTime,
50                                                                                          int rotDampTime,
51                                                                                          float minBound,
52                                                                                          float maxBound,
53                                                                                          float refDir[3],
54                                                                                          int locrotxyz,
55                                                                                          int time,
56                                                                                          int option,
57                                                                                          char *property,
58                                                                                          PyTypeObject* T) : 
59         m_refDirection(refDir),
60         m_currentTime(0),
61         SCA_IActuator(gameobj, T)
62 {
63         m_posDampTime = posDampTime;
64         m_rotDampTime = rotDampTime;
65         m_locrot   = locrotxyz;
66         m_option = option;
67         m_activeTime = time;
68         if (property) {
69                 strncpy(m_property, property, sizeof(m_property));
70                 m_property[sizeof(m_property)-1] = 0;
71         } else {
72                 m_property[0] = 0;
73         }
74         /* The units of bounds are determined by the type of constraint. To      */
75         /* make the constraint application easier and more transparent later on, */
76         /* I think converting the bounds to the applicable domain makes more     */
77         /* sense.                                                                */
78         switch (m_locrot) {
79         case KX_ACT_CONSTRAINT_ORIX:
80         case KX_ACT_CONSTRAINT_ORIY:
81         case KX_ACT_CONSTRAINT_ORIZ:
82                 {
83                         MT_Scalar len = m_refDirection.length();
84                         if (MT_fuzzyZero(len)) {
85                                 // missing a valid direction
86                                 std::cout << "WARNING: Constraint actuator " << GetName() << ":  There is no valid reference direction!" << std::endl;
87                                 m_locrot = KX_ACT_CONSTRAINT_NODEF;
88                         } else {
89                                 m_refDirection /= len;
90                         }
91                         m_minimumBound = cos(minBound);
92                         m_maximumBound = cos(maxBound);
93                         m_minimumSine = sin(minBound);
94                         m_maximumSine = sin(maxBound);
95                 }
96                 break;
97         default:
98                 m_minimumBound = minBound;
99                 m_maximumBound = maxBound;
100                 m_minimumSine = 0.f;
101                 m_maximumSine = 0.f;
102                 break;
103         }
104
105 } /* End of constructor */
106
107 KX_ConstraintActuator::~KX_ConstraintActuator()
108
109         // there's nothing to be done here, really....
110 } /* end of destructor */
111
112 bool KX_ConstraintActuator::RayHit(KX_ClientObjectInfo* client, KX_RayCast* result, void * const data)
113 {
114
115         KX_GameObject* hitKXObj = client->m_gameobject;
116         
117         bool bFound = false;
118
119         if (m_property[0] == 0)
120         {
121                 bFound = true;
122         }
123         else
124         {
125                 if (m_option & KX_ACT_CONSTRAINT_MATERIAL)
126                 {
127                         if (client->m_auxilary_info)
128                         {
129                                 bFound = !strcmp(m_property, ((char*)client->m_auxilary_info));
130                         }
131                 }
132                 else
133                 {
134                         bFound = hitKXObj->GetProperty(m_property) != NULL;
135                 }
136         }
137         // update the hit status
138         result->m_hitFound = bFound;
139         // stop looking
140         return true;
141 }
142
143 /* this function is used to pre-filter the object before casting the ray on them.
144    This is useful for "X-Ray" option when we want to see "through" unwanted object.
145  */
146 bool KX_ConstraintActuator::NeedRayCast(KX_ClientObjectInfo* client)
147 {
148         if (client->m_type > KX_ClientObjectInfo::ACTOR)
149         {
150                 // Unknown type of object, skip it.
151                 // Should not occur as the sensor objects are filtered in RayTest()
152                 printf("Invalid client type %d found in ray casting\n", client->m_type);
153                 return false;
154         }
155         // no X-Ray function yet
156         return true;
157 }
158
159 bool KX_ConstraintActuator::Update(double curtime, bool frame)
160 {
161
162         bool result = false;    
163         bool bNegativeEvent = IsNegativeEvent();
164         RemoveAllEvents();
165
166         if (!bNegativeEvent) {
167                 /* Constraint clamps the values to the specified range, with a sort of    */
168                 /* low-pass filtered time response, if the damp time is unequal to 0.     */
169
170                 /* Having to retrieve location/rotation and setting it afterwards may not */
171                 /* be efficient enough... Somthing to look at later.                      */
172                 KX_GameObject  *obj = (KX_GameObject*) GetParent();
173                 MT_Point3    position = obj->NodeGetWorldPosition();
174                 MT_Point3    newposition;
175                 MT_Vector3   direction, refDirection;
176                 MT_Matrix3x3 rotation = obj->NodeGetWorldOrientation();
177                 MT_Scalar    filter, newdistance, cosangle;
178                 int axis, sign;
179
180                 if (m_posDampTime) {
181                         filter = m_posDampTime/(1.0+m_posDampTime);
182                 }
183                 switch (m_locrot) {
184                 case KX_ACT_CONSTRAINT_ORIX:
185                 case KX_ACT_CONSTRAINT_ORIY:
186                 case KX_ACT_CONSTRAINT_ORIZ:
187                         switch (m_locrot) {
188                         case KX_ACT_CONSTRAINT_ORIX:
189                                 direction[0] = rotation[0][0];
190                                 direction[1] = rotation[1][0];
191                                 direction[2] = rotation[2][0];
192                                 axis = 0;
193                                 break;
194                         case KX_ACT_CONSTRAINT_ORIY:
195                                 direction[0] = rotation[0][1];
196                                 direction[1] = rotation[1][1];
197                                 direction[2] = rotation[2][1];
198                                 axis = 1;
199                                 break;
200                         default:
201                                 direction[0] = rotation[0][2];
202                                 direction[1] = rotation[1][2];
203                                 direction[2] = rotation[2][2];
204                                 axis = 2;
205                                 break;
206                         }
207                         if ((m_maximumBound < (1.0f-FLT_EPSILON)) || (m_minimumBound < (1.0f-FLT_EPSILON))) {
208                                 // reference direction needs to be evaluated
209                                 // 1. get the cosine between current direction and target
210                                 cosangle = direction.dot(m_refDirection);
211                                 if (cosangle >= (m_maximumBound-FLT_EPSILON) && cosangle <= (m_minimumBound+FLT_EPSILON)) {
212                                         // no change to do
213                                         result = true;
214                                         goto CHECK_TIME;
215                                 }
216                                 // 2. define a new reference direction
217                                 //    compute local axis with reference direction as X and
218                                 //    Y in direction X refDirection plane
219                                 MT_Vector3 zaxis = m_refDirection.cross(direction);
220                                 if (MT_fuzzyZero2(zaxis.length2())) {
221                                         // direction and refDirection are identical,
222                                         // choose any other direction to define plane
223                                         if (direction[0] < 0.9999)
224                                                 zaxis = m_refDirection.cross(MT_Vector3(1.0,0.0,0.0));
225                                         else
226                                                 zaxis = m_refDirection.cross(MT_Vector3(0.0,1.0,0.0));
227                                 }
228                                 MT_Vector3 yaxis = zaxis.cross(m_refDirection);
229                                 yaxis.normalize();
230                                 if (cosangle > m_minimumBound) {
231                                         // angle is too close to reference direction,
232                                         // choose a new reference that is exactly at minimum angle
233                                         refDirection = m_minimumBound * m_refDirection + m_minimumSine * yaxis;
234                                 } else {
235                                         // angle is too large, choose new reference direction at maximum angle
236                                         refDirection = m_maximumBound * m_refDirection + m_maximumSine * yaxis;
237                                 }
238                         } else {
239                                 refDirection = m_refDirection;
240                         }
241                         if (m_posDampTime) {
242                                 // apply damping on the direction
243                                 direction = filter*direction + (1.0-filter)*refDirection;
244                         } else {
245                                 direction = refDirection;
246                         }
247                         obj->AlignAxisToVect(direction, axis);
248                         result = true;
249                         goto CHECK_TIME;
250                 case KX_ACT_CONSTRAINT_DIRPX:
251                 case KX_ACT_CONSTRAINT_DIRPY:
252                 case KX_ACT_CONSTRAINT_DIRPZ:
253                 case KX_ACT_CONSTRAINT_DIRNX:
254                 case KX_ACT_CONSTRAINT_DIRNY:
255                 case KX_ACT_CONSTRAINT_DIRNZ:
256                         switch (m_locrot) {
257                         case KX_ACT_CONSTRAINT_DIRPX:
258                                 direction[0] = rotation[0][0];
259                                 direction[1] = rotation[1][0];
260                                 direction[2] = rotation[2][0];
261                                 axis = 0;               // axis according to KX_GameObject::AlignAxisToVect()
262                                 sign = 1;               // X axis will be anti parrallel to normal
263                                 break;
264                         case KX_ACT_CONSTRAINT_DIRPY:
265                                 direction[0] = rotation[0][1];
266                                 direction[1] = rotation[1][1];
267                                 direction[2] = rotation[2][1];
268                                 axis = 1;
269                                 sign = 1;
270                                 break;
271                         case KX_ACT_CONSTRAINT_DIRPZ:
272                                 direction[0] = rotation[0][2];
273                                 direction[1] = rotation[1][2];
274                                 direction[2] = rotation[2][2];
275                                 axis = 2;
276                                 sign = 1;
277                                 break;
278                         case KX_ACT_CONSTRAINT_DIRNX:
279                                 direction[0] = -rotation[0][0];
280                                 direction[1] = -rotation[1][0];
281                                 direction[2] = -rotation[2][0];
282                                 axis = 0;
283                                 sign = 0;
284                                 break;
285                         case KX_ACT_CONSTRAINT_DIRNY:
286                                 direction[0] = -rotation[0][1];
287                                 direction[1] = -rotation[1][1];
288                                 direction[2] = -rotation[2][1];
289                                 axis = 1;
290                                 sign = 0;
291                                 break;
292                         case KX_ACT_CONSTRAINT_DIRNZ:
293                                 direction[0] = -rotation[0][2];
294                                 direction[1] = -rotation[1][2];
295                                 direction[2] = -rotation[2][2];
296                                 axis = 2;
297                                 sign = 0;
298                                 break;
299                         }
300                         direction.normalize();
301                         {
302                                 MT_Point3 topoint = position + (m_maximumBound) * direction;
303                                 PHY_IPhysicsEnvironment* pe = obj->GetPhysicsEnvironment();
304                                 KX_IPhysicsController *spc = obj->GetPhysicsController();
305
306                                 if (!pe) {
307                                         std::cout << "WARNING: Constraint actuator " << GetName() << ":  There is no physics environment!" << std::endl;
308                                         goto CHECK_TIME;
309                                 }        
310                                 if (!spc) {
311                                         // the object is not physical, we probably want to avoid hitting its own parent
312                                         KX_GameObject *parent = obj->GetParent();
313                                         if (parent) {
314                                                 spc = parent->GetPhysicsController();
315                                                 parent->Release();
316                                         }
317                                 }
318                                 KX_RayCast::Callback<KX_ConstraintActuator> callback(this,spc);
319                                 result = KX_RayCast::RayTest(pe, position, topoint, callback);
320                                 if (result)     {
321                                         MT_Vector3 newnormal = callback.m_hitNormal;
322                                         // compute new position & orientation
323                                         if ((m_option & (KX_ACT_CONSTRAINT_NORMAL|KX_ACT_CONSTRAINT_DISTANCE)) == 0) {
324                                                 // if none option is set, the actuator does nothing but detect ray 
325                                                 // (works like a sensor)
326                                                 goto CHECK_TIME;
327                                         }
328                                         if (m_option & KX_ACT_CONSTRAINT_NORMAL) {
329                                                 // the new orientation must be so that the axis is parallel to normal
330                                                 if (sign)
331                                                         newnormal = -newnormal;
332                                                 // apply damping on the direction
333                                                 if (m_rotDampTime) {
334                                                         MT_Scalar rotFilter = 1.0/(1.0+m_rotDampTime);
335                                                         newnormal = (-m_rotDampTime*rotFilter)*direction + rotFilter*newnormal;
336                                                 } else if (m_posDampTime) {
337                                                         newnormal = -filter*direction + (1.0-filter)*newnormal;
338                                                 }
339                                                 obj->AlignAxisToVect(newnormal, axis);
340                                                 direction = -newnormal;
341                                         }
342                                         if (m_option & KX_ACT_CONSTRAINT_DISTANCE) {
343                                                 if (m_posDampTime) {
344                                                         newdistance = filter*(position-callback.m_hitPoint).length()+(1.0-filter)*m_minimumBound;
345                                                 } else {
346                                                         newdistance = m_minimumBound;
347                                                 }
348                                         } else {
349                                                 newdistance = (position-callback.m_hitPoint).length();
350                                         }
351                                         newposition = callback.m_hitPoint-newdistance*direction;
352                                 } else if (m_option & KX_ACT_CONSTRAINT_PERMANENT) {
353                                         // no contact but still keep running
354                                         result = true;
355                                         goto CHECK_TIME;
356                                 }
357                         }
358                         break; 
359                 case KX_ACT_CONSTRAINT_LOCX:
360                 case KX_ACT_CONSTRAINT_LOCY:
361                 case KX_ACT_CONSTRAINT_LOCZ:
362                         newposition = position = obj->GetSGNode()->GetLocalPosition();
363                         switch (m_locrot) {
364                         case KX_ACT_CONSTRAINT_LOCX:
365                                 Clamp(newposition[0], m_minimumBound, m_maximumBound);
366                                 break;
367                         case KX_ACT_CONSTRAINT_LOCY:
368                                 Clamp(newposition[1], m_minimumBound, m_maximumBound);
369                                 break;
370                         case KX_ACT_CONSTRAINT_LOCZ:
371                                 Clamp(newposition[2], m_minimumBound, m_maximumBound);
372                                 break;
373                         }
374                         result = true;
375                         if (m_posDampTime) {
376                                 newposition = filter*position + (1.0-filter)*newposition;
377                         }
378                         obj->NodeSetLocalPosition(newposition);
379                         goto CHECK_TIME;
380                 }
381                 if (result) {
382                         // set the new position but take into account parent if any
383                         obj->NodeSetWorldPosition(newposition);
384                 }
385         CHECK_TIME:
386                 if (result && m_activeTime > 0 ) {
387                         if (++m_currentTime >= m_activeTime)
388                                 result = false;
389                 }
390         }
391         if (!result) {
392                 m_currentTime = 0;
393         }
394         return result;
395 } /* end of KX_ConstraintActuator::Update(double curtime,double deltatime)   */
396
397 void KX_ConstraintActuator::Clamp(MT_Scalar &var, 
398                                                                   float min, 
399                                                                   float max) {
400         if (var < min) {
401                 var = min;
402         } else if (var > max) {
403                 var = max;
404         }
405 }
406
407
408 bool KX_ConstraintActuator::IsValidMode(KX_ConstraintActuator::KX_CONSTRAINTTYPE m) 
409 {
410         bool res = false;
411
412         if ( (m > KX_ACT_CONSTRAINT_NODEF) && (m < KX_ACT_CONSTRAINT_MAX)) {
413                 res = true;
414         }
415
416         return res;
417 }
418
419 /* ------------------------------------------------------------------------- */
420 /* Python functions                                                          */
421 /* ------------------------------------------------------------------------- */
422
423 /* Integration hooks ------------------------------------------------------- */
424 PyTypeObject KX_ConstraintActuator::Type = {
425         PyObject_HEAD_INIT(&PyType_Type)
426         0,
427         "KX_ConstraintActuator",
428         sizeof(KX_ConstraintActuator),
429         0,
430         PyDestructor,
431         0,
432         __getattr,
433         __setattr,
434         0, //&MyPyCompare,
435         __repr,
436         0, //&cvalue_as_number,
437         0,
438         0,
439         0,
440         0
441 };
442
443 PyParentObject KX_ConstraintActuator::Parents[] = {
444         &KX_ConstraintActuator::Type,
445         &SCA_IActuator::Type,
446         &SCA_ILogicBrick::Type,
447         &CValue::Type,
448         NULL
449 };
450
451 PyMethodDef KX_ConstraintActuator::Methods[] = {
452         {"setDamp", (PyCFunction) KX_ConstraintActuator::sPySetDamp, METH_VARARGS, SetDamp_doc},
453         {"getDamp", (PyCFunction) KX_ConstraintActuator::sPyGetDamp, METH_NOARGS, GetDamp_doc},
454         {"setRotDamp", (PyCFunction) KX_ConstraintActuator::sPySetRotDamp, METH_VARARGS, SetRotDamp_doc},
455         {"getRotDamp", (PyCFunction) KX_ConstraintActuator::sPyGetRotDamp, METH_NOARGS, GetRotDamp_doc},
456         {"setDirection", (PyCFunction) KX_ConstraintActuator::sPySetDirection, METH_VARARGS, SetDirection_doc},
457         {"getDirection", (PyCFunction) KX_ConstraintActuator::sPyGetDirection, METH_NOARGS, GetDirection_doc},
458         {"setOption", (PyCFunction) KX_ConstraintActuator::sPySetOption, METH_VARARGS, SetOption_doc},
459         {"getOption", (PyCFunction) KX_ConstraintActuator::sPyGetOption, METH_NOARGS, GetOption_doc},
460         {"setTime", (PyCFunction) KX_ConstraintActuator::sPySetTime, METH_VARARGS, SetTime_doc},
461         {"getTime", (PyCFunction) KX_ConstraintActuator::sPyGetTime, METH_NOARGS, GetTime_doc},
462         {"setProperty", (PyCFunction) KX_ConstraintActuator::sPySetProperty, METH_VARARGS, SetProperty_doc},
463         {"getProperty", (PyCFunction) KX_ConstraintActuator::sPyGetProperty, METH_NOARGS, GetProperty_doc},
464         {"setMin", (PyCFunction) KX_ConstraintActuator::sPySetMin, METH_VARARGS, SetMin_doc},
465         {"getMin", (PyCFunction) KX_ConstraintActuator::sPyGetMin, METH_NOARGS, GetMin_doc},
466         {"setDistance", (PyCFunction) KX_ConstraintActuator::sPySetMin, METH_VARARGS, SetDistance_doc},
467         {"getDistance", (PyCFunction) KX_ConstraintActuator::sPyGetMin, METH_NOARGS, GetDistance_doc},
468         {"setMax", (PyCFunction) KX_ConstraintActuator::sPySetMax, METH_VARARGS, SetMax_doc},
469         {"getMax", (PyCFunction) KX_ConstraintActuator::sPyGetMax, METH_NOARGS, GetMax_doc},
470         {"setRayLength", (PyCFunction) KX_ConstraintActuator::sPySetMax, METH_VARARGS, SetRayLength_doc},
471         {"getRayLength", (PyCFunction) KX_ConstraintActuator::sPyGetMax, METH_NOARGS, GetRayLength_doc},
472         {"setLimit", (PyCFunction) KX_ConstraintActuator::sPySetLimit, METH_VARARGS, SetLimit_doc},
473         {"getLimit", (PyCFunction) KX_ConstraintActuator::sPyGetLimit, METH_NOARGS, GetLimit_doc},
474         {NULL,NULL} //Sentinel
475 };
476
477 PyObject* KX_ConstraintActuator::_getattr(const STR_String& attr) {
478         _getattr_up(SCA_IActuator);
479 }
480
481 /* 2. setDamp                                                                */
482 const char KX_ConstraintActuator::SetDamp_doc[] = 
483 "setDamp(duration)\n"
484 "\t- duration: integer\n"
485 "\tSets the time constant of the orientation and distance constraint.\n"
486 "\tIf the duration is negative, it is set to 0.\n";
487 PyObject* KX_ConstraintActuator::PySetDamp(PyObject* self, 
488                                                                                    PyObject* args, 
489                                                                                    PyObject* kwds) {
490         int dampArg;
491         if(!PyArg_ParseTuple(args, "i", &dampArg)) {
492                 return NULL;            
493         }
494         
495         m_posDampTime = dampArg;
496         if (m_posDampTime < 0) m_posDampTime = 0;
497
498         Py_Return;
499 }
500 /* 3. getDamp                                                                */
501 const char KX_ConstraintActuator::GetDamp_doc[] = 
502 "getDamp()\n"
503 "\tReturns the damping parameter.\n";
504 PyObject* KX_ConstraintActuator::PyGetDamp(PyObject* self){
505         return PyInt_FromLong(m_posDampTime);
506 }
507
508 /* 2. setRotDamp                                                                */
509 const char KX_ConstraintActuator::SetRotDamp_doc[] = 
510 "setRotDamp(duration)\n"
511 "\t- duration: integer\n"
512 "\tSets the time constant of the orientation constraint.\n"
513 "\tIf the duration is negative, it is set to 0.\n";
514 PyObject* KX_ConstraintActuator::PySetRotDamp(PyObject* self, 
515                                                                                       PyObject* args, 
516                                                                                       PyObject* kwds) {
517         int dampArg;
518         if(!PyArg_ParseTuple(args, "i", &dampArg)) {
519                 return NULL;            
520         }
521         
522         m_rotDampTime = dampArg;
523         if (m_rotDampTime < 0) m_rotDampTime = 0;
524
525         Py_Return;
526 }
527 /* 3. getRotDamp                                                                */
528 const char KX_ConstraintActuator::GetRotDamp_doc[] = 
529 "getRotDamp()\n"
530 "\tReturns the damping time for application of the constraint.\n";
531 PyObject* KX_ConstraintActuator::PyGetRotDamp(PyObject* self){
532         return PyInt_FromLong(m_rotDampTime);
533 }
534
535 /* 2. setDirection                                                                */
536 const char KX_ConstraintActuator::SetDirection_doc[] = 
537 "setDirection(vector)\n"
538 "\t- vector: 3-tuple\n"
539 "\tSets the reference direction in world coordinate for the orientation constraint.\n";
540 PyObject* KX_ConstraintActuator::PySetDirection(PyObject* self, 
541                                                                                         PyObject* args, 
542                                                                                         PyObject* kwds) {
543         float x, y, z;
544         MT_Scalar len;
545         MT_Vector3 dir;
546
547         if(!PyArg_ParseTuple(args, "(fff)", &x, &y, &z)) {
548                 return NULL;            
549         }
550         dir[0] = x;
551         dir[1] = y;
552         dir[2] = z;
553         len = dir.length();
554         if (MT_fuzzyZero(len)) {
555                 std::cout << "Invalid direction" << std::endl;
556                 return NULL;
557         }
558         m_refDirection = dir/len;
559
560         Py_Return;
561 }
562 /* 3. getDirection                                                                */
563 const char KX_ConstraintActuator::GetDirection_doc[] = 
564 "getDirection()\n"
565 "\tReturns the reference direction of the orientation constraint as a 3-tuple.\n";
566 PyObject* KX_ConstraintActuator::PyGetDirection(PyObject* self){
567         PyObject *retVal = PyList_New(3);
568
569         PyList_SetItem(retVal, 0, PyFloat_FromDouble(m_refDirection[0]));
570         PyList_SetItem(retVal, 1, PyFloat_FromDouble(m_refDirection[1]));
571         PyList_SetItem(retVal, 2, PyFloat_FromDouble(m_refDirection[2]));
572         return retVal;
573 }
574
575 /* 2. setOption                                                                */
576 const char KX_ConstraintActuator::SetOption_doc[] = 
577 "setOption(option)\n"
578 "\t- option: integer\n"
579 "\tSets several options of the distance  constraint.\n"
580 "\tBinary combination of the following values:\n"
581 "\t\t 64 : Activate alignment to surface\n"
582 "\t\t128 : Detect material rather than property\n"
583 "\t\t256 : No deactivation if ray does not hit target\n"
584 "\t\t512 : Activate distance control\n";
585 PyObject* KX_ConstraintActuator::PySetOption(PyObject* self, 
586                                                                                      PyObject* args, 
587                                                                                      PyObject* kwds) {
588         int option;
589         if(!PyArg_ParseTuple(args, "i", &option)) {
590                 return NULL;            
591         }
592         
593         m_option = option;
594
595         Py_Return;
596 }
597 /* 3. getOption                                                              */
598 const char KX_ConstraintActuator::GetOption_doc[] = 
599 "getOption()\n"
600 "\tReturns the option parameter.\n";
601 PyObject* KX_ConstraintActuator::PyGetOption(PyObject* self){
602         return PyInt_FromLong(m_option);
603 }
604
605 /* 2. setTime                                                                */
606 const char KX_ConstraintActuator::SetTime_doc[] = 
607 "setTime(duration)\n"
608 "\t- duration: integer\n"
609 "\tSets the activation time of the actuator.\n"
610 "\tThe actuator disables itself after this many frame.\n"
611 "\tIf set to 0 or negative, the actuator is not limited in time.\n";
612 PyObject* KX_ConstraintActuator::PySetTime(PyObject* self, 
613                                                                                    PyObject* args, 
614                                                                                    PyObject* kwds) {
615         int t;
616         if(!PyArg_ParseTuple(args, "i", &t)) {
617                 return NULL;            
618         }
619         
620         if (t < 0)
621                 t = 0;
622         m_activeTime = t;
623
624         Py_Return;
625 }
626 /* 3. getTime                                                                */
627 const char KX_ConstraintActuator::GetTime_doc[] = 
628 "getTime()\n"
629 "\tReturns the time parameter.\n";
630 PyObject* KX_ConstraintActuator::PyGetTime(PyObject* self){
631         return PyInt_FromLong(m_activeTime);
632 }
633
634 /* 2. setProperty                                                                */
635 const char KX_ConstraintActuator::SetProperty_doc[] = 
636 "setProperty(property)\n"
637 "\t- property: string\n"
638 "\tSets the name of the property or material for the ray detection of the distance constraint.\n"
639 "\tIf empty, the ray will detect any collisioning object.\n";
640 PyObject* KX_ConstraintActuator::PySetProperty(PyObject* self, 
641                                                                                        PyObject* args, 
642                                                                                        PyObject* kwds) {
643         char *property;
644         if (!PyArg_ParseTuple(args, "s", &property)) {
645                 return NULL;
646         }
647         if (property == NULL) {
648                 m_property[0] = 0;
649         } else {
650                 strncpy(m_property, property, sizeof(m_property));
651                 m_property[sizeof(m_property)-1] = 0;
652         }
653
654         Py_Return;
655 }
656 /* 3. getProperty                                                                */
657 const char KX_ConstraintActuator::GetProperty_doc[] = 
658 "getProperty()\n"
659 "\tReturns the property parameter.\n";
660 PyObject* KX_ConstraintActuator::PyGetProperty(PyObject* self){
661         return PyString_FromString(m_property);
662 }
663
664 /* 4. setDistance                                                                 */
665 const char KX_ConstraintActuator::SetDistance_doc[] = 
666 "setDistance(distance)\n"
667 "\t- distance: float\n"
668 "\tSets the target distance in distance constraint\n";
669 /* 4. setMin                                                                 */
670 const char KX_ConstraintActuator::SetMin_doc[] = 
671 "setMin(lower_bound)\n"
672 "\t- lower_bound: float\n"
673 "\tSets the lower value of the interval to which the value\n"
674 "\tis clipped.\n";
675 PyObject* KX_ConstraintActuator::PySetMin(PyObject* self, 
676                                                                                   PyObject* args, 
677                                                                                   PyObject* kwds) {
678         float minArg;
679         if(!PyArg_ParseTuple(args, "f", &minArg)) {
680                 return NULL;            
681         }
682
683         switch (m_locrot) {
684         default:
685                 m_minimumBound = minArg;
686                 break;
687         case KX_ACT_CONSTRAINT_ROTX:
688         case KX_ACT_CONSTRAINT_ROTY:
689         case KX_ACT_CONSTRAINT_ROTZ:
690                 m_minimumBound = MT_radians(minArg);
691                 break;
692         }
693
694         Py_Return;
695 }
696 /* 5. getDistance                                                                 */
697 const char KX_ConstraintActuator::GetDistance_doc[] = 
698 "getDistance()\n"
699 "\tReturns the distance parameter \n";
700 /* 5. getMin                                                                 */
701 const char KX_ConstraintActuator::GetMin_doc[] = 
702 "getMin()\n"
703 "\tReturns the lower value of the interval to which the value\n"
704 "\tis clipped.\n";
705 PyObject* KX_ConstraintActuator::PyGetMin(PyObject* self) {
706         return PyFloat_FromDouble(m_minimumBound);
707 }
708
709 /* 6. setRayLength                                                                 */
710 const char KX_ConstraintActuator::SetRayLength_doc[] = 
711 "setRayLength(length)\n"
712 "\t- length: float\n"
713 "\tSets the maximum ray length of the distance constraint\n";
714 /* 6. setMax                                                                 */
715 const char KX_ConstraintActuator::SetMax_doc[] = 
716 "setMax(upper_bound)\n"
717 "\t- upper_bound: float\n"
718 "\tSets the upper value of the interval to which the value\n"
719 "\tis clipped.\n";
720 PyObject* KX_ConstraintActuator::PySetMax(PyObject* self, 
721                                                                                   PyObject* args, 
722                                                                                   PyObject* kwds){
723         float maxArg;
724         if(!PyArg_ParseTuple(args, "f", &maxArg)) {
725                 return NULL;            
726         }
727
728         switch (m_locrot) {
729         default:
730                 m_maximumBound = maxArg;
731                 break;
732         case KX_ACT_CONSTRAINT_ROTX:
733         case KX_ACT_CONSTRAINT_ROTY:
734         case KX_ACT_CONSTRAINT_ROTZ:
735                 m_maximumBound = MT_radians(maxArg);
736                 break;
737         }
738
739         Py_Return;
740 }
741 /* 7. getRayLength                                                                 */
742 const char KX_ConstraintActuator::GetRayLength_doc[] = 
743 "getRayLength()\n"
744 "\tReturns the length of the ray\n";
745 /* 7. getMax                                                                 */
746 const char KX_ConstraintActuator::GetMax_doc[] = 
747 "getMax()\n"
748 "\tReturns the upper value of the interval to which the value\n"
749 "\tis clipped.\n";
750 PyObject* KX_ConstraintActuator::PyGetMax(PyObject* self) {
751         return PyFloat_FromDouble(m_maximumBound);
752 }
753
754
755 /* This setter/getter probably for the constraint type                       */
756 /* 8. setLimit                                                               */
757 const char KX_ConstraintActuator::SetLimit_doc[] = 
758 "setLimit(type)\n"
759 "\t- type: integer\n"
760 "\t  1  : LocX\n"
761 "\t  2  : LocY\n"
762 "\t  3  : LocZ\n"
763 "\t  7  : Distance along +X axis\n"
764 "\t  8  : Distance along +Y axis\n"
765 "\t  9  : Distance along +Z axis\n"
766 "\t  10 : Distance along -X axis\n"
767 "\t  11 : Distance along -Y axis\n"
768 "\t  12 : Distance along -Z axis\n"
769 "\t  13 : Align X axis\n"
770 "\t  14 : Align Y axis\n"
771 "\t  15 : Align Z axis\n"
772 "\tSets the type of constraint.\n";
773 PyObject* KX_ConstraintActuator::PySetLimit(PyObject* self, 
774                                                                                         PyObject* args, 
775                                                                                         PyObject* kwds) {
776         int locrotArg;
777         if(!PyArg_ParseTuple(args, "i", &locrotArg)) {
778                 return NULL;            
779         }
780         
781         if (IsValidMode((KX_CONSTRAINTTYPE)locrotArg)) m_locrot = locrotArg;
782
783         Py_Return;
784 }
785 /* 9. getLimit                                                               */
786 const char KX_ConstraintActuator::GetLimit_doc[] = 
787 "getLimit()\n"
788 "\tReturns the type of constraint.\n";
789 PyObject* KX_ConstraintActuator::PyGetLimit(PyObject* self) {
790         return PyInt_FromLong(m_locrot);
791 }
792
793 /* eof */