Gravity And Collision Question Question submitted by Chris Wallace (19 July 2000) | Return to The Archives

| My question is concerning how to implement gravity with collision detection. Probably the best example to explain what I'm trying to do is Starseige Tribes. The collision detection (ray-triangle intersection) route can determine if a player walks up a hill however at the top of the hill there is nothing to intersect with and thus the engine does not know that the player needs to move down the hill. I thought about sending two rays through the collision detection route but this would mean twice the number of intersection tests that need to be preformed. Also the player slides down when the terrain is on a slight incline even if they are not moving. I don't think that it would look really realistic if you couldn't stand on a small hill. Then, I thought that if I combined the rays that I could just send one through. However, how I have it setup is that the engine only checks for intersections when the player is moving. So, if a player is standing over a well then they will not fall if they do not move. In addition, players are unable to walk up the same hills that they were able to before, thanks to the collision response route. My question is how do you perform an efficient ray-triangle intersection collison detection route with gravity?
This question was originally submitted some time ago, to the Fountain Of Knowledge on flipCode.

| For collisions with the ground, performance on the ray-triangle intersection routine doesn't really matter, since you're only doing a few each frame. Even if you did 100 per frame, it wouldn't matter to your frame rate. Adding gravity to your collisions is very simple. If this is the case, all you need to do is add a "gravity vector" to your "inertia vector" (the "inertia vector" is the vector that points in the direction you are moving, with a length of the distance you are trying to cover.) A gravity vector would usually point directly down. The longer this vector is, the stronger your gravity is.
Before you do any collisions, add the gravity vector to your inertia vector and use that vector for collisions. If you've got sliding along polygons working, you'll end up sliding down hill when you're not moving because your inertia vector [0,0,0] added to your gravity vector (for example) [0,-1,0] will result in a non-zero vector [0,-1,0] which will still move you. Your sliding will stop when you reach a surface perpendicular to your gravity vector.
Response provided by Paul Nettle

This article was originally an entry in flipCode's Ask Midnight, a Question and Answer column with Paul Nettle that's no longer active.

Copyright 1999-2008 (C) FLIPCODE.COM and/or the original content author(s). All rights reserved.
Please read our Terms, Conditions, and Privacy information.