Rectangular Vectors From angle and magnitude

Join the discussion
Registration is free. Ask a follow-up in this thread, or start your own.
1 reply · 3K views
steakbbq
Messages
1
Reaction score
0
Not really homework. But I am programming a game. It is using planetary gravity to make a spaceship go into orbit. I have it so that I find the magnitude of gravity and the direction the planet is in from the ship. I need to make gravity act on the ship in the direction of the planet.

I thin it is something like.

Xpos = cos(angle) + xvelocity;
YPos = sin(angle) + yvelocity;

my head hurts. I can post some of the code to see if you can make sense of it.


//Clalculate Force Of Gravity
forceOfGravity = 1 * ((shipMass * earthMass)/Math.pow(distanceEarth,2));
//Calculate Acceleration
acceleration = forceOfGravity * shipMass;
//Calculate Final Velocity;
fVelocityX = iVelocityX + acceleration * timeStep;
fVelocityY = iVelocityY + acceleration * timeStep;
//Calculate Final Position;
fPositionX = iPositionX + velocity * timeStep;
fPositionY = iPositionY + velocity * timeStep;
//Calculate The X and Y Coordinates Based On Velocity And Angle
//this.x = Math.cos(angleRads);
//this.y = Math.sin(angleRads);
 
Physics news on Phys.org
Welcome to PF, Steakbbq!
Your project sounds most interesting. It looks like you have the big picture of the main loop set up well.

But there are problems with the details. At
"fVelocityX = iVelocityX + acceleration * timeStep;"
it is already necessary to know the direction of the acceleration, and use only the x component to find the change in the x component of velocity. Better to find the x and y components of acceleration before doing the velocities. For the position calcs, use only the x component of velocity in the calculation of the x position.

Those formulas at the beginning, Xpos = cos(angle) + xvelocity;,
do not make sense because Xpos has units of meters, the cos term has no units and the velocity term has units of m/s.