Changeset 10799 for code/branches/AI_HS15/src/orxonox
- Timestamp:
- Nov 12, 2015, 1:03:34 PM (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/AI_HS15/src/orxonox/controllers/CommonController.cc
r10798 r10799 9 9 * modify it under the terms of the GNU General Public License 10 10 * as published by the Free Software Foundation; either version 2 11 * of the License, or ( at your option)any later version.11 * of the License, or ( at your option )any later version. 12 12 * 13 13 * This program is distributed in the hope that it will be useful, … … 44 44 { 45 45 46 RegisterClass( CommonController);46 RegisterClass( CommonController ); 47 47 float SPEED = 0.7f/0.02f; 48 48 float ROTATEFACTOR = 0.3f/0.02f; 49 49 50 CommonController::CommonController( Context* context) : Controller(context)50 CommonController::CommonController( Context* context ): Controller( context ) 51 51 { 52 52 this->bSetupWorked = false; … … 56 56 57 57 this->maneuverType_ = ManeuverType::NONE; 58 RegisterObject( CommonController);59 } 60 61 62 CommonController::~CommonController() 63 { 64 } 65 66 void CommonController::XMLPort( Element& xmlelement, XMLPort::Mode mode)67 { 68 SUPER( CommonController, XMLPort, xmlelement, mode);69 XMLPortParam( CommonController, "formationMode", setFormationModeXML, getFormationModeXML, xmlelement, mode);70 71 } 72 void CommonController::setFormationModeXML( std::string val)73 { 74 const std::string valUpper = getUppercase( val);58 RegisterObject( CommonController ); 59 } 60 61 62 CommonController::~CommonController() 63 { 64 } 65 66 void CommonController::XMLPort( Element& xmlelement, XMLPort::Mode mode ) 67 { 68 SUPER( CommonController, XMLPort, xmlelement, mode ); 69 XMLPortParam( CommonController, "formationMode", setFormationModeXML, getFormationModeXML, xmlelement, mode ); 70 71 } 72 void CommonController::setFormationModeXML( std::string val ) 73 { 74 const std::string valUpper = getUppercase( val ); 75 75 FormationMode::Value value; 76 if ( valUpper == "VEE")76 if ( valUpper == "VEE" ) 77 77 value = FormationMode::VEE; 78 else if ( valUpper == "WALL")78 else if ( valUpper == "WALL" ) 79 79 value = FormationMode::WALL; 80 else if ( valUpper == "FINGER4")80 else if ( valUpper == "FINGER4" ) 81 81 value = FormationMode::FINGER4; 82 else if ( valUpper == "DIAMOND")82 else if ( valUpper == "DIAMOND" ) 83 83 value = FormationMode::DIAMOND; 84 84 else 85 ThrowException( ParseError, std::string("Attempting to set an unknown FormationMode: '") + val + "'.");86 this->setFormationMode( value);85 ThrowException( ParseError, std::string( "Attempting to set an unknown FormationMode: '" )+ val + "'." ); 86 this->setFormationMode( value ); 87 87 88 88 } 89 std::string CommonController::getFormationModeXML() 90 { 91 switch ( this->formationMode_)89 std::string CommonController::getFormationModeXML() 90 { 91 switch ( this->formationMode_ ) 92 92 { 93 93 case FormationMode::VEE: … … 117 117 } 118 118 } 119 void CommonController::maneuver() 120 { 121 122 if (this->target_ && this->bHasPositionOfTarget_ && this->getControllableEntity()) 123 { 124 Vector3 diffVector = this->positionOfTarget_ - this->getControllableEntity()->getWorldPosition(); 119 void CommonController::maneuver() 120 { 121 122 if ( this->target_ && this->bHasPositionOfTarget_ && this->getControllableEntity() ) 123 { 124 Vector3 thisPosition = this->getControllableEntity() ->getWorldPosition(); 125 Quaternion thisOrientation = this->getControllableEntity() ->getOrientation(); 126 127 /*this->setPositionOfTarget( getPredictedPosition( 128 thisPosition, 129 hardcoded_projectile_speed, 130 this->target_->getWorldPosition() , 131 this->target_->getVelocity() 132 ) );*/ 133 this->setPositionOfTarget( this->target_->getWorldPosition() ); 134 this->setOrientationOfTarget( this->target_->getControllableEntity() ->getOrientation() ); 135 136 137 Vector3 diffVector = this->positionOfTarget_ - thisPosition; 125 138 float diffLength = diffVector.length(); 126 139 Vector3 diffUnit = diffVector/diffLength; 127 140 128 Vector3 myForwardVector = this->getControllableEntity()->getOrientation() * WorldEntity::FRONT; 129 float myDotProduct = diffVector.dotProduct(myForwardVector); 130 131 Vector3 opponentForwardVector = this->target_->getOrientation() * WorldEntity::FRONT; 132 float opponentDotProduct = diffVector.dotProduct(opponentForwardVector); 133 134 float myAngle = math::arccos ( myForwardVector.dotProduct(diffVector)/(diffLength) ); 135 float targetAngle = math::arccos ( opponentForwardVector.dotProduct(-diffVector)/(diffLength) ); 141 Vector3 thisForwardVector = thisOrientation * WorldEntity::FRONT; 142 float thisDotProduct = diffVector.dotProduct( thisForwardVector ); 143 144 Vector3 targetForwardVector = this->orientationOfTarget_ * WorldEntity::FRONT; 145 float targetDotProduct = diffVector.dotProduct( targetForwardVector ); 146 147 float thisAngle = getAngle( thisPosition, thisForwardVector, this->positionOfTarget_ ); 148 float targetAngle = getAngle( this->positionOfTarget_, targetForwardVector, thisPosition ); 149 150 151 bool bThisIsLookingAtTarget = ( thisAngle/( diffLength*diffLength ) < math::pi/8000000.0f ); 152 bool bTargetIsLookingAtThis = ( targetAngle/( diffLength*diffLength ) < math::pi/8000000.0f ); 136 153 137 bool bThisIsLookingAtTarget = (myAngle/(diffLength*diffLength) < math::pi/8000000.0f); 138 bool bTargetIsLookingAtThis = (targetAngle/(diffLength*diffLength) < math::pi/8000000.0f); 139 140 float angleDiff = targetAngle - myAngle; 154 float angleDiff = targetAngle - thisAngle; 141 155 142 156 //if his angle is bigger than mine … … 152 166 moveToPoint( 153 167 *target, 154 randomInRange( 45, 180)168 randomInRange( 45, 180 ) 155 169 ); 156 170 } … … 158 172 else 159 173 { 160 Vector3 target = ( diffUnit)* 150.0f174 Vector3 target = ( diffUnit )* 150.0f 161 175 Vector3* randVector = new Vector3( 162 randomInRange( -300, 300),163 randomInRange( -300, 300),164 randomInRange( -300, 300)176 randomInRange( -300, 300 ), 177 randomInRange( -300, 300 ), 178 randomInRange( -300, 300 ) 165 179 ); 166 Vector3 projection = randVector.dotProduct( diffUnit)* diffUnit;180 Vector3 projection = randVector.dotProduct( diffUnit )* diffUnit; 167 181 *randVector -= projection; 168 182 target += randVector; 169 183 moveToPoint( 170 184 *target, 171 randomInRange( 45, 180)185 randomInRange( 45, 180 ) 172 186 ); 173 187 } … … 179 193 if ( diffLength < 300 ) 180 194 { 181 this->setTargetPosition( this->getControllableEntity() ->getWorldPosition());195 this->setTargetPosition( this->getControllableEntity() ->getWorldPosition() ); 182 196 } 183 197 //move closer 184 198 else 185 199 { 186 this->setTargetPosition( this->positionOfTarget_ - 0.6f*diffVector );200 this->setTargetPosition( this->positionOfTarget_ - 0.6f*diffVector ); 187 201 } 188 202 } … … 190 204 191 205 } 192 if ( this->getControllableEntity() && !this->target_)206 if ( this->getControllableEntity() && !this->target_ ) 193 207 { 194 208 this->maneuverType_ = ManeuverType::NONE; 195 209 } 196 orxout ( internal_error) << "ManeuverType = " << this->maneuverType_ << endl;197 } 198 void CommonController::chooseManeuverType() 199 { 200 201 if ( this->target_ && this->bHasPositionOfTarget_ && this->getControllableEntity())202 { 203 Vector3 diffVector = this->positionOfTarget_ - this->getControllableEntity() ->getWorldPosition();204 Vector3 myForwardVector = this->getControllableEntity()->getOrientation()* WorldEntity::FRONT;205 float myDotProduct = diffVector.dotProduct(myForwardVector);206 207 Vector3 opponentForwardVector = this->target_->getOrientation()* WorldEntity::FRONT;208 float opponentDotProduct = diffVector.dotProduct(opponentForwardVector);209 210 211 switch ( (myDotProduct > 0) - (myDotProduct < 0))210 orxout ( internal_error ) << "ManeuverType = " << this->maneuverType_ << endl; 211 } 212 void CommonController::chooseManeuverType() 213 { 214 215 if ( this->target_ && this->bHasPositionOfTarget_ && this->getControllableEntity() ) 216 { 217 Vector3 diffVector = this->positionOfTarget_ - this->getControllableEntity() ->getWorldPosition(); 218 Vector3 thisForwardVector = this->getControllableEntity() ->getOrientation() * WorldEntity::FRONT; 219 float thisDotProduct = diffVector.dotProduct( thisForwardVector ); 220 221 Vector3 targetForwardVector = this->target_->getOrientation() * WorldEntity::FRONT; 222 float targetDotProduct = diffVector.dotProduct( targetForwardVector ); 223 224 225 switch ( ( thisDotProduct > 0 )- ( thisDotProduct < 0 )) 212 226 { 213 227 case 1: 214 228 { 215 switch ( (opponentDotProduct > 0) - (opponentDotProduct < 0))229 switch ( ( targetDotProduct > 0 )- ( targetDotProduct < 0 )) 216 230 { 217 231 case 1: … … 235 249 case 0: 236 250 { 237 switch ( (opponentDotProduct > 0) - (opponentDotProduct < 0))251 switch ( ( targetDotProduct > 0 )- ( targetDotProduct < 0 )) 238 252 { 239 253 case 1: … … 258 272 case -1: 259 273 { 260 switch ( (opponentDotProduct > 0) - (opponentDotProduct < 0))274 switch ( ( targetDotProduct > 0 )- ( targetDotProduct < 0 )) 261 275 { 262 276 case 1: … … 280 294 } 281 295 } 282 if ( this->getControllableEntity() && !this->target_)296 if ( this->getControllableEntity() && !this->target_ ) 283 297 { 284 298 this->maneuverType_ = ManeuverType::NONE; 285 299 } 286 orxout ( internal_error) << "ManeuverType = " << this->maneuverType_ << endl;287 } 288 bool CommonController::setWingman ( CommonController* wingman)300 orxout ( internal_error ) << "ManeuverType = " << this->maneuverType_ << endl; 301 } 302 bool CommonController::setWingman ( CommonController* wingman ) 289 303 { 290 304 return false; 291 305 } 292 306 293 bool CommonController::hasWingman() 307 bool CommonController::hasWingman() 294 308 { 295 309 return true; 296 310 } 297 void CommonController::setTarget( ControllableEntity* target)311 void CommonController::setTarget( ControllableEntity* target ) 298 312 { 299 313 this->target_ = target; 300 orxout ( internal_error) << " TARGET SET " << endl;314 orxout ( internal_error ) << " TARGET SET " << endl; 301 315 302 if ( this->target_ )303 { 304 this->setPositionOfTarget( target_->getWorldPosition());305 306 } 307 } 308 bool CommonController::hasTarget() 309 { 310 if ( this->target_)316 if ( this->target_ ) 317 { 318 this->setPositionOfTarget( target_->getWorldPosition() ); 319 320 } 321 } 322 bool CommonController::hasTarget() 323 { 324 if ( this->target_ ) 311 325 return true; 312 326 return false; 313 327 } 314 void CommonController::setPositionOfTarget( const Vector3& target)328 void CommonController::setPositionOfTarget( const Vector3& target ) 315 329 { 316 330 this->positionOfTarget_ = target; 317 331 this->bHasPositionOfTarget_ = true; 318 332 } 319 void CommonController::setOrientationOfTarget( const Quaternion& orient)333 void CommonController::setOrientationOfTarget( const Quaternion& orient ) 320 334 { 321 335 this->orientationOfTarget_=orient; … … 323 337 } 324 338 325 void CommonController::setTargetPosition( const Vector3& target)339 void CommonController::setTargetPosition( const Vector3& target ) 326 340 { 327 341 this->targetPosition_ = target; … … 329 343 } 330 344 331 void CommonController::setTargetOrientation( const Quaternion& orient)345 void CommonController::setTargetOrientation( const Quaternion& orient ) 332 346 { 333 347 this->targetOrientation_=orient; … … 335 349 } 336 350 337 void CommonController::setTargetOrientation( ControllableEntity* target)338 { 339 if ( target)340 setTargetOrientation( target->getOrientation());341 } 342 343 /*void CommonController::spin() 351 void CommonController::setTargetOrientation( ControllableEntity* target ) 352 { 353 if ( target ) 354 setTargetOrientation( target->getOrientation() ); 355 } 356 357 /*void CommonController::spin() 344 358 { 345 359 this->moveToTargetPosition(); 346 this->getControllableEntity() ->rotateRoll(8.0f);347 } 348 void CommonController::turn180() 349 { 350 Vector2 coord = get2DViewdirection( this->getControllableEntity()->getPosition(), this->getControllableEntity()->getOrientation() * WorldEntity::FRONT, this->getControllableEntity()->getOrientation() * WorldEntity::UP, this->targetPosition_);351 352 this->getControllableEntity() ->rotateYaw(-2.0f * sgn(coord.x) * coord.x*coord.x);353 this->getControllableEntity() ->rotatePitch(2.0f * sgn(coord.y) * coord.y*coord.y);354 355 this->getControllableEntity() ->moveFrontBack(SPEED);360 this->getControllableEntity() ->rotateRoll( 8.0f ); 361 } 362 void CommonController::turn180() 363 { 364 Vector2 coord = get2DViewdirection( this->getControllableEntity() ->getPosition() , this->getControllableEntity() ->getOrientation() * WorldEntity::FRONT, this->getControllableEntity() ->getOrientation() * WorldEntity::UP, this->targetPosition_ ); 365 366 this->getControllableEntity() ->rotateYaw( -2.0f * sgn( coord.x )* coord.x*coord.x ); 367 this->getControllableEntity() ->rotatePitch( 2.0f * sgn( coord.y )* coord.y*coord.y ); 368 369 this->getControllableEntity() ->moveFrontBack( SPEED ); 356 370 }*/ 357 371 … … 359 373 360 374 //copy the Roll orientation of given Quaternion. 361 void CommonController::copyOrientation( const Quaternion& orient, float dt)375 void CommonController::copyOrientation( const Quaternion& orient, float dt ) 362 376 { 363 377 //roll angle difference in radian 364 float diff=orient.getRoll( false).valueRadians()-(this->getControllableEntity()->getOrientation().getRoll(false).valueRadians());365 while( diff>math::twoPi)diff-=math::twoPi;366 while( diff<-math::twoPi)diff+=math::twoPi;367 this->getControllableEntity() ->rotateRoll(diff*ROTATEFACTOR * dt);368 } 369 void CommonController::copyTargetOrientation( float dt)370 { 371 if ( bHasTargetOrientation_)378 float diff=orient.getRoll( false ).valueRadians() -( this->getControllableEntity() ->getOrientation() .getRoll( false ).valueRadians() ); 379 while( diff>math::twoPi )diff-=math::twoPi; 380 while( diff<-math::twoPi )diff+=math::twoPi; 381 this->getControllableEntity() ->rotateRoll( diff*ROTATEFACTOR * dt ); 382 } 383 void CommonController::copyTargetOrientation( float dt ) 384 { 385 if ( bHasTargetOrientation_ ) 372 386 { 373 copyOrientation( targetOrientation_, dt);374 } 375 } 376 377 378 379 380 void CommonController::moveToTargetPosition( float dt)381 { 382 this->moveToPosition( this->targetPosition_, dt);383 } 384 void CommonController::moveToPosition( const Vector3& target, float dt)387 copyOrientation( targetOrientation_, dt ); 388 } 389 } 390 391 392 393 394 void CommonController::moveToTargetPosition( float dt ) 395 { 396 this->moveToPosition( this->targetPosition_, dt ); 397 } 398 void CommonController::moveToPosition( const Vector3& target, float dt ) 385 399 { 386 400 float factor = 1; 387 if ( !this->getControllableEntity())401 if ( !this->getControllableEntity() ) 388 402 return; 389 if ( this->rank_ == Rank::DIVISIONLEADER)403 if ( this->rank_ == Rank::DIVISIONLEADER ) 390 404 factor = 0.8; 391 if ( this->rank_ == Rank::SECTIONLEADER)405 if ( this->rank_ == Rank::SECTIONLEADER ) 392 406 factor = 0.9; 393 407 394 //100 is ( so far) the smallest tolerance (empirically found)that can be reached,408 //100 is ( so far )the smallest tolerance ( empirically found )that can be reached, 395 409 //with smaller distance spaceships can't reach position and go circles around it instead 396 410 int tolerance = 60; … … 398 412 ControllableEntity* entity = this->getControllableEntity(); 399 413 Vector2 coord = get2DViewCoordinates 400 ( entity->getPosition(),401 entity->getOrientation() * WorldEntity::FRONT,402 entity->getOrientation() * WorldEntity::UP,403 target );404 405 float distance = ( target - this->getControllableEntity()->getPosition()).length();414 ( entity->getPosition() , 415 entity->getOrientation() * WorldEntity::FRONT, 416 entity->getOrientation() * WorldEntity::UP, 417 target ); 418 419 float distance = ( target - this->getControllableEntity() ->getPosition() ).length(); 406 420 407 421 //rotates should be in range [-1,+1], clamp cuts off all that is not 408 float rotateX = clamp( coord.x * 10, -1.0f, 1.0f);409 float rotateY = clamp( coord.y * 10, -1.0f, 1.0f);422 float rotateX = clamp( coord.x * 10, -1.0f, 1.0f ); 423 float rotateY = clamp( coord.y * 10, -1.0f, 1.0f ); 410 424 411 425 412 if ( distance > tolerance)426 if ( distance > tolerance ) 413 427 { 414 428 //Yaw and Pitch are enough to start facing the target 415 this->getControllableEntity() ->rotateYaw(-2.0f * ROTATEFACTOR * rotateX * dt);416 this->getControllableEntity() ->rotatePitch(2.0f * ROTATEFACTOR * rotateY * dt);429 this->getControllableEntity() ->rotateYaw( -2.0f * ROTATEFACTOR * rotateX * dt ); 430 this->getControllableEntity() ->rotatePitch( 2.0f * ROTATEFACTOR * rotateY * dt ); 417 431 418 432 //300 works, maybe less is better 419 if ( distance < 400)433 if ( distance < 400 ) 420 434 { 421 435 //Change roll when close. When Spaceship faces target, roll doesn't affect it's trajectory … … 423 437 //Wingmen won't face same direction as Leaders, but when Leaders start moving 424 438 //Yaw and Pitch will adapt. 425 if ( bHasTargetOrientation_)426 { 427 copyTargetOrientation( dt);428 } 429 } 430 431 this->getControllableEntity() ->moveFrontBack(1.2f*SPEED*factor * dt);439 if ( bHasTargetOrientation_ ) 440 { 441 copyTargetOrientation( dt ); 442 } 443 } 444 445 this->getControllableEntity() ->moveFrontBack( 1.2f*SPEED*factor * dt ); 432 446 } 433 447 else … … 437 451 } 438 452 } 439 float CommonController::randomInRange( float a, float b)440 { 441 float random = rnd( 1.0f);453 float CommonController::randomInRange( float a, float b ) 454 { 455 float random = rnd( 1.0f ); 442 456 float diff = b - a; 443 457 float r = random * diff; 444 458 return a + r; 445 459 } 446 void CommonController::attack() 447 { 448 if ( !this->getControllableEntity() )460 void CommonController::attack() 461 { 462 if ( !this->getControllableEntity() ) 449 463 return; 450 464 if ( this->target_ ) 451 465 { 452 466 this->positionOfTarget_ = getPredictedPosition( 453 this->getControllableEntity() ->getWorldPosition(),467 this->getControllableEntity() ->getWorldPosition() , 454 468 hardcoded_projectile_speed, 455 this->target_->getWorldPosition() ,456 this->target_->getVelocity() 469 this->target_->getWorldPosition() , 470 this->target_->getVelocity() 457 471 ); 458 Vector3 diffVector = this->positionOfTarget_ - this->getControllableEntity() ->getWorldPosition();472 Vector3 diffVector = this->positionOfTarget_ - this->getControllableEntity() ->getWorldPosition(); 459 473 float diffLength = diffVector.length(); 460 if ( diffLength < 100)474 if ( diffLength < 100 ) 461 475 { 462 476 Vector3* targetPosition; 463 477 targetPosition = new Vector3 ( 464 //randomInRange( 200, 300),478 //randomInRange( 200, 300 ), 465 479 0, 466 //randomInRange( -300, -200),480 //randomInRange( -300, -200 ), 467 481 0, 468 randomInRange( -300, -400)482 randomInRange( -300, -400 ) 469 483 ); 470 Quaternion rotationToTarget = ( this->getControllableEntity()->getOrientation() * WorldEntity::FRONT).getRotationTo(diffVector);471 Vector3 target = rotationToTarget * ( *targetPosition);484 Quaternion rotationToTarget = ( this->getControllableEntity() ->getOrientation() * WorldEntity::FRONT ).getRotationTo( diffVector ); 485 Vector3 target = rotationToTarget * ( *targetPosition ); 472 486 moveToPoint( 473 487 target, 474 randomInRange( 45, 180)488 randomInRange( 45, 180 ) 475 489 ); 476 490 executingMoveToPoint_ = true; … … 479 493 this->bShooting_ = true; 480 494 this->positionOfTarget_ = getPredictedPosition( 481 this->getControllableEntity() ->getWorldPosition(),495 this->getControllableEntity() ->getWorldPosition() , 482 496 hardcoded_projectile_speed, 483 this->target_->getWorldPosition() ,484 this->target_->getVelocity() 497 this->target_->getWorldPosition() , 498 this->target_->getVelocity() 485 499 ); 486 500 this->targetPosition_ = positionOfTarget_; … … 492 506 } 493 507 } 494 void CommonController::scissors() 495 { 496 if ( !this->getControllableEntity() )508 void CommonController::scissors() 509 { 510 if ( !this->getControllableEntity() ) 497 511 return; 498 512 if ( this->target_ ) 499 513 { 500 514 this->positionOfTarget_ = getPredictedPosition( 501 this->getControllableEntity() ->getWorldPosition(),515 this->getControllableEntity() ->getWorldPosition() , 502 516 hardcoded_projectile_speed, 503 this->target_->getWorldPosition() ,504 this->target_->getVelocity() 517 this->target_->getWorldPosition() , 518 this->target_->getVelocity() 505 519 ); 506 Vector3 diffVector = this->positionOfTarget_ - this->getControllableEntity() ->getWorldPosition();520 Vector3 diffVector = this->positionOfTarget_ - this->getControllableEntity() ->getWorldPosition(); 507 521 float diffLength = diffVector.length(); 508 Vector3 opponentForwardVector = this->target_->getOrientation()* WorldEntity::FRONT;509 float opponentDotProduct = diffVector.dotProduct(opponentForwardVector);522 Vector3 targetForwardVector = this->target_->getOrientation() * WorldEntity::FRONT; 523 float targetDotProduct = diffVector.dotProduct( targetForwardVector ); 510 524 511 int f = ( int) rnd(100.0f);512 f = ( f % 2 == 0 ? 1 : -1);513 514 if( !this->executingMoveToPoint_)525 int f = ( int )rnd( 100.0f ); 526 f = ( f % 2 == 0 ? 1 : -1 ); 527 528 if( !this->executingMoveToPoint_ ) 515 529 { 516 530 Vector3* targetPosition; … … 518 532 { 519 533 targetPosition = new Vector3 ( 520 //f * randomInRange( 200, 300),521 0, 522 //f * randomInRange( -300, -200),523 0, 524 //randomInRange( -300, -400)534 //f * randomInRange( 200, 300 ), 535 0, 536 //f * randomInRange( -300, -200 ), 537 0, 538 //randomInRange( -300, -400 ) 525 539 0 526 540 ); … … 528 542 else 529 543 { 530 if ( opponentDotProduct < 0 )544 if ( targetDotProduct < 0 ) 531 545 { 532 546 targetPosition = new Vector3 ( 533 //f * randomInRange( 200, 300),534 0, 535 //f * randomInRange( -300, -200),536 0, 537 //randomInRange( -300, -400)547 //f * randomInRange( 200, 300 ), 548 0, 549 //f * randomInRange( -300, -200 ), 550 0, 551 //randomInRange( -300, -400 ) 538 552 -300 539 553 ); … … 542 556 { 543 557 targetPosition = new Vector3 ( 544 //f * randomInRange( 200, 300),545 0, 546 //f * randomInRange( -300, -200),547 0, 548 //randomInRange( -300, -400)558 //f * randomInRange( 200, 300 ), 559 0, 560 //f * randomInRange( -300, -200 ), 561 0, 562 //randomInRange( -300, -400 ) 549 563 300 550 564 ); 551 565 } 552 566 } 553 Quaternion rotationToTarget = ( this->getControllableEntity()->getOrientation() * WorldEntity::FRONT).getRotationTo(diffVector);554 Vector3 target = rotationToTarget * ( *targetPosition);567 Quaternion rotationToTarget = ( this->getControllableEntity() ->getOrientation() * WorldEntity::FRONT ).getRotationTo( diffVector ); 568 Vector3 target = rotationToTarget * ( *targetPosition ); 555 569 moveToPoint( 556 570 target, 557 randomInRange( 45, 180)571 randomInRange( 45, 180 ) 558 572 ); 559 573 executingMoveToPoint_ = true; … … 566 580 } 567 581 } 568 void CommonController::gunsD() 569 { 570 if ( !this->getControllableEntity() )582 void CommonController::gunsD() 583 { 584 if ( !this->getControllableEntity() ) 571 585 return; 572 586 if ( this->target_ ) 573 587 { 574 588 this->positionOfTarget_ = getPredictedPosition( 575 this->getControllableEntity() ->getWorldPosition(),589 this->getControllableEntity() ->getWorldPosition() , 576 590 hardcoded_projectile_speed, 577 this->target_->getWorldPosition() ,578 this->target_->getVelocity() 591 this->target_->getWorldPosition() , 592 this->target_->getVelocity() 579 593 ); 580 Vector3 diffVector = this->positionOfTarget_ - this->getControllableEntity() ->getWorldPosition();594 Vector3 diffVector = this->positionOfTarget_ - this->getControllableEntity() ->getWorldPosition(); 581 595 float diffLength = diffVector.length(); 582 if( !this->executingMoveToPoint_)596 if( !this->executingMoveToPoint_ ) 583 597 { 584 598 Vector3* targetPosition; … … 586 600 { 587 601 targetPosition = new Vector3 ( 588 //f * randomInRange( 200, 300),589 0, 590 //f * randomInRange( -300, -200),591 0, 592 //randomInRange( -300, -400)602 //f * randomInRange( 200, 300 ), 603 0, 604 //f * randomInRange( -300, -200 ), 605 0, 606 //randomInRange( -300, -400 ) 593 607 0 594 608 ); … … 597 611 { 598 612 targetPosition = new Vector3 ( 599 //randomInRange( 100, 200),600 0, 601 //randomInRange( -200, -100),602 0, 603 //randomInRange( -400, -600)613 //randomInRange( 100, 200 ), 614 0, 615 //randomInRange( -200, -100 ), 616 0, 617 //randomInRange( -400, -600 ) 604 618 500 605 619 ); … … 608 622 { 609 623 targetPosition = new Vector3 ( 610 //randomInRange( 200, 300),611 0, 612 //randomInRange( -300, -200),613 0, 614 //randomInRange( -400, -600)624 //randomInRange( 200, 300 ), 625 0, 626 //randomInRange( -300, -200 ), 627 0, 628 //randomInRange( -400, -600 ) 615 629 500 616 630 ); 617 631 } 618 Quaternion rotationToTarget = ( this->getControllableEntity()->getOrientation() * WorldEntity::FRONT).getRotationTo(diffVector);619 Vector3 target = rotationToTarget * ( *targetPosition);632 Quaternion rotationToTarget = ( this->getControllableEntity() ->getOrientation() * WorldEntity::FRONT ).getRotationTo( diffVector ); 633 Vector3 target = rotationToTarget * ( *targetPosition ); 620 634 moveToPoint( 621 635 target, 622 randomInRange( 45, 180)636 randomInRange( 45, 180 ) 623 637 ); 624 638 executingMoveToPoint_ = true; … … 633 647 //PRE: relativeTargetPosition is desired position relative to the spaceship, 634 648 //angleRoll is the angle in degrees of Roll that should be applied by the end of the movement 635 //POST: target orientation and position are set, so that it can be used by MoveAndRoll() 636 void CommonController::moveToPoint( const Vector3& relativeTargetPosition, float angleRoll)649 //POST: target orientation and position are set, so that it can be used by MoveAndRoll() 650 void CommonController::moveToPoint( const Vector3& relativeTargetPosition, float angleRoll ) 637 651 { 638 652 ControllableEntity* entity = this->getControllableEntity(); 639 if ( !entity)653 if ( !entity ) 640 654 return; 641 655 Quaternion orient = entity->getWorldOrientation(); 642 Quaternion rotation = Quaternion( Degree(angleRoll), Vector3::UNIT_Z);656 Quaternion rotation = Quaternion( Degree( angleRoll ), Vector3::UNIT_Z ); 643 657 644 658 Vector3 target = orient * relativeTargetPosition + entity->getWorldPosition(); 645 setTargetPosition( target);659 setTargetPosition( target ); 646 660 orient = orient * rotation; 647 this->setTargetOrientation( orient);661 this->setTargetOrientation( orient ); 648 662 649 663 } … … 657 671 //otherwise returns true 658 672 //dt is normally around 0.02f, which makes it 1/0.02 = 50 frames/sec 659 bool CommonController::moveAndRoll( float dt)673 bool CommonController::moveAndRoll( float dt ) 660 674 { 661 675 float factor = 1; 662 if ( !this->getControllableEntity())676 if ( !this->getControllableEntity() ) 663 677 return false; 664 if ( this->rank_ == Rank::DIVISIONLEADER)678 if ( this->rank_ == Rank::DIVISIONLEADER ) 665 679 factor = 0.8; 666 if ( this->rank_ == Rank::SECTIONLEADER)680 if ( this->rank_ == Rank::SECTIONLEADER ) 667 681 factor = 0.9; 668 682 int tolerance = 60; 669 683 670 684 ControllableEntity* entity = this->getControllableEntity(); 671 if ( !entity)685 if ( !entity ) 672 686 return true; 673 687 Vector2 coord = get2DViewCoordinates 674 ( entity->getPosition(),675 entity->getOrientation() * WorldEntity::FRONT,676 entity->getOrientation() * WorldEntity::UP,677 targetPosition_ );678 679 float distance = ( targetPosition_ - this->getControllableEntity()->getPosition()).length();688 ( entity->getPosition() , 689 entity->getOrientation() * WorldEntity::FRONT, 690 entity->getOrientation() * WorldEntity::UP, 691 targetPosition_ ); 692 693 float distance = ( targetPosition_ - this->getControllableEntity() ->getPosition() ).length(); 680 694 681 695 //rotates should be in range [-1,+1], clamp cuts off all that is not 682 float rotateX = clamp( coord.x * 10, -1.0f, 1.0f);683 float rotateY = clamp( coord.y * 10, -1.0f, 1.0f);696 float rotateX = clamp( coord.x * 10, -1.0f, 1.0f ); 697 float rotateY = clamp( coord.y * 10, -1.0f, 1.0f ); 684 698 685 699 686 if ( distance > tolerance)700 if ( distance > tolerance ) 687 701 { 688 702 //Yaw and Pitch are enough to start facing the target 689 this->getControllableEntity() ->rotateYaw(-2.0f * ROTATEFACTOR * rotateX * dt);690 this->getControllableEntity() ->rotatePitch(2.0f * ROTATEFACTOR * rotateY * dt);703 this->getControllableEntity() ->rotateYaw( -2.0f * ROTATEFACTOR * rotateX * dt ); 704 this->getControllableEntity() ->rotatePitch( 2.0f * ROTATEFACTOR * rotateY * dt ); 691 705 692 706 //Roll 693 if ( bHasTargetOrientation_)694 { 695 copyTargetOrientation( dt);707 if ( bHasTargetOrientation_ ) 708 { 709 copyTargetOrientation( dt ); 696 710 } 697 711 698 712 //Move 699 this->getControllableEntity() ->moveFrontBack(1.2f * SPEED * factor * dt);713 this->getControllableEntity() ->moveFrontBack( 1.2f * SPEED * factor * dt ); 700 714 //if still moving, return false 701 715 return false; … … 709 723 } 710 724 711 float CommonController::squaredDistanceToTarget() const712 { 713 if ( !this->getControllableEntity() )725 float CommonController::squaredDistanceToTarget() const 726 { 727 if ( !this->getControllableEntity() ) 714 728 return 0; 715 729 if ( !this->target_ ) 716 return ( this->getControllableEntity() ->getPosition().squaredDistance(this->targetPosition_) );730 return ( this->getControllableEntity() ->getPosition() .squaredDistance( this->targetPosition_ ) ); 717 731 else 718 return ( this->getControllableEntity() ->getPosition().squaredDistance(this->target_->getPosition()) );732 return ( this->getControllableEntity() ->getPosition() .squaredDistance( this->target_->getPosition() ) ); 719 733 } 720 734 721 bool CommonController::isLookingAtTarget( float angle)const722 { 723 if ( !this->getControllableEntity())735 bool CommonController::isLookingAtTarget( float angle )const 736 { 737 if ( !this->getControllableEntity() ) 724 738 return false; 725 739 726 return ( getAngle(this->getControllableEntity()->getPosition(), this->getControllableEntity()->getOrientation() * WorldEntity::FRONT, this->targetPosition_) < angle);727 } 728 729 bool CommonController::canFire() 740 return ( getAngle( this->getControllableEntity() ->getPosition() , this->getControllableEntity() ->getOrientation() * WorldEntity::FRONT, this->targetPosition_ ) < angle ); 741 } 742 743 bool CommonController::canFire() 730 744 { 731 745 float squaredDistance = squaredDistanceToTarget(); 732 if ( this->bShooting_ && squaredDistance < 9000000 && squaredDistance > 10000 && this->isLookingAtTarget( math::pi /(0.0002f*squaredDistance)) )746 if ( this->bShooting_ && squaredDistance < 9000000 && squaredDistance > 10000 && this->isLookingAtTarget( math::pi /( 0.0002f*squaredDistance )) ) 733 747 { 734 748 return true; … … 740 754 741 755 } 742 void CommonController::doFire() 743 { 744 if ( !this->target_ || !this->getControllableEntity())756 void CommonController::doFire() 757 { 758 if ( !this->target_ || !this->getControllableEntity() ) 745 759 return; 746 760 static const float hardcoded_projectile_speed = 750; 747 761 748 this->targetPosition_ = getPredictedPosition( this->getControllableEntity()->getWorldPosition(), hardcoded_projectile_speed, this->target_->getWorldPosition(), this->target_->getVelocity());749 this->bHasTargetPosition_ = ( this->targetPosition_ != Vector3::ZERO);750 751 Pawn* pawn = orxonox_cast<Pawn*>( this->getControllableEntity());752 753 if ( pawn)754 //pawn->setAimPosition( this->getControllableEntity()->getWorldPosition() + 4000*(this->getControllableEntity()->getOrientation() * WorldEntity::FRONT));755 pawn->setAimPosition( this->targetPosition_);762 this->targetPosition_ = getPredictedPosition( this->getControllableEntity() ->getWorldPosition() , hardcoded_projectile_speed, this->target_->getWorldPosition() , this->target_->getVelocity() ); 763 this->bHasTargetPosition_ = ( this->targetPosition_ != Vector3::ZERO ); 764 765 Pawn* pawn = orxonox_cast<Pawn*>( this->getControllableEntity() ); 766 767 if ( pawn ) 768 //pawn->setAimPosition( this->getControllableEntity() ->getWorldPosition() + 4000*( this->getControllableEntity() ->getOrientation() * WorldEntity::FRONT )); 769 pawn->setAimPosition( this->targetPosition_ ); 756 770 757 this->getControllableEntity() ->fire(0);771 this->getControllableEntity() ->fire( 0 ); 758 772 } 759 773
Note: See TracChangeset
for help on using the changeset viewer.