Example #1
0
void BannerBox::Update()
{
  double angleToTravel;
  Vector2* moveVector;

  if( CurrentPosition != TargetPosition )
  {
    angleToTravel = CurrentPosition->AngleTo( TargetPosition );
    moveVector = new Vector2( angleToTravel );
    moveVector->Multiply( 1.7 );
    CurrentPosition->Add( moveVector );
    if( abs(CurrentPosition->X - TargetPosition->X) <= 2.0 && abs(CurrentPosition->Y - TargetPosition->Y) <= 2.0 )
    {
      CurrentPosition->X = TargetPosition->X;
      CurrentPosition->Y = TargetPosition->Y;
    }

    delete moveVector;
  }
}
Example #2
0
void Ball::Update()
{
	Vector2* prevPosition = new Vector2(ballPosition->X, ballPosition->Y);

	Vector2* v = new Vector2( ballDirection->ToDegrees() );
	v->Multiply( ballVelocity );
	ballPosition->Add( v );
	delete v;

	if( ballPosition->X < gameArena->Player1WallX )
	{
		ballPosition->X = gameArena->Player1WallX - (ballPosition->X - gameArena->Player1WallX);
		if( ballDirection->ToDegrees() < 180.0f )
		{
			ballDirection->Add( ballDirection->ShortestAngleTo(90.0f) * -2.0f );
		} else {
			ballDirection->Add( (270.0f - ballDirection->ToDegrees()) * 2.0f );
		}
		// Hurt Player 1
		if( gameArena->Player1 != 0 )
		{
			gameArena->Player1->TakeDamage( 2 );
		}
	}
	if( ballPosition->X >= gameArena->Player2WallX )
	{
		ballPosition->X -= ballPosition->X - gameArena->Player2WallX;
		if( ballDirection->ToDegrees() > 270.0f )
		{
			ballDirection->Add( ballDirection->ShortestAngleTo(270.0f) * -2.0f );
		} else {
			ballDirection->Add( 180.0f - (ballDirection->ToDegrees() * 2.0f) );
		}
		// Hurt player 2
		if( gameArena->Player2 != 0 )
		{
			gameArena->Player2->TakeDamage( 2 );
		}
	}
	if( ballPosition->Y < gameArena->RoofY )
	{
		ballPosition->Y = gameArena->RoofY - (ballPosition->Y - gameArena->RoofY);
		if( ballDirection->ToDegrees() < 270.0f )
		{
			ballDirection->Add( ballDirection->ShortestAngleTo(180.0f) * -2.0f );
		} else {
			ballDirection->Add( (360.0f - ballDirection->ToDegrees()) * 2.0f );
		}
	}
	if( ballPosition->Y >= gameArena->FloorY )
	{
		ballPosition->Y -= ballPosition->Y - gameArena->FloorY;
		if( ballDirection->ToDegrees() > 90.0f )
		{
			ballDirection->Add( ballDirection->ShortestAngleTo(180.0f) * 2.0f );
		} else {
			ballDirection->Add( 360.0f - (ballDirection->ToDegrees() * 2.0f) );
		}
	}

	// Check collisions with players
	Line* ballPath = new Line( prevPosition, ballPosition );
	Line* pArea = 0;
	bool ballAdjustXLeft;
	if( ballDirection->ToDegrees() > 90.0f && ballDirection->ToDegrees() < 270.0f && gameArena->Player1 != 0 )
	{
		ballPath->Points[0]->X -= ballRadius;
		ballPath->Points[1]->X -= ballRadius;
		ballAdjustXLeft = true;
		pArea = gameArena->Player1->GetCollisionLine();
	}
	if( (ballDirection->ToDegrees() < 90.0f || ballDirection->ToDegrees() > 270.0f) && gameArena->Player2 != 0 )
	{
		ballPath->Points[0]->X += ballRadius;
		ballPath->Points[1]->X += ballRadius;
		ballAdjustXLeft = false;
		pArea = gameArena->Player2->GetCollisionLine();
	}
	if( pArea != 0 )
	{
		Vector2* pContact = ballPath->GetIntersection( pArea );
		if( pContact != 0 )
		{
			/*
			if( ballDirection->ToDegrees() < 180.0f && ballDirection->ToDegrees() > 90.0f )
			{
				ballDirection->Add( ballDirection->ShortestAngleTo(90.0f) * -2.0f );
				ballPosition->X += (pContact->X - ballPosition->X) * 2.0f;
			} else if( ballDirection->ToDegrees() >= 180.0f && ballDirection->ToDegrees() < 270.0f ) {
				ballDirection->Add( (270.0f - ballDirection->ToDegrees()) * 2.0f );
				ballPosition->X += (pContact->X - ballPosition->X) * 2.0f;
			} else if( ballDirection->ToDegrees() > 270.0f ) {
				ballDirection->Add( ballDirection->ShortestAngleTo(270.0f) * -2.0f );
				ballPosition->X -= (ballPosition->X - pContact->X) * 2.0f;
			} else if( ballDirection->ToDegrees() < 90.0f ) {
				ballDirection->Add( 180.0f - (ballDirection->ToDegrees() * 2.0f) );
				ballPosition->X -= (ballPosition->X - pContact->X) * 2.0f;
			}
			*/
			Angle* a = pArea->Reflection( ballPath );
			ballDirection->Set( a->ToDegrees() );
			ballPosition->X += (!ballAdjustXLeft ? (pContact->X - ballRadius - ballPosition->X) * 2.0f : (ballPosition->X - ballRadius - pContact->X) * -2.0f);
			delete a;

		}
	}
	delete pArea;
	delete ballPath;

	delete prevPosition;
}