4 * ***** BEGIN GPL LICENSE BLOCK *****
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
21 * All rights reserved.
23 * The Original Code is: all of this file.
25 * Contributor(s): none yet.
27 * ***** END GPL LICENSE BLOCK *****
28 * Scenegraph controller for ipos.
32 typedef unsigned __int64 uint_ptr;
34 typedef unsigned long uint_ptr;
37 #if defined(WIN32) && !defined(FREE_WINDOWS)
38 // This warning tells us about truncation of __long__ stl-generated names.
39 // It can occasionally cause DevStudio to have internal compiler warnings.
40 #pragma warning( disable : 4786 )
43 #include "KX_IPO_SGController.h"
44 #include "KX_ScalarInterpolator.h"
45 #include "KX_GameObject.h"
46 #include "KX_IPhysicsController.h"
47 #include "DNA_ipo_types.h"
50 // All objects should start on frame 1! Will we ever need an object to
51 // start on another frame, the 1.0 should change.
52 KX_IpoSGController::KX_IpoSGController()
53 : m_ipo_as_force(false),
58 m_ipo_start_initialized(false),
59 m_ipo_start_euler(0.0,0.0,0.0),
60 m_ipo_euler_initialized(false)
63 for (int i=0; i < KX_MAX_IPO_CHANNELS; i++)
64 m_ipo_channels_active[i] = false;
68 void KX_IpoSGController::SetOption(
73 case SG_CONTR_IPO_IPO_AS_FORCE:
74 m_ipo_as_force = (value != 0);
77 case SG_CONTR_IPO_IPO_ADD:
78 m_ipo_add = (value != 0);
81 case SG_CONTR_IPO_RESET:
82 if (m_ipo_start_initialized && value) {
83 m_ipo_start_initialized = false;
87 case SG_CONTR_IPO_LOCAL:
88 if (value/* && ((SG_Node*)m_pObject)->GetSGParent() == NULL*/) {
89 // only accept local Ipo if the object has no parent
97 ; /* just ignore the rest */
102 KX_IpoSGController::UpdateSumoReference(
111 KX_IpoSGController::SetGameObject(
120 bool KX_IpoSGController::Update(double currentTime)
124 T_InterpolatorList::iterator i;
125 for (i = m_interpolators.begin(); !(i == m_interpolators.end()); ++i) {
126 (*i)->Execute(m_ipotime);//currentTime);
129 SG_Spatial* ob = (SG_Spatial*)m_pObject;
131 //initialization on the first frame of the IPO
132 if (! m_ipo_start_initialized && currentTime > 0.0) {
133 m_ipo_start_point = ob->GetLocalPosition();
134 m_ipo_start_orient = ob->GetLocalOrientation();
135 m_ipo_start_scale = ob->GetLocalScale();
136 m_ipo_start_initialized = true;
137 if (!m_ipo_euler_initialized) {
138 // do it only once to avoid angle discontinuities
139 m_ipo_start_orient.getEuler(m_ipo_start_euler[0], m_ipo_start_euler[1], m_ipo_start_euler[2]);
140 m_ipo_euler_initialized = true;
145 if (m_ipo_channels_active[OB_LOC_X] || m_ipo_channels_active[OB_LOC_Y] || m_ipo_channels_active[OB_LOC_Z] || m_ipo_channels_active[OB_DLOC_X] || m_ipo_channels_active[OB_DLOC_Y] || m_ipo_channels_active[OB_DLOC_Z])
147 if (m_ipo_as_force == true)
149 if (m_game_object && ob && m_game_object->GetPhysicsController())
151 m_game_object->GetPhysicsController()->ApplyForce(m_ipo_local ?
152 ob->GetWorldOrientation() * m_ipo_xform.GetPosition() :
153 m_ipo_xform.GetPosition(), false);
158 // Local ipo should be defined with the object position at (0,0,0)
159 // Local transform is applied to the object based on initial position
160 MT_Point3 newPosition(0.0,0.0,0.0);
163 newPosition = ob->GetLocalPosition();
164 //apply separate IPO channels if there is any data in them
165 //Loc and dLoc act by themselves or are additive
167 if (m_ipo_channels_active[OB_LOC_X]) {
168 newPosition[0] = (m_ipo_channels_active[OB_DLOC_X] ? m_ipo_xform.GetPosition()[0] + m_ipo_xform.GetDeltaPosition()[0] : m_ipo_xform.GetPosition()[0]);
170 else if (m_ipo_channels_active[OB_DLOC_X] && m_ipo_start_initialized) {
171 newPosition[0] = (((!m_ipo_add)?m_ipo_start_point[0]:0.0) + m_ipo_xform.GetDeltaPosition()[0]);
174 if (m_ipo_channels_active[OB_LOC_Y]) {
175 newPosition[1] = (m_ipo_channels_active[OB_DLOC_Y] ? m_ipo_xform.GetPosition()[1] + m_ipo_xform.GetDeltaPosition()[1] : m_ipo_xform.GetPosition()[1]);
177 else if (m_ipo_channels_active[OB_DLOC_Y] && m_ipo_start_initialized) {
178 newPosition[1] = (((!m_ipo_add)?m_ipo_start_point[1]:0.0) + m_ipo_xform.GetDeltaPosition()[1]);
181 if (m_ipo_channels_active[OB_LOC_Z]) {
182 newPosition[2] = (m_ipo_channels_active[OB_DLOC_Z] ? m_ipo_xform.GetPosition()[2] + m_ipo_xform.GetDeltaPosition()[2] : m_ipo_xform.GetPosition()[2]);
184 else if (m_ipo_channels_active[OB_DLOC_Z] && m_ipo_start_initialized) {
185 newPosition[2] = (((!m_ipo_add)?m_ipo_start_point[2]:0.0) + m_ipo_xform.GetDeltaPosition()[2]);
189 newPosition = m_ipo_start_point + m_ipo_start_scale*(m_ipo_start_orient*newPosition);
191 newPosition = m_ipo_start_point + newPosition;
194 m_game_object->NodeSetLocalPosition(newPosition);
197 //modifies orientation?
198 if (m_ipo_channels_active[OB_ROT_X] || m_ipo_channels_active[OB_ROT_Y] || m_ipo_channels_active[OB_ROT_Z] || m_ipo_channels_active[OB_DROT_X] || m_ipo_channels_active[OB_DROT_Y] || m_ipo_channels_active[OB_DROT_Z]) {
199 if (m_ipo_as_force) {
201 if (m_game_object && ob) {
202 m_game_object->ApplyTorque(m_ipo_local ?
203 ob->GetWorldOrientation() * m_ipo_xform.GetEulerAngles() :
204 m_ipo_xform.GetEulerAngles(), false);
206 } else if (m_ipo_add) {
207 if (m_ipo_start_initialized) {
208 double yaw=0, pitch=0, roll=0; //delta Euler angles
211 if (m_ipo_channels_active[OB_ROT_X])
212 yaw += m_ipo_xform.GetEulerAngles()[0];
213 if (m_ipo_channels_active[OB_DROT_X])
214 yaw += m_ipo_xform.GetDeltaEulerAngles()[0];
217 if (m_ipo_channels_active[OB_ROT_Y])
218 pitch += m_ipo_xform.GetEulerAngles()[1];
219 if (m_ipo_channels_active[OB_DROT_Y])
220 pitch += m_ipo_xform.GetDeltaEulerAngles()[1];
223 if (m_ipo_channels_active[OB_ROT_Z])
224 roll += m_ipo_xform.GetEulerAngles()[2];
225 if (m_ipo_channels_active[OB_DROT_Z])
226 roll += m_ipo_xform.GetDeltaEulerAngles()[2];
228 MT_Matrix3x3 rotation(MT_Vector3(yaw, pitch, roll));
230 rotation = m_ipo_start_orient * rotation;
232 rotation = rotation * m_ipo_start_orient;
234 m_game_object->NodeSetLocalOrientation(rotation);
236 } else if (m_ipo_channels_active[OB_ROT_X] || m_ipo_channels_active[OB_ROT_Y] || m_ipo_channels_active[OB_ROT_Z]) {
237 if (m_ipo_euler_initialized) {
238 // assume all channel absolute
239 // All 3 channels should be specified but if they are not, we will take
240 // the value at the start of the game to avoid angle sign reversal
241 double yaw=m_ipo_start_euler[0], pitch=m_ipo_start_euler[1], roll=m_ipo_start_euler[2];
244 if (m_ipo_channels_active[OB_ROT_X]) {
245 yaw = (m_ipo_channels_active[OB_DROT_X] ? (m_ipo_xform.GetEulerAngles()[0] + m_ipo_xform.GetDeltaEulerAngles()[0]) : m_ipo_xform.GetEulerAngles()[0] );
247 else if (m_ipo_channels_active[OB_DROT_X]) {
248 yaw += m_ipo_xform.GetDeltaEulerAngles()[0];
252 if (m_ipo_channels_active[OB_ROT_Y]) {
253 pitch = (m_ipo_channels_active[OB_DROT_Y] ? (m_ipo_xform.GetEulerAngles()[1] + m_ipo_xform.GetDeltaEulerAngles()[1]) : m_ipo_xform.GetEulerAngles()[1] );
255 else if (m_ipo_channels_active[OB_DROT_Y]) {
256 pitch += m_ipo_xform.GetDeltaEulerAngles()[1];
260 if (m_ipo_channels_active[OB_ROT_Z]) {
261 roll = (m_ipo_channels_active[OB_DROT_Z] ? (m_ipo_xform.GetEulerAngles()[2] + m_ipo_xform.GetDeltaEulerAngles()[2]) : m_ipo_xform.GetEulerAngles()[2] );
263 else if (m_ipo_channels_active[OB_DROT_Z]) {
264 roll += m_ipo_xform.GetDeltaEulerAngles()[2];
267 m_game_object->NodeSetLocalOrientation(MT_Vector3(yaw, pitch, roll));
269 } else if (m_ipo_start_initialized) {
270 // only DROT, treat as Add
271 double yaw=0, pitch=0, roll=0; //delta Euler angles
274 if (m_ipo_channels_active[OB_DROT_X])
275 yaw = m_ipo_xform.GetDeltaEulerAngles()[0];
278 if (m_ipo_channels_active[OB_DROT_Y])
279 pitch = m_ipo_xform.GetDeltaEulerAngles()[1];
282 if (m_ipo_channels_active[OB_DROT_Z])
283 roll = m_ipo_xform.GetDeltaEulerAngles()[2];
285 // dRot are always local
286 MT_Matrix3x3 rotation(MT_Vector3(yaw, pitch, roll));
287 rotation = m_ipo_start_orient * rotation;
289 m_game_object->NodeSetLocalOrientation(rotation);
293 if (m_ipo_channels_active[OB_SIZE_X] || m_ipo_channels_active[OB_SIZE_Y] || m_ipo_channels_active[OB_SIZE_Z] || m_ipo_channels_active[OB_DSIZE_X] || m_ipo_channels_active[OB_DSIZE_Y] || m_ipo_channels_active[OB_DSIZE_Z]) {
294 //default is no scale change
295 MT_Vector3 newScale(1.0,1.0,1.0);
297 newScale = ob->GetLocalScale();
299 if (m_ipo_channels_active[OB_SIZE_X]) {
300 newScale[0] = (m_ipo_channels_active[OB_DSIZE_X] ? (m_ipo_xform.GetScaling()[0] + m_ipo_xform.GetDeltaScaling()[0]) : m_ipo_xform.GetScaling()[0]);
302 else if (m_ipo_channels_active[OB_DSIZE_X] && m_ipo_start_initialized) {
303 newScale[0] = (m_ipo_xform.GetDeltaScaling()[0] + ((!m_ipo_add)?m_ipo_start_scale[0]:0.0));
307 if (m_ipo_channels_active[OB_SIZE_Y]) {
308 newScale[1] = (m_ipo_channels_active[OB_DSIZE_Y] ? (m_ipo_xform.GetScaling()[1] + m_ipo_xform.GetDeltaScaling()[1]): m_ipo_xform.GetScaling()[1]);
310 else if (m_ipo_channels_active[OB_DSIZE_Y] && m_ipo_start_initialized) {
311 newScale[1] = (m_ipo_xform.GetDeltaScaling()[1] + ((!m_ipo_add)?m_ipo_start_scale[1]:0.0));
315 if (m_ipo_channels_active[OB_SIZE_Z]) {
316 newScale[2] = (m_ipo_channels_active[OB_DSIZE_Z] ? (m_ipo_xform.GetScaling()[2] + m_ipo_xform.GetDeltaScaling()[2]) : m_ipo_xform.GetScaling()[2]);
318 else if (m_ipo_channels_active[OB_DSIZE_Z] && m_ipo_start_initialized) {
319 newScale[2] = (m_ipo_xform.GetDeltaScaling()[2] + ((!m_ipo_add)?m_ipo_start_scale[2]:1.0));
323 newScale = m_ipo_start_scale * newScale;
326 m_game_object->NodeSetLocalScale(newScale);
335 void KX_IpoSGController::AddInterpolator(KX_IInterpolator* interp)
337 this->m_interpolators.push_back(interp);
340 SG_Controller* KX_IpoSGController::GetReplica(class SG_Node* destnode)
342 KX_IpoSGController* iporeplica = new KX_IpoSGController(*this);
343 // clear object that ipo acts on in the replica.
344 iporeplica->ClearObject();
345 iporeplica->SetGameObject((KX_GameObject*)destnode->GetSGClientObject());
347 // dirty hack, ask Gino for a better solution in the ipo implementation
348 // hacken en zagen, in what we call datahiding, not written for replication :(
350 T_InterpolatorList oldlist = m_interpolators;
351 iporeplica->m_interpolators.clear();
353 T_InterpolatorList::iterator i;
354 for (i = oldlist.begin(); !(i == oldlist.end()); ++i) {
355 KX_ScalarInterpolator* copyipo = new KX_ScalarInterpolator(*((KX_ScalarInterpolator*)*i));
356 iporeplica->AddInterpolator(copyipo);
358 MT_Scalar* scaal = ((KX_ScalarInterpolator*)*i)->GetTarget();
359 uint_ptr orgbase = (uint_ptr)&m_ipo_xform;
360 uint_ptr orgloc = (uint_ptr)scaal;
361 uint_ptr offset = orgloc-orgbase;
362 uint_ptr newaddrbase = (uint_ptr)&iporeplica->m_ipo_xform;
363 newaddrbase += offset;
364 MT_Scalar* blaptr = (MT_Scalar*) newaddrbase;
365 copyipo->SetNewTarget((MT_Scalar*)blaptr);
371 KX_IpoSGController::~KX_IpoSGController()
374 T_InterpolatorList::iterator i;
375 for (i = m_interpolators.begin(); !(i == m_interpolators.end()); ++i) {