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 *****
30 /** \file gameengine/SceneGraph/SG_Spatial.cpp
36 #include "SG_Spatial.h"
37 #include "SG_Controller.h"
38 #include "SG_ParentRelation.h"
44 SG_Callbacks& callbacks
47 SG_IObject(clientobj,clientinfo,callbacks),
48 m_localPosition(0.0,0.0,0.0),
49 m_localRotation(1.0,0.0,0.0,0.0,1.0,0.0,0.0,0.0,1.0),
50 m_localScaling(1.f,1.f,1.f),
52 m_worldPosition(0.0,0.0,0.0),
53 m_worldRotation(1.0,0.0,0.0,0.0,1.0,0.0,0.0,0.0,1.0),
54 m_worldScaling(1.f,1.f,1.f),
56 m_parent_relation (NULL),
58 m_bbox(MT_Point3(-1.0, -1.0, -1.0), MT_Point3(1.0, 1.0, 1.0)),
67 const SG_Spatial& other
70 m_localPosition(other.m_localPosition),
71 m_localRotation(other.m_localRotation),
72 m_localScaling(other.m_localScaling),
74 m_worldPosition(other.m_worldPosition),
75 m_worldRotation(other.m_worldRotation),
76 m_worldScaling(other.m_worldScaling),
78 m_parent_relation(NULL),
81 m_radius(other.m_radius),
85 // duplicate the parent relation for this object
86 m_parent_relation = other.m_parent_relation->NewCopy();
92 delete (m_parent_relation);
98 SG_ParentRelation *relation
100 delete (m_parent_relation);
101 m_parent_relation = relation;
107 * Update Spatial Data.
108 * Calculates WorldTransform., (either doing itsself or using the linked SGControllers)
115 const SG_Spatial *parent,
120 bool bComputesWorldTransform = false;
122 // update spatial controllers
124 SGControllerList::iterator cit = GetSGControllerList().begin();
125 SGControllerList::const_iterator c_end = GetSGControllerList().end();
127 for (;cit!=c_end;++cit)
129 if ((*cit)->Update(time))
130 bComputesWorldTransform = true;
133 // If none of the objects updated our values then we ask the
134 // parent_relation object owned by this class to update
135 // our world coordinates.
137 if (!bComputesWorldTransform)
138 bComputesWorldTransform = ComputeWorldTransforms(parent, parentUpdated);
140 return bComputesWorldTransform;
144 * Position and translation methods
151 const MT_Vector3& trans,
152 const SG_Spatial *parent,
156 m_localPosition += m_localRotation * trans;
159 m_localPosition += trans * parent->GetWorldOrientation();
161 m_localPosition += trans;
174 * Orientation and rotation methods.
181 const MT_Matrix3x3& rot,
184 m_localRotation = m_localRotation * (
188 (GetWorldOrientation().inverse() * rot * GetWorldOrientation()));
194 MT_Transform SG_Spatial::GetWorldTransform() const
196 return MT_Transform(m_worldPosition,
197 m_worldRotation.scaled(
198 m_worldScaling[0], m_worldScaling[1], m_worldScaling[2]));
201 bool SG_Spatial::inside(const MT_Point3 &point) const
203 MT_Scalar radius = m_worldScaling[m_worldScaling.closestAxis()]*m_radius;
204 return (m_worldPosition.distance2(point) <= radius*radius) ?
205 m_bbox.transform(GetWorldTransform()).inside(point) :
209 void SG_Spatial::getBBox(MT_Point3 *box) const
211 m_bbox.get(box, GetWorldTransform());
214 void SG_Spatial::getAABBox(MT_Point3 *box) const
216 m_bbox.getaa(box, GetWorldTransform());