Re: Collision detection



In article <1169215409.851823.142130@xxxxxxxxxxxxxxxxxxxxxxxxxxxx>,
sredmyer@xxxxxxxxxx says...

David Kerber wrote:
In article <1169212421.304953.314500@xxxxxxxxxxxxxxxxxxxxxxxxxxx>,
sredmyer@xxxxxxxxxx says...
Can anybody point me at some example code showing how to do collision
detection. What I would like to do is have two rectangular objects
moving about the screen. These objects should "bounce" off the sides,
top and bottom as well as "bounce" off each other. This bouncing
should appear realistic, that is to say that when the objects collide,
the direction of both objects should be changed based on how they
actually collided.

Are the collisions elastic, or is there some energy loss (i.e.
realistic)?
Not entirely sure what you mean by elastic here but the collision
should just result in a simple bounce. That bounce would alter the

When real objects collide, there is always some energy loss in the
collision, meaning that the total kinetic energy (mv^2) of the objects
in the system drops. This means that if the objects have the same mass,
they slow down, even if they bounce off of each other.


direction based on the axis of the collision. If an object is moving
down and to the left collides with something on it 'Y' axis it
direction of motion would be changed to up and to the left




Do the rectangles rotate as well, so you have to handle
their rotational moments of inertia? Do you know the math behind
collisions? If so, the coding is no problem.
No there is no rotation. I have attempted the collision detection but
have a problem figuring how to identify which axis of motion (X or Y)
caused the collision. The attempts I have tried include both simple
math using left, width, top, height properties and API method using
PtInRect. Testing for window boundary collision is fairly straight
forward. Testing for object to object collision is also not difficult
but knowing how the two objects collided and therefore how the
direction of each should change is what is giving me problems.

I don't think you're going to be able to do this using windows' built-in
capabilities. You're going to have to work out the actual mathematics
to describe the objects and their motion, and go from there by writing
code to work the math. It's not a trivial exercise, and game designers
are constantly working to improve their collision detection algorithms.

There are many simplifications you can use to get you started, such as
making the rectangles always be "square with the world", meaning that
their sides are always vertical and horizontal, and not at angles.

You can assume that the objects rebound as if they were both the same
mass.

You can also assume that the collisions occur AS IF they were point
objects (like the old Pong cursor).

If you take both of these last two assumptions, it makes things
enormously simpler, in that the sum of the magnitudes of the horizontal
speeds is always the same, as is the sum of magnitudes of the vertical
speeds, even though their signs may change as they bounce off walls and
each other.


I would like to see an example of a speedy algorithm that I could study
to learn how it is done.

If you can work out the code to determine the points on the lines
comprising each rectangle, then collision detection is just checking to
see if any point is included in both rectangles, which is equivalent to
seeing if any of the line segments of a rectangle intersect those of the
other one. As for speed, any modern hardware should be able to do this
faster than you can watch them on the screen, even with an inefficient
algorithm.

--
Remove the ns_ from if replying by e-mail (but keep posts in the
newsgroups if possible).



--
Remove the ns_ from if replying by e-mail (but keep posts in the
newsgroups if possible).
.



Relevant Pages

  • Re: Collision detection
    ... top and bottom as well as "bounce" off each other. ... Not entirely sure what you mean by elastic here but the collision ... I have attempted the collision detection but ... seeing if any of the line segments of a rectangle intersect those of the ...
    (microsoft.public.vb.general.discussion)
  • Re: Collision detection
    ... top and bottom as well as "bounce" off each other. ... Are the collisions elastic, or is there some energy loss (i.e. ... Do the rectangles rotate as well, ... then collision detection is just checking to ...
    (microsoft.public.vb.general.discussion)
  • Re: Bitmap collision detection in VB6
    ... rectangles calculation - simple enough, ... sprite collision detection, and it is *very fast* in a compiled exe ... does contain a black pixel then the actual sprite pixels are touching, ... bmp file. ...
    (comp.lang.basic.visual.misc)
  • Re: Collision detection
    ... Collision detection is made simpler if you track each object's center position. ... amount of overlap and determining that the lesser overlap ... determines which way the bounce should happen. ... it has overlap in both directions. ...
    (microsoft.public.vb.general.discussion)
  • Re: Bouncing circles, simple
    ... >>>relative velocity to simplify the math, I'll convert to absolute when I ... it refers to how much of a "bounce force" to add. ... hold (whenever there is a collision, a little fudging is made to the ... the effects of this are related to multiple contacts. ...
    (comp.games.development.programming.misc)

Loading