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
|
 |
«
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
|
 |
«
Reply #3 - Posted
2012-07-03 12:27:25 » |
|
Compounding errors will make that drift.
|
|
|
|
Riven
|
 |
«
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
|
 |
«
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
|
 |
«
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
|
 |
«
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
|
 |
«
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
|
|
|
|
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
|
 |
«
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.
|
|
|
|
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
|
 |
«
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. 
|
|
|
|
|