Game Development Community

Detecting if Player/Object is on ground or not

by Alex Stittle · in Torque X 3D · 01/16/2009 (12:52 pm) · 3 replies

Hi all,

I'm trying to disable player controls based on their current status. I'd like to have the controls enabled if they are standing on something, and disabled otherwise.

So for example, running around on the ground is ok, jumping is not ok, landing on a box is ok. (Ok = user controls enabled).

My problem is, I don't know how to tell when a user is on an object, and when they're not on an object.

How can I tell when a user is running on land, and then as soon as they run off a cliff (free fall) disable their controls?

I've tried measuring z-velocity, but if they are running down a hill then this is no good. I also tried using OnCollision and dotting the normal, and in certain conditions set the player status to OnGround. But then I don't know how to tell when the player left the ground (runs off a cliff).

Does anyone have any suggestions?

#1
01/17/2009 (1:08 pm)
Alright, well I've come up with a system that seems to work. It covers the case of running off a cliff, the ground disappearing below, jumping on blocks and so on.

For those interested, I created an OnCollision method for my player object, checked the angle of the normal to see if it was appropriate (too big of an angle I considered to be too steep of a slope) and then if the angle condition passed, I set the a variable to say the player was on ground.

Inside the update input method, I checked to see if the on ground variable was true, and if so allowed the player to control. At the end of the update input method, I set the on ground variable to false. That way, we always assume the player is not on the ground and get told otherwise by the onCollision.

I'm sure this can be optimized in some way, but until it's necessary this seems to work just fine :)
#2
01/18/2009 (8:16 pm)
Well done Alex! Sorry I was to late to get back to you. I've gone a different route and use the RigidCollisionManager to cast a ray from the player downwards and return the distance to the terrain (or other structure). I'll find the code and paste it in, but it ended up in the latest Torque X 3.0 3D Builder to drop 3D objects to the ground.

John K.
www.envygames.com
#3
01/19/2009 (8:00 am)
Thanks John! It's pretty cool to hear from the author of the book that taught me how to use Torque. Good book by the way, I like your style.

It's also nice to see the way other people have done this kind of thing. You've just opened up some ideas for me with your technique. I can imagine a player falling and as the distance shrinks between the player and some spikes (or other, death inducing object) he could blurt out "Oh mama..." or something right before he dies. With your technique, all that info is good to go.