Rectangular Vectors From angle and magnitude

Click For Summary
SUMMARY

The discussion focuses on implementing gravitational physics in a game, specifically calculating the effect of gravity on a spaceship's trajectory using angle and magnitude. The user is attempting to derive the spaceship's position and velocity based on gravitational forces, but faces issues with the accuracy of their calculations. Key formulas discussed include the calculation of gravitational force, acceleration, final velocity, and position, with emphasis on the need to separate x and y components for accurate results.

PREREQUISITES
  • Understanding of Newton's Law of Universal Gravitation
  • Familiarity with vector mathematics and trigonometry
  • Knowledge of programming concepts, particularly in JavaScript
  • Experience with game physics and motion simulation
NEXT STEPS
  • Implement vector decomposition for acceleration in both x and y components
  • Learn about integrating physics in game development using Unity or Unreal Engine
  • Research the use of time steps in physics simulations for accurate motion
  • Explore advanced gravitational modeling techniques for realistic space simulations
USEFUL FOR

Game developers, physics programmers, and anyone interested in simulating realistic motion and gravitational effects in game environments.

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.
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 4 ·
Replies
4
Views
6K
  • · Replies 5 ·
Replies
5
Views
3K