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

Author Topic: How do I fix position after collision?  (Read 2738 times)

0 Members and 1 Guest are viewing this topic.

Tobberoth

  • Guest
How do I fix position after collision?
« on: September 29, 2013, 01:58:14 pm »
Hello,

I'm playing around with collision detection and my current situation is this: I have a ball which is controlled by WASD, and I have a wall object. The ball is 16x16 px, the wall is 32x32. I have gotten to the point where I can successfully detect a collision and thus stop movement. However, since the ball moves a variant amount of pixels each frame, the distance it stops from the wall object varies, and it almost never stops perfectly side by side. What I want is to make it so that the ball stops right at the wall.

I can't come up with a good solution how to get this done. I get the collision and I get the overlap, but I don't understand how I can calculate a new position for the ball based on this overlap. Here's my current code which obviously doesn't work as intended (sorry for the C# code, but I'm sure you can all understand it, it's not all that important to my question anyway):

Code: [Select]
FloatRect overlap;
 if (CheckCollision(ball.sprite.GetGlobalBounds(), wall.sprite.GetGlobalBounds(), out overlap))
{
new_pos.X = new_pos.X + overlap.Width;
new_pos.Y = new_pos.Y + overlap.Height;
ball.sprite.Position = new_pos;
}

The CheckCollision function is simply doing a normal FloatRect intersect call. The problem here, of course, is that what I want to do to new_pos depends on where the intersection happened. If the ball hits the wall from the left, I want to subtract the overlap width, but if I hit it from the right, I want to add the width. In both cases, I don't want to mess with the Y value. However, if I hit the wall from the top, I obviously want to subtract the height of the overlap.

I'm just really confused since it seems like I would need quite a few of conditionals to find out what collision it is before I can calculate the new position, it seems like there must be a smarter way to do it.

If you want to put code in your answer, feel free to use C++ if you prefer, I have no problem translating it for my needs.
« Last Edit: September 29, 2013, 02:05:58 pm by Tobberoth »

AlexAUT

  • Sr. Member
  • ****
  • Posts: 396
    • View Profile
Re: How do I fix position after collision?
« Reply #1 on: September 29, 2013, 07:33:54 pm »
Quote
If the ball hits the wall from the left, I want to subtract the overlap width, but if I hit it from the right, I want to add the width. In both cases, I don't want to mess with the Y value. However, if I hit the wall from the top, I obviously want to subtract the height of the overlap.

I think it's the easiest way.



AlexAUT

Geheim

  • Full Member
  • ***
  • Posts: 201
    • View Profile
    • Email
Re: How do I fix position after collision?
« Reply #2 on: September 29, 2013, 10:06:24 pm »
There are two ways of doing this:

1) You split your movement into the axis, because otherwise you don't know from where you collided.
Basicially you move your ball on the X-axis, then check for collision, if you collide you check which side (left or right) and set the position to the position of the wall. The same then for the Y-axis.

2) If you collide, you move back you ball with baby steps (0.1f or lower), till you don't collide anymore.

I recommend you 1) ;)
Failing to succeed does not mean failing to progress!

Tobberoth

  • Guest
Re: How do I fix position after collision?
« Reply #3 on: September 30, 2013, 07:31:18 pm »
There are two ways of doing this:

1) You split your movement into the axis, because otherwise you don't know from where you collided.
Basicially you move your ball on the X-axis, then check for collision, if you collide you check which side (left or right) and set the position to the position of the wall. The same then for the Y-axis.

2) If you collide, you move back you ball with baby steps (0.1f or lower), till you don't collide anymore.

I recommend you 1) ;)

Thank you. It took me a while to realize what you meant, but your idea to split the movement into the axis finally clicked and I got it working. Seems like a bit of a performance hit (the algorithm obviously becomes O(2N) instead of O(N) ) but from what I hear, collision detection will generally not be a performance issue in 2D games.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
AW: Re: How do I fix position after collision?
« Reply #4 on: September 30, 2013, 08:32:58 pm »
Seems like a bit of a performance hit (the algorithm obviously becomes O(2N) instead of O(N) ) but from what I hear, collision detection will generally not be a performance issue in 2D games.
Seems like you don't really understand the O notation. ;)
Constants can be removed, thus O(2N) is "equal" to O(N).

Also you'll only have a performance issue once your "bug free" code is running at framerates below 60 or even 30. Until then you shouldn't have to try optimizing stuff too much. But it's of course good to think about smart designs.  ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/