0d69da83081f8e8375642ef1bd1e1ce64a28ab9e
[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 #include "KX_PythonInit.h" // KX_GetActiveScene
40
41 #ifdef HAVE_CONFIG_H
42 #include <config.h>
43 #endif
44
45 /* ------------------------------------------------------------------------- */
46 /* Native functions                                                          */
47 /* ------------------------------------------------------------------------- */
48
49 KX_ConstraintActuator::KX_ConstraintActuator(SCA_IObject *gameobj, 
50                                                                                          int posDampTime,
51                                                                                          int rotDampTime,
52                                                                                          float minBound,
53                                                                                          float maxBound,
54                                                                                          float refDir[3],
55                                                                                          int locrotxyz,
56                                                                                          int time,
57                                                                                          int option,
58                                                                                          char *property) :
59         SCA_IActuator(gameobj),
60         m_refDirVector(refDir),
61         m_currentTime(0)
62 {
63         m_refDirection[0] = refDir[0];
64         m_refDirection[1] = refDir[1];
65         m_refDirection[2] = refDir[2];
66         m_posDampTime = posDampTime;
67         m_rotDampTime = rotDampTime;
68         m_locrot   = locrotxyz;
69         m_option = option;
70         m_activeTime = time;
71         if (property) {
72                 m_property = property;
73         } else {
74                 m_property = "";
75         }
76         /* The units of bounds are determined by the type of constraint. To      */
77         /* make the constraint application easier and more transparent later on, */
78         /* I think converting the bounds to the applicable domain makes more     */
79         /* sense.                                                                */
80         switch (m_locrot) {
81         case KX_ACT_CONSTRAINT_ORIX:
82         case KX_ACT_CONSTRAINT_ORIY:
83         case KX_ACT_CONSTRAINT_ORIZ:
84                 {
85                         MT_Scalar len = m_refDirVector.length();
86                         if (MT_fuzzyZero(len)) {
87                                 // missing a valid direction
88                                 std::cout << "WARNING: Constraint actuator " << GetName() << ":  There is no valid reference direction!" << std::endl;
89                                 m_locrot = KX_ACT_CONSTRAINT_NODEF;
90                         } else {
91                                 m_refDirection[0] /= len;
92                                 m_refDirection[1] /= len;
93                                 m_refDirection[2] /= len;
94                                 m_refDirVector /= len;
95                         }
96                         m_minimumBound = cos(minBound);
97                         m_maximumBound = cos(maxBound);
98                         m_minimumSine = sin(minBound);
99                         m_maximumSine = sin(maxBound);
100                 }
101                 break;
102         default:
103                 m_minimumBound = minBound;
104                 m_maximumBound = maxBound;
105                 m_minimumSine = 0.f;
106                 m_maximumSine = 0.f;
107                 break;
108         }
109
110 } /* End of constructor */
111
112 KX_ConstraintActuator::~KX_ConstraintActuator()
113
114         // there's nothing to be done here, really....
115 } /* end of destructor */
116
117 bool KX_ConstraintActuator::RayHit(KX_ClientObjectInfo* client, KX_RayCast* result, void * const data)
118 {
119
120         m_hitObject = client->m_gameobject;
121         
122         bool bFound = false;
123
124         if (m_property.IsEmpty())
125         {
126                 bFound = true;
127         }
128         else
129         {
130                 if (m_option & KX_ACT_CONSTRAINT_MATERIAL)
131                 {
132                         if (client->m_auxilary_info)
133                         {
134                                 bFound = !strcmp(m_property.Ptr(), ((char*)client->m_auxilary_info));
135                         }
136                 }
137                 else
138                 {
139                         bFound = m_hitObject->GetProperty(m_property) != NULL;
140                 }
141         }
142         // update the hit status
143         result->m_hitFound = bFound;
144         // stop looking
145         return true;
146 }
147
148 /* this function is used to pre-filter the object before casting the ray on them.
149    This is useful for "X-Ray" option when we want to see "through" unwanted object.
150  */
151 bool KX_ConstraintActuator::NeedRayCast(KX_ClientObjectInfo* client)
152 {
153         if (client->m_type > KX_ClientObjectInfo::ACTOR)
154         {
155                 // Unknown type of object, skip it.
156                 // Should not occur as the sensor objects are filtered in RayTest()
157                 printf("Invalid client type %d found in ray casting\n", client->m_type);
158                 return false;
159         }
160         // no X-Ray function yet
161         return true;
162 }
163
164 bool KX_ConstraintActuator::Update(double curtime, bool frame)
165 {
166
167         bool result = false;    
168         bool bNegativeEvent = IsNegativeEvent();
169         RemoveAllEvents();
170
171         if (!bNegativeEvent) {
172                 /* Constraint clamps the values to the specified range, with a sort of    */
173                 /* low-pass filtered time response, if the damp time is unequal to 0.     */
174
175                 /* Having to retrieve location/rotation and setting it afterwards may not */
176                 /* be efficient enough... Somthing to look at later.                      */
177                 KX_GameObject  *obj = (KX_GameObject*) GetParent();
178                 MT_Point3    position = obj->NodeGetWorldPosition();
179                 MT_Point3    newposition;
180                 MT_Vector3   normal, direction, refDirection;
181                 MT_Matrix3x3 rotation = obj->NodeGetWorldOrientation();
182                 MT_Scalar    filter, newdistance, cosangle;
183                 int axis, sign;
184
185                 if (m_posDampTime) {
186                         filter = m_posDampTime/(1.0+m_posDampTime);
187                 } else {
188                         filter = 0.0;
189                 }
190                 switch (m_locrot) {
191                 case KX_ACT_CONSTRAINT_ORIX:
192                 case KX_ACT_CONSTRAINT_ORIY:
193                 case KX_ACT_CONSTRAINT_ORIZ:
194                         switch (m_locrot) {
195                         case KX_ACT_CONSTRAINT_ORIX:
196                                 direction[0] = rotation[0][0];
197                                 direction[1] = rotation[1][0];
198                                 direction[2] = rotation[2][0];
199                                 axis = 0;
200                                 break;
201                         case KX_ACT_CONSTRAINT_ORIY:
202                                 direction[0] = rotation[0][1];
203                                 direction[1] = rotation[1][1];
204                                 direction[2] = rotation[2][1];
205                                 axis = 1;
206                                 break;
207                         default:
208                                 direction[0] = rotation[0][2];
209                                 direction[1] = rotation[1][2];
210                                 direction[2] = rotation[2][2];
211                                 axis = 2;
212                                 break;
213                         }
214                         if ((m_maximumBound < (1.0f-FLT_EPSILON)) || (m_minimumBound < (1.0f-FLT_EPSILON))) {
215                                 // reference direction needs to be evaluated
216                                 // 1. get the cosine between current direction and target
217                                 cosangle = direction.dot(m_refDirVector);
218                                 if (cosangle >= (m_maximumBound-FLT_EPSILON) && cosangle <= (m_minimumBound+FLT_EPSILON)) {
219                                         // no change to do
220                                         result = true;
221                                         goto CHECK_TIME;
222                                 }
223                                 // 2. define a new reference direction
224                                 //    compute local axis with reference direction as X and
225                                 //    Y in direction X refDirection plane
226                                 MT_Vector3 zaxis = m_refDirVector.cross(direction);
227                                 if (MT_fuzzyZero2(zaxis.length2())) {
228                                         // direction and refDirection are identical,
229                                         // choose any other direction to define plane
230                                         if (direction[0] < 0.9999)
231                                                 zaxis = m_refDirVector.cross(MT_Vector3(1.0,0.0,0.0));
232                                         else
233                                                 zaxis = m_refDirVector.cross(MT_Vector3(0.0,1.0,0.0));
234                                 }
235                                 MT_Vector3 yaxis = zaxis.cross(m_refDirVector);
236                                 yaxis.normalize();
237                                 if (cosangle > m_minimumBound) {
238                                         // angle is too close to reference direction,
239                                         // choose a new reference that is exactly at minimum angle
240                                         refDirection = m_minimumBound * m_refDirVector + m_minimumSine * yaxis;
241                                 } else {
242                                         // angle is too large, choose new reference direction at maximum angle
243                                         refDirection = m_maximumBound * m_refDirVector + m_maximumSine * yaxis;
244                                 }
245                         } else {
246                                 refDirection = m_refDirVector;
247                         }
248                         // apply damping on the direction
249                         direction = filter*direction + (1.0-filter)*refDirection;
250                         obj->AlignAxisToVect(direction, axis);
251                         result = true;
252                         goto CHECK_TIME;
253                 case KX_ACT_CONSTRAINT_DIRPX:
254                 case KX_ACT_CONSTRAINT_DIRPY:
255                 case KX_ACT_CONSTRAINT_DIRPZ:
256                 case KX_ACT_CONSTRAINT_DIRNX:
257                 case KX_ACT_CONSTRAINT_DIRNY:
258                 case KX_ACT_CONSTRAINT_DIRNZ:
259                         switch (m_locrot) {
260                         case KX_ACT_CONSTRAINT_DIRPX:
261                                 normal[0] = rotation[0][0];
262                                 normal[1] = rotation[1][0];
263                                 normal[2] = rotation[2][0];
264                                 axis = 0;               // axis according to KX_GameObject::AlignAxisToVect()
265                                 sign = 0;               // X axis will be parrallel to direction of ray
266                                 break;
267                         case KX_ACT_CONSTRAINT_DIRPY:
268                                 normal[0] = rotation[0][1];
269                                 normal[1] = rotation[1][1];
270                                 normal[2] = rotation[2][1];
271                                 axis = 1;
272                                 sign = 0;
273                                 break;
274                         case KX_ACT_CONSTRAINT_DIRPZ:
275                                 normal[0] = rotation[0][2];
276                                 normal[1] = rotation[1][2];
277                                 normal[2] = rotation[2][2];
278                                 axis = 2;
279                                 sign = 0;
280                                 break;
281                         case KX_ACT_CONSTRAINT_DIRNX:
282                                 normal[0] = -rotation[0][0];
283                                 normal[1] = -rotation[1][0];
284                                 normal[2] = -rotation[2][0];
285                                 axis = 0;
286                                 sign = 1;
287                                 break;
288                         case KX_ACT_CONSTRAINT_DIRNY:
289                                 normal[0] = -rotation[0][1];
290                                 normal[1] = -rotation[1][1];
291                                 normal[2] = -rotation[2][1];
292                                 axis = 1;
293                                 sign = 1;
294                                 break;
295                         case KX_ACT_CONSTRAINT_DIRNZ:
296                                 normal[0] = -rotation[0][2];
297                                 normal[1] = -rotation[1][2];
298                                 normal[2] = -rotation[2][2];
299                                 axis = 2;
300                                 sign = 1;
301                                 break;
302                         }
303                         normal.normalize();
304                         if (m_option & KX_ACT_CONSTRAINT_LOCAL) {
305                                 // direction of the ray is along the local axis
306                                 direction = normal;
307                         } else {
308                                 switch (m_locrot) {
309                                 case KX_ACT_CONSTRAINT_DIRPX:
310                                         direction = MT_Vector3(1.0,0.0,0.0);
311                                         break;
312                                 case KX_ACT_CONSTRAINT_DIRPY:
313                                         direction = MT_Vector3(0.0,1.0,0.0);
314                                         break;
315                                 case KX_ACT_CONSTRAINT_DIRPZ:
316                                         direction = MT_Vector3(0.0,0.0,1.0);
317                                         break;
318                                 case KX_ACT_CONSTRAINT_DIRNX:
319                                         direction = MT_Vector3(-1.0,0.0,0.0);
320                                         break;
321                                 case KX_ACT_CONSTRAINT_DIRNY:
322                                         direction = MT_Vector3(0.0,-1.0,0.0);
323                                         break;
324                                 case KX_ACT_CONSTRAINT_DIRNZ:
325                                         direction = MT_Vector3(0.0,0.0,-1.0);
326                                         break;
327                                 }
328                         }
329                         {
330                                 MT_Point3 topoint = position + (m_maximumBound) * direction;
331                                 PHY_IPhysicsEnvironment* pe = KX_GetActiveScene()->GetPhysicsEnvironment();
332                                 KX_IPhysicsController *spc = obj->GetPhysicsController();
333
334                                 if (!pe) {
335                                         std::cout << "WARNING: Constraint actuator " << GetName() << ":  There is no physics environment!" << std::endl;
336                                         goto CHECK_TIME;
337                                 }        
338                                 if (!spc) {
339                                         // the object is not physical, we probably want to avoid hitting its own parent
340                                         KX_GameObject *parent = obj->GetParent();
341                                         if (parent) {
342                                                 spc = parent->GetPhysicsController();
343                                                 parent->Release();
344                                         }
345                                 }
346                                 KX_RayCast::Callback<KX_ConstraintActuator> callback(this,spc);
347                                 result = KX_RayCast::RayTest(pe, position, topoint, callback);
348                                 if (result)     {
349                                         MT_Vector3 newnormal = callback.m_hitNormal;
350                                         // compute new position & orientation
351                                         if ((m_option & (KX_ACT_CONSTRAINT_NORMAL|KX_ACT_CONSTRAINT_DISTANCE)) == 0) {
352                                                 // if none option is set, the actuator does nothing but detect ray 
353                                                 // (works like a sensor)
354                                                 goto CHECK_TIME;
355                                         }
356                                         if (m_option & KX_ACT_CONSTRAINT_NORMAL) {
357                                                 MT_Scalar rotFilter;
358                                                 // apply damping on the direction
359                                                 if (m_rotDampTime) {
360                                                         rotFilter = m_rotDampTime/(1.0+m_rotDampTime);
361                                                 } else {
362                                                         rotFilter = filter;
363                                                 }
364                                                 newnormal = rotFilter*normal - (1.0-rotFilter)*newnormal;
365                                                 obj->AlignAxisToVect((sign)?-newnormal:newnormal, axis);
366                                                 if (m_option & KX_ACT_CONSTRAINT_LOCAL) {
367                                                         direction = newnormal;
368                                                         direction.normalize();
369                                                 }
370                                         }
371                                         if (m_option & KX_ACT_CONSTRAINT_DISTANCE) {
372                                                 if (m_posDampTime) {
373                                                         newdistance = filter*(position-callback.m_hitPoint).length()+(1.0-filter)*m_minimumBound;
374                                                 } else {
375                                                         newdistance = m_minimumBound;
376                                                 }
377                                                 // logically we should cancel the speed along the ray direction as we set the
378                                                 // position along that axis
379                                                 spc = obj->GetPhysicsController();
380                                                 if (spc && spc->IsDyna()) {
381                                                         MT_Vector3 linV = spc->GetLinearVelocity();
382                                                         // cancel the projection along the ray direction
383                                                         MT_Scalar fallspeed = linV.dot(direction);
384                                                         if (!MT_fuzzyZero(fallspeed))
385                                                                 spc->SetLinearVelocity(linV-fallspeed*direction,false);
386                                                 }
387                                         } else {
388                                                 newdistance = (position-callback.m_hitPoint).length();
389                                         }
390                                         newposition = callback.m_hitPoint-newdistance*direction;
391                                 } else if (m_option & KX_ACT_CONSTRAINT_PERMANENT) {
392                                         // no contact but still keep running
393                                         result = true;
394                                         goto CHECK_TIME;
395                                 }
396                         }
397                         break; 
398                 case KX_ACT_CONSTRAINT_FHPX:
399                 case KX_ACT_CONSTRAINT_FHPY:
400                 case KX_ACT_CONSTRAINT_FHPZ:
401                 case KX_ACT_CONSTRAINT_FHNX:
402                 case KX_ACT_CONSTRAINT_FHNY:
403                 case KX_ACT_CONSTRAINT_FHNZ:
404                         switch (m_locrot) {
405                         case KX_ACT_CONSTRAINT_FHPX:
406                                 normal[0] = -rotation[0][0];
407                                 normal[1] = -rotation[1][0];
408                                 normal[2] = -rotation[2][0];
409                                 direction = MT_Vector3(1.0,0.0,0.0);
410                                 break;
411                         case KX_ACT_CONSTRAINT_FHPY:
412                                 normal[0] = -rotation[0][1];
413                                 normal[1] = -rotation[1][1];
414                                 normal[2] = -rotation[2][1];
415                                 direction = MT_Vector3(0.0,1.0,0.0);
416                                 break;
417                         case KX_ACT_CONSTRAINT_FHPZ:
418                                 normal[0] = -rotation[0][2];
419                                 normal[1] = -rotation[1][2];
420                                 normal[2] = -rotation[2][2];
421                                 direction = MT_Vector3(0.0,0.0,1.0);
422                                 break;
423                         case KX_ACT_CONSTRAINT_FHNX:
424                                 normal[0] = rotation[0][0];
425                                 normal[1] = rotation[1][0];
426                                 normal[2] = rotation[2][0];
427                                 direction = MT_Vector3(-1.0,0.0,0.0);
428                                 break;
429                         case KX_ACT_CONSTRAINT_FHNY:
430                                 normal[0] = rotation[0][1];
431                                 normal[1] = rotation[1][1];
432                                 normal[2] = rotation[2][1];
433                                 direction = MT_Vector3(0.0,-1.0,0.0);
434                                 break;
435                         case KX_ACT_CONSTRAINT_FHNZ:
436                                 normal[0] = rotation[0][2];
437                                 normal[1] = rotation[1][2];
438                                 normal[2] = rotation[2][2];
439                                 direction = MT_Vector3(0.0,0.0,-1.0);
440                                 break;
441                         }
442                         normal.normalize();
443                         {
444                                 PHY_IPhysicsEnvironment* pe = KX_GetActiveScene()->GetPhysicsEnvironment();
445                                 KX_IPhysicsController *spc = obj->GetPhysicsController();
446
447                                 if (!pe) {
448                                         std::cout << "WARNING: Constraint actuator " << GetName() << ":  There is no physics environment!" << std::endl;
449                                         goto CHECK_TIME;
450                                 }        
451                                 if (!spc || !spc->IsDyna()) {
452                                         // the object is not dynamic, it won't support setting speed
453                                         goto CHECK_TIME;
454                                 }
455                                 m_hitObject = NULL;
456                                 // distance of Fh area is stored in m_minimum
457                                 MT_Point3 topoint = position + (m_minimumBound+spc->GetRadius()) * direction;
458                                 KX_RayCast::Callback<KX_ConstraintActuator> callback(this,spc);
459                                 result = KX_RayCast::RayTest(pe, position, topoint, callback);
460                                 // we expect a hit object
461                                 if (!m_hitObject)
462                                         result = false;
463                                 if (result)     
464                                 {
465                                         MT_Vector3 newnormal = callback.m_hitNormal;
466                                         // compute new position & orientation
467                                         MT_Scalar distance = (callback.m_hitPoint-position).length()-spc->GetRadius(); 
468                                         // estimate the velocity of the hit point
469                                         MT_Point3 relativeHitPoint;
470                                         relativeHitPoint = (callback.m_hitPoint-m_hitObject->NodeGetWorldPosition());
471                                         MT_Vector3 velocityHitPoint = m_hitObject->GetVelocity(relativeHitPoint);
472                                         MT_Vector3 relativeVelocity = spc->GetLinearVelocity() - velocityHitPoint;
473                                         MT_Scalar relativeVelocityRay = direction.dot(relativeVelocity);
474                                         MT_Scalar springExtent = 1.0 - distance/m_minimumBound;
475                                         // Fh force is stored in m_maximum
476                                         MT_Scalar springForce = springExtent * m_maximumBound;
477                                         // damping is stored in m_refDirection [0] = damping, [1] = rot damping
478                                         MT_Scalar springDamp = relativeVelocityRay * m_refDirVector[0];
479                                         MT_Vector3 newVelocity = spc->GetLinearVelocity()-(springForce+springDamp)*direction;
480                                         if (m_option & KX_ACT_CONSTRAINT_NORMAL)
481                                         {
482                                                 newVelocity+=(springForce+springDamp)*(newnormal-newnormal.dot(direction)*direction);
483                                         }
484                                         spc->SetLinearVelocity(newVelocity, false);
485                                         if (m_option & KX_ACT_CONSTRAINT_DOROTFH)
486                                         {
487                                                 MT_Vector3 angSpring = (normal.cross(newnormal))*m_maximumBound;
488                                                 MT_Vector3 angVelocity = spc->GetAngularVelocity();
489                                                 // remove component that is parallel to normal
490                                                 angVelocity -= angVelocity.dot(newnormal)*newnormal;
491                                                 MT_Vector3 angDamp = angVelocity * ((m_refDirVector[1]>MT_EPSILON)?m_refDirVector[1]:m_refDirVector[0]);
492                                                 spc->SetAngularVelocity(spc->GetAngularVelocity()+(angSpring-angDamp), false);
493                                         }
494                                 } else if (m_option & KX_ACT_CONSTRAINT_PERMANENT) {
495                                         // no contact but still keep running
496                                         result = true;
497                                 }
498                                 // don't set the position with this constraint
499                                 goto CHECK_TIME;
500                         }
501                         break; 
502                 case KX_ACT_CONSTRAINT_LOCX:
503                 case KX_ACT_CONSTRAINT_LOCY:
504                 case KX_ACT_CONSTRAINT_LOCZ:
505                         newposition = position = obj->GetSGNode()->GetLocalPosition();
506                         switch (m_locrot) {
507                         case KX_ACT_CONSTRAINT_LOCX:
508                                 Clamp(newposition[0], m_minimumBound, m_maximumBound);
509                                 break;
510                         case KX_ACT_CONSTRAINT_LOCY:
511                                 Clamp(newposition[1], m_minimumBound, m_maximumBound);
512                                 break;
513                         case KX_ACT_CONSTRAINT_LOCZ:
514                                 Clamp(newposition[2], m_minimumBound, m_maximumBound);
515                                 break;
516                         }
517                         result = true;
518                         if (m_posDampTime) {
519                                 newposition = filter*position + (1.0-filter)*newposition;
520                         }
521                         obj->NodeSetLocalPosition(newposition);
522                         goto CHECK_TIME;
523                 }
524                 if (result) {
525                         // set the new position but take into account parent if any
526                         obj->NodeSetWorldPosition(newposition);
527                 }
528         CHECK_TIME:
529                 if (result && m_activeTime > 0 ) {
530                         if (++m_currentTime >= m_activeTime)
531                                 result = false;
532                 }
533         }
534         if (!result) {
535                 m_currentTime = 0;
536         }
537         return result;
538 } /* end of KX_ConstraintActuator::Update(double curtime,double deltatime)   */
539
540 void KX_ConstraintActuator::Clamp(MT_Scalar &var, 
541                                                                   float min, 
542                                                                   float max) {
543         if (var < min) {
544                 var = min;
545         } else if (var > max) {
546                 var = max;
547         }
548 }
549
550
551 bool KX_ConstraintActuator::IsValidMode(KX_ConstraintActuator::KX_CONSTRAINTTYPE m) 
552 {
553         bool res = false;
554
555         if ( (m > KX_ACT_CONSTRAINT_NODEF) && (m < KX_ACT_CONSTRAINT_MAX)) {
556                 res = true;
557         }
558
559         return res;
560 }
561
562 /* ------------------------------------------------------------------------- */
563 /* Python functions                                                          */
564 /* ------------------------------------------------------------------------- */
565
566 /* Integration hooks ------------------------------------------------------- */
567 PyTypeObject KX_ConstraintActuator::Type = {
568 #if (PY_VERSION_HEX >= 0x02060000)
569         PyVarObject_HEAD_INIT(NULL, 0)
570 #else
571         /* python 2.5 and below */
572         PyObject_HEAD_INIT( NULL )  /* required py macro */
573         0,                          /* ob_size */
574 #endif
575         "KX_ConstraintActuator",
576         sizeof(PyObjectPlus_Proxy),
577         0,
578         py_base_dealloc,
579         0,
580         0,
581         0,
582         0,
583         py_base_repr,
584         0,0,0,0,0,0,0,0,0,
585         Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
586         0,0,0,0,0,0,0,
587         Methods,
588         0,
589         0,
590         &SCA_IActuator::Type,
591         0,0,0,0,0,0,
592         py_base_new
593 };
594
595 PyMethodDef KX_ConstraintActuator::Methods[] = {
596         // Deprecated -->
597         {"setDamp", (PyCFunction) KX_ConstraintActuator::sPySetDamp, METH_VARARGS, (PY_METHODCHAR)SetDamp_doc},
598         {"getDamp", (PyCFunction) KX_ConstraintActuator::sPyGetDamp, METH_NOARGS, (PY_METHODCHAR)GetDamp_doc},
599         {"setRotDamp", (PyCFunction) KX_ConstraintActuator::sPySetRotDamp, METH_VARARGS, (PY_METHODCHAR)SetRotDamp_doc},
600         {"getRotDamp", (PyCFunction) KX_ConstraintActuator::sPyGetRotDamp, METH_NOARGS, (PY_METHODCHAR)GetRotDamp_doc},
601         {"setDirection", (PyCFunction) KX_ConstraintActuator::sPySetDirection, METH_VARARGS, (PY_METHODCHAR)SetDirection_doc},
602         {"getDirection", (PyCFunction) KX_ConstraintActuator::sPyGetDirection, METH_NOARGS, (PY_METHODCHAR)GetDirection_doc},
603         {"setOption", (PyCFunction) KX_ConstraintActuator::sPySetOption, METH_VARARGS, (PY_METHODCHAR)SetOption_doc},
604         {"getOption", (PyCFunction) KX_ConstraintActuator::sPyGetOption, METH_NOARGS, (PY_METHODCHAR)GetOption_doc},
605         {"setTime", (PyCFunction) KX_ConstraintActuator::sPySetTime, METH_VARARGS, (PY_METHODCHAR)SetTime_doc},
606         {"getTime", (PyCFunction) KX_ConstraintActuator::sPyGetTime, METH_NOARGS, (PY_METHODCHAR)GetTime_doc},
607         {"setProperty", (PyCFunction) KX_ConstraintActuator::sPySetProperty, METH_VARARGS, (PY_METHODCHAR)SetProperty_doc},
608         {"getProperty", (PyCFunction) KX_ConstraintActuator::sPyGetProperty, METH_NOARGS, (PY_METHODCHAR)GetProperty_doc},
609         {"setMin", (PyCFunction) KX_ConstraintActuator::sPySetMin, METH_VARARGS, (PY_METHODCHAR)SetMin_doc},
610         {"getMin", (PyCFunction) KX_ConstraintActuator::sPyGetMin, METH_NOARGS, (PY_METHODCHAR)GetMin_doc},
611         {"setDistance", (PyCFunction) KX_ConstraintActuator::sPySetMin, METH_VARARGS, (PY_METHODCHAR)SetDistance_doc},
612         {"getDistance", (PyCFunction) KX_ConstraintActuator::sPyGetMin, METH_NOARGS, (PY_METHODCHAR)GetDistance_doc},
613         {"setMax", (PyCFunction) KX_ConstraintActuator::sPySetMax, METH_VARARGS, (PY_METHODCHAR)SetMax_doc},
614         {"getMax", (PyCFunction) KX_ConstraintActuator::sPyGetMax, METH_NOARGS, (PY_METHODCHAR)GetMax_doc},
615         {"setRayLength", (PyCFunction) KX_ConstraintActuator::sPySetMax, METH_VARARGS, (PY_METHODCHAR)SetRayLength_doc},
616         {"getRayLength", (PyCFunction) KX_ConstraintActuator::sPyGetMax, METH_NOARGS, (PY_METHODCHAR)GetRayLength_doc},
617         {"setLimit", (PyCFunction) KX_ConstraintActuator::sPySetLimit, METH_VARARGS, (PY_METHODCHAR)SetLimit_doc},
618         {"getLimit", (PyCFunction) KX_ConstraintActuator::sPyGetLimit, METH_NOARGS, (PY_METHODCHAR)GetLimit_doc},
619         // <--
620         {NULL,NULL} //Sentinel
621 };
622
623 PyAttributeDef KX_ConstraintActuator::Attributes[] = {
624         KX_PYATTRIBUTE_INT_RW("damp",0,100,true,KX_ConstraintActuator,m_posDampTime),
625         KX_PYATTRIBUTE_INT_RW("rotDamp",0,100,true,KX_ConstraintActuator,m_rotDampTime),
626         KX_PYATTRIBUTE_FLOAT_ARRAY_RW_CHECK("direction",-FLT_MAX,FLT_MAX,KX_ConstraintActuator,m_refDirection,3,pyattr_check_direction),
627         KX_PYATTRIBUTE_INT_RW("option",0,0xFFFF,false,KX_ConstraintActuator,m_option),
628         KX_PYATTRIBUTE_INT_RW("time",0,1000,true,KX_ConstraintActuator,m_activeTime),
629         KX_PYATTRIBUTE_STRING_RW("propName",0,32,true,KX_ConstraintActuator,m_property),
630         KX_PYATTRIBUTE_FLOAT_RW("min",-FLT_MAX,FLT_MAX,KX_ConstraintActuator,m_minimumBound),
631         KX_PYATTRIBUTE_FLOAT_RW("distance",-FLT_MAX,FLT_MAX,KX_ConstraintActuator,m_minimumBound),
632         KX_PYATTRIBUTE_FLOAT_RW("max",-FLT_MAX,FLT_MAX,KX_ConstraintActuator,m_maximumBound),
633         KX_PYATTRIBUTE_FLOAT_RW("rayLength",0,2000.f,KX_ConstraintActuator,m_maximumBound),
634         KX_PYATTRIBUTE_INT_RW("limit",KX_ConstraintActuator::KX_ACT_CONSTRAINT_NODEF+1,KX_ConstraintActuator::KX_ACT_CONSTRAINT_MAX-1,false,KX_ConstraintActuator,m_locrot),
635         { NULL }        //Sentinel
636 };
637
638 int KX_ConstraintActuator::pyattr_check_direction(void *self, const struct KX_PYATTRIBUTE_DEF *attrdef)
639 {
640         KX_ConstraintActuator* act = static_cast<KX_ConstraintActuator*>(self);
641         MT_Vector3 dir(act->m_refDirection);
642         MT_Scalar len = dir.length();
643         if (MT_fuzzyZero(len)) {
644                 PyErr_SetString(PyExc_ValueError, "actuator.direction = vec: KX_ConstraintActuator, invalid direction");
645                 return 1;
646         }
647         act->m_refDirVector = dir/len;
648         return 0;       
649 }
650
651 /* 2. setDamp                                                                */
652 const char KX_ConstraintActuator::SetDamp_doc[] = 
653 "setDamp(duration)\n"
654 "\t- duration: integer\n"
655 "\tSets the time constant of the orientation and distance constraint.\n"
656 "\tIf the duration is negative, it is set to 0.\n";
657 PyObject* KX_ConstraintActuator::PySetDamp(PyObject* args) {
658         ShowDeprecationWarning("setDamp()", "the damp property");
659         int dampArg;
660         if(!PyArg_ParseTuple(args, "i:setDamp", &dampArg)) {
661                 return NULL;            
662         }
663         
664         m_posDampTime = dampArg;
665         if (m_posDampTime < 0) m_posDampTime = 0;
666
667         Py_RETURN_NONE;
668 }
669 /* 3. getDamp                                                                */
670 const char KX_ConstraintActuator::GetDamp_doc[] = 
671 "getDamp()\n"
672 "\tReturns the damping parameter.\n";
673 PyObject* KX_ConstraintActuator::PyGetDamp(){
674         ShowDeprecationWarning("getDamp()", "the damp property");
675         return PyLong_FromSsize_t(m_posDampTime);
676 }
677
678 /* 2. setRotDamp                                                                */
679 const char KX_ConstraintActuator::SetRotDamp_doc[] = 
680 "setRotDamp(duration)\n"
681 "\t- duration: integer\n"
682 "\tSets the time constant of the orientation constraint.\n"
683 "\tIf the duration is negative, it is set to 0.\n";
684 PyObject* KX_ConstraintActuator::PySetRotDamp(PyObject* args) {
685         ShowDeprecationWarning("setRotDamp()", "the rotDamp property");
686         int dampArg;
687         if(!PyArg_ParseTuple(args, "i:setRotDamp", &dampArg)) {
688                 return NULL;            
689         }
690         
691         m_rotDampTime = dampArg;
692         if (m_rotDampTime < 0) m_rotDampTime = 0;
693
694         Py_RETURN_NONE;
695 }
696 /* 3. getRotDamp                                                                */
697 const char KX_ConstraintActuator::GetRotDamp_doc[] = 
698 "getRotDamp()\n"
699 "\tReturns the damping time for application of the constraint.\n";
700 PyObject* KX_ConstraintActuator::PyGetRotDamp(){
701         ShowDeprecationWarning("getRotDamp()", "the rotDamp property");
702         return PyLong_FromSsize_t(m_rotDampTime);
703 }
704
705 /* 2. setDirection                                                                */
706 const char KX_ConstraintActuator::SetDirection_doc[] = 
707 "setDirection(vector)\n"
708 "\t- vector: 3-tuple\n"
709 "\tSets the reference direction in world coordinate for the orientation constraint.\n";
710 PyObject* KX_ConstraintActuator::PySetDirection(PyObject* args) {
711         ShowDeprecationWarning("setDirection()", "the direction property");
712         float x, y, z;
713         MT_Scalar len;
714         MT_Vector3 dir;
715
716         if(!PyArg_ParseTuple(args, "(fff):setDirection", &x, &y, &z)) {
717                 return NULL;            
718         }
719         dir[0] = x;
720         dir[1] = y;
721         dir[2] = z;
722         len = dir.length();
723         if (MT_fuzzyZero(len)) {
724                 std::cout << "Invalid direction" << std::endl;
725                 return NULL;
726         }
727         m_refDirVector = dir/len;
728         m_refDirection[0] = x/len;
729         m_refDirection[1] = y/len;
730         m_refDirection[2] = z/len;
731
732         Py_RETURN_NONE;
733 }
734 /* 3. getDirection                                                                */
735 const char KX_ConstraintActuator::GetDirection_doc[] = 
736 "getDirection()\n"
737 "\tReturns the reference direction of the orientation constraint as a 3-tuple.\n";
738 PyObject* KX_ConstraintActuator::PyGetDirection(){
739         ShowDeprecationWarning("getDirection()", "the direction property");
740         PyObject *retVal = PyList_New(3);
741
742         PyList_SET_ITEM(retVal, 0, PyFloat_FromDouble(m_refDirection[0]));
743         PyList_SET_ITEM(retVal, 1, PyFloat_FromDouble(m_refDirection[1]));
744         PyList_SET_ITEM(retVal, 2, PyFloat_FromDouble(m_refDirection[2]));
745         return retVal;
746 }
747
748 /* 2. setOption                                                                */
749 const char KX_ConstraintActuator::SetOption_doc[] = 
750 "setOption(option)\n"
751 "\t- option: integer\n"
752 "\tSets several options of the distance  constraint.\n"
753 "\tBinary combination of the following values:\n"
754 "\t\t 64 : Activate alignment to surface\n"
755 "\t\t128 : Detect material rather than property\n"
756 "\t\t256 : No deactivation if ray does not hit target\n"
757 "\t\t512 : Activate distance control\n";
758 PyObject* KX_ConstraintActuator::PySetOption(PyObject* args) {
759         ShowDeprecationWarning("setOption()", "the option property");
760         int option;
761         if(!PyArg_ParseTuple(args, "i:setOption", &option)) {
762                 return NULL;            
763         }
764         
765         m_option = option;
766
767         Py_RETURN_NONE;
768 }
769 /* 3. getOption                                                              */
770 const char KX_ConstraintActuator::GetOption_doc[] = 
771 "getOption()\n"
772 "\tReturns the option parameter.\n";
773 PyObject* KX_ConstraintActuator::PyGetOption(){
774         ShowDeprecationWarning("getOption()", "the option property");
775         return PyLong_FromSsize_t(m_option);
776 }
777
778 /* 2. setTime                                                                */
779 const char KX_ConstraintActuator::SetTime_doc[] = 
780 "setTime(duration)\n"
781 "\t- duration: integer\n"
782 "\tSets the activation time of the actuator.\n"
783 "\tThe actuator disables itself after this many frame.\n"
784 "\tIf set to 0 or negative, the actuator is not limited in time.\n";
785 PyObject* KX_ConstraintActuator::PySetTime(PyObject* args) {
786         ShowDeprecationWarning("setTime()", "the time property");
787         int t;
788         if(!PyArg_ParseTuple(args, "i:setTime", &t)) {
789                 return NULL;            
790         }
791         
792         if (t < 0)
793                 t = 0;
794         m_activeTime = t;
795
796         Py_RETURN_NONE;
797 }
798 /* 3. getTime                                                                */
799 const char KX_ConstraintActuator::GetTime_doc[] = 
800 "getTime()\n"
801 "\tReturns the time parameter.\n";
802 PyObject* KX_ConstraintActuator::PyGetTime(){
803         ShowDeprecationWarning("getTime()", "the time property");
804         return PyLong_FromSsize_t(m_activeTime);
805 }
806
807 /* 2. setProperty                                                                */
808 const char KX_ConstraintActuator::SetProperty_doc[] = 
809 "setProperty(property)\n"
810 "\t- property: string\n"
811 "\tSets the name of the property or material for the ray detection of the distance constraint.\n"
812 "\tIf empty, the ray will detect any collisioning object.\n";
813 PyObject* KX_ConstraintActuator::PySetProperty(PyObject* args) {
814         ShowDeprecationWarning("setProperty()", "the 'property' property");
815         char *property;
816         if (!PyArg_ParseTuple(args, "s:setProperty", &property)) {
817                 return NULL;
818         }
819         if (property == NULL) {
820                 m_property = "";
821         } else {
822                 m_property = property;
823         }
824
825         Py_RETURN_NONE;
826 }
827 /* 3. getProperty                                                                */
828 const char KX_ConstraintActuator::GetProperty_doc[] = 
829 "getProperty()\n"
830 "\tReturns the property parameter.\n";
831 PyObject* KX_ConstraintActuator::PyGetProperty(){
832         ShowDeprecationWarning("getProperty()", "the 'property' property");
833         return PyUnicode_FromString(m_property.Ptr());
834 }
835
836 /* 4. setDistance                                                                 */
837 const char KX_ConstraintActuator::SetDistance_doc[] = 
838 "setDistance(distance)\n"
839 "\t- distance: float\n"
840 "\tSets the target distance in distance constraint\n";
841 /* 4. setMin                                                                 */
842 const char KX_ConstraintActuator::SetMin_doc[] = 
843 "setMin(lower_bound)\n"
844 "\t- lower_bound: float\n"
845 "\tSets the lower value of the interval to which the value\n"
846 "\tis clipped.\n";
847 PyObject* KX_ConstraintActuator::PySetMin(PyObject* args) {
848         ShowDeprecationWarning("setMin() or setDistance()", "the min or distance property");
849         float minArg;
850         if(!PyArg_ParseTuple(args, "f:setMin", &minArg)) {
851                 return NULL;            
852         }
853
854         switch (m_locrot) {
855         default:
856                 m_minimumBound = minArg;
857                 break;
858         case KX_ACT_CONSTRAINT_ROTX:
859         case KX_ACT_CONSTRAINT_ROTY:
860         case KX_ACT_CONSTRAINT_ROTZ:
861                 m_minimumBound = MT_radians(minArg);
862                 break;
863         }
864
865         Py_RETURN_NONE;
866 }
867 /* 5. getDistance                                                                 */
868 const char KX_ConstraintActuator::GetDistance_doc[] = 
869 "getDistance()\n"
870 "\tReturns the distance parameter \n";
871 /* 5. getMin                                                                 */
872 const char KX_ConstraintActuator::GetMin_doc[] = 
873 "getMin()\n"
874 "\tReturns the lower value of the interval to which the value\n"
875 "\tis clipped.\n";
876 PyObject* KX_ConstraintActuator::PyGetMin() {
877         ShowDeprecationWarning("getMin() or getDistance()", "the min or distance property");
878         return PyFloat_FromDouble(m_minimumBound);
879 }
880
881 /* 6. setRayLength                                                                 */
882 const char KX_ConstraintActuator::SetRayLength_doc[] = 
883 "setRayLength(length)\n"
884 "\t- length: float\n"
885 "\tSets the maximum ray length of the distance constraint\n";
886 /* 6. setMax                                                                 */
887 const char KX_ConstraintActuator::SetMax_doc[] = 
888 "setMax(upper_bound)\n"
889 "\t- upper_bound: float\n"
890 "\tSets the upper value of the interval to which the value\n"
891 "\tis clipped.\n";
892 PyObject* KX_ConstraintActuator::PySetMax(PyObject* args){
893         ShowDeprecationWarning("setMax() or setRayLength()", "the max or rayLength property");
894         float maxArg;
895         if(!PyArg_ParseTuple(args, "f:setMax", &maxArg)) {
896                 return NULL;            
897         }
898
899         switch (m_locrot) {
900         default:
901                 m_maximumBound = maxArg;
902                 break;
903         case KX_ACT_CONSTRAINT_ROTX:
904         case KX_ACT_CONSTRAINT_ROTY:
905         case KX_ACT_CONSTRAINT_ROTZ:
906                 m_maximumBound = MT_radians(maxArg);
907                 break;
908         }
909
910         Py_RETURN_NONE;
911 }
912 /* 7. getRayLength                                                                 */
913 const char KX_ConstraintActuator::GetRayLength_doc[] = 
914 "getRayLength()\n"
915 "\tReturns the length of the ray\n";
916 /* 7. getMax                                                                 */
917 const char KX_ConstraintActuator::GetMax_doc[] = 
918 "getMax()\n"
919 "\tReturns the upper value of the interval to which the value\n"
920 "\tis clipped.\n";
921 PyObject* KX_ConstraintActuator::PyGetMax() {
922         ShowDeprecationWarning("getMax() or getRayLength()", "the max or rayLength property");
923         return PyFloat_FromDouble(m_maximumBound);
924 }
925
926
927 /* This setter/getter probably for the constraint type                       */
928 /* 8. setLimit                                                               */
929 const char KX_ConstraintActuator::SetLimit_doc[] = 
930 "setLimit(type)\n"
931 "\t- type: integer\n"
932 "\t  1  : LocX\n"
933 "\t  2  : LocY\n"
934 "\t  3  : LocZ\n"
935 "\t  7  : Distance along +X axis\n"
936 "\t  8  : Distance along +Y axis\n"
937 "\t  9  : Distance along +Z axis\n"
938 "\t  10 : Distance along -X axis\n"
939 "\t  11 : Distance along -Y axis\n"
940 "\t  12 : Distance along -Z axis\n"
941 "\t  13 : Align X axis\n"
942 "\t  14 : Align Y axis\n"
943 "\t  15 : Align Z axis\n"
944 "\tSets the type of constraint.\n";
945 PyObject* KX_ConstraintActuator::PySetLimit(PyObject* args) {
946         ShowDeprecationWarning("setLimit()", "the limit property");
947         int locrotArg;
948         if(!PyArg_ParseTuple(args, "i:setLimit", &locrotArg)) {
949                 return NULL;            
950         }
951         
952         if (IsValidMode((KX_CONSTRAINTTYPE)locrotArg)) m_locrot = locrotArg;
953
954         Py_RETURN_NONE;
955 }
956 /* 9. getLimit                                                               */
957 const char KX_ConstraintActuator::GetLimit_doc[] = 
958 "getLimit()\n"
959 "\tReturns the type of constraint.\n";
960 PyObject* KX_ConstraintActuator::PyGetLimit() {
961         ShowDeprecationWarning("setLimit()", "the limit property");
962         return PyLong_FromSsize_t(m_locrot);
963 }
964
965 /* eof */