Changeset 8194 for code/branches/dockingsystem/src/modules/pong/PongBall.cc
- Timestamp:
- Apr 6, 2011, 12:07:29 PM (13 years ago)
- Location:
- code/branches/dockingsystem
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/dockingsystem
- Property svn:mergeinfo changed
/code/branches/lastmanstanding3 (added) merged: 7903,8155,8165,8170-8175 /code/branches/tetris (added) merged: 8104-8107 /code/trunk (added) merged: 8108,8178-8179,8193
- Property svn:mergeinfo changed
-
code/branches/dockingsystem/src/modules/pong/PongBall.cc
r7886 r8194 27 27 */ 28 28 29 /** 30 @file PongBall.cc 31 @brief Implementation of the PongBall class. 32 */ 33 29 34 #include "PongBall.h" 30 35 31 36 #include "core/CoreIncludes.h" 32 37 #include "core/GameMode.h" 38 33 39 #include "gametypes/Gametype.h" 40 34 41 #include "PongBat.h" 35 42 … … 40 47 const float PongBall::MAX_REL_Z_VELOCITY = 1.5; 41 48 49 /** 50 @brief 51 Constructor. Registers and initializes the object. 52 */ 42 53 PongBall::PongBall(BaseObject* creator) 43 54 : MovableEntity(creator) … … 57 68 } 58 69 70 /** 71 @brief 72 Destructor. 73 */ 59 74 PongBall::~PongBall() 60 75 { … … 68 83 } 69 84 85 /** 86 @brief 87 Register variables to synchronize over the network. 88 */ 70 89 void PongBall::registerVariables() 71 90 { … … 79 98 } 80 99 100 /** 101 @brief 102 Is called every tick. 103 Handles the movement of the ball and its interaction with the boundaries and bats. 104 @param dt 105 The time since the last tick. 106 */ 81 107 void PongBall::tick(float dt) 82 108 { 83 109 SUPER(PongBall, tick, dt); 84 110 111 // Get the current position, velocity and acceleration of the ball. 85 112 Vector3 position = this->getPosition(); 86 113 Vector3 velocity = this->getVelocity(); 87 114 Vector3 acceleration = this->getAcceleration(); 88 115 116 // 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). 89 117 if (position.z > this->fieldHeight_ / 2 || position.z < -this->fieldHeight_ / 2) 90 118 { 119 // Its velocity in z-direction is inverted (i.e. it bounces off). 91 120 velocity.z = -velocity.z; 121 // And its position is set as to not overstep the boundary it has just crossed. 92 122 if (position.z > this->fieldHeight_ / 2) 93 123 position.z = this->fieldHeight_ / 2; … … 98 128 } 99 129 130 // If the ball has crossed the left or right boundary of the playing field (i.e. a player has just scored, if the bat isn't there to parry). 100 131 if (position.x > this->fieldWidth_ / 2 || position.x < -this->fieldWidth_ / 2) 101 132 { 102 133 float distance = 0; 103 134 104 if (this->bat_ )135 if (this->bat_ != NULL) // If there are bats. 105 136 { 106 if (position.x > this->fieldWidth_ / 2 && this->bat_[1]) 137 // If the right boundary has been crossed. 138 if (position.x > this->fieldWidth_ / 2 && this->bat_[1] != NULL) 107 139 { 140 // Calculate the distance (in z-direction) between the ball and the center of the bat, weighted by half of the effective length of the bat (with additional 10%) 108 141 distance = (position.z - this->bat_[1]->getPosition().z) / (this->fieldHeight_ * (this->batlength_ * 1.10f) / 2); 109 if (fabs(distance) <= 1) 110 { 142 if (fabs(distance) <= 1) // If the bat is there to parry. 143 { 144 // Set the ball to be exactly at the boundary. 111 145 position.x = this->fieldWidth_ / 2; 146 // Invert its velocity in x-direction (i.e. it bounces off). 112 147 velocity.x = -velocity.x; 148 // Adjust the velocity in the z-direction, depending on where the ball hit the bat. 113 149 velocity.z = distance * distance * sgn(distance) * PongBall::MAX_REL_Z_VELOCITY * this->speed_; 114 150 acceleration = this->bat_[1]->getVelocity() * this->accelerationFactor_ * -1; … … 116 152 this->fireEvent(); 117 153 } 154 // If the left player scores. 118 155 else if (GameMode::isMaster() && position.x > this->fieldWidth_ / 2 * (1 + this->relMercyOffset_)) 119 156 { … … 125 162 } 126 163 } 127 if (position.x < -this->fieldWidth_ / 2 && this->bat_[0]) 164 // If the left boundary has been crossed. 165 else if (position.x < -this->fieldWidth_ / 2 && this->bat_[0] != NULL) 128 166 { 167 // Calculate the distance (in z-direction) between the ball and the center of the bat, weighted by half of the effective length of the bat (with additional 10%) 129 168 distance = (position.z - this->bat_[0]->getPosition().z) / (this->fieldHeight_ * (this->batlength_ * 1.10f) / 2); 130 if (fabs(distance) <= 1) 131 { 169 if (fabs(distance) <= 1) // If the bat is there to parry. 170 { 171 // Set the ball to be exactly at the boundary. 132 172 position.x = -this->fieldWidth_ / 2; 173 // Invert its velocity in x-direction (i.e. it bounces off). 133 174 velocity.x = -velocity.x; 175 // Adjust the velocity in the z-direction, depending on where the ball hit the bat. 134 176 velocity.z = distance * distance * sgn(distance) * PongBall::MAX_REL_Z_VELOCITY * this->speed_; 135 177 acceleration = this->bat_[0]->getVelocity() * this->accelerationFactor_ * -1; … … 137 179 this->fireEvent(); 138 180 } 181 // If the right player scores. 139 182 else if (GameMode::isMaster() && position.x < -this->fieldWidth_ / 2 * (1 + this->relMercyOffset_)) 140 183 { … … 149 192 } 150 193 194 // Set the position, velocity and acceleration of the ball, if they have changed. 151 195 if (acceleration != this->getAcceleration()) 152 196 this->setAcceleration(acceleration); … … 157 201 } 158 202 203 /** 204 @brief 205 Set the speed of the ball (in x-direction). 206 @param speed 207 The speed to be set. 208 */ 159 209 void PongBall::setSpeed(float speed) 160 210 { 161 if (speed != this->speed_) 211 if (speed != this->speed_) // If the speed changes 162 212 { 163 213 this->speed_ = speed; 164 214 215 // Set the speed in the direction of the balls current velocity. 165 216 Vector3 velocity = this->getVelocity(); 166 217 if (velocity.x != 0) 167 218 velocity.x = sgn(velocity.x) * this->speed_; 168 else 219 else // If the balls current velocity is zero, the speed is set in a random direction. 169 220 velocity.x = this->speed_ * sgn(rnd(-1,1)); 170 221 … … 173 224 } 174 225 226 /** 227 @brief 228 Set the bats for the ball. 229 @param bats 230 An array (of size 2) of weak pointers, to be set as the new bats. 231 */ 175 232 void PongBall::setBats(WeakPtr<PongBat>* bats) 176 233 { 177 if (this->bDeleteBats_) 234 if (this->bDeleteBats_) // If there are already some bats, delete them. 178 235 { 179 236 delete[] this->bat_; … … 182 239 183 240 this->bat_ = bats; 241 // Also store their object IDs, for synchronization. 184 242 this->batID_[0] = this->bat_[0]->getObjectID(); 185 243 this->batID_[1] = this->bat_[1]->getObjectID(); 186 244 } 187 245 246 /** 247 @brief 248 Get the bats over the network. 249 */ 188 250 void PongBall::applyBats() 189 251 { 190 if (!this->bat_) 252 // Make space for the bats, if they don't exist, yet. 253 if (this->bat_ == NULL) 191 254 { 192 255 this->bat_ = new WeakPtr<PongBat>[2];
Note: See TracChangeset
for help on using the changeset viewer.