School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become an Expert!

Join 300,475 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,747 people online right now. Registration is fast and FREE... Join Now!




2D Collision Response

 

2D Collision Response, Breakout style game

Jmatt110

4 Jul, 2009 - 03:15 AM
Post #1

New D.I.C Head
*

Joined: 4 Jul, 2009
Posts: 4

Hey All,

I'm having some issues with my collision response for a breakout style game. Currently my circle (ball) to rectangle (bricks/paddle) detection works fine, however the response is acting odd. Engine used is SFML.

I searched up the vector reflection equation, however I think I may be implementing it wrong (particularly the normal).

CODE
r=u-2(u.n)n


Where n is the normal of the plane and u is the incident vector.

Here is my code implementation:

CODE
ballVel = ballVel - 2 * ((paddlePos.normalise() * ballVel) + (paddlePos.normalise() * ballVel)) * paddlePos.normalise;


I believe I may be caclulating the normal wrong, however I havn't been able to find any defnitive formulas.

Currently upon a collision with the paddle, the ball will bounce in an upwards direction, continuing this until it is moving perfectly up and down. Playing around with the equation etc just causes the ball to bounce back in the direction it came, although at a slightly different angle. Any ideas on how to implement correct circle-rectangle collision response?

Sorry if I havn't posted enough code etc, first time asking on a forum for programming help.

Thanks, Jmatt.

User is offlineProfile CardPM
+Quote Post


JackOfAllTrades

RE: 2D Collision Response

4 Jul, 2009 - 05:46 AM
Post #2

I exist to Google your problems.
Group Icon

Joined: 23 Aug, 2008
Posts: 4,948



Thanked: 424 times
Dream Kudos: 50
Expert In: Being annoyed with lazy people.

My Contributions
Off to Game Programming.
User is offlineProfile CardPM
+Quote Post

SixOfEleven

RE: 2D Collision Response

4 Jul, 2009 - 06:21 AM
Post #3

Code Guru
Group Icon

Joined: 18 Oct, 2008
Posts: 2,910



Thanked: 165 times
Dream Kudos: 725
Expert In: C, C#, XNA, Game Programming, Programming Concepts

My Contributions
QUOTE(Jmatt110 @ 4 Jul, 2009 - 05:15 AM) *

Hey All,

I'm having some issues with my collision response for a breakout style game. Currently my circle (ball) to rectangle (bricks/paddle) detection works fine, however the response is acting odd. Engine used is SFML.

I searched up the vector reflection equation, however I think I may be implementing it wrong (particularly the normal).

CODE
r=u-2(u.n)n


Where n is the normal of the plane and u is the incident vector.

Here is my code implementation:

CODE
ballVel = ballVel - 2 * ((paddlePos.normalise() * ballVel) + (paddlePos.normalise() * ballVel)) * paddlePos.normalise;


I believe I may be caclulating the normal wrong, however I havn't been able to find any defnitive formulas.

Currently upon a collision with the paddle, the ball will bounce in an upwards direction, continuing this until it is moving perfectly up and down. Playing around with the equation etc just causes the ball to bounce back in the direction it came, although at a slightly different angle. Any ideas on how to implement correct circle-rectangle collision response?

Sorry if I havn't posted enough code etc, first time asking on a forum for programming help.

Thanks, Jmatt.


Have you tried using this:

