Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (580)
games submitted by our members
Games in WIP (499)
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 3438 times)
0 Members and 1 Guest are viewing this topic.
Offline Sonic96PL

Senior Newbie





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

I'm new to JGO, so hello Smiley

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

JGO Overlord


Medals: 605
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #1 - Posted 2012-07-03 12: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
Offline zngga
« Reply #2 - Posted 2012-07-03 14: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!
Legends of Yore - The Casual Retro Roguelike
Offline Roquen
« Reply #3 - Posted 2012-07-03 14:27:25 »

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

JGO Overlord


Medals: 605
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #4 - Posted 2012-07-03 14: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
Offline Sonic96PL

Senior Newbie





« Reply #5 - Posted 2012-07-03 15: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 Smiley
Offline ReBirth
« Reply #6 - Posted 2012-07-03 16: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.

Offline Sonic96PL

Senior Newbie





« Reply #7 - Posted 2012-07-03 20: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
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 605
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #8 - Posted 2012-07-03 21: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
Offline Sonic96PL

Senior Newbie





« Reply #9 - Posted 2012-07-03 21: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!
Legends of Yore - The Casual Retro Roguelike
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 605
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #10 - Posted 2012-07-03 21: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
Offline Roquen
« Reply #11 - Posted 2012-07-03 21:43:23 »

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

Senior Newbie





« Reply #12 - Posted 2012-07-03 21: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 Smiley
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 605
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #13 - Posted 2012-07-03 22: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
Offline StonePickaxes

JGO Coder


Medals: 4
Projects: 2


Nathan Kramber


« Reply #14 - Posted 2012-07-03 22: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!
Offline Sonic96PL

Senior Newbie





« Reply #15 - Posted 2012-07-03 22: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.
Offline zngga
« Reply #16 - Posted 2012-07-03 23: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 Smiley

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!
Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 12


Game Engineer


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

Just KISS.  Pointing

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
Offline Roquen
« Reply #18 - Posted 2012-07-04 07: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;
Offline Sonic96PL

Senior Newbie





« Reply #19 - Posted 2012-07-04 09: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" Smiley

Thank you zngga for your post, it explains everything.

Greetings,
Sonic96PL
Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 12


Game Engineer


« Reply #20 - Posted 2012-07-04 15: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. Smiley

See my work:
OTC Software
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

xsi3rr4x (44 views)
2014-04-15 18:08:23

BurntPizza (40 views)
2014-04-15 03:46:01

UprightPath (56 views)
2014-04-14 17:39:50

UprightPath (38 views)
2014-04-14 17:35:47

Porlus (54 views)
2014-04-14 15:48:38

tom_mai78101 (77 views)
2014-04-10 04:04:31

BurntPizza (136 views)
2014-04-08 23:06:04

tom_mai78101 (236 views)
2014-04-05 13:34:39

trollwarrior1 (197 views)
2014-04-04 12:06:45

CJLetsGame (205 views)
2014-04-01 02:16:10
List of Learning Resources
by SHC
2014-04-18 03:17:39

List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30
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
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!