- Timestamp:
- Apr 18, 2019, 4:04:33 PM (6 years ago)
- Location:
- code/branches/OrxoBlox_FS19/src/modules/weapons/projectiles
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/OrxoBlox_FS19/src/modules/weapons/projectiles/BallProjectile.cc
r12291 r12310 40 40 #include "core/command/Executor.h" 41 41 #include "util/Convert.h" 42 #include <bullet/BulletCollision/NarrowPhaseCollision/btManifoldPoint.h> 43 #include <bullet/LinearMath/btVector3.h> 42 44 43 45 namespace orxonox … … 58 60 void BallProjectile::registerVariables() 59 61 { 62 registerVariable( this->fieldWidth_ ); 63 registerVariable( this->fieldHeight_ ); 60 64 registerVariable(this->materialBase_); 65 registerVariable( this->speed_ ); 61 66 } 62 67 … … 87 92 } 88 93 94 95 89 96 void BallProjectile::Bounce(WorldEntity* otherObject, btManifoldPoint& contactPoint, const btCollisionShape* cs) { 90 97 91 98 Vector3 velocity = this->getVelocity(); 92 Vector3 positionOtherObject= otherObject->getPosition();93 Vector3 contactPosition = this->getPosition();99 Vector3 myPosition = otherObject->getPosition(); 100 btVector3 positionOtherObject = contactPoint.getPositionWorldOnA(); 94 101 orxout() << "About to Bounce >D" << endl; 95 102 //if (positionOtherObject.y < 0) { … … 98 105 //else { 99 106 100 int distance_X = positionOtherObject. x - contactPosition.x;101 int distance_ Y = positionOtherObject.y - contactPosition.y;107 int distance_X = positionOtherObject.getX() - myPosition.x; 108 int distance_Z = positionOtherObject.getZ() - myPosition.z; 102 109 103 110 if (distance_X < 0) 104 distance_ Y = -distance_Y;111 distance_X = -distance_X; 105 112 106 113 107 if (distance_Y < 0) 108 distance_X = -distance_X; 109 110 if (distance_X < distance_Y) 111 velocity.y = -velocity.y; 112 if (distance_Y < distance_X) 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) { 113 125 velocity.x = -velocity.x; 126 orxout() << "x" << endl; 127 } 114 128 else { 115 129 velocity.x = -velocity.x; 116 velocity.y = -velocity.y; 130 velocity.z = -velocity.z; 131 orxout() << "both" << endl; 117 132 } 133 this->setVelocity(velocity); 118 134 //} 119 135 } 120 136 121 /** 122 @brief 123 The function called when a projectile hits another thing. 124 Calls the hit-function, starts the shield recharge countdown, displays visual hit effects defined in Pawn. 125 Needs to be called in the collidesAgainst() function by every Class directly inheriting from BasicProjectile. 126 @param otherObject 127 A pointer to the object the Projectile has collided against. 128 @param contactPoint 129 A btManifoldPoint indicating the point of contact/impact. 130 @param cs 131 The btCollisionShape of the other object 132 @return 133 Returns true if the collision resulted in a successful hit. 134 @see Pawn.h 135 */ 137 138 139 136 140 137 141 bool BallProjectile::processCollision(WorldEntity* otherObject, btManifoldPoint& contactPoint, const btCollisionShape* cs) … … 145 149 return result; 146 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 147 235 } -
code/branches/OrxoBlox_FS19/src/modules/weapons/projectiles/BallProjectile.h
r12281 r12310 54 54 void Bounce(WorldEntity* otherObject, btManifoldPoint& contactPoint, const btCollisionShape* cs); 55 55 virtual void setMaterial(const std::string& material) override; 56 virtual void tick(float dt) override; 57 void setSpeed(float speed); 58 56 59 57 60 protected: … … 63 66 void changeTexture(); 64 67 68 float speed_; //!< The speed (in x-direction) of the ball. 69 float fieldWidth_; //!< The width of the playing field. 70 float fieldHeight_; //!< The height of the playing field. 65 71 unsigned int textureIndex_; //!< The current index of the texture. (i.e. the index of the currently displayed texture) 66 72 unsigned int maxTextureIndex_; //!< The maximal index.
Note: See TracChangeset
for help on using the changeset viewer.