Java-Gaming.org Hi !
 Featured games (90) games approved by the League of Dukes Games in Showcase (753) Games in Android Showcase (228) games submitted by our members Games in WIP (842) games currently in development
 News: Read the Java Gaming Resources, or peek at the official Java tutorials
 Home Help Search Login Register
Pages: [1]
 ignore  |  Print
 Planet orbiting algorithm  (Read 13931 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.
How to do that? Please help.

Greetings,
Sonic96PL
Riven
Administrator

« JGO Overlord »

Medals: 1336
Projects: 4
Exp: 16 years

Hand over your head.

 « 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!
 Games published by our own members! Check 'em out!
Roquen

JGO Kernel

Medals: 517

 « Reply #3 - Posted 2012-07-03 12:27:25 »

Compounding errors will make that drift.
Riven
Administrator

« JGO Overlord »

Medals: 1336
Projects: 4
Exp: 16 years

Hand over your head.

 « 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 »

Thanks guys for your replies!

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
Administrator

« JGO Overlord »

Medals: 1336
Projects: 4
Exp: 16 years

Hand over your head.

 « 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
 Games published by our own members! Check 'em out!
Riven
Administrator

« JGO Overlord »

Medals: 1336
Projects: 4
Exp: 16 years

Hand over your head.

 « 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

JGO Kernel

Medals: 517

 « 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
Administrator

« JGO Overlord »

Medals: 1336
Projects: 4
Exp: 16 years

Hand over your head.

 « 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).

Buy a book. Read it.

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

JGO Kernel

Medals: 517

 « 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

 ivj94 (583 views) 2018-03-24 14:47:39 ivj94 (47 views) 2018-03-24 14:46:31 ivj94 (374 views) 2018-03-24 14:43:53 Solater (61 views) 2018-03-17 05:04:08 nelsongames (108 views) 2018-03-05 17:56:34 Gornova (150 views) 2018-03-02 22:15:33 buddyBro (693 views) 2018-02-28 16:59:18 buddyBro (91 views) 2018-02-28 16:45:17 xxMrPHDxx (493 views) 2017-12-31 17:17:51 xxMrPHDxx (731 views) 2017-12-31 17:15:51
 Java Gaming Resourcesby philfrei2017-12-05 19:38:37Java Gaming Resourcesby philfrei2017-12-05 19:37:39Java Gaming Resourcesby philfrei2017-12-05 19:36:10Java Gaming Resourcesby philfrei2017-12-05 19:33:10List of Learning Resourcesby elect2017-03-13 14:05:44List of Learning Resourcesby elect2017-03-13 14:04:45SF/X Librariesby philfrei2017-03-02 08:45:19SF/X Librariesby philfrei2017-03-02 08:44:05
 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