Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: [Solved] Problems with collision detection  (Read 4665 times)

0 Members and 1 Guest are viewing this topic.

PM-Heavy

  • Newbie
  • *
  • Posts: 2
    • View Profile
[Solved] Problems with collision detection
« on: July 15, 2008, 08:41:09 pm »
Sorry if this isn't the right forum, I'm not sure where it best fits.

I'm working on a pong clone, but am having problems getting collision detection for the left paddle working.  The code is the same for both paddles, but it doesn't always work for the left paddle.  Some times the ball goes right through it, sometimes it works.  I'm using the first approach given in  this article, but with 'if' statements instead of a function.  I had the game working with SFML 1.2, but I wanted to rewrite it with 1.3.  I've created my own Ball and Paddle classes derived from sf::Sprite, and am using sf::Vector2 to get positions of the sides.

I can post the code if it will help, but someone will have to tell me how.

Thanks in advance for any help.

Puck

  • Newbie
  • *
  • Posts: 8
    • View Profile
[Solved] Problems with collision detection
« Reply #1 on: July 15, 2008, 11:46:17 pm »
The easiest way to do this would probably be to use SFML directly:

Code: [Select]
IntRect PaddleRect(Paddle.GetPosition().x, Paddle.GetPosition().y, Paddle.GetPosition().x + Paddle.GetSize().x, Paddle.GetPosition().y + Paddle.GetSize().y);
IntRect BallRect(Ball.GetPosition().x, Ball.GetPosition().y, Ball.GetPosition().x + Ball.GetSize().x, Ball.GetPosition().y + Ball.GetSize().y);
IntRect Intersection;
//Find out if the ball is touching the paddle, and get a rectangle representing the overlap if so
if (BallRect.Intersects(PaddleRect, &Intersection)) {
//Move the ball so it's no longer overlapping the paddle, otherwise it would change directions forever (get "stuck")
Ball.Move((Ball.xSpeed > 0 ? -Intersection.GetWidth() : Intersection.GetWidth())); //If the ball is moving right, slide it left, otherwise slide it right
//Reverse the ball's direction
Ball.xSpeed = -Ball.xSpeed;
}


That code assumes a lot about the structure of your "Ball" and "Paddle" classes, but hopefully you get the idea. :)

As a side note, it would be nice if sf::Sprite had a function like this:

Code: [Select]
const IntRect Sprite::GetRectangle() {
return IntRect(myPosition.x, myPosition.y, myPosition.x + GetSize().x, myPosition.y + GetSize().y);
}


Since it would greatly simplify this kind of collision code. :)

Hiura

  • SFML Team
  • Hero Member
  • *****
  • Posts: 4321
    • View Profile
    • Email
[Solved] Problems with collision detection
« Reply #2 on: July 16, 2008, 09:07:34 am »
Your Sprite::GetRectangle is not good for one thing : if you rotate your sprite, the FloatRect (no Int*) can be false.

In the case of a ball and a paddle, you can take the top/right/left/bottom of the paddle and the radius of the ball (and its center, and its new position, and play with them) to check the collision.

On the french wiki side there are some collisions code, you can look at them. (maybe the source is in English.)
SFML / OS X developer

Puck

  • Newbie
  • *
  • Posts: 8
    • View Profile
[Solved] Problems with collision detection
« Reply #3 on: July 16, 2008, 09:22:56 am »
Quote from: "Hiura"
Your Sprite::GetRectangle is not good for one thing : if you rotate your sprite, the FloatRect (no Int*) can be false.


Ah, good point. I guessed there must be a reason why the ability wasn't already included, but I couldn't figure out what it was. :)

Still though, the ability to get the (axis aligned) bounding box of a (possibly rotated) Sprite could be a useful feature to add at some point..

PM-Heavy

  • Newbie
  • *
  • Posts: 2
    • View Profile
[Solved] Problems with collision detection
« Reply #4 on: July 16, 2008, 01:36:24 pm »
Right now I'm using Hiura's method, except the ball is a square.  That's why I wrote my own ball and paddle classes, I wanted functions to return the top/right/bottom/left of the sprites.

I don't plan on ever rotating the sprites, so I may try Puck's way.  I'll also check out the wiki.  I've got a few friends who know some French, so they might be able to help me if Google translate can't.  :)

Thanks a lot guys  :D