programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering Languages Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

Simple 2D collision response

Jimmy Praet
Greenhorn
Posts: 21
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

Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
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.

Jim Yingst
Wanderer
Sheriff
Posts: 18671
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 ]

Layne Lund
Ranch Hand
Posts: 3061
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

Jim Yingst
Wanderer
Sheriff
Posts: 18671
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.