Orbit rotation: start rotating from a given position
in
Programming Questions
•
10 months ago
Hi there, I need some input here. I'm having some trouble on calculationg some coords when making orbit rotations. I've made up a simple sketch just to point what I'm trying to do:
I got a planet which can have satellites. Each of them, rotates on its orbit.
Everything ok so far.
In this sketch, you can add satellites dynamically by pressing the mouse, so it calculates the rotation angle and the orbit size based on the planet and mouse coords.
This works, but since the satellite animation coords is based on
millis(), once it starts getting drawn, it starts from a random (well, not really random) point in the orbit.
What I want to achieve is that it starts its path right from the initial position provided by the mouse input, or the closer I can get. Any ideas on how can I handle this?
I'm trying to figure out this to apply it to another app I'm playing with, where I have particles that attract other particles and transform them into their satellites, but I made this sketch to keep it simple.
- import processing.opengl.*;
- Planet planet;
- void setup(){
- size(800, 600, OPENGL);
- planet = new Planet(15, width/2, height/2, 0);
- sphereDetail(4);
- }
- void draw(){
- background(0);
- frame.setTitle(int(frameRate) + " fps");
- planet.display();
- }
- void mousePressed(){
- planet.addSatellite();
- }
- class Planet{
- float size, x, y, z;
- ArrayList<Satellite> satellites;
- Planet(float size, float x, float y, float z){
- this.size = size;
- this.x = x;
- this.y = y;
- this.z = z;
- satellites = new ArrayList<Satellite>();
- }
- void display(){
- pushMatrix();
- translate(x,y,x);
- fill(150); stroke(200);
- sphere(size);
- for (Iterator<Satellite> it = satellites.iterator(); it.hasNext(); ) {
- it.next().display();
- }
- popMatrix();
- }
- void addSatellite(){
- satellites.add(new Satellite(this, mouseX, mouseY, 0, random(2,5)));
- }
- }
- class Satellite{
- float size, x, y, z, orbitDuration, orbitRad, rotz;
- Satellite(Planet parent, float sx, float sy, float sz, float size){
- this.size = size;
- orbitDuration = random(2000,4000); //random orbit cycle duration
- orbitRad = dist(sx, sy, sz, parent.x, parent.y, parent.z)/4.3; //calculate the ortbit radius, based on the distance between planet coords (parent) and mouse coords. (WHY SHOULD I DIVIDE IT?)
- //calculate the angle between the planet coords and mouse coords.
- float dx = sx - parent.x;
- float dy = sy - parent.y;
- float ang = atan2(dy,dx);
- if (ang < 0) ang += TWO_PI;
- rotz = ang;
- }
- void display(){
- //calculate coords
- float ang = TWO_PI * millis()/orbitDuration*-1; //*-1 -> clockwise
- x = cos(ang)*orbitRad;
- y = 0;
- z = sin(ang)*orbitRad;
- //draw
- pushMatrix();
- rotateZ(rotz);
- translate(x,y,z);
- fill(75); stroke(125);
- sphere(size);
- popMatrix();
- }
- }
Note I'm calculating the orbit size (as I mentioned before) based on the distance between the mouse coords and the planet (parent)... but I had to divide it by 4 to get a closer result... not sure why.
Any help, suggestion or tip would be really appreciated.
Thanks in advance,
Jon
1