void handleBallPaddleCollision(Ball* ball, Paddle* paddle)
{
	if(rectsCollide(paddle->rect, ball->rect))
	{
		ball->rect.y = paddle->rect.y - ball->rect.h;
	
		//TODO: Mejorar
		float collisionAngle = getAngleBetweenPoints(paddle->rect.x + (paddle->rect.w/2), 
													 paddle->rect.y + (paddle->rect.h/2),
													 ball->rect.x + (ball->rect.w/2),
													 ball->rect.y + (ball->rect.h/2));

		ball->sx = cos(collisionAngle)*BALL_SPEED;
		ball->sy = -sin(collisionAngle)*BALL_SPEED;
	}
}
示例#2
0
void ew::RectCollidePhase::execute(const float delta)
{
  auto iterable = state->entities.get<RectCollidable>();
  for(auto c1i = iterable.begin(); c1i != iterable.end(); ++c1i)
  {
    for(auto c2i = c1i; c2i != iterable.end(); ++c2i)
    {
      if(c2i == c1i)
        continue;

      RectCollidable* c1 = *c1i;
      RectCollidable* c2 = *c2i;
      RectCollidable::RectCollisionInformation i1 = c1->getRectCollisionInformation();
      RectCollidable::RectCollisionInformation i2 = c2->getRectCollisionInformation();

      if(rectsCollide(i1.x, i1.y, i1.w, i1.h, i2.x, i2.y, i2.w, i2.h))
      {
        c1->handleRectCollision(c2);
        c2->handleRectCollision(c1);
      }
    }
  }
}
void ew::RectBlockCollidePhase::execute(const float delta)
{
  for(RectBlockCollidableActor* actor : state->entities.get<RectBlockCollidableActor>())
  {
    bool collisions = false;

    int xDir1 = 0;
    int yDir1 = 0;
    bool squished = false;

    collisions = false;

    for(RectBlockCollidableBlock* block : state->entities.get<RectBlockCollidableBlock>())
    {
      RectCollidable::RectCollisionInformation a = actor->getRectCollisionInformation();
      RectCollidable::RectCollisionInformation b = block->getRectCollisionInformation();

      bool horizontalCollision = rectsCollide(a.x, a.y - a.vy * delta, a.w, a.h, b.x, b.y - b.vy * delta, b.w, b.h);

      if(horizontalCollision)
      {
        int xDir2 = b.x > a.x ? -1 : 1;
        if(xDir1 != 0 && xDir1 != xDir2)
        {
          squished = true;
          break;
        }
        else
        {
          xDir1 = xDir2;
          collisions = actor->horizontalRectBlockCollision(*block, delta);
        }
      }
    }
    for(RectBlockCollidableBlock* block : state->entities.get<RectBlockCollidableBlock>())
    {
      RectCollidable::RectCollisionInformation a = actor->getRectCollisionInformation();
      RectCollidable::RectCollisionInformation b = block->getRectCollisionInformation();

      bool verticalCollision = rectsCollide(a.x, a.y, a.w, a.h, b.x, b.y, b.w, b.h);

      if(verticalCollision)
      {
        int yDir2 = b.y > a.y ? -1 : 1;
        if(yDir1 != 0 && yDir1 != yDir2)
        {
          squished = true;
          break;
        }
        else
        {
          yDir1 = yDir2;
          collisions = actor->verticalRectBlockCollision(*block, delta);
        }
      }
    }

    if(squished)
    {
      actor->squishRectBlockCollision();
    }
  }
}