Game Development Community

TX2D detecting if an actor is "on the ground"

by Jim Slemin · in Torque X 2D · 07/13/2009 (10:57 pm) · 4 replies

I am new to this game engine and looking to make a simple 2d platformer. Yes I know there is a development kit, but I would prefer to learn as I make one from the base 2D game.

Anyways, I have made a singleton component which adds gravity to all my physics objects, and I set collisions to "clamp" between my actor and the ground (the only collision type that didn't result in problems).

Now I want to add a "Jump" feature. However, this requires me to know if I am on ground (because I don't want to be able to jump while in the air). So, I am looking for a good way to detect if I am on ground (i.e. if the actor is on a platform). What I have thought of thus far is to have an 'onGround' boolean that is set to false if the jump button is pressed and true on collision with the ground. I foresee problems with this, however, if for some reason I press the jump button but it doesn't result in a collide event (say I have no room to jump).

Any suggestions on how I might be able to tell at any time if my player is "on the ground"? Also any comments on my method for gravity would always be welcome.

Thanks in advance

-Jim

About the author

Recent Threads


#1
07/13/2009 (11:26 pm)
Most likely found the solution myself (of course it happens after I post) with the testmove method in the collision class. I test a collision after a very small downward (and only downward) movement and if its collides with a platform then I know I am resting on it.
#2
07/14/2009 (10:05 am)
just check to see what your vertical velocity (positive or negative) is.
if your present vertical position is different from your last one on the last frame, then you are either already jumping or falling.
if your present vertical position is the same as the frame before.... chances are, ur not jumping or falling.
#3
07/16/2009 (6:34 am)
I wouldn't want to rely on "chances are...". Jim's idea works much better in the torque engine. In particular, it also allows things like platforms that disappear from under the player - the player would be detected as not on platform and fall.
#4
07/16/2009 (10:29 am)
I see a little problem with just checking relative height in that you could be going up or down without jumping or falling if you are walking along an incline, in which case you should still be able to jump.

You could try having a T2DTriggerComponent on the ground or player that sets the OnGround flag in the OnEnter callback and clears it in the OnLeave callback.

I haven't really worked much with trigger comps yet, but that might be a way to go.

P.S. I see this is your first post on these threads, Jim--welcome aboard!