#Sidescrolling Plaformer - Sprite rotation based on angle of slope?

2 messages · Page 1 of 1 (latest)

dim jasper
#

Game type: side scrolling platformer.
Character: 4-legged dog.

Hello. I was wondering if anyone has experience on rotating a player's sprite if the player is on a slope? Without rotating the bounding box, just the sprite.

The two ideas I've come up with are:
#1) Raycasting: Cast multiple rays a set distance (left/right cast: bottom, middle, top, casting the direction I face) (down cast: bottom left, bottom center, bottom right, casting down) and calculating the angle of the slope I'm on. The thinking here is by having three rays, if I crest a slope, making the top ray not hit anything, then I want to be able to remove that from the angle calculation so I still maintain the slope until I am fully off of the slope.

#2) Slope Object Collision Detection: I could hardcode the player to detect what slope object I am colliding with. Pairing this with a direction facing variable, I could hardcode the rotations. I think this would have issues of shoving my sprite into the ground until my bounding box fully stopped making contact with the slope object.

Am I on the right track? Or does anyone know of an even better way?

This is how I imagine the raycasting idea would work. If the top one no longer collided with a slope object, it would be removed from the angle calculation. The green line represents the calculated slope angle, which would rotate my sprite by the same amount. Note: I kept the dog's sprite further to the side to show the imaginary lines more clearer:

cursive kestrel
#

I haven't worked with slopes or side scrollers, so idk how you're checking for collision for your object to walk up the slope, but I'd imagine you'd be able to use that check in some way to do a point_distance from the right side of your bounding box and another check from your left side of your bounding box going down to the slope. Then you could get the angle between those two points with point_direction, store that in a variable, then use sprite_draw_ext in your draw event, manually drawing your sprite, which has a rotation argument you can plug that variable into