# Change sprite angle on sloping ground

I have a problem here with GameMaker built-in physics environment.My player is designed to be on a skateboard. I want my player to rotate its sprite, according to the kind of fixture(up or down slopes) he is moving on.I've tried everything i could and everything i could find, but it didn't help me much, because the object is rotating in a very strange way.I enabled some flags to see the mask. Also, I've been studying vector algebra and i know it the angle between vector x speed and vector y speed should be the direction. Please help. Here's what i have so far:

1 object which is the player
Sprite: sprite7
Solid: false
Visible: true
Depth: 0
Persistent: false
Parent: obj_Static_Parent
Children:
Physics
Start Awake: true
Is Kinematic: false
Is Sensor: false
Density: 0.5
Restitution: 0.1
Group: 0
Linear Damping: 0
Angular Damping: 0
Friction: 0.2
Shape: Polygon
Points:
(76, 52) (76, 60) (0, 56) (0, 52) Create Event: execute code:

///Initialize the tires
var half = sprite_width / 2;
var halfy = sprite_height / 2;
var tire = instance_create(x-half+12,y+halfy,obj_Wheel);
physics_joint_revolute_create(id,tire,tire.x,tire.y,0,0,0,0,0,0,false);
var tire = instance_create(x+half+12,y+halfy,obj_Wheel);
physics_joint_revolute_create(id,tire,tire.x,tire.y,0,0,0,0,0,0,false);


Step Event: execute code:

//Code
facing = -point_direction(0, 0, phy_speed_x, phy_speed_y);
phy_fixed_rotation = true;
phy_rotation = facing;

//Move him
if(keyboard_check(vk_space)) {
physics_apply_impulse(x,y,0,-100);
}
if mouse_check_button(mb_left)
{
phy_position_x = mouse_x;
phy_position_y = mouse_y;
}


Information about object: obj_Wheel Sprite: spr_Wheel
Solid: false
Visible: true
Depth: 0
Persistent: false
Parent: obj_Static_Parent
Children:
Physics
Start Awake: true
Is Kinematic: false
Is Sensor: false
Density: 0.05
Restitution: 0.1
Group: 0
Linear Damping: 0.1
Angular Damping: 0.1
Friction: 30
Shape: Circle
Points: (8, 8) (8, 8) Create Event: execute code:
Keyboard Event for Key:
execute code:
physics_apply_torque(-100);
Keyboard Event for Key:
execute code:
physics_apply_torque(100);

And here is the result:
https://i.stack.imgur.com/sn1qh.gif

I'm currently at work so I can't get my example of a game I did with similar physics in GameMaker (Awesome you're using it bte) but I made the object cast a ray down from the center of the player's object, then I set the sprite to rotate based on the rotation of the ray cast to what it was touching If that made any sense this can be smooth, but also may not be.

way2:

You could always consider using a method that rotates based on the object it touches, for example your game will most likely have pre-made ramps, etc. so have it when the player touches one ramp, they will rotate to a certain degree Good cheat until you have a quarter pipe

way3:

image_angle = <Some sort of formula for your object, maybe object_angle>;


The above needs some sort of code that will let it get the angle of an object based on its sprite

way4:

Make it quirky as hell and have the player have to move the player, otherwise they will just manual down the ramp using

image_angle = point_direction(x, y, mouse_x, mouse_y);


This way isn't a fix, but would look funny right?

But yeah, these are a few ways of the top of my head I can think of to make it work. without my projects in front of me it would take more time than its worth for me to remember how I did it. Hope it helps.

• I dont know if what i have said will help at all, but in a few hours ill post my solution i came up with on my project when i get home. Aug 8, 2016 at 14:37
• I've tried to implement some algebra with vectors. It's quite good and it determines the angle of the slope easily. However, this can be done with Pythagorean Formula as well. If the sprite_width and height are the same , then its a 45 * ramp.
– Mark
Aug 17, 2016 at 20:48
• The 2nd Method is what i implemented cause at the moment i don't have any experience with GameMaker but i will try my best. Way 3 is not the best solution cause image angle spins the mask too, making the player sometimes get stuck in objects or terrain. Way 4 : I'm not going to use the mouse in the game.
– Mark
Aug 17, 2016 at 20:50
• yeah way 4 would be like the worst solution lol Aug 18, 2016 at 8:03

Depending on your movement logic, you could approximate the slope angle depending on the relative direction your character is moving towards:

var _slope = 0;
if (isGrounded)
{
_slope = point_direction(xprevious, yprevious, x, y);
}
return _slope;


Here, isGrounded is an instance variable that detects whenever your character is walking on solid ground rather than, say, jumping.

The above function doesn't account for the sprite scaling (used to implement left/right facing), but you can fix this by either tracking the intended facing side via a dedicated variable, or forcing the character to face their walking direction.