CODE
ballVel = ballVel - 2 * ((paddlePos.normalise() * ballVel) * paddlePos.normalise();


From the formula you gave, ballVel would be u and paddlePos.normalise would be n. If you look at the formula. Taking the formula and substituting the above would give you the above code.

This post has been edited by SixOfEleven: 4 Jul, 2009 - 06:22 AM
User is offlineProfile CardPM
+Quote Post

jyth

RE: 2D Collision Response

4 Jul, 2009 - 10:14 AM
Post #4

New D.I.C Head
Group Icon

Joined: 29 Jun, 2009
Posts: 23


Dream Kudos: 50
My Contributions
As far as the actual math is concerned, if you need the normal of two vectors, calculate a cross product, if you need the normal of one 2D vector, use a negative reciprocal
User is offlineProfile CardPM
+Quote Post

Jmatt110

RE: 2D Collision Response

4 Jul, 2009 - 12:28 PM
Post #5

New D.I.C Head
*

Joined: 4 Jul, 2009
Posts: 4

QUOTE(SixOfEleven @ 4 Jul, 2009 - 06:21 AM) *

Have you tried using this:

CODE
ballVel = ballVel - 2 * ((paddlePos.normalise() * ballVel) * paddlePos.normalise();


From the formula you gave, ballVel would be u and paddlePos.normalise would be n. If you look at the formula. Taking the formula and substituting the above would give you the above code.


Using that formula just causes to ball to jump randomly accross the screen.

User is offlineProfile CardPM
+Quote Post

jyth

RE: 2D Collision Response

4 Jul, 2009 - 07:40 PM
Post #6

New D.I.C Head
Group Icon

Joined: 29 Jun, 2009
Posts: 23


Dream Kudos: 50
My Contributions
So are you trying to find the velocity of the ball after its reflection with the paddle?

If so, you don't need the normal

This post has been edited by jyth: 4 Jul, 2009 - 07:42 PM
User is offlineProfile CardPM
+Quote Post

Jmatt110

RE: 2D Collision Response

4 Jul, 2009 - 07:43 PM
Post #7

New D.I.C Head
*

Joined: 4 Jul, 2009
Posts: 4

QUOTE(jyth @ 4 Jul, 2009 - 07:40 PM) *

So are you trying to find the velocity of the ball after its reflection with the paddle?

If so, you don't need the normal


No, i'm trying to calculate the reflection.
User is offlineProfile CardPM
+Quote Post

jyth

RE: 2D Collision Response

4 Jul, 2009 - 07:45 PM
Post #8

New D.I.C Head
Group Icon

Joined: 29 Jun, 2009
Posts: 23


Dream Kudos: 50
My Contributions
QUOTE(Jmatt110 @ 4 Jul, 2009 - 07:43 PM) *

QUOTE(jyth @ 4 Jul, 2009 - 07:40 PM) *

So are you trying to find the velocity of the ball after its reflection with the paddle?

If so, you don't need the normal


No, i'm trying to calculate the reflection.


What do you mean by the reflection? the angle?
User is offlineProfile CardPM
+Quote Post

Jmatt110

RE: 2D Collision Response

4 Jul, 2009 - 07:53 PM
Post #9

New D.I.C Head
*

Joined: 4 Jul, 2009
Posts: 4

QUOTE(jyth @ 4 Jul, 2009 - 07:45 PM) *

What do you mean by the reflection? the angle?


I want to reflect the vector that the ball is coming in at, so that it bounces off the paddle at the opposite that it came in at. For now I've just reversed the y of the vector, however this fails when hitting the sides of the paddle. Eventually I'd like to get the ball bouncing off at different angles depending upon where it collides.

User is offlineProfile CardPM
+Quote Post

stayscrisp

RE: 2D Collision Response

5 Jul, 2009 - 08:23 AM
Post #10

Mouth->Insert(Foot);
Group Icon

Joined: 14 Feb, 2008
Posts: 1,380



Thanked: 53 times
Dream Kudos: 300
My Contributions
You can work out the angle of collision like this
dx = velocity * cos(angle)
dy = velocity * sin(angle)

the bounce would be like this
dx = -dx;

Work out the velocity and angle like this
CODE

newVelocity = velocity + ( rand() % 3 - 1 ); // velocity +/- 1
newAngle = angle + ( rand() % 3 - 1 ); // angle +/- 1 degree


Hope this helps a little smile.gif

*edit* Code Tags

This post has been edited by stayscrisp: 5 Jul, 2009 - 08:24 AM
User is online!Profile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/8/09 03:25AM

Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month