9 Replies - 1820 Views - Last Post: 05 January 2009 - 06:17 AM Rate Topic: -----

#1 mastersmith98  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 39
  • Joined: 22-December 08

direction of collision?

Post icon  Posted 23 December 2008 - 11:48 AM

I'm working on a pong game using SDL with C++ and although everything is going fairly smoothly without a hitch, I have yet to actually get a good collision response(?) method working without problems showing up.

Right now I'm simply inverting the X velocity of my pong ball whenever it comes into contact with either of the paddles. This is problematic because when it hits the top part of the paddle, the ball goes inside of the paddle and spazes out.

I've tried several methods to see which direction the ball hit the paddle at, but every single one comes up dry.
(my various attempts can be found here).

none of them worked at all (even the last one that has the words success by it).

Does anyone have a suggestion to remedy the problem I'm having? I'm not very good at maths (which is evident by those pictures) and some of the methods I've found online seem needlessly complex.

EDIT: NVM, I found a much much simpler solution.

This post has been edited by mastersmith98: 23 December 2008 - 12:51 PM


Is This A Good Question/Topic? 0
  • +

Replies To: direction of collision?

#2 WolfCoder  Icon User is offline

  • Isn't a volcano just an angry hill?
  • member icon


Reputation: 784
  • View blog
  • Posts: 7,613
  • Joined: 05-May 05

Re: direction of collision?

Posted 24 December 2008 - 08:47 PM

If you solution isn't this already, you could have also subtracted the velocity vector of the ball to the object on impact with anything at all at any direction and then reverse it.
Was This Post Helpful? 0
  • +
  • -

#3 Tom9729  Icon User is offline

  • Segmentation fault
  • member icon

Reputation: 180
  • View blog
  • Posts: 2,641
  • Joined: 30-December 07

Re: direction of collision?

Posted 24 December 2008 - 08:55 PM

View Postmastersmith98, on 23 Dec, 2008 - 01:48 PM, said:

I'm working on a pong game using SDL with C++ and although everything is going fairly smoothly without a hitch, I have yet to actually get a good collision response(?) method working without problems showing up.

Right now I'm simply inverting the X velocity of my pong ball whenever it comes into contact with either of the paddles. This is problematic because when it hits the top part of the paddle, the ball goes inside of the paddle and spazes out.

I've tried several methods to see which direction the ball hit the paddle at, but every single one comes up dry.
(my various attempts can be found here).

none of them worked at all (even the last one that has the words success by it).

Does anyone have a suggestion to remedy the problem I'm having? I'm not very good at maths (which is evident by those pictures) and some of the methods I've found online seem needlessly complex.

EDIT: NVM, I found a much much simpler solution.

From what you've said, you had some code that was testing every update if the ball was colliding with the paddle, and then if true, reversing the ball's direction.

I'm guessing the problem was that the ball ended up "colliding" with the paddle for more than one update, and was therefore having it's direction changed before it could escape the paddle (hence the spazzing out).

I'm guessing your solution either had something to do with steps, or just using a line for the paddle and a point for the ball.

Please post your solution for others to see though. :)
Was This Post Helpful? 0
  • +
  • -

#4 mastersmith98  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 39
  • Joined: 22-December 08

Re: direction of collision?

Posted 26 December 2008 - 12:18 PM

Quote

If you solution isn't this already, you could have also subtracted the velocity vector of the ball to the object on impact with anything at all at any direction and then reverse it.


I did try doing that, but because the paddle isn't a prefect square, telling which angle the ball hit at was a bit inaccurate, at least for determining whether it hit the top/bottom/right/left side of the paddle. it might work better now because I'm updating the x and y velocities of the ball with the same value (basically making the ball travel at a 45 degree angle).

Quote

From what you've said, you had some code that was testing every update if the ball was colliding with the paddle, and then if true, reversing the ball's direction.

I'm guessing the problem was that the ball ended up "colliding" with the paddle for more than one update, and was therefore having it's direction changed before it could escape the paddle (hence the spazzing out).

I'm guessing your solution either had something to do with steps, or just using a line for the paddle and a point for the ball.

Please post your solution for others to see though. smile.gif


what was happening is that I had a general collision method and each time the ball hit the paddle at any place, it would reverse the x velocity. This worked when it hit the right and left sides of the paddle, but if it hit the top or bottom it would go into the paddle and spas out.

