Changeset 5935 for code/branches/pickup/src/modules/pong
- Timestamp:
- Oct 13, 2009, 5:05:17 PM (15 years ago)
- Location:
- code/branches/pickup
- Files:
-
- 1 deleted
- 12 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/pickup
- Property svn:mergeinfo changed
-
code/branches/pickup/src/modules/pong/CMakeLists.txt
r5781 r5935 1 1 SET_SOURCE_FILES(PONG_SRC_FILES 2 COMPILATION_BEGIN PongCompilation.cc 2 3 Pong.cc 3 4 PongAI.cc … … 7 8 PongCenterpoint.cc 8 9 PongScore.cc 10 COMPILATION_END 9 11 ) 10 12 … … 12 14 MODULE 13 15 FIND_HEADER_FILES 14 PCH_FILE15 PongPrecompiledHeaders.h16 PCH_NO_DEFAULT17 16 DEFINE_SYMBOL 18 17 "PONG_SHARED_BUILD" -
code/branches/pickup/src/modules/pong/Pong.cc
r5781 r5935 30 30 31 31 #include "core/CoreIncludes.h" 32 #include "core/EventIncludes.h" 32 33 #include "core/Executor.h" 33 34 #include "PongCenterpoint.h" … … 39 40 namespace orxonox 40 41 { 42 CreateEventName(PongCenterpoint, right); 43 CreateEventName(PongCenterpoint, left); 44 41 45 CreateUnloadableFactory(Pong); 42 46 … … 52 56 this->setHUDTemplate("PongHUD"); 53 57 54 this->starttimer_.setTimer(1.0, false, this, createExecutor(createFunctor(&Pong::startBall)));58 this->starttimer_.setTimer(1.0, false, createExecutor(createFunctor(&Pong::startBall, this))); 55 59 this->starttimer_.stopTimer(); 56 60 … … 72 76 this->ball_->setFieldDimension(this->center_->getFieldDimension()); 73 77 this->ball_->setSpeed(0); 78 this->ball_->setAccelerationFactor(this->center_->getBallAccelerationFactor()); 74 79 this->ball_->setBatLength(this->center_->getBatLength()); 75 80 … … 120 125 if (this->ball_) 121 126 { 122 delete this->ball_;127 this->ball_->destroy(); 123 128 this->ball_ = 0; 124 129 } … … 155 160 if (this->center_) 156 161 { 157 this->center_->fireEvent(); 158 162 if (player == this->getRightPlayer()) 163 this->center_->fireEvent(FireEventName(PongCenterpoint, right)); 164 else if (player == this->getLeftPlayer()) 165 this->center_->fireEvent(FireEventName(PongCenterpoint, left)); 166 159 167 if (player) 160 this->gtinfo_ .sendAnnounceMessage(player->getName() + " scored");168 this->gtinfo_->sendAnnounceMessage(player->getName() + " scored"); 161 169 } 162 170 … … 165 173 this->ball_->setPosition(Vector3::ZERO); 166 174 this->ball_->setVelocity(Vector3::ZERO); 175 this->ball_->setAcceleration(Vector3::ZERO); 167 176 this->ball_->setSpeed(0); 168 177 } -
code/branches/pickup/src/modules/pong/Pong.h
r5781 r5935 62 62 PongBall* ball_; 63 63 PongBat* bat_[2]; 64 Timer <Pong>starttimer_;64 Timer starttimer_; 65 65 }; 66 66 } -
code/branches/pickup/src/modules/pong/PongAI.cc
r5781 r5935 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; … … 60 61 PongAI::~PongAI() 61 62 { 62 for (std::list<std::pair<Timer <PongAI>*, char> >::iterator it = this->reactionTimers_.begin(); it != this->reactionTimers_.end(); ++it)63 delete (*it).first;63 for (std::list<std::pair<Timer*, char> >::iterator it = this->reactionTimers_.begin(); it != this->reactionTimers_.end(); ++it) 64 (*it).first->destroy(); 64 65 } 65 66 … … 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 } … … 231 312 232 313 // Add a new Timer 233 Timer <PongAI>* timer = new Timer<PongAI>(delay, false, this, createExecutor(createFunctor(&PongAI::delayedMove)));234 this->reactionTimers_.push_back(std::pair<Timer <PongAI>*, char>(timer, direction));314 Timer* timer = new Timer(delay, false, createExecutor(createFunctor(&PongAI::delayedMove, this))); 315 this->reactionTimers_.push_back(std::pair<Timer*, char>(timer, direction)); 235 316 } 236 317 else … … 246 327 247 328 // Destroy the timer and remove it from the list 248 Timer <PongAI>* timer = this->reactionTimers_.front().first;249 delete timer;329 Timer* timer = this->reactionTimers_.front().first; 330 timer->destroy(); 250 331 251 332 this->reactionTimers_.pop_front(); -
code/branches/pickup/src/modules/pong/PongAI.h
r5781 r5935 62 62 float ballEndPosition_; 63 63 float randomOffset_; 64 bool bChangedRandomOffset_; 64 65 float relHysteresisOffset_; 65 66 float strength_; 66 67 67 std::list<std::pair<Timer <PongAI>*, char> > reactionTimers_;68 std::list<std::pair<Timer*, char> > reactionTimers_; 68 69 char movement_; 69 70 char oldMove_; -
code/branches/pickup/src/modules/pong/PongBall.cc
r5781 r5935 33 33 #include "gametypes/Gametype.h" 34 34 #include "PongBat.h" 35 #include "sound/SoundBase.h"36 35 37 36 namespace orxonox … … 41 40 const float PongBall::MAX_REL_Z_VELOCITY = 1.5; 42 41 43 PongBall::PongBall(BaseObject* creator) : MovableEntity(creator) 42 PongBall::PongBall(BaseObject* creator) 43 : MovableEntity(creator) 44 44 { 45 45 RegisterObject(PongBall); 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]; … … 53 54 54 55 this->registerVariables(); 56 } 55 57 56 this->sidesound_ = new SoundBase(this); 57 this->sidesound_->loadFile("sounds/pong_side.wav"); 58 59 this->batsound_ = new SoundBase(this); 60 this->batsound_->loadFile("sounds/pong_bat.wav"); 61 62 this->scoresound_ = new SoundBase(this); 63 this->scoresound_->loadFile("sounds/pong_score.wav"); 58 PongBall::~PongBall() 59 { 64 60 } 65 61 … … 79 75 SUPER(PongBall, tick, dt); 80 76 81 if (GameMode::isMaster()) 77 Vector3 position = this->getPosition(); 78 Vector3 velocity = this->getVelocity(); 79 Vector3 acceleration = this->getAcceleration(); 80 81 if (position.z > this->fieldHeight_ / 2 || position.z < -this->fieldHeight_ / 2) 82 82 { 83 Vector3 position = this->getPosition(); 84 Vector3 velocity = this->getVelocity(); 83 velocity.z = -velocity.z; 84 if (position.z > this->fieldHeight_ / 2) 85 position.z = this->fieldHeight_ / 2; 86 if (position.z < -this->fieldHeight_ / 2) 87 position.z = -this->fieldHeight_ / 2; 85 88 86 if (position.z > this->fieldHeight_ / 2 || position.z < -this->fieldHeight_ / 2) 89 this->fireEvent(); 90 } 91 92 if (position.x > this->fieldWidth_ / 2 || position.x < -this->fieldWidth_ / 2) 93 { 94 float distance = 0; 95 96 if (this->bat_) 87 97 { 88 velocity.z = -velocity.z; 89 this->sidesound_->play(); 90 91 if (position.z > this->fieldHeight_ / 2) 92 position.z = this->fieldHeight_ / 2; 93 if (position.z < -this->fieldHeight_ / 2) 94 position.z = -this->fieldHeight_ / 2; 95 } 96 97 if (position.x > this->fieldWidth_ / 2 || position.x < -this->fieldWidth_ / 2) 98 { 99 float distance = 0; 100 101 if (this->bat_) 98 if (position.x > this->fieldWidth_ / 2 && this->bat_[1]) 102 99 { 103 if (position.x > this->fieldWidth_ / 2 && this->bat_[1]) 100 distance = (position.z - this->bat_[1]->getPosition().z) / (this->fieldHeight_ * (this->batlength_ * 1.10f) / 2); 101 if (fabs(distance) <= 1) 104 102 { 105 distance = (position.z - this->bat_[1]->getPosition().z) / (this->fieldHeight_ * (this->batlength_ * 1.10f) / 2); 106 if (fabs(distance) <= 1) 103 position.x = this->fieldWidth_ / 2; 104 velocity.x = -velocity.x; 105 velocity.z = distance * distance * sgn(distance) * PongBall::MAX_REL_Z_VELOCITY * this->speed_; 106 acceleration = this->bat_[1]->getVelocity() * this->accelerationFactor_ * -1; 107 108 this->fireEvent(); 109 } 110 else if (GameMode::isMaster() && position.x > this->fieldWidth_ / 2 * (1 + this->relMercyOffset_)) 111 { 112 if (this->getGametype() && this->bat_[0]) 107 113 { 108 position.x = this->fieldWidth_ / 2; 109 velocity.x = -velocity.x; 110 velocity.z = distance * distance * sgn(distance) * PongBall::MAX_REL_Z_VELOCITY * this->speed_; 111 this->batsound_->play(); 112 } 113 else if (position.x > this->fieldWidth_ / 2 * (1 + this->relMercyOffset_)) 114 { 115 if (this->getGametype() && this->bat_[0]) 116 { 117 this->getGametype()->playerScored(this->bat_[0]->getPlayer()); 118 this->scoresound_->play(); 119 return; 120 } 114 this->getGametype()->playerScored(this->bat_[0]->getPlayer()); 115 return; 121 116 } 122 117 } 123 if (position.x < -this->fieldWidth_ / 2 && this->bat_[0]) 118 } 119 if (position.x < -this->fieldWidth_ / 2 && this->bat_[0]) 120 { 121 distance = (position.z - this->bat_[0]->getPosition().z) / (this->fieldHeight_ * (this->batlength_ * 1.10f) / 2); 122 if (fabs(distance) <= 1) 124 123 { 125 distance = (position.z - this->bat_[0]->getPosition().z) / (this->fieldHeight_ * (this->batlength_ * 1.10f) / 2); 126 if (fabs(distance) <= 1) 124 position.x = -this->fieldWidth_ / 2; 125 velocity.x = -velocity.x; 126 velocity.z = distance * distance * sgn(distance) * PongBall::MAX_REL_Z_VELOCITY * this->speed_; 127 acceleration = this->bat_[0]->getVelocity() * this->accelerationFactor_ * -1; 128 129 this->fireEvent(); 130 } 131 else if (GameMode::isMaster() && position.x < -this->fieldWidth_ / 2 * (1 + this->relMercyOffset_)) 132 { 133 if (this->getGametype() && this->bat_[1]) 127 134 { 128 position.x = -this->fieldWidth_ / 2; 129 velocity.x = -velocity.x; 130 velocity.z = distance * distance * sgn(distance) * PongBall::MAX_REL_Z_VELOCITY * this->speed_; 131 this->batsound_->play(); 132 } 133 else if (position.x < -this->fieldWidth_ / 2 * (1 + this->relMercyOffset_)) 134 { 135 if (this->getGametype() && this->bat_[1]) 136 { 137 this->scoresound_->play(); 138 this->getGametype()->playerScored(this->bat_[1]->getPlayer()); 139 return; 140 } 135 this->getGametype()->playerScored(this->bat_[1]->getPlayer()); 136 return; 141 137 } 142 138 } 143 139 } 144 140 } 141 } 145 142 146 if (velocity != this->getVelocity()) 147 this->setVelocity(velocity); 148 if (position != this->getPosition()) 149 this->setPosition(position); 150 } 151 else 152 { 153 Vector3 position = this->getPosition(); 154 Vector3 velocity = this->getVelocity(); 155 156 if (position.z > this->fieldHeight_ / 2 || position.z < -this->fieldHeight_ / 2) 157 { 158 velocity.z = -velocity.z; 159 this->sidesound_->play(); 160 161 if (position.z > this->fieldHeight_ / 2) 162 position.z = this->fieldHeight_ / 2; 163 if (position.z < -this->fieldHeight_ / 2) 164 position.z = -this->fieldHeight_ / 2; 165 } 166 167 if (position.x > this->fieldWidth_ / 2 || position.x < -this->fieldWidth_ / 2) 168 { 169 float distance = 0; 170 171 if (this->bat_) 172 { 173 if (position.x > this->fieldWidth_ / 2 && this->bat_[1]) 174 { 175 distance = (position.z - this->bat_[1]->getPosition().z) / (this->fieldHeight_ * (this->batlength_ * 1.10f) / 2); 176 if (fabs(distance) <= 1) 177 { 178 position.x = this->fieldWidth_ / 2; 179 velocity.x = -velocity.x; 180 this->batsound_->play(); 181 velocity.z = distance * distance * sgn(distance) * PongBall::MAX_REL_Z_VELOCITY * this->speed_; 182 } 183 } 184 if (position.x < -this->fieldWidth_ / 2 && this->bat_[0]) 185 { 186 distance = (position.z - this->bat_[0]->getPosition().z) / (this->fieldHeight_ * (this->batlength_ * 1.10f) / 2); 187 if (fabs(distance) <= 1) 188 { 189 position.x = -this->fieldWidth_ / 2; 190 velocity.x = -velocity.x; 191 this->batsound_->play(); 192 velocity.z = distance * distance * sgn(distance) * PongBall::MAX_REL_Z_VELOCITY * this->speed_; 193 } 194 } 195 } 196 } 197 198 if (velocity != this->getVelocity()) 143 if (acceleration != this->getAcceleration()) 144 this->setAcceleration(acceleration); 145 if (velocity != this->getVelocity()) 199 146 this->setVelocity(velocity); 200 147 if (position != this->getPosition()) 201 148 this->setPosition(position); 202 }203 149 } 204 150 -
code/branches/pickup/src/modules/pong/PongBall.h
r5781 r5935 41 41 public: 42 42 PongBall(BaseObject* creator); 43 virtual ~PongBall() {}43 virtual ~PongBall(); 44 44 45 45 virtual void tick(float dt); … … 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_; 76 82 unsigned int* batID_; 77 83 float relMercyOffset_; 78 79 SoundBase* sidesound_;80 SoundBase* batsound_;81 SoundBase* scoresound_;82 84 }; 83 85 } -
code/branches/pickup/src/modules/pong/PongCenterpoint.cc
r5781 r5935 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); … … 73 75 if (this->getGametype() && this->getGametype()->isA(Class(Pong))) 74 76 { 75 Pong* pong_gametype = orxonox_cast<Pong*>(this->getGametype() );77 Pong* pong_gametype = orxonox_cast<Pong*>(this->getGametype().get()); 76 78 pong_gametype->setCenterpoint(this); 77 79 } -
code/branches/pickup/src/modules/pong/PongCenterpoint.h
r5781 r5935 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_; -
code/branches/pickup/src/modules/pong/PongPrereqs.h
r5794 r5935 28 28 29 29 /** 30 @file 31 @brief Contains all the necessary forward declarations for all classes and structs. 30 @file 31 @brief 32 Shared library macros, enums, constants and forward declarations for the pong module 32 33 */ 33 34 … … 36 37 37 38 #include "OrxonoxConfig.h" 39 #include "OrxonoxPrereqs.h" 38 40 39 41 //----------------------------------------------------------------------- 40 42 // Shared library settings 41 43 //----------------------------------------------------------------------- 44 42 45 #if defined(ORXONOX_PLATFORM_WINDOWS) && !defined(ORXONOX_STATIC_BUILD) 43 46 # ifdef PONG_SHARED_BUILD -
code/branches/pickup/src/modules/pong/PongScore.cc
r5781 r5935 133 133 134 134 if (this->getOwner() && this->getOwner()->getGametype()) 135 this->owner_ = orxonox_cast<Pong*>(this->getOwner()->getGametype() );135 this->owner_ = orxonox_cast<Pong*>(this->getOwner()->getGametype().get()); 136 136 else 137 137 this->owner_ = 0;
Note: See TracChangeset
for help on using the changeset viewer.