We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › Orbiting stuck
Page Index Toggle Pages: 1
Orbiting stuck (Read 502 times)
Orbiting stuck
Feb 19th, 2008, 9:38am
 
Hi i've trying the planet-orbit thing to learn the animation.I have writtent the following code,but i can't get things to work.How can i make my planet revolve arround another planet.What's wrong with my code?What can i do about that?


Code:


void setup()
{size(800,600);
smooth();
background(1);
}
void draw()
{
sun();
plotplanet(400,400,20);
}

void sun()
{
fill(#FE5502);
ellipse(400, 280, 55, 55);
flames();
}

void flames()
{
fill(#FE5502);


}
void plotplanet (int c,int d,int z)
{
int i=1,j=1;
int cx=c;
int cy=d;
int r=z;
int theta;
fill(#94A0AA);
for (theta=0 ; theta<=360; theta++)
{
ellipse(i+theta,j+theta,20,20);

}

}



Please help me.Thanks in advance.
Re: Orbiting stuck
Reply #1 - Feb 19th, 2008, 10:55am
 
Indeed, using a for-loop to animate an object is not the right way to proceed, and that's why you're stuck.

The code in draw() method is supposed to be executed x times per second. What you are supposed to do is :

 * clear the background
 * draw the sun
 * calculate the coordinates of the planet
 * draw the planet

That means, theta has to be a global variable. See :
http://processing.org/learning/basics/variablescope.html

By the way, i+theta, j+theta are linear coordinates. I guess you would prefer a trajectory that looks like an ellipse : sunPositionX + cos(theta)*ellipseWidth, sunPositionY + sin(theta)*ellipseHeight (saying theta is a float between 0 and TWO_PI).
Re: Orbiting stuck
Reply #2 - Feb 22nd, 2008, 3:15pm
 
sorry bro
can't get it to working.I tried.,i think i can't do that.i need to better try this at Flash actionscript than processing.

it's tough with processing.Do u have any suggstns?
Re: Orbiting stuck
Reply #3 - Feb 22nd, 2008, 4:55pm
 
don't become discouraged! we're here to help Wink

if there is something you don't understand in the code below, just ask.

Code:
float theta = 0.0;

void setup() {
size(800,600);
smooth();
}

void draw() {
background(0);
sun();
plotplanet();
}

void sun() {
fill(#FE5502);
ellipse(400, 280, 55, 55);
}

void plotplanet () {
theta += PI/80;
if (theta > TWO_PI) theta -= TWO_PI;
fill(255);
ellipse(400 + cos(theta)*150, 280 + sin(theta)*100, 20, 20);
}
Re: Orbiting stuck
Reply #4 - Feb 24th, 2008, 12:24pm
 
@antiplastik:i was frustrated with a lot of coding with processing.actually it was worst expen.

Questions:
I didn't understand the pi/80 & formula below:

ellipse(400 + cos(theta)*150, 280 + sin(theta)*100, 20, 20);

How it works?
I mean how you get it working?
I'll just go thru the code and i'll post more question as soon as  encounter.


--- Thanks for stayin with me.I needed that badly. Smiley
Re: Orbiting stuck
Reply #5 - Feb 24th, 2008, 5:45pm
 
I'll try to explain in english, though it is not my native language. So please excuse me in advance ;-)

To calculate the position of your planet, which has an ellipsoidal trajectory, you have to work with cos & sin. This is the standard equation of an ellipse (you can change the ratios - 150 and 100 - and see what happens, it may flatten the ellipse, for instance) :
x = cos(theta)*150
y = sin(theta)*100

I add 400 to the x position and 280 to the y-position to center the trajectory around the sun.

Theta is the angle of rotation in radians : between 0 and TWO_PI (which is the radian equivalent to a complete rotation). At each frame, theta increases a little (PI/80) so the planet moves a little.

Code:
if (theta > TWO_PI) theta -= TWO_PI; 


This formula is optional, since cos(0) and cos(TWO_PI) and cos(every TWO_PI multiple) return the same. It is just here to check that theta don't get too high, in case you would let the applet run forever (because theta is a float and has an upper limit).
Re: Orbiting stuck
Reply #6 - Feb 28th, 2008, 3:53pm
 
Thank you very much for the explaination and code-fix.I really appreciate that again.

One more question is:
in processing moving a pixel or circle or square from one location to another,which logic do i need to consider?

Also in this code i have used planet to move arround another,apart from that if i want random movement in clockwise or anticlockwise arround sun then what do i need to do?

another question apart of this code is particle system,is it possible to create them?like emmision of small particle from central blob like we used to see in dragonball z.That could be good particle system example.
Re: Orbiting stuck
Reply #7 - Feb 28th, 2008, 9:27pm
 
Quote:
in processing moving a pixel or circle or square from one location to another,which logic do i need to consider?

object-oriented programming. make your pixel/circle/square an object with x,y coordinates and its own display method. something like :
Code:
class MyEllipse {
float x, y;
float size;
void moveTo(float x, float y) {
this.x = x;
this.y = y;
}
void display() {
ellipseMode(CENTER);
ellipse(x, y, size, size);
}
}


Quote:
if i want random movement in clockwise or anticlockwise arround sun then what do i need to do?


it is similar to the code above : increase or decrease the angle of rotation. check the cos(), sin() and random() methods in the reference.

Quote:
particle system,is it possible to create them?

check the libraries and the learning section, there are some examples.
Page Index Toggle Pages: 1