Changeset 2885 for code/trunk
- Timestamp:
- Apr 1, 2009, 12:37:16 AM (16 years ago)
- Location:
- code/trunk/src/orxonox/objects
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
code/trunk/src/orxonox/objects/controllers/PongAI.cc
r2872 r2885 53 53 this->strength_ = 0.5; 54 54 this->movement_ = 0; 55 this->oldMove_ = 0; 56 this->bOscillationAvoidanceActive_ = false; 55 57 56 58 this->setConfigValues(); … … 79 81 80 82 char move = 0; 83 bool delay = false; 81 84 82 85 // Check in which direction the ball is flying … … 86 89 this->ballDirection_.x = -1; 87 90 this->ballDirection_.y = 0; 91 this->bOscillationAvoidanceActive_ = false; 88 92 89 93 // Move to the middle … … 98 102 this->ballDirection_.x = 0; 99 103 this->ballDirection_.y = 0; 104 this->bOscillationAvoidanceActive_ = false; 100 105 } 101 106 else … … 112 117 this->calculateRandomOffset(); 113 118 this->calculateBallEndPosition(); 119 delay = true; 120 this->bOscillationAvoidanceActive_ = false; 114 121 } 115 122 … … 120 127 121 128 this->calculateBallEndPosition(); 129 delay = true; 130 this->bOscillationAvoidanceActive_ = false; 122 131 } 123 132 124 133 // Move to the predicted end position with an additional offset (to hit the ball with the side of the bat) 125 float desiredZValue = this->ballEndPosition_ + this->randomOffset_; 126 127 if (mypos.z > desiredZValue + hysteresisOffset * (this->randomOffset_ < 0)) 128 move = 1; 129 else if (mypos.z < desiredZValue - hysteresisOffset * (this->randomOffset_ > 0)) 130 move = -1; 131 } 132 133 this->move(move); 134 if (!this->bOscillationAvoidanceActive_) 135 { 136 float desiredZValue = this->ballEndPosition_ + this->randomOffset_; 137 138 if (mypos.z > desiredZValue + hysteresisOffset * (this->randomOffset_ < 0)) 139 move = 1; 140 else if (mypos.z < desiredZValue - hysteresisOffset * (this->randomOffset_ > 0)) 141 move = -1; 142 } 143 144 if (move != 0 && this->oldMove_ != 0 && move != this->oldMove_ && !delay) 145 { 146 // We had to correct our position because we moved too far 147 // (and delay ist false, so we're not in the wrong place because of a new end-position prediction) 148 if (fabs(mypos.z - this->ballEndPosition_) < 0.5 * this->ball_->getBatLength() * this->ball_->getFieldDimension().y) 149 { 150 // We're not at the right position, but we still hit the ball, so just stay there to avoid oscillation 151 move = 0; 152 this->bOscillationAvoidanceActive_ = true; 153 } 154 } 155 } 156 157 this->oldMove_ = move; 158 this->move(move, delay); 134 159 this->getControllableEntity()->moveFrontBack(this->movement_); 135 160 } … … 168 193 for (float limit = 0.35; limit < this->strength_ || this->strength_ > 0.99; limit += 0.4) 169 194 { 170 // Bounce from the upper bound 195 // Calculate a random prediction error, based on the vertical speed of the ball and the strength of the AI 196 float randomError = rnd(-1, 1) * dimension.y * (velocity.z / velocity.x / PongBall::MAX_REL_Z_VELOCITY) * (1 - this->strength_); 197 198 // Bounce from the lower bound 171 199 if (this->ballEndPosition_ > dimension.y / 2) 172 200 { 173 201 // Mirror the predicted position at the upper bound and add some random error 174 this->ballEndPosition_ = dimension.y - this->ballEndPosition_ + (rnd(-1, 1) * dimension.y * (1 - this->strength_));202 this->ballEndPosition_ = dimension.y - this->ballEndPosition_ + randomError; 175 203 continue; 176 204 } … … 179 207 { 180 208 // Mirror the predicted position at the lower bound and add some random error 181 this->ballEndPosition_ = -dimension.y - this->ballEndPosition_ + (rnd(-1, 1) * dimension.y * (1 - this->strength_));209 this->ballEndPosition_ = -dimension.y - this->ballEndPosition_ + randomError; 182 210 continue; 183 211 } … … 187 215 } 188 216 189 void PongAI::move(char direction )217 void PongAI::move(char direction, bool bUseDelay) 190 218 { 191 219 // The current direction is either what we're doing right now (movement_) or what is last in the queue … … 198 226 return; 199 227 200 // Calculate delay, but only to change direction or start moving (stop works without delay)201 if (direction != 0)202 {228 if (bUseDelay) 229 { 230 // Calculate delay 203 231 float delay = MAX_REACTION_TIME * (1 - this->strength_); 204 232 … … 209 237 else 210 238 { 211 this->movement_ = 0;239 this->movement_ = direction; 212 240 } 213 241 } -
code/trunk/src/orxonox/objects/controllers/PongAI.h
r2872 r2885 56 56 void calculateRandomOffset(); 57 57 void calculateBallEndPosition(); 58 void move(char direction );58 void move(char direction, bool bUseDelay); 59 59 void delayedMove(); 60 60 … … 68 68 std::list<std::pair<Timer<PongAI>*, char> > reactionTimers_; 69 69 char movement_; 70 char oldMove_; 71 bool bOscillationAvoidanceActive_; 70 72 }; 71 73 } -
code/trunk/src/orxonox/objects/gametypes/Pong.cc
r2872 r2885 53 53 this->bat_[0] = 0; 54 54 this->bat_[1] = 0; 55 56 this->bForceSpawn_ = true;57 55 58 56 this->starttimer_.setTimer(1.0, false, this, createExecutor(createFunctor(&Pong::startBall))); … … 111 109 this->starttimer_.startTimer(); 112 110 111 112 bool temp = this->bForceSpawn_; 113 this->bForceSpawn_ = true; 114 113 115 Deathmatch::start(); 116 117 this->bForceSpawn_ = temp; 114 118 } 115 119 -
code/trunk/src/orxonox/objects/worldentities/PongBall.cc
r2839 r2885 37 37 { 38 38 CreateFactory(PongBall); 39 40 const float PongBall::MAX_REL_Z_VELOCITY = 1.5; 39 41 40 42 PongBall::PongBall(BaseObject* creator) : MovableEntity(creator) … … 79 81 position.x = this->fieldWidth_ / 2; 80 82 velocity.x = -velocity.x; 81 velocity.z = distance * distance * sgn(distance) * 1.5* this->speed_;83 velocity.z = distance * distance * sgn(distance) * PongBall::MAX_REL_Z_VELOCITY * this->speed_; 82 84 } 83 85 else if (position.x > this->fieldWidth_ / 2 * (1 + this->relMercyOffset_)) … … 97 99 position.x = -this->fieldWidth_ / 2; 98 100 velocity.x = -velocity.x; 99 velocity.z = distance * distance * sgn(distance) * 1.5* this->speed_;101 velocity.z = distance * distance * sgn(distance) * PongBall::MAX_REL_Z_VELOCITY * this->speed_; 100 102 } 101 103 else if (position.x < -this->fieldWidth_ / 2 * (1 + this->relMercyOffset_)) -
code/trunk/src/orxonox/objects/worldentities/PongBall.h
r2839 r2885 63 63 { this->bat_ = bats; } 64 64 65 static const float MAX_REL_Z_VELOCITY; 66 65 67 private: 66 68 float fieldWidth_;
Note: See TracChangeset
for help on using the changeset viewer.