1 | /* |
---|
2 | * ORXONOX - the hottest 3D action shooter ever to exist |
---|
3 | * > www.orxonox.net < |
---|
4 | * |
---|
5 | * |
---|
6 | * License notice: |
---|
7 | * |
---|
8 | * This program is free software; you can redistribute it and/or |
---|
9 | * modify it under the terms of the GNU General Public License |
---|
10 | * as published by the Free Software Foundation; either version 2 |
---|
11 | * of the License, or (at your option) any later version. |
---|
12 | * |
---|
13 | * This program is distributed in the hope that it will be useful, |
---|
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
16 | * GNU General Public License for more details. |
---|
17 | * |
---|
18 | * You should have received a copy of the GNU General Public License |
---|
19 | * along with this program; if not, write to the Free Software |
---|
20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
---|
21 | * |
---|
22 | * Author: |
---|
23 | * Reto Grieder |
---|
24 | * Co-authors: |
---|
25 | * Martin Stypinski |
---|
26 | * |
---|
27 | */ |
---|
28 | |
---|
29 | #include "OrxonoxStableHeaders.h" |
---|
30 | #include "MobileEntity.h" |
---|
31 | |
---|
32 | #include "BulletDynamics/Dynamics/btRigidBody.h" |
---|
33 | |
---|
34 | #include "util/Debug.h" |
---|
35 | #include "util/MathConvert.h" |
---|
36 | #include "util/Exception.h" |
---|
37 | #include "core/CoreIncludes.h" |
---|
38 | #include "core/XMLPort.h" |
---|
39 | |
---|
40 | #include "objects/Scene.h" |
---|
41 | |
---|
42 | namespace orxonox |
---|
43 | { |
---|
44 | MobileEntity::MobileEntity(BaseObject* creator) : WorldEntity(creator) |
---|
45 | { |
---|
46 | RegisterObject(MobileEntity); |
---|
47 | |
---|
48 | this->linearAcceleration_ = Vector3::ZERO; |
---|
49 | this->linearVelocity_ = Vector3::ZERO; |
---|
50 | this->angularAcceleration_ = Vector3::ZERO; |
---|
51 | this->angularVelocity_ = Vector3::ZERO; |
---|
52 | |
---|
53 | this->registerVariables(); |
---|
54 | } |
---|
55 | |
---|
56 | MobileEntity::~MobileEntity() |
---|
57 | { |
---|
58 | } |
---|
59 | |
---|
60 | void MobileEntity::XMLPort(Element& xmlelement, XMLPort::Mode mode) |
---|
61 | { |
---|
62 | SUPER(MobileEntity, XMLPort, xmlelement, mode); |
---|
63 | |
---|
64 | XMLPortParamTemplate(MobileEntity, "velocity", setVelocity, getVelocity, xmlelement, mode, const Vector3&); |
---|
65 | XMLPortParamTemplate(MobileEntity, "rotationaxis", setRotationAxis, getRotationAxis, xmlelement, mode, const Vector3&); |
---|
66 | XMLPortParam(MobileEntity, "rotationrate", setRotationRate, getRotationRate, xmlelement, mode); |
---|
67 | } |
---|
68 | |
---|
69 | void MobileEntity::registerVariables() |
---|
70 | { |
---|
71 | } |
---|
72 | |
---|
73 | void MobileEntity::tick(float dt) |
---|
74 | { |
---|
75 | if (this->isActive()) |
---|
76 | { |
---|
77 | // Check whether Bullet doesn't do the physics for us |
---|
78 | if (!this->isDynamic()) |
---|
79 | { |
---|
80 | // Linear part |
---|
81 | this->linearVelocity_.x += this->linearAcceleration_.x * dt; |
---|
82 | this->linearVelocity_.y += this->linearAcceleration_.y * dt; |
---|
83 | this->linearVelocity_.z += this->linearAcceleration_.z * dt; |
---|
84 | linearVelocityChanged(true); |
---|
85 | this->node_->translate(this->linearVelocity_ * dt); |
---|
86 | positionChanged(true); |
---|
87 | |
---|
88 | // Angular part |
---|
89 | // Note: angularVelocity_ is a Quaternion with w = 0 while angularAcceleration_ is a Vector3 |
---|
90 | this->angularVelocity_.x += angularAcceleration_.x * dt; |
---|
91 | this->angularVelocity_.y += angularAcceleration_.y * dt; |
---|
92 | this->angularVelocity_.z += angularAcceleration_.z * dt; |
---|
93 | angularVelocityChanged(true); |
---|
94 | // Calculate new orientation with quaternion derivative. This is about 30% faster than with angle/axis method. |
---|
95 | float mult = dt * 0.5; |
---|
96 | // TODO: this could be optimized by writing it out. The calls currently create 4 new Quaternions! |
---|
97 | Quaternion newOrientation(0.0f, this->angularVelocity_.x * mult, this->angularVelocity_.y * mult, this->angularVelocity_.z * mult); |
---|
98 | newOrientation = this->node_->getOrientation() + newOrientation * this->node_->getOrientation(); |
---|
99 | newOrientation.normalise(); |
---|
100 | this->node_->setOrientation(newOrientation); |
---|
101 | orientationChanged(true); |
---|
102 | } |
---|
103 | } |
---|
104 | } |
---|
105 | |
---|
106 | void MobileEntity::setPosition(const Vector3& position) |
---|
107 | { |
---|
108 | if (this->isDynamic()) |
---|
109 | { |
---|
110 | btTransform transf = this->physicalBody_->getWorldTransform(); |
---|
111 | transf.setOrigin(btVector3(position.x, position.y, position.z)); |
---|
112 | this->physicalBody_->setWorldTransform(transf); |
---|
113 | } |
---|
114 | |
---|
115 | this->node_->setPosition(position); |
---|
116 | positionChanged(false); |
---|
117 | } |
---|
118 | |
---|
119 | void MobileEntity::translate(const Vector3& distance, Ogre::Node::TransformSpace relativeTo) |
---|
120 | { |
---|
121 | if (this->isDynamic()) |
---|
122 | { |
---|
123 | OrxAssert(relativeTo == Ogre::Node::TS_LOCAL, "Cannot translate physical object relative \ |
---|
124 | to any other space than TS_LOCAL."); |
---|
125 | this->physicalBody_->translate(btVector3(distance.x, distance.y, distance.z)); |
---|
126 | } |
---|
127 | |
---|
128 | this->node_->translate(distance, relativeTo); |
---|
129 | positionChanged(false); |
---|
130 | } |
---|
131 | |
---|
132 | void MobileEntity::setOrientation(const Quaternion& orientation) |
---|
133 | { |
---|
134 | if (this->isDynamic()) |
---|
135 | { |
---|
136 | btTransform transf = this->physicalBody_->getWorldTransform(); |
---|
137 | transf.setRotation(btQuaternion(orientation.x, orientation.y, orientation.z, orientation.w)); |
---|
138 | this->physicalBody_->setWorldTransform(transf); |
---|
139 | } |
---|
140 | |
---|
141 | this->node_->setOrientation(orientation); |
---|
142 | orientationChanged(false); |
---|
143 | } |
---|
144 | |
---|
145 | void MobileEntity::rotate(const Quaternion& rotation, Ogre::Node::TransformSpace relativeTo) |
---|
146 | { |
---|
147 | if (this->isDynamic()) |
---|
148 | { |
---|
149 | OrxAssert(relativeTo == Ogre::Node::TS_LOCAL, "Cannot rotate physical object relative \ |
---|
150 | to any other space than TS_LOCAL."); |
---|
151 | btTransform transf = this->physicalBody_->getWorldTransform(); |
---|
152 | this->physicalBody_->setWorldTransform(transf * btTransform(btQuaternion(rotation.x, rotation.y, rotation.z, rotation.w))); |
---|
153 | } |
---|
154 | |
---|
155 | this->node_->rotate(rotation, relativeTo); |
---|
156 | orientationChanged(false); |
---|
157 | } |
---|
158 | |
---|
159 | void MobileEntity::yaw(const Degree& angle, Ogre::Node::TransformSpace relativeTo) |
---|
160 | { |
---|
161 | if (this->isDynamic()) |
---|
162 | { |
---|
163 | OrxAssert(relativeTo == Ogre::Node::TS_LOCAL, "Cannot yaw physical object relative \ |
---|
164 | to any other space than TS_LOCAL."); |
---|
165 | btTransform transf = this->physicalBody_->getWorldTransform(); |
---|
166 | btTransform rotation(btQuaternion(angle.valueRadians(), 0.0f, 0.0f)); |
---|
167 | this->physicalBody_->setWorldTransform(transf * rotation); |
---|
168 | } |
---|
169 | |
---|
170 | this->node_->yaw(angle, relativeTo); |
---|
171 | orientationChanged(false); |
---|
172 | } |
---|
173 | |
---|
174 | void MobileEntity::pitch(const Degree& angle, Ogre::Node::TransformSpace relativeTo) |
---|
175 | { |
---|
176 | if (this->isDynamic()) |
---|
177 | { |
---|
178 | OrxAssert(relativeTo == Ogre::Node::TS_LOCAL, "Cannot pitch physical object relative \ |
---|
179 | to any other space than TS_LOCAL."); |
---|
180 | btTransform transf = this->physicalBody_->getWorldTransform(); |
---|
181 | btTransform rotation(btQuaternion(0.0f, angle.valueRadians(), 0.0f)); |
---|
182 | this->physicalBody_->setWorldTransform(transf * rotation); |
---|
183 | } |
---|
184 | |
---|
185 | this->node_->pitch(angle, relativeTo); |
---|
186 | orientationChanged(false); |
---|
187 | } |
---|
188 | |
---|
189 | void MobileEntity::roll(const Degree& angle, Ogre::Node::TransformSpace relativeTo) |
---|
190 | { |
---|
191 | if (this->isDynamic()) |
---|
192 | { |
---|
193 | OrxAssert(relativeTo == Ogre::Node::TS_LOCAL, "Cannot roll physical object relative \ |
---|
194 | to any other space than TS_LOCAL."); |
---|
195 | btTransform transf = this->physicalBody_->getWorldTransform(); |
---|
196 | btTransform rotation(btQuaternion(angle.valueRadians(), 0.0f, 0.0f)); |
---|
197 | this->physicalBody_->setWorldTransform(transf * rotation); |
---|
198 | } |
---|
199 | |
---|
200 | this->node_->roll(angle, relativeTo); |
---|
201 | orientationChanged(false); |
---|
202 | } |
---|
203 | |
---|
204 | void MobileEntity::lookAt(const Vector3& target, Ogre::Node::TransformSpace relativeTo, const Vector3& localDirectionVector) |
---|
205 | { |
---|
206 | if (this->isDynamic()) |
---|
207 | { |
---|
208 | ThrowException(NotImplemented, "ControllableEntity::lookAt() is not yet supported for physical objects."); |
---|
209 | OrxAssert(relativeTo == Ogre::Node::TS_LOCAL, "Cannot align physical object relative \ |
---|
210 | to any other space than TS_LOCAL."); |
---|
211 | } |
---|
212 | |
---|
213 | this->node_->lookAt(target, relativeTo, localDirectionVector); |
---|
214 | orientationChanged(false); |
---|
215 | } |
---|
216 | |
---|
217 | void MobileEntity::setDirection(const Vector3& direction, Ogre::Node::TransformSpace relativeTo, const Vector3& localDirectionVector) |
---|
218 | { |
---|
219 | if (this->isDynamic()) |
---|
220 | { |
---|
221 | ThrowException(NotImplemented, "ControllableEntity::setDirection() is not yet supported for physical objects."); |
---|
222 | OrxAssert(relativeTo == Ogre::Node::TS_LOCAL, "Cannot align physical object relative \ |
---|
223 | to any other space than TS_LOCAL."); |
---|
224 | } |
---|
225 | |
---|
226 | this->node_->setDirection(direction, relativeTo, localDirectionVector); |
---|
227 | orientationChanged(false); |
---|
228 | } |
---|
229 | |
---|
230 | void MobileEntity::setVelocity(const Vector3& velocity) |
---|
231 | { |
---|
232 | if (this->isDynamic()) |
---|
233 | this->physicalBody_->setLinearVelocity(btVector3(velocity.x, velocity.y, velocity.z)); |
---|
234 | |
---|
235 | this->linearVelocity_ = velocity; |
---|
236 | linearVelocityChanged(false); |
---|
237 | } |
---|
238 | |
---|
239 | void MobileEntity::setAngularVelocity(const Vector3& velocity) |
---|
240 | { |
---|
241 | if (this->isDynamic()) |
---|
242 | this->physicalBody_->setAngularVelocity(btVector3(velocity.x, velocity.y, velocity.z)); |
---|
243 | |
---|
244 | this->angularVelocity_ = velocity; |
---|
245 | angularVelocityChanged(false); |
---|
246 | } |
---|
247 | |
---|
248 | void MobileEntity::setAcceleration(const Vector3& acceleration) |
---|
249 | { |
---|
250 | if (this->isDynamic()) |
---|
251 | this->physicalBody_->applyCentralForce(btVector3(acceleration.x * this->getMass(), acceleration.y * this->getMass(), acceleration.z * this->getMass())); |
---|
252 | |
---|
253 | this->linearAcceleration_ = acceleration; |
---|
254 | } |
---|
255 | |
---|
256 | void MobileEntity::setAngularAcceleration(const Vector3& acceleration) |
---|
257 | { |
---|
258 | if (this->isDynamic()) |
---|
259 | { |
---|
260 | btVector3 inertia(btVector3(1, 1, 1) / this->physicalBody_->getInvInertiaDiagLocal()); |
---|
261 | this->physicalBody_->applyTorque(btVector3(acceleration.x, acceleration.y, acceleration.z) * inertia); |
---|
262 | } |
---|
263 | |
---|
264 | this->angularAcceleration_ = acceleration; |
---|
265 | } |
---|
266 | |
---|
267 | bool MobileEntity::isCollisionTypeLegal(WorldEntity::CollisionType type) const |
---|
268 | { |
---|
269 | if (type == WorldEntity::Static) |
---|
270 | { |
---|
271 | ThrowException(PhysicsViolation, "Cannot tell a MobileEntity to have static collision type"); |
---|
272 | return false; |
---|
273 | } |
---|
274 | else |
---|
275 | return true; |
---|
276 | } |
---|
277 | |
---|
278 | void MobileEntity::setWorldTransform(const btTransform& worldTrans) |
---|
279 | { |
---|
280 | // We use a dynamic body. So we translate our node accordingly. |
---|
281 | this->node_->setPosition(Vector3(worldTrans.getOrigin().x(), worldTrans.getOrigin().y(), worldTrans.getOrigin().z())); |
---|
282 | this->node_->setOrientation(Quaternion(worldTrans.getRotation().w(), worldTrans.getRotation().x(), worldTrans.getRotation().y(), worldTrans.getRotation().z())); |
---|
283 | this->linearVelocity_.x = this->physicalBody_->getLinearVelocity().x(); |
---|
284 | this->linearVelocity_.y = this->physicalBody_->getLinearVelocity().y(); |
---|
285 | this->linearVelocity_.z = this->physicalBody_->getLinearVelocity().z(); |
---|
286 | this->angularVelocity_.x = this->physicalBody_->getAngularVelocity().x(); |
---|
287 | this->angularVelocity_.y = this->physicalBody_->getAngularVelocity().y(); |
---|
288 | this->angularVelocity_.z = this->physicalBody_->getAngularVelocity().z(); |
---|
289 | linearVelocityChanged(true); |
---|
290 | angularVelocityChanged(true); |
---|
291 | positionChanged(true); |
---|
292 | orientationChanged(true); |
---|
293 | } |
---|
294 | |
---|
295 | void MobileEntity::getWorldTransform(btTransform& worldTrans) const |
---|
296 | { |
---|
297 | // We use a kinematic body |
---|
298 | worldTrans.setOrigin(btVector3(node_->getPosition().x, node_->getPosition().y, node_->getPosition().z)); |
---|
299 | worldTrans.setRotation(btQuaternion(node_->getOrientation().x, node_->getOrientation().y, node_->getOrientation().z, node_->getOrientation().w)); |
---|
300 | if (this->isDynamic()) |
---|
301 | { |
---|
302 | // This function gets called only once for dynamic objects to set the initial conditions |
---|
303 | // We have to set the velocities too. |
---|
304 | this->physicalBody_->setLinearVelocity(btVector3(linearVelocity_.x, linearVelocity_.y, linearVelocity_.z)); |
---|
305 | this->physicalBody_->setAngularVelocity(btVector3(angularVelocity_.x, angularVelocity_.y, angularVelocity_.z)); |
---|
306 | } |
---|
307 | } |
---|
308 | } |
---|