Im trying to make a sketch to simulate the movement of a planet around the sun. I use as parameters the initial position, the initial speed, the gravity constant, an the angle of the initial speed.
Usually the trajectory is an ellipse, as it should, and the planet dont go away from the ellipse. I think that the sketch is ok, and the elipse is only the case of stable orbits. ¿Do you think the strange case of the parameters chosen below is due to any error in the code?
Code://parámeters
float g=20000;//gravity constant
float din=200;//Initial distance to the sun
float vin=5;//Initial speed
float ang=-PI/10;//angle respect perpendicular to the distance to the sun. downside is positive
//variables
Ball planet;
float X;
float Y;
float r2;
void setup(){
size (700,700);
background(0);
smooth();
planet =new Ball(width/2,height/2-din,1,vin*cos(ang),vin*sin(ang),10);
frameRate(60);
}
void draw(){
background(0);
ellipse (width/2,height/2,30,30);
planet.ecuation();
planet.move();
planet.paint();
}
class Ball {
float radio;
float x,y;
int id;
float vx,vy;
Ball[] resto;
Ball(float xin,float yin, int idin,float vxin, float vyin,float radin){
x=xin;
y=yin;
id=idin;
vx=vxin;
vy=vyin;
radio=radin;
}
void ecuation(){
X=x-width/2;
Y=y-height/2;
r2=X*X+Y*Y;
if (r2>0){
vx-=g*X/(sqrt(r2)*r2);
vy-=g*Y/(sqrt(r2)*r2);
}
}
void move(){
x+=vx;
y+=vy;
}
void paint(){
ellipse (x,y,radio,radio);
}
}
This for example is stable:
Code:float g=10000;//gravity constant
float din=300;//Initial distance to the sun
float vin=5;//Initial speed
float ang=0;//angle respect perpendicular to the distance to the sun. downside is positive
But try to increase the g value, it starts to be insestable, and I think it shouldn't and in some cases of stronger gravity it expel the earth away!