3

Alright, so I'm trying to create my own Physics engine for a 3D XNA game, and I'm having troubles calculating how much I should move my object by for gravity.

XNA's game timer occurs every 16 milliseconds, so after some calculations, and using 9.81m/s as my gravitational velocity, you can see that you should increase the velocity of the object that has gravity by:

0.15696 meters/16milliseconds
- basically every update call should increase the object by 0.15696 meters

The questions is, how do I convert 0.15696 meters into pixels. Obviously if I just use a 1:1 relationship the object will only move 9.81 pixels/second. Which does not really simulate gravity :P Does anyone have a good idea on how I can determine how many pixels I should move the object by?

Thanks for all the help!

4
  • Any reason your creating your own framework? Commented Apr 21, 2011 at 15:19
  • For fun :) Basically just want to get the experience playing with physics in Xna. Commented Apr 21, 2011 at 15:20
  • "I'm trying to create my own Physics engine for a 3D XNA game" Commented Apr 21, 2011 at 15:28
  • 7
    Although physics in 3d game dev is an approximation of physics at best, you should give your engine its best shot at accuracy... The game clock doesn't occur every 16 milliseconds. If you are running at 60 FPS, it occurs at approximately 16.666667 seconds and sometimes misses a beat depending on the workload... That can make a big difference during integration. Use gameTime.ElapsedGameTime.TotalSeconds instead of just 16 to give the engine the best precision you can. Commented Apr 21, 2011 at 18:55

2 Answers 2

5

Although 2d game dev is more pixel-centric, 3d game dev doesn't concern itself with pixels for this kind of stuff, but rather units. Unit size is completely arbitrary.

Typically, you have a velocity vector whose magnitude is equivalent to the meters(or feet or ??) per second that your object is going. That way the position is updated each frame by adding this velocity * the elapsed time since last frame (the 16.6666 ms). The acceleration from gravity is added to the velocity vector in the same way:

Vector3 gravity = Vector3.Down * 9.81f; //in the update loop float elapsed = (float)gameTime.ElapsedGameTime.TotalSeconds;//0.01666667 (or 16.67 ms) @ 60FPS velocity += gravity * elapsed; position += velocity * elapsed; 

In this example, I've arbitrarily established that 1 xna unit == 1 meter.

Sign up to request clarification or add additional context in comments.

Comments

1

You just have to figure out how large an "area" your screen represents; then its simple scaling. If you have a 2D representation of a satellite orbiting earth, then your "Y" pixels represent ground to satellite (approximately 35,000 meters).

Use your equations from classical physics (http://en.wikipedia.org/wiki/List_of_equations_in_classical_mechanics), and then just scale real dimensions to your screen.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.