bool KX_CameraActuator::Update(double curtime, bool frame) { /* wondering... is it really necessary/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]={0}, fp2[3]={0}, 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.0f * from[2] + lookat[2] + m_height) / 16.0f; /* C4: camera behind actor */ switch (m_axis) { case OB_POSX: /* 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]; break; case OB_POSY: /* Y */ 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]; break; case OB_NEGX: /* -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]; break; case OB_NEGY: /* -Y */ 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]; break; default: assert(0); break; } inp = fp1[0]*fp2[0] + fp1[1]*fp2[1] + fp1[2]*fp2[2]; fac = (-1.0f + inp) * m_damping; from[0] += fac * fp1[0]; from[1] += fac * fp1[1]; from[2] += fac * fp1[2]; /* only for it lies: cross test and perpendicular bites up */ if (inp < 0.0f) { /* Don't do anything if the cross product is too small. * The camera up-axis becomes unstable and starts to oscillate. * The 0.01f threshold is arbitrary but seems to work well in practice. */ float cross = fp1[0] * fp2[1] - fp1[1] * fp2[0]; if (cross > 0.01f) { from[0] -= fac * fp1[1]; from[1] += fac * fp1[0]; } else if (cross < -0.01f) { from[0] += fac * fp1[1]; from[1] -= fac * fp1[0]; } } /* CONSTRAINT 5: minimum / maximum distance */ 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.15f * (distsq - maxdistsq) / distsq; from[0] += distsq*rc[0]; from[1] += distsq*rc[1]; from[2] += distsq*rc[2]; } else if (distsq < mindistsq) { distsq = 0.15f * (mindistsq - distsq) / mindistsq; from[0] -= distsq*rc[0]; from[1] -= distsq*rc[1]; from[2] -= distsq*rc[2]; } /* CONSTRAINT 7: track to floor below actor */ 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; }
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) * m_damping; 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; }