2 * ***** BEGIN GPL LICENSE BLOCK *****
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software Foundation,
16 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
19 * All rights reserved.
21 * The Original Code is: all of this file.
23 * Contributor(s): none yet.
25 * ***** END GPL LICENSE BLOCK *****
28 /** \file gameengine/SceneGraph/SG_Spatial.cpp
34 #include "SG_Spatial.h"
35 #include "SG_Controller.h"
36 #include "SG_ParentRelation.h"
42 SG_Callbacks& callbacks
45 SG_IObject(clientobj,clientinfo,callbacks),
46 m_localPosition(0.0,0.0,0.0),
47 m_localRotation(1.0,0.0,0.0,0.0,1.0,0.0,0.0,0.0,1.0),
48 m_localScaling(1.f,1.f,1.f),
50 m_worldPosition(0.0,0.0,0.0),
51 m_worldRotation(1.0,0.0,0.0,0.0,1.0,0.0,0.0,0.0,1.0),
52 m_worldScaling(1.f,1.f,1.f),
54 m_parent_relation (NULL),
56 m_bbox(MT_Point3(-1.0, -1.0, -1.0), MT_Point3(1.0, 1.0, 1.0)),
65 const SG_Spatial& other
68 m_localPosition(other.m_localPosition),
69 m_localRotation(other.m_localRotation),
70 m_localScaling(other.m_localScaling),
72 m_worldPosition(other.m_worldPosition),
73 m_worldRotation(other.m_worldRotation),
74 m_worldScaling(other.m_worldScaling),
76 m_parent_relation(NULL),
79 m_radius(other.m_radius),
83 // duplicate the parent relation for this object
84 m_parent_relation = other.m_parent_relation->NewCopy();
90 delete (m_parent_relation);
96 SG_ParentRelation *relation
98 delete (m_parent_relation);
99 m_parent_relation = relation;
105 * Update Spatial Data.
106 * Calculates WorldTransform., (either doing itsself or using the linked SGControllers)
113 const SG_Spatial *parent,
117 bool bComputesWorldTransform = false;
119 // update spatial controllers
121 SGControllerList::iterator cit = GetSGControllerList().begin();
122 SGControllerList::const_iterator c_end = GetSGControllerList().end();
124 for (;cit!=c_end;++cit)
126 if ((*cit)->Update(time))
127 bComputesWorldTransform = true;
130 // If none of the objects updated our values then we ask the
131 // parent_relation object owned by this class to update
132 // our world coordinates.
134 if (!bComputesWorldTransform)
135 bComputesWorldTransform = ComputeWorldTransforms(parent, parentUpdated);
137 return bComputesWorldTransform;
141 * Position and translation methods
148 const MT_Vector3& trans,
149 const SG_Spatial *parent,
153 m_localPosition += m_localRotation * trans;
156 m_localPosition += trans * parent->GetWorldOrientation();
158 m_localPosition += trans;
171 * Orientation and rotation methods.
178 const MT_Matrix3x3& rot,
181 m_localRotation = m_localRotation * (
185 (GetWorldOrientation().inverse() * rot * GetWorldOrientation()));
191 MT_Transform SG_Spatial::GetWorldTransform() const
193 return MT_Transform(m_worldPosition,
194 m_worldRotation.scaled(
195 m_worldScaling[0], m_worldScaling[1], m_worldScaling[2]));
198 bool SG_Spatial::inside(const MT_Point3 &point) const
200 MT_Scalar radius = m_worldScaling[m_worldScaling.closestAxis()]*m_radius;
201 return (m_worldPosition.distance2(point) <= radius*radius) ?
202 m_bbox.transform(GetWorldTransform()).inside(point) :
206 void SG_Spatial::getBBox(MT_Point3 *box) const
208 m_bbox.get(box, GetWorldTransform());
211 void SG_Spatial::getAABBox(MT_Point3 *box) const
213 m_bbox.getaa(box, GetWorldTransform());