1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
|
/**
* KX_CameraActuator.cpp
*
* $Id$
*
* ***** BEGIN GPL LICENSE BLOCK *****
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
* The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
* All rights reserved.
*
* The Original Code is: all of this file.
*
* Contributor(s): none yet.
*
* ***** END GPL LICENSE BLOCK *****
*
*/
#include "KX_CameraActuator.h"
#include <iostream>
#include <math.h>
#include "KX_GameObject.h"
#include "PyObjectPlus.h"
#include "blendef.h"
STR_String KX_CameraActuator::X_AXIS_STRING = "x";
STR_String KX_CameraActuator::Y_AXIS_STRING = "y";
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
/* ------------------------------------------------------------------------- */
/* Native functions */
/* ------------------------------------------------------------------------- */
KX_CameraActuator::KX_CameraActuator(
SCA_IObject* gameobj,
SCA_IObject *obj,
float hght,
float minhght,
float maxhght,
bool xytog,
PyTypeObject* T
):
SCA_IActuator(gameobj, T),
m_ob (obj),
m_height (hght),
m_minHeight (minhght),
m_maxHeight (maxhght),
m_x (xytog)
{
if (m_ob)
m_ob->RegisterActuator(this);
}
KX_CameraActuator::~KX_CameraActuator()
{
if (m_ob)
m_ob->UnregisterActuator(this);
}
CValue*
KX_CameraActuator::
GetReplica(
) {
KX_CameraActuator* replica = new KX_CameraActuator(*this);
replica->ProcessReplica();
return replica;
};
void KX_CameraActuator::ProcessReplica()
{
if (m_ob)
m_ob->RegisterActuator(this);
SCA_IActuator::ProcessReplica();
}
bool KX_CameraActuator::UnlinkObject(SCA_IObject* clientobj)
{
if (clientobj == m_ob)
{
// this object is being deleted, we cannot continue to track it.
m_ob = NULL;
return true;
}
return false;
}
void KX_CameraActuator::Relink(GEN_Map<GEN_HashedPtr, void*> *obj_map)
{
void **h_obj = (*obj_map)[m_ob];
if (h_obj) {
if (m_ob)
m_ob->UnregisterActuator(this);
m_ob = (SCA_IObject*)(*h_obj);
m_ob->RegisterActuator(this);
}
}
/* three functions copied from blender arith... don't know if there's an equivalent */
static float Kx_Normalize(float *n)
{
float d;
d= n[0]*n[0]+n[1]*n[1]+n[2]*n[2];
/* FLT_EPSILON is too large! A larger value causes normalize errors in a scaled down utah teapot */
if(d>0.0000000000001) {
d= sqrt(d);
n[0]/=d;
n[1]/=d;
n[2]/=d;
} else {
n[0]=n[1]=n[2]= 0.0;
d= 0.0;
}
return d;
}
static void Kx_Crossf(float *c, float *a, float *b)
{
c[0] = a[1] * b[2] - a[2] * b[1];
c[1] = a[2] * b[0] - a[0] * b[2];
c[2] = a[0] * b[1] - a[1] * b[0];
}
static void Kx_VecUpMat3(float *vec, float mat[][3], short axis)
{
// Construct a camera matrix s.t. the specified axis
// maps to the given vector (*vec). Also defines the rotation
// about this axis by mapping one of the other axis to the y-axis.
float inp;
short cox = 0, coy = 0, coz = 0;
/* up varieeren heeft geen zin, is eigenlijk helemaal geen up!
* zie VecUpMat3old
*/
if(axis==0) {
cox= 0; coy= 1; coz= 2; /* Y up Z tr */
}
if(axis==1) {
cox= 1; coy= 2; coz= 0; /* Z up X tr */
}
if(axis==2) {
cox= 2; coy= 0; coz= 1; /* X up Y tr */
}
if(axis==3) {
cox= 0; coy= 1; coz= 2; /* Y op -Z tr */
vec[0]= -vec[0];
vec[1]= -vec[1];
vec[2]= -vec[2];
}
if(axis==4) {
cox= 1; coy= 0; coz= 2; /* */
}
if(axis==5) {
cox= 2; coy= 1; coz= 0; /* Y up X tr */
}
mat[coz][0]= vec[0];
mat[coz][1]= vec[1];
mat[coz][2]= vec[2];
if (Kx_Normalize((float *)mat[coz]) == 0.f) {
/* this is a very abnormal situation: the camera has reach the object center exactly
We will choose a completely arbitrary direction */
mat[coz][0] = 1.0f;
mat[coz][1] = 0.0f;
mat[coz][2] = 0.0f;
}
inp= mat[coz][2];
mat[coy][0]= - inp*mat[coz][0];
mat[coy][1]= - inp*mat[coz][1];
mat[coy][2]= 1.0 - inp*mat[coz][2];
if (Kx_Normalize((float *)mat[coy]) == 0.f) {
/* the camera is vertical, chose the y axis arbitrary */
mat[coy][0] = 0.f;
mat[coy][1] = 1.f;
mat[coy][2] = 0.f;
}
Kx_Crossf(mat[cox], mat[coy], mat[coz]);
}
bool KX_CameraActuator::Update(double curtime, bool frame)
{
/* wondering... is it really neccesary/desirable to suppress negative */
/* events here? */
bool bNegativeEvent = IsNegativeEvent();
RemoveAllEvents();
if (bNegativeEvent || !m_ob)
return false;
KX_GameObject *obj = (KX_GameObject*) GetParent();
MT_Point3 from = obj->NodeGetWorldPosition();
MT_Matrix3x3 frommat = obj->NodeGetWorldOrientation();
/* These casts are _very_ dangerous!!! */
MT_Point3 lookat = ((KX_GameObject*)m_ob)->NodeGetWorldPosition();
MT_Matrix3x3 actormat = ((KX_GameObject*)m_ob)->NodeGetWorldOrientation();
float fp1[3], fp2[3], rc[3];
float inp, fac; //, factor = 0.0; /* some factor... */
float mindistsq, maxdistsq, distsq;
float mat[3][3];
/* The rules: */
/* CONSTRAINT 1: not implemented */
/* CONSTRAINT 2: can camera see actor? */
/* CONSTRAINT 3: fixed height relative to floor below actor. */
/* CONSTRAINT 4: camera rotates behind actor */
/* CONSTRAINT 5: minimum / maximum distance */
/* CONSTRAINT 6: again: fixed height relative to floor below actor */
/* CONSTRAINT 7: track to floor below actor */
/* CONSTRAINT 8: look a little bit left or right, depending on how the
character is looking (horizontal x)
*/
/* ...and then set the camera position. Since we assume the parent of */
/* this actuator is always a camera, just set the parent position and */
/* rotation. We do not check whether we really have a camera as parent. */
/* It may be better to turn this into a general tracking actuator later */
/* on, since lots of plausible relations can be filled in here. */
/* ... set up some parameters ... */
/* missing here: the 'floorloc' of the actor's shadow */
mindistsq= m_minHeight*m_minHeight;
maxdistsq= m_maxHeight*m_maxHeight;
/* C1: not checked... is a future option */
/* C2: blender test_visibility function. Can this be a ray-test? */
/* C3: fixed height */
from[2] = (15.0*from[2] + lookat[2] + m_height)/16.0;
/* C4: camera behind actor */
if (m_x) {
fp1[0] = actormat[0][0];
fp1[1] = actormat[1][0];
fp1[2] = actormat[2][0];
fp2[0] = frommat[0][0];
fp2[1] = frommat[1][0];
fp2[2] = frommat[2][0];
}
else {
fp1[0] = actormat[0][1];
fp1[1] = actormat[1][1];
fp1[2] = actormat[2][1];
fp2[0] = frommat[0][1];
fp2[1] = frommat[1][1];
fp2[2] = frommat[2][1];
}
inp= fp1[0]*fp2[0] + fp1[1]*fp2[1] + fp1[2]*fp2[2];
fac= (-1.0 + inp)/32.0;
from[0]+= fac*fp1[0];
from[1]+= fac*fp1[1];
from[2]+= fac*fp1[2];
/* alleen alstie ervoor ligt: cross testen en loodrechte bijtellen */
if(inp<0.0) {
if(fp1[0]*fp2[1] - fp1[1]*fp2[0] > 0.0) {
from[0]-= fac*fp1[1];
from[1]+= fac*fp1[0];
}
else {
from[0]+= fac*fp1[1];
from[1]-= fac*fp1[0];
}
}
/* CONSTRAINT 5: minimum / maximum afstand */
rc[0]= (lookat[0]-from[0]);
rc[1]= (lookat[1]-from[1]);
rc[2]= (lookat[2]-from[2]);
distsq= rc[0]*rc[0] + rc[1]*rc[1] + rc[2]*rc[2];
if(distsq > maxdistsq) {
distsq = 0.15*(distsq-maxdistsq)/distsq;
from[0] += distsq*rc[0];
from[1] += distsq*rc[1];
from[2] += distsq*rc[2];
}
else if(distsq < mindistsq) {
distsq = 0.15*(mindistsq-distsq)/mindistsq;
from[0] -= distsq*rc[0];
from[1] -= distsq*rc[1];
from[2] -= distsq*rc[2];
}
/* CONSTRAINT 7: track to schaduw */
rc[0]= (lookat[0]-from[0]);
rc[1]= (lookat[1]-from[1]);
rc[2]= (lookat[2]-from[2]);
Kx_VecUpMat3(rc, mat, 3); /* y up Track -z */
/* now set the camera position and rotation */
obj->NodeSetLocalPosition(from);
actormat[0][0]= mat[0][0]; actormat[0][1]= mat[1][0]; actormat[0][2]= mat[2][0];
actormat[1][0]= mat[0][1]; actormat[1][1]= mat[1][1]; actormat[1][2]= mat[2][1];
actormat[2][0]= mat[0][2]; actormat[2][1]= mat[1][2]; actormat[2][2]= mat[2][2];
obj->NodeSetLocalOrientation(actormat);
return true;
}
CValue *KX_CameraActuator::findObject(char *obName)
{
/* hook to object system */
return NULL;
}
bool KX_CameraActuator::string2axischoice(const char *axisString)
{
bool res = true;
res = !(axisString == Y_AXIS_STRING);
return res;
}
/* ------------------------------------------------------------------------- */
/* Python functions */
/* ------------------------------------------------------------------------- */
/* Integration hooks ------------------------------------------------------- */
PyTypeObject KX_CameraActuator::Type = {
PyObject_HEAD_INIT(NULL)
0,
"KX_CameraActuator",
sizeof(PyObjectPlus_Proxy),
0,
py_base_dealloc,
0,
0,
0,
0,
py_base_repr,
0,0,0,0,0,0,
py_base_getattro,
py_base_setattro,
0,0,0,0,0,0,0,0,0,
Methods
};
PyParentObject KX_CameraActuator::Parents[] = {
&KX_CameraActuator::Type,
&SCA_IActuator::Type,
&SCA_ILogicBrick::Type,
&CValue::Type,
NULL
};
PyMethodDef KX_CameraActuator::Methods[] = {
// ---> deprecated (all)
{"setObject",(PyCFunction) KX_CameraActuator::sPySetObject, METH_O, (PY_METHODCHAR)SetObject_doc},
{"getObject",(PyCFunction) KX_CameraActuator::sPyGetObject, METH_VARARGS, (PY_METHODCHAR)GetObject_doc},
{"setMin" ,(PyCFunction) KX_CameraActuator::sPySetMin, METH_VARARGS, (PY_METHODCHAR)SetMin_doc},
{"getMin" ,(PyCFunction) KX_CameraActuator::sPyGetMin, METH_NOARGS, (PY_METHODCHAR)GetMin_doc},
{"setMax" ,(PyCFunction) KX_CameraActuator::sPySetMax, METH_VARARGS, (PY_METHODCHAR)SetMax_doc},
{"getMax" ,(PyCFunction) KX_CameraActuator::sPyGetMax, METH_NOARGS, (PY_METHODCHAR)GetMax_doc},
{"setHeight",(PyCFunction) KX_CameraActuator::sPySetHeight, METH_VARARGS, (PY_METHODCHAR)SetHeight_doc},
{"getHeight",(PyCFunction) KX_CameraActuator::sPyGetHeight, METH_NOARGS, (PY_METHODCHAR)GetHeight_doc},
{"setXY" ,(PyCFunction) KX_CameraActuator::sPySetXY, METH_VARARGS, (PY_METHODCHAR)SetXY_doc},
{"getXY" ,(PyCFunction) KX_CameraActuator::sPyGetXY, METH_NOARGS, (PY_METHODCHAR)GetXY_doc},
{NULL,NULL,NULL,NULL} //Sentinel
};
PyAttributeDef KX_CameraActuator::Attributes[] = {
KX_PYATTRIBUTE_FLOAT_RW("min",-MAXFLOAT,MAXFLOAT,KX_CameraActuator,m_minHeight),
KX_PYATTRIBUTE_FLOAT_RW("max",-MAXFLOAT,MAXFLOAT,KX_CameraActuator,m_maxHeight),
KX_PYATTRIBUTE_FLOAT_RW("height",-MAXFLOAT,MAXFLOAT,KX_CameraActuator,m_height),
KX_PYATTRIBUTE_BOOL_RW("xy",KX_CameraActuator,m_x),
KX_PYATTRIBUTE_RW_FUNCTION("object", KX_CameraActuator, pyattr_get_object, pyattr_set_object),
{NULL}
};
PyObject* KX_CameraActuator::py_getattro(PyObject *attr) {
py_getattro_up(SCA_IActuator);
}
PyObject* KX_CameraActuator::py_getattro_dict() {
py_getattro_dict_up(SCA_IActuator);
}
int KX_CameraActuator::py_setattro(PyObject *attr, PyObject* value) {
py_setattro_up(SCA_IActuator);
}
/* get obj ---------------------------------------------------------- */
const char KX_CameraActuator::GetObject_doc[] =
"getObject(name_only = 1)\n"
"name_only - optional arg, when true will return the KX_GameObject rather then its name\n"
"\tReturns the object this sensor reacts to.\n";
PyObject* KX_CameraActuator::PyGetObject(PyObject* args)
{
int ret_name_only = 1;
ShowDeprecationWarning("getObject()", "the object property");
if (!PyArg_ParseTuple(args, "|i:getObject", &ret_name_only))
return NULL;
if (!m_ob)
Py_RETURN_NONE;
if (ret_name_only)
return PyString_FromString(m_ob->GetName());
else
return m_ob->GetProxy();
}
/* set obj ---------------------------------------------------------- */
const char KX_CameraActuator::SetObject_doc[] =
"setObject(object)\n"
"\t- object: KX_GameObject, string or None\n"
"\tSets the object this sensor reacts to.\n";
PyObject* KX_CameraActuator::PySetObject(PyObject* value)
{
KX_GameObject *gameobj;
ShowDeprecationWarning("setObject()", "the object property");
if (!ConvertPythonToGameObject(value, &gameobj, true, "actuator.setObject(value): KX_CameraActuator"))
return NULL; // ConvertPythonToGameObject sets the error
if (m_ob != NULL)
m_ob->UnregisterActuator(this);
m_ob = (SCA_IObject*)gameobj;
if (m_ob)
m_ob->RegisterActuator(this);
Py_RETURN_NONE;
}
/* get min ---------------------------------------------------------- */
const char KX_CameraActuator::GetMin_doc[] =
"getMin\n"
"\tReturns the minimum value set in the Min: field.\n";
PyObject* KX_CameraActuator::PyGetMin()
{
ShowDeprecationWarning("getMin()", "the min property");
return PyFloat_FromDouble(m_minHeight);
}
/* set min ---------------------------------------------------------- */
const char KX_CameraActuator::SetMin_doc[] =
"setMin\n"
"\tSets the minimum value.\n";
PyObject* KX_CameraActuator::PySetMin(PyObject* args)
{
ShowDeprecationWarning("setMin()", "the min property");
float min;
if(PyArg_ParseTuple(args,"f:setMin", &min))
{
m_minHeight = min;
Py_RETURN_NONE;
}
return NULL;
}
/* get min ---------------------------------------------------------- */
const char KX_CameraActuator::GetMax_doc[] =
"getMax\n"
"\tReturns the maximum value set in the Max: field.\n";
PyObject* KX_CameraActuator::PyGetMax()
{
ShowDeprecationWarning("getMax()", "the max property");
return PyFloat_FromDouble(m_maxHeight);
}
/* set min ---------------------------------------------------------- */
const char KX_CameraActuator::SetMax_doc[] =
"setMax\n"
"\tSets the maximum value.\n";
PyObject* KX_CameraActuator::PySetMax(PyObject* args)
{
ShowDeprecationWarning("getMax()", "the max property");
float max;
if(PyArg_ParseTuple(args,"f:setMax", &max))
{
m_maxHeight = max;
Py_RETURN_NONE;
}
return NULL;
}
/* get height ---------------------------------------------------------- */
const char KX_CameraActuator::GetHeight_doc[] =
"getHeight\n"
"\tReturns the height value set in the height: field.\n";
PyObject* KX_CameraActuator::PyGetHeight()
{
ShowDeprecationWarning("getHeight()", "the height property");
return PyFloat_FromDouble(m_height);
}
/* set height ---------------------------------------------------------- */
const char KX_CameraActuator::SetHeight_doc[] =
"setHeight\n"
"\tSets the height value.\n";
PyObject* KX_CameraActuator::PySetHeight(PyObject* args)
{
ShowDeprecationWarning("getHeight()", "the height property");
float height;
if(PyArg_ParseTuple(args,"f:setHeight", &height))
{
m_height = height;
Py_RETURN_NONE;
}
return NULL;
}
/* set XY ---------------------------------------------------------- */
const char KX_CameraActuator::SetXY_doc[] =
"setXY\n"
"\tSets axis the camera tries to get behind.\n"
"\t1=x, 0=y\n";
PyObject* KX_CameraActuator::PySetXY(PyObject* args)
{
ShowDeprecationWarning("setXY()", "the xy property");
int value;
if(PyArg_ParseTuple(args,"i:setXY", &value))
{
m_x = value != 0;
Py_RETURN_NONE;
}
return NULL;
}
/* get XY -------------------------------------------------------------*/
const char KX_CameraActuator::GetXY_doc[] =
"getXY\n"
"\tGets the axis the camera tries to get behind.\n"
"\tTrue = X, False = Y\n";
PyObject* KX_CameraActuator::PyGetXY()
{
ShowDeprecationWarning("getXY()", "the xy property");
return PyInt_FromLong(m_x);
}
PyObject* KX_CameraActuator::pyattr_get_object(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
{
KX_CameraActuator* self= static_cast<KX_CameraActuator*>(self_v);
if (self->m_ob==NULL)
Py_RETURN_NONE;
else
return self->m_ob->GetProxy();
}
int KX_CameraActuator::pyattr_set_object(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef, PyObject *value)
{
KX_CameraActuator* self= static_cast<KX_CameraActuator*>(self_v);
KX_GameObject *gameobj;
if (!ConvertPythonToGameObject(value, &gameobj, true, "actuator.object = value: KX_CameraActuator"))
return 1; // ConvertPythonToGameObject sets the error
if (self->m_ob)
self->m_ob->UnregisterActuator(self);
if ((self->m_ob = (SCA_IObject*)gameobj))
self->m_ob->RegisterActuator(self);
return 0;
}
/* eof */
|