Java-Gaming.org Hi !
 Featured games (83) games approved by the League of Dukes Games in Showcase (582) Games in Android Showcase (163) games submitted by our members Games in WIP (633) games currently in development
 News: Read the Java Gaming Resources, or peek at the official Java tutorials
Pages: [1]
 ignore  |  Print
 Planet orbiting algorithm  (Read 5320 times) 0 Members and 1 Guest are viewing this topic.
Sonic96PL

Senior Newbie

 « Posted 2012-07-03 10:24:59 »

I'm new to JGO, so hello

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.

Greetings,
Sonic96PL
Riven
« League of Dukes »

« JGO Overlord »

Medals: 979
Projects: 4
Exp: 16 years

 « Reply #1 - Posted 2012-07-03 10:28:44 »

sin and cos would do just fine.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings!
zngga
 « Reply #2 - Posted 2012-07-03 12:21:15 »

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!

My code never has bugs... it just develops unexpected features!
Roquen
 « Reply #3 - Posted 2012-07-03 12:27:25 »

Compounding errors will make that drift.
Riven
« League of Dukes »

« JGO Overlord »

Medals: 979
Projects: 4
Exp: 16 years

 « Reply #4 - Posted 2012-07-03 12:30:12 »

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.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings!
Sonic96PL

Senior Newbie

 « Reply #5 - Posted 2012-07-03 13:07:01 »

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

What is delta? It is a time in milis between frames?
Sorry I'm new to Java Game Dev
ReBirth
 « Reply #6 - Posted 2012-07-03 14:05:07 »

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

Sonic96PL

Senior Newbie

 « Reply #7 - Posted 2012-07-03 18:02:27 »

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
Riven
« League of Dukes »

« JGO Overlord »

Medals: 979
Projects: 4
Exp: 16 years

 « Reply #8 - Posted 2012-07-03 19:20:36 »

I'm new to trigonometric functions...
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.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings!
Sonic96PL

Senior Newbie

 « Reply #9 - Posted 2012-07-03 19:29:33 »

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
Riven
« League of Dukes »

« JGO Overlord »

Medals: 979
Projects: 4
Exp: 16 years

 « Reply #10 - Posted 2012-07-03 19:38:24 »

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.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings!
Roquen
 « Reply #11 - Posted 2012-07-03 19:43:23 »

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

Senior Newbie

 « Reply #12 - Posted 2012-07-03 19:45:44 »

if there are people that are able to answer your question without you having to learn basic trigonometry, I'd be surprised.

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
Riven
« League of Dukes »

« JGO Overlord »

Medals: 979
Projects: 4
Exp: 16 years

 « Reply #13 - Posted 2012-07-03 20:15:07 »

@Riven - I was talking about time & angular drift...but I've obviously come to the wrong place.  Everyone ignore me.
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.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings!
StonePickaxes

JGO Coder

Medals: 5
Projects: 2

Nathan Kramber

 « Reply #14 - Posted 2012-07-03 20:22:01 »

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

Check out my website!
Sonic96PL

Senior Newbie

 « Reply #15 - Posted 2012-07-03 20:40:50 »

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.

if there are people that are able to answer your question without you having to learn basic trigonometry, I'd be surprised.

So you should be surprised, because I'm that person.
zngga
 « Reply #16 - Posted 2012-07-03 21:38:25 »

if there are people that are able to answer your question without you having to learn basic trigonometry, I'd be surprised.

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

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.

My code never has bugs... it just develops unexpected features!
Eli Delventhal

JGO Kernel

Medals: 42
Projects: 11
Exp: 10 years

Game Engineer

 « Reply #17 - Posted 2012-07-04 00:38:03 »

Just KISS.

 1  2  3 `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.

See my work:
OTC Software
Roquen
 « Reply #18 - Posted 2012-07-04 05:38:02 »

@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
 1 `planet.x = Math.cos( planet.orbitRotation ) * planet.distanceFromStar + star.x;`
Sonic96PL

Senior Newbie

 « Reply #19 - Posted 2012-07-04 07:37:53 »

In my case:

 1  2  3  4  5 `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"

Thank you zngga for your post, it explains everything.

Greetings,
Sonic96PL
Eli Delventhal

JGO Kernel

Medals: 42
Projects: 11
Exp: 10 years

Game Engineer

 « Reply #20 - Posted 2012-07-04 13:42:36 »

@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
 1 `planet.x = Math.cos( planet.orbitRotation ) * planet.distanceFromStar + star.x;`

Whoops! Thanks for the correction.

See my work:
OTC Software
Pages: [1]
 ignore  |  Print

You cannot reply to this message, because it is very, very old.

 Ralphanese (18 views) 2015-05-29 13:07:55 Gibbo3771 (15 views) 2015-05-29 06:54:17 Ralphanese (23 views) 2015-05-28 16:52:52 theagentd (31 views) 2015-05-27 22:21:17 theagentd (36 views) 2015-05-27 22:21:08 CopyableCougar4 (25 views) 2015-05-27 19:24:50 MrMapcom (24 views) 2015-05-23 20:26:16 MrMapcom (33 views) 2015-05-23 20:23:34 Waterwolf (37 views) 2015-05-20 15:01:45 chrislo27 (44 views) 2015-05-20 03:42:21
 Riven 25x Rayvolution 24x theagentd 22x BurntPizza 19x Spasi 17x orangepascal 16x Drenius 15x KevinWorkman 14x ra4king 13x DavidBVal 12x princec 12x opiop65 12x EgonOlsen 11x Husk 11x kevglass 9x Slyth2727 8x
 Intersection Methodsby Roquen2015-05-29 08:19:33List of Learning Resources2015-05-05 10:20:32How to: JGO Wikiby Mac702015-02-17 20:56:162D Dynamic Lighting2015-01-01 20:25:42How do I start Java Game Development?by gouessej2014-12-27 19:41:21Resources for WIP gamesby kpars2014-12-18 10:26:14Understanding relations between setOrigin, setScale and setPosition in libGdx2014-10-09 22:35:00Definite guide to supporting multiple device resolutions on Android (2014)2014-10-02 22:36:02
 java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org