Skip to main content
GameDev.net gamedev.net
🔒 Locked

Physics/Collision detection

Started by arasmussen Mar 30, 2011 at 4:22 PM 4 replies 1.9k views
Original Post
arasmussen
arasmussen
Hey,

I have two possible implementations for the collision detection/physics in my game with static terrain, and I'm trying to figure out which one you guys might recommend more.

1. Realistic physics

When the character walks into a wall or falls onto the ground, they actually penetrate the ground, and a force is applied to the character to push him out, just as the real world works. The farther the ground is displaced (not visually), the more the force onto the character which pushes them out of the ground. How would you calculate how deep the character has penetrated, and which direction to apply the outward force?

2. Estimate boundaries

When the character walks into a wall or falls onto the ground, the game checks the bounding box of the character versus all nearby platforms and the heightmap. If it sees that a character has penetrated something, it uses the (assumed to be acceptable) previous coordinates of the character and the coordinates the character would like to travel to, and calculates the precise point in between that is as close to the wall/platform as possible without actually colliding with it.

-- FYI
I'm currently using a heightmap for the absolute lowest position of the terrain, and arbitrary meshes for platforms/structures on top of the terrain. So far, I've implemented #2 with a heightmap, assuming the character is an infinitely small point (no platforms yet). I've also heard some stuff about a number of spheres working better than a bounding box for collision detection. Ultimately I'd be able to implement #1 with spheres for a character (rather than a bounding box), as I've heard these are the best simple solutions for a 3D indie game.

Another small question: if the character is on the slope of a heightmap, I'd like to orient him in a way such that he stands straight up in the direction of the normal vector from the slope he stands on. However, what if he's standing on multiple triangles simultaneously (characters aren't infinitely small, you don't want him to jump from one normal to the next, it should be a transition), should I take a weighted average of the normals of all triangles he's standing over?

Ideas/comments? Thanks!
Buckeye
Buckeye
I think you'll find choice #1 easier to implement, if you want "normal" physics reactions in your game. Choice #2 isn't "physics" - it's just magic and a bit more difficult to implement correctly when an object has multiple simultaneous collisions.

With regard to calculating collision data ( collision points, normals, penetration depth, etc. ), the algorithm depends on what objects are colliding. When I wrote a collision/physics engine I choose several "useful" shapes ( spheres, boxes, planes and rays ). Each combination (sphere-sphere, sphere-box, etc.) has a different algoritm to determine that collision data. I "stole" algorithms unmercifully from several open source engines ( e.g., ODE ).

Using a sphere or multiple spheres for a character may be a good choice in that, in general, sphere collisions are easier and faster to determine.

I'd like to orient him in a way such that he stands straight up in the direction of the normal vector from the slope he stands on.[/quote]
Hmm. I would think you'd want the character to stand straight up, with a constant vertical direction. When you walk on a slope, you don't lean back to remain normal to the slope - you lean forward to remain vertical. It might look peculiar for a character walking up a 45 degree slope to be leaning backwards by 45 degrees.
Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly. You don't forget how to play when you grow old; you grow old when you forget how to play.
arasmussen
arasmussen

I'd like to orient him in a way such that he stands straight up in the direction of the normal vector from the slope he stands on.

Hmm. I would think you'd want the character to stand straight up, with a constant vertical direction. When you walk on a slope, you don't lean back to remain normal to the slope - you lean forward to remain vertical. It might look peculiar for a character walking up a 45 degree slope to be leaning backwards by 45 degrees.
[/quote]

But then on a 45 degree slope, if the character is vertical, won't he either be partially floating in the air, or partially penetrating the ground? Or both? How do you get a perfectly flat bounding box to sit vertically on a 45 degree slope without floating or penetrating the ground?
Bow_vernon
Bow_vernon
Well, make sure non of the 8 vertices of the box penetrates the terrain, right?
arasmussen
arasmussen

Well, make sure non of the 8 vertices of the box penetrates the terrain, right?


Right, but then at a 45 degree slope, the character will be floating in the air. Am I the only one who thinks the box should be tiled to follow the slope?
Bow_vernon
Bow_vernon
I guess a lil bit 'floating' is better than 'too much penetrating' case. About orienting character, if the character is non biped (maybe it has four legs, eg horse) it (IMO) should be oriented like that. Watch mount and blade. The horse's orientation follows terrain shape...

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.