Changeset 5898 for code/branches/core5/src
- Timestamp:
- Oct 7, 2009, 2:02:15 AM (15 years ago)
- Location:
- code/branches/core5/src/modules/pong
- Files:
-
- 7 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/core5/src/modules/pong/Pong.cc
r5893 r5898 76 76 this->ball_->setFieldDimension(this->center_->getFieldDimension()); 77 77 this->ball_->setSpeed(0); 78 this->ball_->setAccelerationFactor(this->center_->getBallAccelerationFactor()); 78 79 this->ball_->setBatLength(this->center_->getBatLength()); 79 80 … … 172 173 this->ball_->setPosition(Vector3::ZERO); 173 174 this->ball_->setVelocity(Vector3::ZERO); 175 this->ball_->setAcceleration(Vector3::ZERO); 174 176 this->ball_->setSpeed(0); 175 177 } -
code/branches/core5/src/modules/pong/PongAI.cc
r5831 r5898 49 49 this->ballEndPosition_ = 0; 50 50 this->randomOffset_ = 0; 51 this->bChangedRandomOffset_ = false; 51 52 this->relHysteresisOffset_ = 0.02f; 52 53 this->strength_ = 0.5f; … … 113 114 this->ballEndPosition_ = 0; 114 115 this->randomOffset_ = 0; 116 this->bChangedRandomOffset_ = false; 115 117 116 118 this->calculateRandomOffset(); … … 129 131 this->bOscillationAvoidanceActive_ = false; 130 132 } 133 134 // If the ball is close enough, calculate another random offset to accelerate the ball 135 if (!this->bChangedRandomOffset_) 136 { 137 float timetohit = (-this->ball_->getPosition().x + this->ball_->getFieldDimension().x / 2 * sgn(this->ball_->getVelocity().x)) / this->ball_->getVelocity().x; 138 if (timetohit < 0.05) 139 { 140 this->bChangedRandomOffset_ = true; 141 if (rnd() < this->strength_) 142 this->calculateRandomOffset(); 143 } 144 } 131 145 132 146 // Move to the predicted end position with an additional offset (to hit the ball with the side of the bat) … … 184 198 Vector3 position = this->ball_->getPosition(); 185 199 Vector3 velocity = this->ball_->getVelocity(); 200 Vector3 acceleration = this->ball_->getAcceleration(); 186 201 Vector2 dimension = this->ball_->getFieldDimension(); 187 202 188 // calculate end-height: current height + slope * distance 189 this->ballEndPosition_ = position.z + velocity.z / velocity.x * (-position.x + dimension.x / 2 * sgn(velocity.x)); 190 191 // Calculate bounces 192 for (float limit = 0.35f; limit < this->strength_ || this->strength_ > 0.99f; limit += 0.4f) 193 { 194 // Calculate a random prediction error, based on the vertical speed of the ball and the strength of the AI 195 float randomError = rnd(-1, 1) * dimension.y * (velocity.z / velocity.x / PongBall::MAX_REL_Z_VELOCITY) * (1 - this->strength_); 196 197 // Bounce from the lower bound 198 if (this->ballEndPosition_ > dimension.y / 2) 199 { 200 // Mirror the predicted position at the upper bound and add some random error 201 this->ballEndPosition_ = dimension.y - this->ballEndPosition_ + randomError; 202 continue; 203 } 204 // Bounce from the upper bound 205 if (this->ballEndPosition_ < -dimension.y / 2) 206 { 207 // Mirror the predicted position at the lower bound and add some random error 208 this->ballEndPosition_ = -dimension.y - this->ballEndPosition_ + randomError; 209 continue; 210 } 211 // No bounce - break 212 break; 203 // Calculate bounces. The number of predicted bounces is limited by the AIs strength 204 for (float limit = -0.05f; limit < this->strength_ || this->strength_ > 0.99f; limit += 0.4f) 205 { 206 // calculate the time until the ball reaches the other side 207 float totaltime = (-position.x + dimension.x / 2 * sgn(velocity.x)) / velocity.x; 208 209 // calculate wall bounce position (four possible solutions of the equation: pos.z + vel.z*t + acc.z/2*t^2 = +/- dim.z/2) 210 float bouncetime = totaltime; 211 bool bUpperWall = false; 212 213 if (acceleration.z == 0) 214 { 215 if (velocity.z > 0) 216 { 217 bUpperWall = true; 218 bouncetime = (dimension.y/2 - position.z) / velocity.z; 219 } 220 else if (velocity.z < 0) 221 { 222 bUpperWall = false; 223 bouncetime = (-dimension.y/2 - position.z) / velocity.z; 224 } 225 } 226 else 227 { 228 // upper wall 229 float temp = velocity.z*velocity.z + 2*acceleration.z*(dimension.y/2 - position.z); 230 if (temp >= 0) 231 { 232 float t1 = (sqrt(temp) - velocity.z) / acceleration.z; 233 float t2 = (sqrt(temp) + velocity.z) / acceleration.z * (-1); 234 if (t1 > 0 && t1 < bouncetime) 235 { 236 bouncetime = t1; 237 bUpperWall = true; 238 } 239 if (t2 > 0 && t2 < bouncetime) 240 { 241 bouncetime = t2; 242 bUpperWall = true; 243 } 244 } 245 // lower wall 246 temp = velocity.z*velocity.z - 2*acceleration.z*(dimension.y/2 + position.z); 247 if (temp >= 0) 248 { 249 float t1 = (sqrt(temp) - velocity.z) / acceleration.z; 250 float t2 = (sqrt(temp) + velocity.z) / acceleration.z * (-1); 251 if (t1 > 0 && t1 < bouncetime) 252 { 253 bouncetime = t1; 254 bUpperWall = false; 255 } 256 if (t2 > 0 && t2 < bouncetime) 257 { 258 bouncetime = t2; 259 bUpperWall = false; 260 } 261 } 262 } 263 264 if (bouncetime < totaltime) 265 { 266 // Calculate a random prediction error, based on the vertical speed of the ball and the strength of the AI 267 float randomErrorX = rnd(-1, 1) * dimension.y * (velocity.z / velocity.x / PongBall::MAX_REL_Z_VELOCITY) * (1 - this->strength_); 268 float randomErrorZ = rnd(-1, 1) * dimension.y * (velocity.z / velocity.x / PongBall::MAX_REL_Z_VELOCITY) * (1 - this->strength_); 269 270 // ball bounces after <bouncetime> seconds, update the position and continue 271 velocity.z = velocity.z + acceleration.z * bouncetime; 272 273 if (bUpperWall) 274 { 275 position.z = dimension.y / 2; 276 velocity.z = -fabs(velocity.z) + fabs(randomErrorZ); 277 } 278 else 279 { 280 position.z = -dimension.y / 2; 281 velocity.z = fabs(velocity.z) - fabs(randomErrorZ); 282 } 283 284 position.x = position.x + velocity.x * bouncetime + randomErrorX; 285 this->ballEndPosition_ = position.z; 286 } 287 else 288 { 289 // ball doesn't bounce, calculate the end position and return 290 // calculate end-height: current height + slope * distance incl. acceleration 291 this->ballEndPosition_ = position.z + velocity.z * totaltime + acceleration.z / 2 * totaltime * totaltime; 292 return; 293 } 213 294 } 214 295 } -
code/branches/core5/src/modules/pong/PongAI.h
r5831 r5898 62 62 float ballEndPosition_; 63 63 float randomOffset_; 64 bool bChangedRandomOffset_; 64 65 float relHysteresisOffset_; 65 66 float strength_; -
code/branches/core5/src/modules/pong/PongBall.cc
r5892 r5898 46 46 47 47 this->speed_ = 0; 48 this->accelerationFactor_ = 1.0f; 48 49 this->bat_ = 0; 49 50 this->batID_ = new unsigned int[2]; … … 76 77 Vector3 position = this->getPosition(); 77 78 Vector3 velocity = this->getVelocity(); 79 Vector3 acceleration = this->getAcceleration(); 78 80 79 81 if (position.z > this->fieldHeight_ / 2 || position.z < -this->fieldHeight_ / 2) … … 102 104 velocity.x = -velocity.x; 103 105 velocity.z = distance * distance * sgn(distance) * PongBall::MAX_REL_Z_VELOCITY * this->speed_; 106 acceleration = this->bat_[1]->getVelocity() * this->accelerationFactor_ * -1; 104 107 105 108 this->fireEvent(); … … 122 125 velocity.x = -velocity.x; 123 126 velocity.z = distance * distance * sgn(distance) * PongBall::MAX_REL_Z_VELOCITY * this->speed_; 127 acceleration = this->bat_[0]->getVelocity() * this->accelerationFactor_ * -1; 124 128 125 129 this->fireEvent(); … … 137 141 } 138 142 143 if (acceleration != this->getAcceleration()) 144 this->setAcceleration(acceleration); 139 145 if (velocity != this->getVelocity()) 140 146 this->setVelocity(velocity); -
code/branches/core5/src/modules/pong/PongBall.h
r5892 r5898 58 58 { return this->speed_; } 59 59 60 void setAccelerationFactor(float factor) 61 { this->accelerationFactor_ = factor; } 62 float getAccelerationFactor() const 63 { return this->accelerationFactor_; } 64 60 65 void setBatLength(float batlength) 61 66 { this->batlength_ = batlength; } … … 72 77 float fieldHeight_; 73 78 float speed_; 79 float accelerationFactor_; 74 80 float batlength_; 75 81 PongBat** bat_; -
code/branches/core5/src/modules/pong/PongCenterpoint.cc
r5806 r5898 44 44 this->height_ = 120; 45 45 this->ballspeed_ = 100; 46 this->ballaccfactor_ = 1.0; 46 47 this->batspeed_ = 60; 47 48 this->batlength_ = 0.25; … … 58 59 XMLPortParam(PongCenterpoint, "battemplate", setBattemplate, getBattemplate, xmlelement, mode); 59 60 XMLPortParam(PongCenterpoint, "ballspeed", setBallSpeed, getBallSpeed, xmlelement, mode); 61 XMLPortParam(PongCenterpoint, "ballaccfactor", setBallAccelerationFactor, getBallAccelerationFactor, xmlelement, mode); 60 62 XMLPortParam(PongCenterpoint, "batspeed", setBatSpeed, getBatSpeed, xmlelement, mode); 61 63 XMLPortParam(PongCenterpoint, "batlength", setBatLength, getBatLength, xmlelement, mode); -
code/branches/core5/src/modules/pong/PongCenterpoint.h
r5738 r5898 68 68 { return this->ballspeed_; } 69 69 70 void setBallAccelerationFactor(float ballaccfactor) 71 { this->ballaccfactor_ = ballaccfactor; } 72 float getBallAccelerationFactor() const 73 { return this->ballaccfactor_; } 74 70 75 void setBatSpeed(float batspeed) 71 76 { this->batspeed_ = batspeed; } … … 85 90 86 91 float ballspeed_; 92 float ballaccfactor_; 87 93 float batspeed_; 88 94 float batlength_;
Note: See TracChangeset
for help on using the changeset viewer.