3.png (u)

**[**Hide**]** (25KB, 707x252) >>96689
>it's a 2D game engine
>yes i'm an enginefag
Ah, that does make a significant difference!
The first thing you need is a way to get the normal of the slope the character is currently on. I mentioned a raycast, but it sounds like you don't have anything like that in the engine yet. Without knowing how you've plated your spaghetti code I can't really give anything more useful than you'd get from a google search when it comes to how to get the normal of a slope and/or build raycast functionality into your engine. But once you do have that normal vector (we'll call it N) we can use some math to get the move direction.
Your normalized horizontal move vector is super simple. If you're moving right it's (1,0) if you're moving left it's (-1,0), if you're not moving it's (0,0). We'll call this vector H, and the x value of H is H.x.
The normal for your slope is perpendicular to two different vectors, one going perfectly up the slope and one going perfectly down the slope. But it'll be better for us to think about it like the two vectors going right and left perfectly along the slope. Whether moving right is going up the slope or down the slope doesn't matter to the math.
So if the player is moving right, to get the vector along the slope in the direction of horizontal player movement we rotate the normal vector N 90 degrees clockwise. If the player is moving left, we rotate N 90 degrees counter clockwise. How do we rotate a 2d vector by 90 degrees? We multiply x by -1 if we're going clockwise, or y by -1 if we're going counter-clockwise, then swap x and y.
Whether we rotate clockwise/counter-clockwise is based on if the player is moving left or right, which is information contained in H.x, so we can use that.
Moving right means H.x is 1. We need to rotate N clockwise, meaning we multiply N.x by -1 and keep N.y the same.
Moving left means H.x is -1. We need to rotate N counter-clockwise, meaning we multiply N.y by -1 and keep N.x the same.
As you can see, to get things rotated in the right direction we always multiply N.y by H.x, and we always multiply N.x by -H.x.
The pseudo-code for this looks something like.
>multiply N.y by H.x
>multiply N.x by -H.x
>swap x and y in N
>the resulting vector is the move direction
Which should translate into a nice two line solution
>moveDirection.y = N.x * -H.x
>moveDirection.x = N.y * H.x
The resulting vector will be the exact direction the player needs to move to stay on the slope without going above or under it. You can then multiply this vector by the movement speed and the time delta and whatever else.
There are edge cases like going between slopes of different angles, but I've spent enough time on this already I'm not going to explain how to deal with those. They're left as an exercise for the reader.