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 | * Co-authors: |
---|
25 | * ... |
---|
26 | * |
---|
27 | */ |
---|
28 | |
---|
29 | /** |
---|
30 | @file ParticleProjectile.h |
---|
31 | @brief Implementation of the ParticleProjectile class. |
---|
32 | */ |
---|
33 | |
---|
34 | #include "BallProjectile.h" |
---|
35 | |
---|
36 | #include <OgreParticleEmitter.h> |
---|
37 | #include "core/CoreIncludes.h" |
---|
38 | #include "tools/ParticleInterface.h" |
---|
39 | #include "Scene.h" |
---|
40 | #include "core/command/Executor.h" |
---|
41 | #include "util/Convert.h" |
---|
42 | #include <bullet/BulletCollision/NarrowPhaseCollision/btManifoldPoint.h> |
---|
43 | #include <bullet/LinearMath/btVector3.h> |
---|
44 | |
---|
45 | namespace orxonox |
---|
46 | { |
---|
47 | RegisterClass(BallProjectile); |
---|
48 | |
---|
49 | BallProjectile::BallProjectile(Context* context) : BillboardProjectile(context) |
---|
50 | { |
---|
51 | RegisterObject(BallProjectile); |
---|
52 | this->textureIndex_ = 1; |
---|
53 | this->setMass(0.1f); |
---|
54 | this->maxTextureIndex_ = 8; |
---|
55 | this->setDestroyAfterCollision(false); //I want the ball to bounce, not to be destroyed |
---|
56 | |
---|
57 | //setEffect("Orxonox/sparks2"); |
---|
58 | } |
---|
59 | |
---|
60 | void BallProjectile::registerVariables() |
---|
61 | { |
---|
62 | registerVariable( this->fieldWidth_ ); |
---|
63 | registerVariable( this->fieldHeight_ ); |
---|
64 | registerVariable(this->materialBase_); |
---|
65 | registerVariable( this->speed_ ); |
---|
66 | } |
---|
67 | |
---|
68 | /** |
---|
69 | @brief |
---|
70 | Set the material. |
---|
71 | @param material |
---|
72 | The name of the material. Material names with 1 to 8 appended must exist. |
---|
73 | */ |
---|
74 | void BallProjectile::setMaterial(const std::string& material) |
---|
75 | { |
---|
76 | this->materialBase_ = material; |
---|
77 | |
---|
78 | BillboardProjectile::setMaterial(material + multi_cast<std::string>(this->textureIndex_)); |
---|
79 | } |
---|
80 | |
---|
81 | /** |
---|
82 | @brief |
---|
83 | Change the texture. |
---|
84 | */ |
---|
85 | void BallProjectile::changeTexture() |
---|
86 | { |
---|
87 | this->textureIndex_++; |
---|
88 | if (this->textureIndex_ > this->maxTextureIndex_) |
---|
89 | this->textureIndex_ = 1; |
---|
90 | |
---|
91 | this->setMaterial(this->materialBase_); |
---|
92 | } |
---|
93 | |
---|
94 | |
---|
95 | |
---|
96 | void BallProjectile::Bounce(WorldEntity* otherObject, btManifoldPoint& contactPoint, const btCollisionShape* cs) { |
---|
97 | |
---|
98 | Vector3 velocity = this->getVelocity(); |
---|
99 | Vector3 myPosition = otherObject->getPosition(); |
---|
100 | btVector3 positionOtherObject = contactPoint.getPositionWorldOnA(); |
---|
101 | orxout() << "About to Bounce >D" << endl; |
---|
102 | //if (positionOtherObject.y < 0) { |
---|
103 | //this.destroy() |
---|
104 | //}S |
---|
105 | //else { |
---|
106 | |
---|
107 | int distance_X = positionOtherObject.getX() - myPosition.x; |
---|
108 | int distance_Z = positionOtherObject.getZ() - myPosition.z; |
---|
109 | |
---|
110 | if (distance_X < 0) |
---|
111 | distance_X = -distance_X; |
---|
112 | |
---|
113 | |
---|
114 | if (distance_Z < 0) |
---|
115 | distance_Z = -distance_Z; |
---|
116 | |
---|
117 | orxout() << distance_X << endl; |
---|
118 | orxout() << distance_Z << endl; |
---|
119 | |
---|
120 | if (distance_X < distance_Z) { |
---|
121 | velocity.z = -velocity.z; |
---|
122 | orxout() << "z" << endl; |
---|
123 | } |
---|
124 | if (distance_Z < distance_X) { |
---|
125 | velocity.x = -velocity.x; |
---|
126 | orxout() << "x" << endl; |
---|
127 | } |
---|
128 | else { |
---|
129 | velocity.x = -velocity.x; |
---|
130 | velocity.z = -velocity.z; |
---|
131 | orxout() << "both" << endl; |
---|
132 | } |
---|
133 | this->setVelocity(velocity); |
---|
134 | //} |
---|
135 | } |
---|
136 | |
---|
137 | |
---|
138 | |
---|
139 | |
---|
140 | |
---|
141 | bool BallProjectile::processCollision(WorldEntity* otherObject, btManifoldPoint& contactPoint, const btCollisionShape* cs) |
---|
142 | { |
---|
143 | |
---|
144 | orxout() << "wanna bounce..." << endl; |
---|
145 | bool result = BasicProjectile::processCollision(otherObject, contactPoint, cs); |
---|
146 | Bounce(otherObject, contactPoint, cs); |
---|
147 | orxout() << "BOUNCED!" << endl; |
---|
148 | |
---|
149 | return result; |
---|
150 | } |
---|
151 | |
---|
152 | |
---|
153 | |
---|
154 | |
---|
155 | |
---|
156 | |
---|
157 | void BallProjectile::tick(float dt) |
---|
158 | { |
---|
159 | SUPER(BallProjectile, tick, dt); |
---|
160 | |
---|
161 | // Get the current position, velocity and acceleration of the ball. |
---|
162 | Vector3 position = this->getPosition(); |
---|
163 | Vector3 velocity = this->getVelocity(); |
---|
164 | Vector3 acceleration = this->getAcceleration(); |
---|
165 | |
---|
166 | // If the ball has gone over the top or bottom boundary of the playing field (i.e. the ball has hit the top or bottom delimiters). |
---|
167 | if (position.z > this->fieldHeight_ / 2 || position.z < -this->fieldHeight_ / 2) |
---|
168 | { |
---|
169 | // Its velocity in z-direction is inverted (i.e. it bounces off). |
---|
170 | velocity.z = -velocity.z; |
---|
171 | // And its position is set as to not overstep the boundary it has just crossed. |
---|
172 | if (position.z > this->fieldHeight_ / 2) |
---|
173 | position.z = this->fieldHeight_ / 2; |
---|
174 | if (position.z < -this->fieldHeight_ / 2) |
---|
175 | position.z = -this->fieldHeight_ / 2; |
---|
176 | |
---|
177 | this->fireEvent(); |
---|
178 | } |
---|
179 | |
---|
180 | //Ball hits the right or left wall and should bounce back. |
---|
181 | // If the ball has crossed the left or right boundary of the playing field. |
---|
182 | if (position.x > this->fieldWidth_ / 2 || position.x < -this->fieldWidth_ / 2) |
---|
183 | { |
---|
184 | //Ball hits the right Wall |
---|
185 | if (position.x > this->fieldWidth_ / 2) |
---|
186 | { |
---|
187 | // Set the ball to be exactly at the boundary. |
---|
188 | position.x = this->fieldWidth_ / 2; |
---|
189 | // Invert its velocity in x-direction (i.e. it bounces off). |
---|
190 | velocity.x = -velocity.x; |
---|
191 | this->fireEvent(); |
---|
192 | } |
---|
193 | |
---|
194 | //Ball hits the left wall |
---|
195 | else if (position.x < -this->fieldWidth_ / 2) |
---|
196 | { |
---|
197 | // Set the ball to be exactly at the boundary. |
---|
198 | position.x = -this->fieldWidth_ / 2; |
---|
199 | // Invert its velocity in x-direction (i.e. it bounces off). |
---|
200 | velocity.x = -velocity.x; |
---|
201 | this->fireEvent(); |
---|
202 | } |
---|
203 | } |
---|
204 | |
---|
205 | // Set the position, velocity and acceleration of the ball, if they have changed. |
---|
206 | if (acceleration != this->getAcceleration()) |
---|
207 | this->setAcceleration(acceleration); |
---|
208 | if (velocity != this->getVelocity()) |
---|
209 | this->setVelocity(velocity); |
---|
210 | if (position != this->getPosition()) |
---|
211 | this->setPosition(position); |
---|
212 | } |
---|
213 | |
---|
214 | |
---|
215 | |
---|
216 | |
---|
217 | void BallProjectile::setSpeed(float speed) |
---|
218 | { |
---|
219 | if (speed != this->speed_) // If the speed changes |
---|
220 | { |
---|
221 | this->speed_ = speed; |
---|
222 | |
---|
223 | // Set the speed in the direction of the balls current velocity. |
---|
224 | Vector3 velocity = this->getVelocity(); |
---|
225 | if (velocity.x != 0) |
---|
226 | velocity.x = sgn(velocity.x) * this->speed_; |
---|
227 | else // If the balls current velocity is zero, the speed is set in a random direction. |
---|
228 | velocity.x = this->speed_ * sgn(rnd(-1,1)); |
---|
229 | |
---|
230 | this->setVelocity(velocity); |
---|
231 | } |
---|
232 | } |
---|
233 | |
---|
234 | |
---|
235 | } |
---|