# Mars Lander physics simulator

i’ve made a physics simulator for the mars lander problem, but seems like it’s having an error in position calculations, and it’s growing fast with turns

newPos = pos+(dir+(GravityVec+velocity)/2).rounded();
newVelocity = GravityVec+velocity;

newVelocity is the same as in Game Info for all the time, but newPos’s getting a huge error in 30 turns
what’s the right formula?

• Work with Double precision types ;
• Keep not rounded values of the parameters.

``` // game loop long double x,y,vx,vy; for(int k=0;;++k) { int X, Y; int hSpeed, vSpeed; // the horizontal & vertical speed (in m/s), can be negative. int fuel; // the quantity of remaining fuel in liters. int rotate; // the rotation angle in degrees (-90 to 90). int power; // the thrust power (0 to 4). scanf("%d%d%d%d%d%d%d", &X, &Y, &hSpeed, &vSpeed, &fuel, &rotate, &power); if(k==0) { x = X; y = Y; vx = hSpeed; vy = vSpeed; } int angle = 18, thrust = 3; if(angle>rotate+15) angle = rotate+15; if(angle<rotate-15) angle = rotate-15; if(thrust>power+1) thrust = power+1; if(thrust<power-1) thrust = power-1; x = x+vx-0.5*sin(angle*PI/180.)*thrust; y = y+vy+0.5*(cos(angle*PI/180.)*thrust-G); vx = vx-1.*sin(angle*PI/180.)*thrust; vy = vy+1.*(cos(angle*PI/180.)*thrust-G); fprintf(stderr,"x -> %.0Lf ; y = %.0Lf\n",x,y); fprintf(stderr,"vx -> %.0Lf ; vy = %.0Lf\n",vx,vy); printf("%d %d\n",angle,thrust); }```

1 Like

while (1) {
cout << “-90 4” << endl;
if(rotate>-90)rotate-=15;
if(power!=4)power++;
vec2d dir=vec2d(-sin(rotatePI/180.),cos(rotatePI/180.))*power;
pos+=velocity+(G+dir)*0.5;
velocity+=G+dir;
cerr<< pos<<" "<< velocity<< endl;
}

yea, looks like now it works
thk