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

Author Topic: Collision detection question  (Read 3849 times)

0 Members and 1 Guest are viewing this topic.

mentor

  • Newbie
  • *
  • Posts: 31
    • View Profile
Collision detection question
« on: August 18, 2013, 05:30:11 pm »
Hi,

I'm writing a simple arkanoid game with SFML 2.1. I have a question about collision detection. How to detect which side of the paddle (top, left, right) the ball hit?

Karsomir

  • Newbie
  • *
  • Posts: 14
    • View Profile
Re: Collision detection question
« Reply #1 on: August 18, 2013, 05:51:22 pm »
U can make 4 rectangles for each side, or get information about intersection - so if height of intersection is bigger than width its vertical collision and if else its horizontal, else its corner :)

mentor

  • Newbie
  • *
  • Posts: 31
    • View Profile
Re: Collision detection question
« Reply #2 on: August 18, 2013, 05:56:23 pm »
3 rectangles ;) I rather don't need fourth rectangle for the bottom of the paddle.

Height of intersection - you mean this: mysprite.getGlobalBounds().height?

Karsomir

  • Newbie
  • *
  • Posts: 14
    • View Profile
Re: Collision detection question
« Reply #3 on: August 18, 2013, 06:34:33 pm »
I mean this:
http://www.sfml-dev.org/documentation/2.1/classsf_1_1Rect.php#a5f1874792b04c7e221bb786b31f5836e

And eventually u will have to check collision at 4 sides for obstacles ;)

mentor

  • Newbie
  • *
  • Posts: 31
    • View Profile
Re: Collision detection question
« Reply #4 on: August 19, 2013, 01:03:40 pm »
OK, I had a problem with collision detection... And I still have this problem. I wrote this simple collision detection:

Code: [Select]
sf::FloatRect bound = pad.getsprite().getGlobalBounds();

sf::FloatRect sprbound = spr.getGlobalBounds();
if (bound.intersects(sprbound)) {
                //bounce
vec = 180.0f - vec;
yspeed = -yspeed;
}

Where "spr" is my ball sprite and "pad" is a reference to the Paddle object.

But the problem is that sometimes when ball hits the very top of the paddle, the ball starts to behave "jittery". I think it shouldn't behave like that, I tried many ways to fix this (including own collision detection system), but nothing seems to work. What's wrong?

G.

  • Hero Member
  • *****
  • Posts: 1593
    • View Profile
Re: Collision detection question
« Reply #5 on: August 19, 2013, 02:29:04 pm »
Your ball enters the paddle (collision), and gets its speed reversed.
Next frame, your ball is moved a few pixels but is still inside the paddle (collision) so it gets its speed reversed again, and never (or rarely) exits the paddle, so it seems jittery.

When a collision happens you could just move your ball to the top of the paddle, so you're sure that it won't get stuck inside. (moreover, there won't be any frame displayed with the ball inside the pad, so it may be more visually pleasing)

mentor

  • Newbie
  • *
  • Posts: 31
    • View Profile
Re: Collision detection question
« Reply #6 on: August 19, 2013, 03:25:47 pm »
So, right after
Code: [Select]
if (bound.intersects(sprbound))I should add
Code: [Select]
spr.setPosition(spr.getPosition().x, pad.getsprite().getPosition().y - spr.getTextureRect().height);Is that right? Seems to work fine :) I already tried this, but later decided to write own collision detection system... Thanks!

G.

  • Hero Member
  • *****
  • Posts: 1593
    • View Profile
Re: Collision detection question
« Reply #7 on: August 19, 2013, 03:33:21 pm »
Yes, something like that.
Anyway try it, if it looks and feels good then keep it. :p

mentor

  • Newbie
  • *
  • Posts: 31
    • View Profile
Re: Collision detection question
« Reply #8 on: August 19, 2013, 03:38:41 pm »
Yeah, it looks good ;) I'll do some more testing to be sure it works ok, but thanks again!