• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Windows Rotate an Object around its Centre

A

Angelos

Guest
I am trying to rotate the following object around its own centre.
I saw there is a way to do it on every frame, but I want to make the player change its rotation with the left (anticlockwise) and right (clockwise) direction keys.
Any advice please? :)
Thanks in advance!
QUESTION.jpg
 

jo-thijs

Member
I am trying to rotate the following object around its own centre.
I saw there is a way to do it on every frame, but I want to make the player change its rotation with the left (anticlockwise) and right (clockwise) direction keys.
Any advice please? :)
Thanks in advance!
View attachment 22411
Hi and welcome to the GMC!

Objects in GameMaker have a built-in variable "image_angle", which automatically rotates the sprite of the instance around the origin of the sprite when drawn.

In GML, to rotate an instance 5 degrees clockwise when holding the right arrow key
and 5 degrees counterclockwise when holding the left arrow key, you can put the following in the step event of the rotating object:
Code:
var input = keyboard_check(vk_right) - keyboard_check(vk_left);

image_angle -= input * 5;
 
if you want to rotate an object around a point, you can do something like this:

// oSun CREATE EVENT
rotation_speed = 5;
angle = 0;
distance_from_centre = 64;
centre_x = room_width/2;
centre_y = room_height/2;

/// oSun STEP EVENT

var input = (keyboard_check(vk_right) - keyboard_check(vk_left));

angle += (input * rotation_speed);

x = centre_x + lengthdir_x(distance_from_centre, angle);
y = centre_y + lengthdir_y(distance_from_centre, angle);


if you have multiple planets you can have a oPlanetParent object with the code written above.
Then make oSun and oEarth and whatever planet you have inherit from the oPlanetParent object.
 
Top