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 | * Fabian 'x3n' Landau |
---|
24 | * Reto Grieder (physics) |
---|
25 | * Co-authors: |
---|
26 | * ... |
---|
27 | * |
---|
28 | */ |
---|
29 | |
---|
30 | #include "OrxonoxStableHeaders.h" |
---|
31 | #include "StaticEntity.h" |
---|
32 | |
---|
33 | #include "BulletDynamics/Dynamics/btRigidBody.h" |
---|
34 | |
---|
35 | #include "util/Exception.h" |
---|
36 | #include "core/CoreIncludes.h" |
---|
37 | |
---|
38 | namespace orxonox |
---|
39 | { |
---|
40 | CreateFactory(StaticEntity); |
---|
41 | |
---|
42 | StaticEntity::StaticEntity(BaseObject* creator) : WorldEntity(creator) |
---|
43 | { |
---|
44 | RegisterObject(StaticEntity); |
---|
45 | |
---|
46 | this->registerVariables(); |
---|
47 | } |
---|
48 | |
---|
49 | StaticEntity::~StaticEntity() |
---|
50 | { |
---|
51 | } |
---|
52 | |
---|
53 | void StaticEntity::registerVariables() |
---|
54 | { |
---|
55 | REGISTERDATA(this->getPosition(), network::direction::toclient, new network::NetworkCallback<StaticEntity>(this, &StaticEntity::positionChanged)); |
---|
56 | REGISTERDATA(this->getOrientation(), network::direction::toclient, new network::NetworkCallback<StaticEntity>(this, &StaticEntity::orientationChanged)); |
---|
57 | } |
---|
58 | |
---|
59 | |
---|
60 | void StaticEntity::setPosition(const Vector3& position) |
---|
61 | { |
---|
62 | if (this->isPhysicsRunning()) |
---|
63 | ThrowException(PhysicsViolation, "Cannot change position or orientation of a StaticEntity with physics at run time."); |
---|
64 | if (this->isStatic()) |
---|
65 | { |
---|
66 | btTransform transf = this->physicalBody_->getWorldTransform(); |
---|
67 | transf.setOrigin(btVector3(position.x, position.y, position.z)); |
---|
68 | this->physicalBody_->setWorldTransform(transf); |
---|
69 | } |
---|
70 | |
---|
71 | this->node_->setPosition(position); |
---|
72 | } |
---|
73 | |
---|
74 | void StaticEntity::translate(const Vector3& distance, Ogre::Node::TransformSpace relativeTo) |
---|
75 | { |
---|
76 | if (this->isPhysicsRunning()) |
---|
77 | ThrowException(PhysicsViolation, "Cannot change position or orientation of a StaticEntity with physics at run time."); |
---|
78 | if (this->isStatic()) |
---|
79 | { |
---|
80 | OrxAssert(relativeTo == Ogre::Node::TS_LOCAL, "Cannot translate physical object relative \ |
---|
81 | to any other space than TS_LOCAL."); |
---|
82 | this->physicalBody_->translate(btVector3(distance.x, distance.y, distance.z)); |
---|
83 | } |
---|
84 | |
---|
85 | this->node_->translate(distance, relativeTo); |
---|
86 | } |
---|
87 | |
---|
88 | void StaticEntity::setOrientation(const Quaternion& orientation) |
---|
89 | { |
---|
90 | if (this->isPhysicsRunning()) |
---|
91 | ThrowException(PhysicsViolation, "Cannot change position or orientation of a StaticEntity with physics at run time."); |
---|
92 | if (this->isStatic()) |
---|
93 | { |
---|
94 | btTransform transf = this->physicalBody_->getWorldTransform(); |
---|
95 | transf.setRotation(btQuaternion(orientation.x, orientation.y, orientation.z, orientation.w)); |
---|
96 | this->physicalBody_->setWorldTransform(transf); |
---|
97 | } |
---|
98 | |
---|
99 | this->node_->setOrientation(orientation); |
---|
100 | } |
---|
101 | |
---|
102 | void StaticEntity::rotate(const Quaternion& rotation, Ogre::Node::TransformSpace relativeTo) |
---|
103 | { |
---|
104 | if (this->isPhysicsRunning()) |
---|
105 | ThrowException(PhysicsViolation, "Cannot change position or orientation of a StaticEntity with physics at run time."); |
---|
106 | if (this->isStatic()) |
---|
107 | { |
---|
108 | OrxAssert(relativeTo == Ogre::Node::TS_LOCAL, "Cannot rotate physical object relative \ |
---|
109 | to any other space than TS_LOCAL."); |
---|
110 | btTransform transf = this->physicalBody_->getWorldTransform(); |
---|
111 | this->physicalBody_->setWorldTransform(transf * btTransform(btQuaternion(rotation.x, rotation.y, rotation.z, rotation.w))); |
---|
112 | } |
---|
113 | |
---|
114 | this->node_->rotate(rotation, relativeTo); |
---|
115 | } |
---|
116 | |
---|
117 | void StaticEntity::yaw(const Degree& angle, Ogre::Node::TransformSpace relativeTo) |
---|
118 | { |
---|
119 | if (this->isPhysicsRunning()) |
---|
120 | ThrowException(PhysicsViolation, "Cannot change position or orientation of a StaticEntity with physics at run time."); |
---|
121 | if (this->isStatic()) |
---|
122 | { |
---|
123 | OrxAssert(relativeTo == Ogre::Node::TS_LOCAL, "Cannot yaw physical object relative \ |
---|
124 | to any other space than TS_LOCAL."); |
---|
125 | btTransform transf = this->physicalBody_->getWorldTransform(); |
---|
126 | btTransform rotation(btQuaternion(angle.valueRadians(), 0.0f, 0.0f)); |
---|
127 | this->physicalBody_->setWorldTransform(transf * rotation); |
---|
128 | } |
---|
129 | |
---|
130 | this->node_->yaw(angle, relativeTo); |
---|
131 | } |
---|
132 | |
---|
133 | void StaticEntity::pitch(const Degree& angle, Ogre::Node::TransformSpace relativeTo) |
---|
134 | { |
---|
135 | if (this->isPhysicsRunning()) |
---|
136 | ThrowException(PhysicsViolation, "Cannot change position or orientation of a StaticEntity with physics at run time."); |
---|
137 | if (this->isStatic()) |
---|
138 | { |
---|
139 | OrxAssert(relativeTo == Ogre::Node::TS_LOCAL, "Cannot pitch physical object relative \ |
---|
140 | to any other space than TS_LOCAL."); |
---|
141 | btTransform transf = this->physicalBody_->getWorldTransform(); |
---|
142 | btTransform rotation(btQuaternion(0.0f, angle.valueRadians(), 0.0f)); |
---|
143 | this->physicalBody_->setWorldTransform(transf * rotation); |
---|
144 | } |
---|
145 | |
---|
146 | this->node_->pitch(angle, relativeTo); |
---|
147 | } |
---|
148 | |
---|
149 | void StaticEntity::roll(const Degree& angle, Ogre::Node::TransformSpace relativeTo) |
---|
150 | { |
---|
151 | if (this->isPhysicsRunning()) |
---|
152 | ThrowException(PhysicsViolation, "Cannot change position or orientation of a StaticEntity with physics at run time."); |
---|
153 | if (this->isStatic()) |
---|
154 | { |
---|
155 | OrxAssert(relativeTo == Ogre::Node::TS_LOCAL, "Cannot roll physical object relative \ |
---|
156 | to any other space than TS_LOCAL."); |
---|
157 | btTransform transf = this->physicalBody_->getWorldTransform(); |
---|
158 | btTransform rotation(btQuaternion(angle.valueRadians(), 0.0f, 0.0f)); |
---|
159 | this->physicalBody_->setWorldTransform(transf * rotation); |
---|
160 | } |
---|
161 | |
---|
162 | this->node_->roll(angle, relativeTo); |
---|
163 | } |
---|
164 | |
---|
165 | void StaticEntity::lookAt(const Vector3& target, Ogre::Node::TransformSpace relativeTo, const Vector3& localDirectionVector) |
---|
166 | { |
---|
167 | if (this->isPhysicsRunning()) |
---|
168 | ThrowException(PhysicsViolation, "Cannot change position or orientation of a StaticEntity with physics at run time."); |
---|
169 | if (this->isStatic()) |
---|
170 | { |
---|
171 | ThrowException(NotImplemented, "ControllableEntity::lookAt() is not yet supported for physical objects."); |
---|
172 | OrxAssert(relativeTo == Ogre::Node::TS_LOCAL, "Cannot align physical object relative \ |
---|
173 | to any other space than TS_LOCAL."); |
---|
174 | } |
---|
175 | |
---|
176 | this->node_->lookAt(target, relativeTo, localDirectionVector); |
---|
177 | } |
---|
178 | |
---|
179 | void StaticEntity::setDirection(const Vector3& direction, Ogre::Node::TransformSpace relativeTo, const Vector3& localDirectionVector) |
---|
180 | { |
---|
181 | if (this->isPhysicsRunning()) |
---|
182 | ThrowException(PhysicsViolation, "Cannot change position or orientation of a StaticEntity with physics at run time."); |
---|
183 | if (this->isStatic()) |
---|
184 | { |
---|
185 | ThrowException(NotImplemented, "ControllableEntity::setDirection() is not yet supported for physical objects."); |
---|
186 | OrxAssert(relativeTo == Ogre::Node::TS_LOCAL, "Cannot align physical object relative \ |
---|
187 | to any other space than TS_LOCAL."); |
---|
188 | } |
---|
189 | |
---|
190 | this->node_->setDirection(direction, relativeTo, localDirectionVector); |
---|
191 | } |
---|
192 | |
---|
193 | bool StaticEntity::isCollisionTypeLegal(WorldEntity::CollisionType type) const |
---|
194 | { |
---|
195 | if (type == WorldEntity::Kinematic || type == WorldEntity::Dynamic) |
---|
196 | { |
---|
197 | ThrowException(PhysicsViolation, "Cannot tell a StaticEntity to have kinematic or dynamic collision type"); |
---|
198 | return false; |
---|
199 | } |
---|
200 | else |
---|
201 | return true; |
---|
202 | } |
---|
203 | |
---|
204 | void StaticEntity::setWorldTransform(const btTransform& worldTrans) |
---|
205 | { |
---|
206 | OrxAssert(false, "Setting world transform of a StaticEntity, which is CF_STATIC!"); |
---|
207 | //COUT(0) << "Setting world transform of a StaticEntity, which is static!" << std::endl; |
---|
208 | } |
---|
209 | |
---|
210 | void StaticEntity::getWorldTransform(btTransform& worldTrans) const |
---|
211 | { |
---|
212 | worldTrans.setOrigin(btVector3(node_->getPosition().x, node_->getPosition().y, node_->getPosition().z)); |
---|
213 | worldTrans.setRotation(btQuaternion(node_->getOrientation().x, node_->getOrientation().y, node_->getOrientation().z, node_->getOrientation().w)); |
---|
214 | } |
---|
215 | } |
---|