# Simple 2D collision response

Jimmy Praet

Greenhorn

Posts: 21

posted 12 years ago

Hi,

I am having some problems in calculating the collision response of the following situation:

I have a rectangle moving at a certain dx and dy velocity inside an octagon (It's a dice game). When it collides with an edge of the octagon I need to calculate the new dx and dy values.

The top and bottom edge is offcourse: dy=-dy

And left and right edge: dx=-dx

But what about the other edges

I am having some problems in calculating the collision response of the following situation:

I have a rectangle moving at a certain dx and dy velocity inside an octagon (It's a dice game). When it collides with an edge of the octagon I need to calculate the new dx and dy values.

The top and bottom edge is offcourse: dy=-dy

And left and right edge: dx=-dx

But what about the other edges

posted 12 years ago

Hi Jimmy,

As this is basically a geometry question, it's a perfect candidate for our games programming forum. I'm going to move this thread there, where I'm sure it will be answered quickly.

As this is basically a geometry question, it's a perfect candidate for our games programming forum. I'm going to move this thread there, where I'm sure it will be answered quickly.

Jim Yingst

Wanderer

Sheriff

Sheriff

Posts: 18671

posted 12 years ago

For the bottom right and upper left corners:

newDx = dy

newDy = dx

For upper right and bottom left:

newDx = -dy

newDy = -dx

Probably you'll want to implement these as something like

double temp = dy;

dy = dx;

dx = temp;

and

double temp = -dy;

dy = -dx;

dx = temp;

[ October 03, 2004: Message edited by: Jim Yingst ]

newDx = dy

newDy = dx

For upper right and bottom left:

newDx = -dy

newDy = -dx

Probably you'll want to implement these as something like

double temp = dy;

dy = dx;

dx = temp;

and

double temp = -dy;

dy = -dx;

dx = temp;

[ October 03, 2004: Message edited by: Jim Yingst ]

"I'm not back." - Bill Harding, *Twister*

Layne Lund

Ranch Hand

Posts: 3061

posted 12 years ago

It will take a little bit of trigonometry to make the equations for the other for "diagonal" sides of the octogon. I don't know all the equations you need. However, a simple google search turned up a promising tutorial. I'm sure you can google for more material if this is too far over your head.

HTH

Layne

HTH

Layne

Jim Yingst

Wanderer

Sheriff

Sheriff

Posts: 18671

posted 12 years ago

Um, all 8 sides of the octagon are covered already, aren't they? IT's true that a little trig would be needed to handle bouncing off surfaces at other angles, but for surfaces at 0, 45, 90, 135, 180, 225, 270, and 315 degrees, no trig is needed.

"I'm not back." - Bill Harding, *Twister*