## Frustrations, Collisions, et al.

Member
Posts: 196
Joined: 2003.10
Post: #1
Over the past few years I've tried to maintain my passion for programming, but have been frustrated by the lack of immediate results. My only finished product has been Offender (a 21 days Vectorized entry) - since switching to OpenGL, all my projects have died at the same stage: collision detection.

It's one of the first things that I try to implement when doing a game, and it invariably fails. I have limited math knowledge (none since high school) - I find the math of programming tough as it is... Does anyone have any hints? Is anyone willing to write a clear, concise tutorial on this subject (Like Themsalltook's excellent tutorials). All the tutorials I've seen skip steps and assume you know something that I don't! It's gotten to the point where I look at things like 21 Days: Accelerated, and go "oh goodness. I can't do that. You need to know how to do collisions to do that." That's a bit frustrating to me.

I can see someone going: You had collisions in Offender. But in Offender I had to just check for a collision, end of story. If you collided, you blew up. In games where you can bounce of things it's infinitely more complicated. Help! This is killing my hobby!
Sage
Posts: 1,482
Joined: 2002.09
Post: #2
Here's a great tutorial for separating axis tests.
Physics from N

There's also ODE if you want 3D stuff and just want a library that's already made. I'm also still working SPK for 2D games.
Member
Posts: 184
Joined: 2004.07
Post: #3
That's probably far more complicated than you want at this point, if you've never done anything with collision response. Give us more information about what you're trying to do exactly and then it will be easier to give you a simple way to go about it. You're biting off way more than you can chew if you just jump into polygons and realistic physics if you've never done rectangles or circles before.

For example, if you're doing a 2d traditional racing game, the easiest way to handle collisions with walls is probably to do what it looks like skyhawk has and make everything circles. Then, if the circle representing the car collides one of the circles representing the wall, you simply displace the car circle so that it's no longer inside the wall circle, or you could give the car some velocity in the opposite direction than the penetration, or both of these if it works the best (you probably have to experiment a little to get the effect you want. You could make the walls polygons or tiles, though that may be more complicated that you need, too.
Moderator
Posts: 916
Joined: 2002.10
Post: #4
1) develop collision detection for game engine
2) reuse same engine for 3 years
3) ...
4) Microbian: Fighter
5) Profit!
Member
Posts: 196
Joined: 2003.10
Post: #5
Wow, that's a wonderful tutorial. The flash animations really illustrate the concepts well.

Basically an example situation for me would be: I have a brick and a ball. I know the size of the brick and the ball, and I know their positions (in position vectors). I know their direction and velocity, and I want to test each frame if they're overlapping. If they are, I want the ball to bounce off the brick appropriately. This seems simple in real life, but programatically it seems impossible.

In the case of racing - I have a car that moves around. What's difficult is not detecting the collision (I can do that through bounding boxes), but rather determining what the resulting angle should be after the bounce. I'm looking forward to seeing some of the source code from 21 days...
Member
Posts: 89
Joined: 2002.04
Post: #6
Here is a good introductory book to general math and physics: Beginning Math and Physics for Game Programmers

The book assumes the reader has very little math or physics background so you will probably only skim over some of the chapters. It does, however, have some good examples of axis aligned and non-axis aligned reflection which would solve the problems you are having.
Member
Posts: 184
Joined: 2004.07
Post: #7
A particular reason why it's particularly difficult is due to the fact you are doing ball vs. box, so you have kind of the annoying case where the ball collides with a corner of the box and you have to figure out what to do. It's not that hard if you completely hit the side of the box- if you bounced a ball off a vertical line, you would simply reverse the ball's horizontal velocity.

I find this is easiest to do if you do each axis independently. The car has some x and y velocity. In a timestep, try moving the car its x velocity only and see if it collides. If it collides, reverse the x velocity and displace the car in the x direction so that it is no longer in the wall- you can 'cheat' a little with this just by giving the car its old x position, or you can try to do it more accurately. Then, do the same thing in the y direction. For more inelastic collisions you can, in additon to reversing each velocity component, simply scale it by a fraction, like 50% of the original velocity component.

This gets it right most of the time, but if you collide into a corner particularly fast, you might get weird results. I say, implement this easy way first, then modify it to get it to work the way you want when handling corners.