2
\$\begingroup\$

I have a 2d platformer game where my slope can be set at any angle, so I've setup a middle raycast that points to the ground onto the player to handle the collision. This works perfectly fine on a flat platform, but when I rotate it to a slope the player is offset increasingly more.

When the player falls onto a flat platform Player falls onto the platform with no rotation (works fine).

enter image description here The player falls onto a platform at an angle with no rotation on the player.

enter image description here Then when I rotate the player to the angle of the platform.

As you can see the player is offset wrong. Do I have to rotate the rays attached to the player and then push him out of the platform? Or is this overkill and I'm doing something wrong?

\$\endgroup\$

1 Answer 1

3
\$\begingroup\$

Do I have to rotate the rays attached to the player and then push him out of the platform? Or is this overkill and I'm doing something wrong?

This is not overkill, and is in fact the correct technique. It is not too difficult: Imagine this problem from the perspective of the platform... The "up" direction will be the vector <0, 1> rotated by the same amount that your platform is rotated. The code for rotating a vector <x, y> (counterclockwise) by an angle a is:

<x * cos(a) - y * sin(a), x * sin(a) + y * cos(a)>
\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .