Download

2D Planet Orbit

I’ve been searching around for a way of how to make an object orbit around a planet but all I found were 3D ones. I need to do it for my 2D game and whenever I tried to adapt to work for my 2D game it would mess up in a way. I can’t figure out how to do on my own as I’m still slightly new to Blueprints. This a core mechanic of my game and if anyone knows an answer on how to do it. It would be much appreciated

If you just need the object to circle around without any physical correctness use sine and cosine:



x = sin(t)*radius + center.location.x;
y = cos(t)*radius + center.location.y;


where t is a continous time variable in the range of [0,2PI]

If you want to take velocity etc into account,

add the difference vector between your object and the oribtting object to the orbitting object’s velocty;



Object a; //Orbitting Object
Object b;

a.velocity += b.location - a.location;


if you need 2d, just remove a dimension.

Depending on your setup and requirements, you might be able to get away with it by rotating an image/object around a pivot point since your game is 2D…

I tried what you said, only the first piece of code for now, and I only get an error.

I am doing this in the planet code but I’m not too sure that I’ve done it correctly.

I want multiple planets in my level so I don’t think that that will work.

Read the Message.
It says Accessed None. Which means your Object does not exist.

But it does.

Sorry to say that, but it doesn’t. It tells you the Variable you use is empty.

Reference variable (the blue ones) are empty if you don’t put a reference into them.

You can get the Player with “GetPlayerCharacter” as long as your PlayerController has it possessed.

Also, in the future, use “IsValid” nodes on your Reference variables to make sure they are filled.

In addition, if you use “EventTick”, make use of the DeltaSeconds.

They are the difference between two ticks and if your game lags (or runs on very high fps), the Tick event
will be called less or more often. The DeltaSeconds compensate that.

So if you let something rotate and don’t use the DeltaSeconds (multiply them to the time or what ever you use),
and someone has a bad PC, the thing won’t rotate as fast as normal.
Other way round, a person with a good PC (200+ fps for example) will have a really fast spinning object.

The object works now but he isn’t orbiting around the planet, he is just jittering.

Ok so, did you think about using a “Spring Arm” component? It is the thing you normally use to offset the Camera on Third Person Characters.

You would set the location of it in the center of your Actor and put (what ever you want to rotate) as a child of it.

Then you would simply Add Rotation to it on tick.

This red line here is a spring arm. The Character would be what ever you have as the center and the Camera would be the thing you want to rotate:

https://docs.unrealengine.com/latest/images/Engine/Components/Camera/camera_springArm.jpg

Here is information on the SpringArm Component:

And I’m sure you will find the answer to “how to make it rotate”. It really just calling “Add Rotation” on it.

Or you get the current RELATIVE Rotation of it and then SET the RELATIVE Rotation of it to the same + X * DeltaSeconds.

Where X is the number of degree it should turn per second.

You would add this only to Pitch or Roll, depending on how you orientate your 2D.

I’ve tried rotating the spring arm but I get accessed none. I am using the ‘Is Valid’ node, its valid so then I get it to print a string. It won’t however rotate the spring arm because it doesn’t exist. I’m just confused and a bit lost to be honest. This is what I tried:

PlayerRef is the player character but because i couldn’t make a node off that ‘Get spring arm’ I thought if I made another variable with my player as the type it’d work, which it did in a way. I can get the spring arm but it just doesn’t exist.

Okay, one step after another.

WHERE is that being called? Where are you in that Screenshot?
WHAT is the center of your Rotation?
WHAT do you want to rotate around it?

I can create you a dummy blueprint so you can see how it’s done.
But you need to answer me these questions.

So the blueprint is being called in the actually planet itself. The center of my rotation is the center of my character as seen here - Screenshot - 8fc4dcfbfd716a116f10e315200a51a4 - Gyazo. And I want it to rotate around the center of my planet.

So in basic words, you want to let the Character rotate around the Planet?

Yep, also later on once it actually works i want to be able to have a collision round the planet that will trigger the rotation so I have multiple planets.

So if I got you correctly, this is how you can rotate the Player around a Planet (constantly!):

I made a new Blueprint for the Planet. I added a Sphere Mesh to it (I don’t have 2D Assets but the idea is the exact same)
and a Spring Arm. The Spring arm is in the middle of the Sphere and the length is at 300.
Make sure to disable the collision check on the Spring Arm, so it doesn’t get shortend by the actual Planet Mesh or something.

The Scale in this image is 2.0 because I scaled down the sphere to 0.5 and it tries to compensate the SpringArm size (it’s a child of the Mesh)
by multiplying it with 2 again. So don’t get confused.

http://puu.sh/sFoTy/347b4ddba9.jpg

Now I do this in the EventGraph of the Planet:

I get me the PlayerCharacter, attach it to the SpringArm (check the Settings on the Attach node!).
And in the EventTick, I add an offset of 50 to the Pitch.

If it’s pitch or roll is up to you and how your setup the spring arm. By default, it’s facing into -X, so the rotation
should be around the Y Axis, which is Pitch. You can see that in the first image. We rotate along the green arch:

http://puu.sh/sFoYu/d72c109dcd.png

This rotates the Player around the Planet. Though I saw that rotating the Player (at least my first person one) is pretty buggy.
I spawned an Actor that had a small Sphere in it (called BP_Moon) and used that for the Attach node and that looked quite ok (: