Rotating an object's x and y axis? (Or other ways to make npcs walk around a planet)

Faexie

Member
In my game, I'd like to make the characters move around a circle (a planet, for example), with the center of gravity in the middle of the planet. Kind of like if mario galaxy was a 2d sidescroller. I already know how to make them fall towards that center and to adjust their angle accordingly, but I'm not to sure on how to make them move towards their left and right.

I was wondering if it was possible to rotate an object's x and y axis along with it's image (or let gamemaker know that I want the character to move to the right relative to what the right of the image is), as this might be the simplest way to achieve it if it's doable.

I am open to other ways of doing things though

chamaeleon

Member
Instances does not have axes, they just have a position. You need to do the math.

Faexie

Member
That's what I thought... would have been simpler if they did, but turns out it's actually not all that complicated if you know math well (I'm very rusty on it though so I followed a tutorial ^^')

So far it's been going well, the only issue I have now is that the player moves faster "horizontally" the further away it is from the point of gravity. Working on figuring that out now.

Here's the code so far:

Create event:
GML:
``````angle = 1
distance = 50 // equivalent to y axis
orbit = 2 // 2 means circular, do not change
centerX = Oplanet.x
centerY = Oplanet.y``````
Step event:
Code:
``````#region sets controls
right = keyboard_check(vk_right)
left = keyboard_check(vk_left)
up = keyboard_check(vk_up)
down = keyboard_check(vk_down)
#endregion
#region move left and right
hdir = right - left // direction
sp = 1 // speed

angle += sp * hdir; // equivalent to x axis, change the number to change speed, change the sign to change direction
newX = lengthdir_x(distance,angle);
newY = lengthdir_y(distance,angle) * (1 - orbit);
x = centerX + newX - newY;
y = centerY + newY + newX;
#endregion
#region move up and down
if (up = true) and (down = false)
{
distance += 1
}
if (up = false) and (down = true)
{
distance -= 1
}
#endregion``````

TheouAegis

Member
If you wanted to get creative, you could treat the surface of the planet as a level surface traversing along a single axis rather than a circle, simply telling the instance how far along the planetary x-axis it has moved, and then convert that to degrees or radians relative to the circumference of the planet, then calculate the angle based off that.

Last edited:

TheouAegis

Member
In my game, I'd like to make the characters move around a circle (a planet, for example), with the center of gravity in the middle of the planet. Kind of like if mario galaxy was a 2d sidescroller. I already know how to make them fall towards that center and to adjust their angle accordingly, but I'm not to sure on how to make them move towards their left and right.

I was wondering if it was possible to rotate an object's x and y axis along with it's image (or let gamemaker know that I want the character to move to the right relative to what the right of the image is), as this might be the simplest way to achieve it if it's doable.

I am open to other ways of doing things though
I wrote up a quick blog post about what I suggested in my previous post. Wrapping your brain around working in two different perspectives while coding for one is the only real difficult part. But otherwise, it's actually very simple to implement.