Moving up a slope,
Moving down a slope
Moving up a slope:
If the orange cross is your current position, and you want to move up the slope. Instead of following the light blue line, you are first moved along the dark blue line, then moved up along the green line before ending at the end of the green line. As you can see, the end position (top of green line) is much further away than the intended position (top of light blue line), making you move up slopes faster than flat surfaces. This slope example is extreme, but illustrates the point. In the game environment, slopes would be much less angled but the effect still happens, so as you traverse over the terrain, your speed would fluctuate depending on the surface you are on. We don't want that now, do we? If anything, you'd want the speed to decrease depending on the angle, but I'm just going for a fixed speed.
Moving down a slope:
The root of both these problems lies in the way the character moves. When moving, the direction of movement will always be calculated as if you were always moving along a flat surface. The direction is always flat relative to the players orientation. This means that on an angled surface, the character still tries to move exactly forward, not up or down depending on the surface you are on. To fix this issue, I grab the surface normal of the polygon the player is currently on and combined with the "raw direction" we want to move, figure out a direction that is parallel to the surface. I can then move the player in that direction, add a little gravity and the player now sticks to the surface all nice.
This is the piece of code that ultimately works it out. Not the most intense piece ever, but one of the many little parts that make up the controller and certainly has a big impact on gameplay! This is obviously not the entire implementation of the system, but this is the piece that gives that final direction. Ive made sure that this system doesn't interfere with other aspects of movement. A problem I was getting was that 1 frame after leaving a surface and being in the air, the player still thought you were on a surface so performed the surface checker code, this in turn adds extra gravity to ensure you stay on the ground, but we don't want that part to happen if we're in the air. To overcome this, I do a post movement check, if you are now in the air, and you were on the grounded the previous frame, it removes the extra gravity which then stops the sudden snap and everything is allll goooooood.
safe as fuck
xoxoxox

0 comments:
Post a Comment