Updates‎ > ‎

Detecting the Player's Groundtype

posted Jun 2, 2011, 10:30 PM by T. J. Chapman
So, I have a variable within my engine that is called 'groundType.' It is an integer value that holds the value of the collisionType of the block that the player is currently standing on. This variable is what will be used to determine what animation or sprite to display when the character is standing on certain slopes-- the character will stand sure-footed on a flat block (square, or rectangle...), but will display a different graphic for when he is standing on a sloped platform.

Initially, one would think that this problem would be easy as pie to figure out-- Couldn't you just pick the obstacle that is right underneath the player's feet, at the middle of player's width? This is not necessarily the case. What if a player is standing under half of the player's width ontop of a block, with the middle of the player dangling over thin air? What then?

The answer: multiple checks. There are multiple checks that must be done, in order to correctly figure out exactly which block (if any) he is standing on. The following graphic can be found in our Dropbox folder-- it goes over the possible collision spots for proper detection of the player's groundType:

(Click the image to view full resolution)

Well, most commonly, the player generally isn't going to be dangling precariously off of a ledge. So, we first check right underneath the middle point (1) on the graphic. If there is an obstacle underneath-- great! That is the correct groundtype. Even when the player is ontop of multiple obstacles, the one right underneath the middle of the player will be the correct graphic to be displayed.

Next, we check the player's height-width/2 location, (2) on the graphic. This point is checked next. Why not check (4) or (3) next? Well, because, a player CAN partially enter into platforms when standing on the edge of a sloped platform with a square platform directly underneath (the bottom right case in the graphic)-- so, first we eliminate the farthest up point, and work our way down to point (4).

After checking the center, and then the left, the right three points are checked similarly to the left side.

If no groundType has been detected, the groundType is set to 1 (unless the player is jumping or falling, in which case it is set to -1), which is flat ground. This is perfect for what is needed, as occasionally, the player can walk flatly along the top of a lone sloped platform, as in the rightmost middle case in the graphic.

I had been wrestling with this issue for awhile when originally programming this engine in Allegro, but just a few hours ago this solution came to me quite painlessly-- everything worked nearly immediately, with little headache... There may be a better way to detect which platform the player is standing on, but I can't think of any. This solution is quite simple to implement, and simple is good.

Anyhow-- mainly been fixing bugs today, and I suppose I have two more hours until I should be heading off to bed-- so more bugs are to be eliminated shortly.

Anyhow, later!
- Falcon Five