my solution to the problem (which isn't mine, I got it off of some tutorial site) is to simply disregard collision detection for the top and bottom sides of the paddle and see if the ball is in the specified x range and y range to actually hit the left/right side of the paddle. kind of a hack, but it works for what I was doing.

here's the code I basically ripped for the collision detection logic
http://alleg.sourcef...ng_game.en.html

This post has been edited by mastersmith98: 26 December 2008 - 12:35 PM

Was This Post Helpful? 0
  • +
  • -

#5 WolfCoder  Icon User is offline

  • Isn't a volcano just an angry hill?
  • member icon


Reputation: 784
  • View blog
  • Posts: 7,613
  • Joined: 05-May 05

Re: direction of collision?

Posted 28 December 2008 - 11:43 AM

View Postmastersmith98, on 26 Dec, 2008 - 12:18 PM, said:

Quote

If you solution isn't this already, you could have also subtracted the velocity vector of the ball to the object on impact with anything at all at any direction and then reverse it.


I did try doing that, but because the paddle isn't a prefect square, telling which angle the ball hit at was a bit inaccurate, at least for determining whether it hit the top/bottom/right/left side of the paddle. it might work better now because I'm updating the x and y velocities of the ball with the same value (basically making the ball travel at a 45 degree angle).
...


x += vx;
y += vy;
if(inside any solid object)
{
x -= vx;
y -= vy;
vx = -vx;
}

Pretty much does what I just said for pong. If you really want proof I'll implement it with my engine. (EDIT) Which I actually already did, I just remembered this is exactly how I got solid objects to stop all entities from moving past it without any of them getting stuck inside it in NovaForce.

This post has been edited by WolfCoder: 28 December 2008 - 11:47 AM

Was This Post Helpful? 0
  • +
  • -

#6 Lillefix  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 37
  • View blog
  • Posts: 204
  • Joined: 19-September 08

Re: direction of collision?

Posted 31 December 2008 - 10:37 AM

x += vx //Add the x-speed to the x-coordinate
if (collision()) //If the object has collided
{
	x -= vx;  //Subtract the x-speed we added earlier
	vx *= -1; //Invert the x-speed. We are now travelling in the other direction.
}

y += vy //Add the y-speed to the y-coordinate
if (collision()) //If the object has collided
{
	y -= vy;  //Subtract the y-speed we added earlier
	vy *= -1; //Invert the y-speed. We are now travelling in the other direction.
}

This post has been edited by Lillefix: 31 December 2008 - 10:43 AM

Was This Post Helpful? 0
  • +
  • -

#7 WolfCoder  Icon User is offline

  • Isn't a volcano just an angry hill?
  • member icon


Reputation: 784
  • View blog
  • Posts: 7,613
  • Joined: 05-May 05

Re: direction of collision?

Posted 31 December 2008 - 12:41 PM

In a standard game, you would want to call collision() as few times as possible for more speed. And depending on the compiler, to be safe you shouldn't multiply (use v = -v instead) although some compilers optimize it to the fastest case anyway.
Was This Post Helpful? 0
  • +
  • -

#8 Lillefix  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 37
  • View blog
  • Posts: 204
  • Joined: 19-September 08

Re: direction of collision?

Posted 04 January 2009 - 07:10 AM

True, but i your example vy never changes, which means that if the ball were to be hit the righ or the left, it would not bounce back.

Or have I misunderstood your nippet?

This post has been edited by Lillefix: 04 January 2009 - 07:12 AM

Was This Post Helpful? 0
  • +
  • -

#9 Tom9729  Icon User is offline

  • Segmentation fault
  • member icon

Reputation: 180
  • View blog
  • Posts: 2,641
  • Joined: 30-December 07

Re: direction of collision?

Posted 04 January 2009 - 12:24 PM

View PostLillefix, on 4 Jan, 2009 - 09:10 AM, said:

True, but i your example vy never changes, which means that if the ball were to be hit the righ or the left, it would not bounce back.

Or have I misunderstood your nippet?

vy is the vertical component of velocity, I think you mean vx?
Was This Post Helpful? 0
  • +
  • -

#10 Lillefix  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 37
  • View blog
  • Posts: 204
  • Joined: 19-September 08

Re: direction of collision?

Posted 05 January 2009 - 06:17 AM

No, I ment vx, but I messed up "right and left" with "top and bottom".
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1