Planet orbiting algorithm

I’m new to JGO, so hello :slight_smile:

I’m developing 2D space game in Java. I’m doing well except one major thing: I have no idea how to make planet orbit.

My Planet have a float x, float y, and float called distanceFromSun which is our radius. I have update() method (run every frame).
Orbit must be perfect circle.
How to do that? Please help.

Greetings,
Sonic96PL

sin and cos would do just fine.

It is funny that I just figured out all the math for a game I am working on!

to make a planet orbit:

variables:
radius r: where r is the distance from the sun
position x & y: where x & y represent the position of the sun
angle a: a is the angle of rotation

a += velocity * delta;
planetX = x + r * cos(a);
planetY = y + r * sin(a);

To make a planet orbit something that is moving simply add in the x and y positions

a += velocity * delta;
planetX = sun.getx + r * cos(a);
planetY = sun.gety + r * sin(a);

simple as that!

Compounding errors will make that drift.

Well, drift within the orbit, which doesn’t violate the demand for ‘perfect circles’.

On a serious note: calculate the angle from the current (game) time, multiplied by a velocity, don’t use ‘delta’ as it will indeed introduce cumulative errors.

Thanks guys for your replies!

What is delta? It is a time in milis between frames?
Sorry I’m new to Java Game Dev :slight_smile:

You can use sin or cos, by increasing theta (polar). You can also use the coordinate (cartesian) if your orbit is circle, remember xx + yy = r*r.

Thank you for your help but…
I still don’t know how to do that. Maybe I’m doing something wrong?
Any one else can help?

I’m new to trigonometric functions…

Greetings,
Sonic96PL

[quote=“Sonic96PL,post:8,topic:39211”]
You’re bound to run into situations where you need at least trigonometric functions to be able to code anything in (simulation) games. Maybe you need to buy a textbook covering highschool level math, or else you’re going to be stuck with these kinds of questions all the time.

It’s not simulation, and I will be in highschool in three years (hopefully).
I’m doing it because I want to learn something, and i thought that you will help me.
I’m not asking for solution for my problem, just help.

Greetings,
Sonic96PL

My honest attempt to help you was to suggest you’d use the sine and cosine functions in Java. When you said you weren’t aware of their existence, or how they worked, I thought it was best to tell you how you could learn more about them.

But that’s just my $0.02 - if there are people that are able to answer your question without you having to learn basic trigonometry, I’d be surprised.

@Riven - I was talking about time & angular drift…but I’ve obviously come to the wrong place. Everyone ignore me.

I said that I want to learn it. I appreciate your help, and I want to know more.
I was wondering what is velocity and delta form zngga’s post.

I don’t want to piss off anybody :slight_smile:

I basically said that in my post. I jokingly said that this (angular) drift would not affect his ability to rotate objects in perfect circles. For added clearity, I added ‘on a serious note’, which is an indication that the previous remark was not serious, after which I explained to him how he could get rid of this angular drift, by using different parameters (absolute time, instead of relative time), so errors cannot accumulate. I thought that was clear, but obviously I’ve come to the wrong place. Everybody ignore me.

So much anger between fellow java game devs -_-

Sonic, you aren’t going to be able to do much without at least a basic understanding of higher-level math, including advanced algebra and trigonometry. Buy a book. Read it.

| Nathan

I have almost do that. But it’s moving only in vertical line.
Keep working.

EDIT: Not really, X coord is almost the same (difference of 15f).

That’s a good idea. I’ll think about it later.

EDIT2: I have do that. It was easy. I should have use Wikipedia first.

So you should be surprised, because I’m that person.

Velocity allows you to set a speed for the orbit, it isn’t entirely necessarily. Delta is from a game loop development style called “Delta Timing”. Essentially it represents the time between updates. And as Roquen said, it will lead to compounding errors… meaning that the delta time is never perfectly accurate and after larger sums of time, that imperfection will add up to a noticeable error.

Just KISS. :point:


planet.orbitRotation = planet.orbitRotation + planet.orbitSpeed;
planet.x = Math.cos( planet.orbitRotation ) * planet.distanceFromStar + star.x;
planet.y = Math.sin( planet.orbitRotation) * planet.distanceFromStar + star.y;

You might want to make sure your rotation doesn’t go above 2 pi ( 360º ) with a modulus but I figure just KISS as much as possible.

@Riven - OP doesn’t understand basic sinusoid functions, so talking about the compounding of errors coupled with summing up a sampled function in this thread (place) doesn’t make sense. So ignore my comment as it was out-of-place.

@Eli - edit


planet.x = Math.cos( planet.orbitRotation ) * planet.distanceFromStar + star.x;

In my case:


SolarSystem s = Main.getGame().getCurrentSolarSystem();

planet.orbitRotation = planet.orbitRotation + planet.orbitSpeed;
planet.x = Math.cos( planet.orbitRotation ) * (planet.distanceFromStar * s.getZoom()) + star.x;
planet.y = Math.sin( planet.orbitRotation) * planet.distanceFromStar + star.y;

Because I had implemented zooming, and x is only value that I’m “zooming” :slight_smile:

Thank you zngga for your post, it explains everything.

Greetings,
Sonic96PL