2 * KX_CameraActuator.cpp
6 * ***** BEGIN GPL LICENSE BLOCK *****
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.
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.
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.
22 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
23 * All rights reserved.
25 * The Original Code is: all of this file.
27 * Contributor(s): none yet.
29 * ***** END GPL LICENSE BLOCK *****
33 #include "KX_CameraActuator.h"
36 #include "KX_GameObject.h"
38 STR_String KX_CameraActuator::X_AXIS_STRING = "x";
39 STR_String KX_CameraActuator::Y_AXIS_STRING = "y";
46 /* ------------------------------------------------------------------------- */
47 /* Native functions */
48 /* ------------------------------------------------------------------------- */
50 KX_CameraActuator::KX_CameraActuator(
59 SCA_IActuator(gameobj, T),
62 m_minHeight (minhght),
63 m_maxHeight (maxhght),
67 m_ob->RegisterActuator(this);
70 KX_CameraActuator::~KX_CameraActuator()
73 m_ob->UnregisterActuator(this);
80 KX_CameraActuator* replica = new KX_CameraActuator(*this);
81 replica->ProcessReplica();
82 // this will copy properties and so on...
83 CValue::AddDataToReplica(replica);
87 void KX_CameraActuator::ProcessReplica()
90 m_ob->RegisterActuator(this);
91 SCA_IActuator::ProcessReplica();
94 bool KX_CameraActuator::UnlinkObject(SCA_IObject* clientobj)
96 if (clientobj == m_ob)
98 // this object is being deleted, we cannot continue to track it.
106 void KX_CameraActuator::Relink(GEN_Map<GEN_HashedPtr, void*> *obj_map)
108 void **h_obj = (*obj_map)[m_ob];
111 m_ob->UnregisterActuator(this);
112 m_ob = (SCA_IObject*)(*h_obj);
113 m_ob->RegisterActuator(this);
117 /* three functions copied from blender arith... don't know if there's an equivalent */
119 static float Kx_Normalize(float *n)
123 d= n[0]*n[0]+n[1]*n[1]+n[2]*n[2];
124 /* FLT_EPSILON is too large! A larger value causes normalize errors in a scaled down utah teapot */
125 if(d>0.0000000000001) {
138 static void Kx_Crossf(float *c, float *a, float *b)
140 c[0] = a[1] * b[2] - a[2] * b[1];
141 c[1] = a[2] * b[0] - a[0] * b[2];
142 c[2] = a[0] * b[1] - a[1] * b[0];
146 static void Kx_VecUpMat3(float *vec, float mat[][3], short axis)
149 // Construct a camera matrix s.t. the specified axis
151 // maps to the given vector (*vec). Also defines the rotation
153 // about this axis by mapping one of the other axis to the y-axis.
157 short cox = 0, coy = 0, coz = 0;
159 /* up varieeren heeft geen zin, is eigenlijk helemaal geen up!
164 cox= 0; coy= 1; coz= 2; /* Y up Z tr */
167 cox= 1; coy= 2; coz= 0; /* Z up X tr */
170 cox= 2; coy= 0; coz= 1; /* X up Y tr */
173 cox= 0; coy= 1; coz= 2; /* Y op -Z tr */
179 cox= 1; coy= 0; coz= 2; /* */
182 cox= 2; coy= 1; coz= 0; /* Y up X tr */
188 if (Kx_Normalize((float *)mat[coz]) == 0.f) {
189 /* this is a very abnormal situation: the camera has reach the object center exactly
190 We will choose a completely arbitrary direction */
197 mat[coy][0]= - inp*mat[coz][0];
198 mat[coy][1]= - inp*mat[coz][1];
199 mat[coy][2]= 1.0 - inp*mat[coz][2];
201 if (Kx_Normalize((float *)mat[coy]) == 0.f) {
202 /* the camera is vertical, chose the y axis arbitrary */
208 Kx_Crossf(mat[cox], mat[coy], mat[coz]);
212 bool KX_CameraActuator::Update(double curtime, bool frame)
214 /* wondering... is it really neccesary/desirable to suppress negative */
216 bool bNegativeEvent = IsNegativeEvent();
219 if (bNegativeEvent || !m_ob)
222 KX_GameObject *obj = (KX_GameObject*) GetParent();
223 MT_Point3 from = obj->NodeGetWorldPosition();
224 MT_Matrix3x3 frommat = obj->NodeGetWorldOrientation();
225 /* These casts are _very_ dangerous!!! */
226 MT_Point3 lookat = ((KX_GameObject*)m_ob)->NodeGetWorldPosition();
227 MT_Matrix3x3 actormat = ((KX_GameObject*)m_ob)->NodeGetWorldOrientation();
229 float fp1[3], fp2[3], rc[3];
230 float inp, fac; //, factor = 0.0; /* some factor... */
231 float mindistsq, maxdistsq, distsq;
235 /* CONSTRAINT 1: not implemented */
236 /* CONSTRAINT 2: can camera see actor? */
237 /* CONSTRAINT 3: fixed height relative to floor below actor. */
238 /* CONSTRAINT 4: camera rotates behind actor */
239 /* CONSTRAINT 5: minimum / maximum distance */
240 /* CONSTRAINT 6: again: fixed height relative to floor below actor */
241 /* CONSTRAINT 7: track to floor below actor */
242 /* CONSTRAINT 8: look a little bit left or right, depending on how the
244 character is looking (horizontal x)
247 /* ...and then set the camera position. Since we assume the parent of */
248 /* this actuator is always a camera, just set the parent position and */
249 /* rotation. We do not check whether we really have a camera as parent. */
250 /* It may be better to turn this into a general tracking actuator later */
251 /* on, since lots of plausible relations can be filled in here. */
253 /* ... set up some parameters ... */
254 /* missing here: the 'floorloc' of the actor's shadow */
256 mindistsq= m_minHeight*m_minHeight;
257 maxdistsq= m_maxHeight*m_maxHeight;
259 /* C1: not checked... is a future option */
261 /* C2: blender test_visibility function. Can this be a ray-test? */
263 /* C3: fixed height */
264 from[2] = (15.0*from[2] + lookat[2] + m_height)/16.0;
267 /* C4: camera behind actor */
269 fp1[0] = actormat[0][0];
270 fp1[1] = actormat[1][0];
271 fp1[2] = actormat[2][0];
273 fp2[0] = frommat[0][0];
274 fp2[1] = frommat[1][0];
275 fp2[2] = frommat[2][0];
278 fp1[0] = actormat[0][1];
279 fp1[1] = actormat[1][1];
280 fp1[2] = actormat[2][1];
282 fp2[0] = frommat[0][1];
283 fp2[1] = frommat[1][1];
284 fp2[2] = frommat[2][1];
287 inp= fp1[0]*fp2[0] + fp1[1]*fp2[1] + fp1[2]*fp2[2];
288 fac= (-1.0 + inp)/32.0;
290 from[0]+= fac*fp1[0];
291 from[1]+= fac*fp1[1];
292 from[2]+= fac*fp1[2];
294 /* alleen alstie ervoor ligt: cross testen en loodrechte bijtellen */
296 if(fp1[0]*fp2[1] - fp1[1]*fp2[0] > 0.0) {
297 from[0]-= fac*fp1[1];
298 from[1]+= fac*fp1[0];
301 from[0]+= fac*fp1[1];
302 from[1]-= fac*fp1[0];
306 /* CONSTRAINT 5: minimum / maximum afstand */
308 rc[0]= (lookat[0]-from[0]);
309 rc[1]= (lookat[1]-from[1]);
310 rc[2]= (lookat[2]-from[2]);
311 distsq= rc[0]*rc[0] + rc[1]*rc[1] + rc[2]*rc[2];
313 if(distsq > maxdistsq) {
314 distsq = 0.15*(distsq-maxdistsq)/distsq;
316 from[0] += distsq*rc[0];
317 from[1] += distsq*rc[1];
318 from[2] += distsq*rc[2];
320 else if(distsq < mindistsq) {
321 distsq = 0.15*(mindistsq-distsq)/mindistsq;
323 from[0] -= distsq*rc[0];
324 from[1] -= distsq*rc[1];
325 from[2] -= distsq*rc[2];
329 /* CONSTRAINT 7: track to schaduw */
330 rc[0]= (lookat[0]-from[0]);
331 rc[1]= (lookat[1]-from[1]);
332 rc[2]= (lookat[2]-from[2]);
333 Kx_VecUpMat3(rc, mat, 3); /* y up Track -z */
338 /* now set the camera position and rotation */
340 obj->NodeSetLocalPosition(from);
342 actormat[0][0]= mat[0][0]; actormat[0][1]= mat[1][0]; actormat[0][2]= mat[2][0];
343 actormat[1][0]= mat[0][1]; actormat[1][1]= mat[1][1]; actormat[1][2]= mat[2][1];
344 actormat[2][0]= mat[0][2]; actormat[2][1]= mat[1][2]; actormat[2][2]= mat[2][2];
345 obj->NodeSetLocalOrientation(actormat);
350 CValue *KX_CameraActuator::findObject(char *obName)
352 /* hook to object system */
356 bool KX_CameraActuator::string2axischoice(const char *axisString)
360 res = !(axisString == Y_AXIS_STRING);
365 /* ------------------------------------------------------------------------- */
366 /* Python functions */
367 /* ------------------------------------------------------------------------- */
369 /* Integration hooks ------------------------------------------------------- */
370 PyTypeObject KX_CameraActuator::Type = {
371 PyObject_HEAD_INIT(&PyType_Type)
374 sizeof(KX_CameraActuator),
382 0, //&cvalue_as_number,
389 PyParentObject KX_CameraActuator::Parents[] = {
390 &KX_CameraActuator::Type,
391 &SCA_IActuator::Type,
392 &SCA_ILogicBrick::Type,
397 PyMethodDef KX_CameraActuator::Methods[] = {
398 {"setObject",(PyCFunction) KX_CameraActuator::sPySetObject, METH_O, SetObject_doc},
399 {"getObject",(PyCFunction) KX_CameraActuator::sPyGetObject, METH_VARARGS, GetObject_doc},
400 {"setMin" ,(PyCFunction) KX_CameraActuator::sPySetMin, METH_VARARGS, SetMin_doc},
401 {"getMin" ,(PyCFunction) KX_CameraActuator::sPyGetMin, METH_NOARGS, GetMin_doc},
402 {"setMax" ,(PyCFunction) KX_CameraActuator::sPySetMax, METH_VARARGS, SetMax_doc},
403 {"getMax" ,(PyCFunction) KX_CameraActuator::sPyGetMax, METH_NOARGS, GetMax_doc},
404 {"setHeight",(PyCFunction) KX_CameraActuator::sPySetHeight, METH_VARARGS, SetHeight_doc},
405 {"getHeight",(PyCFunction) KX_CameraActuator::sPyGetHeight, METH_NOARGS, GetHeight_doc},
406 {"setXY" ,(PyCFunction) KX_CameraActuator::sPySetXY, METH_VARARGS, SetXY_doc},
407 {"getXY" ,(PyCFunction) KX_CameraActuator::sPyGetXY, METH_VARARGS, GetXY_doc},
408 {NULL,NULL,NULL,NULL} //Sentinel
411 PyObject* KX_CameraActuator::_getattr(const STR_String& attr) {
412 _getattr_up(SCA_IActuator);
414 /* get obj ---------------------------------------------------------- */
415 char KX_CameraActuator::GetObject_doc[] =
416 "getObject(name_only = 1)\n"
417 "name_only - optional arg, when true will return the KX_GameObject rather then its name\n"
418 "\tReturns the object this sensor reacts to.\n";
419 PyObject* KX_CameraActuator::PyGetObject(PyObject* self, PyObject* args)
421 int ret_name_only = 1;
422 if (!PyArg_ParseTuple(args, "|i", &ret_name_only))
429 return PyString_FromString(m_ob->GetName());
431 return m_ob->AddRef();
433 /* set obj ---------------------------------------------------------- */
434 char KX_CameraActuator::SetObject_doc[] =
435 "setObject(object)\n"
436 "\t- object: KX_GameObject, string or None\n"
437 "\tSets the object this sensor reacts to.\n";
438 PyObject* KX_CameraActuator::PySetObject(PyObject* self, PyObject* value)
440 KX_GameObject *gameobj;
442 if (!ConvertPythonToGameObject(value, &gameobj, true))
443 return NULL; // ConvertPythonToGameObject sets the error
446 m_ob->UnregisterActuator(this);
448 m_ob = (SCA_IObject*)gameobj;
450 m_ob->RegisterActuator(this);
455 /* get min ---------------------------------------------------------- */
456 char KX_CameraActuator::GetMin_doc[] =
458 "\tReturns the minimum value set in the Min: field.\n";
459 PyObject* KX_CameraActuator::PyGetMin(PyObject* self,
463 return PyFloat_FromDouble(m_minHeight);
465 /* set min ---------------------------------------------------------- */
466 char KX_CameraActuator::SetMin_doc[] =
468 "\tSets the minimum value.\n";
469 PyObject* KX_CameraActuator::PySetMin(PyObject* self,
474 if(PyArg_ParseTuple(args,"f", &min))
481 /* get min ---------------------------------------------------------- */
482 char KX_CameraActuator::GetMax_doc[] =
484 "\tReturns the maximum value set in the Max: field.\n";
485 PyObject* KX_CameraActuator::PyGetMax(PyObject* self,
489 return PyFloat_FromDouble(m_maxHeight);
491 /* set min ---------------------------------------------------------- */
492 char KX_CameraActuator::SetMax_doc[] =
494 "\tSets the maximum value.\n";
495 PyObject* KX_CameraActuator::PySetMax(PyObject* self,
500 if(PyArg_ParseTuple(args,"f", &max))
507 /* get height ---------------------------------------------------------- */
508 char KX_CameraActuator::GetHeight_doc[] =
510 "\tReturns the height value set in the height: field.\n";
511 PyObject* KX_CameraActuator::PyGetHeight(PyObject* self,
515 return PyFloat_FromDouble(m_height);
517 /* set height ---------------------------------------------------------- */
518 char KX_CameraActuator::SetHeight_doc[] =
520 "\tSets the height value.\n";
521 PyObject* KX_CameraActuator::PySetHeight(PyObject* self,
526 if(PyArg_ParseTuple(args,"f", &height))
533 /* set XY ---------------------------------------------------------- */
534 char KX_CameraActuator::SetXY_doc[] =
536 "\tSets axis the camera tries to get behind.\n"
538 PyObject* KX_CameraActuator::PySetXY(PyObject* self,
543 if(PyArg_ParseTuple(args,"i", &value))
551 /* get XY -------------------------------------------------------------*/
552 char KX_CameraActuator::GetXY_doc[] =
554 "\tGets the axis the camera tries to get behind.\n"
555 "\tTrue = X, False = Y\n";
556 PyObject* KX_CameraActuator::PyGetXY(PyObject* self,
560 return PyInt_FromLong(m_x);