• Advertisement
Sign in to follow this  

2d newtonian physics?

This topic is 4677 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Where can I find some articles / tutorials on newtonian physics related to space in a 2d game? I'm aware of the basic concepts but I'm trying to find some resources that will point me in the right direction to actually implementing them using C/C++. I understand the laws of motion but applying them could be tricky ;) Thanks

Share this post


Link to post
Share on other sites
Advertisement
What you need is the Euler method in combination with vectors. There's a good tutorial (including header) around here, that does exactly that.

You'd apply a vector Force on a mass, which because of F = ma results in a vector Acceleration. Then with Acceleration, a new position can be calculated using the Euler method:

v += ( F / m ) * dt;
pos += v * dt;

Note that F/m is the acceleration and dt is the change of time at which you want to apply the new calculation (which could be the time between frames). The vectors are [v, pos and F], [m and dt] are scalar numbers.

Here's the tutorial combining the Euler method and Vectors:

http://nehe.gamedev.net/data/lessons/lesson.asp?lesson=39

Ofcourse, note that the Euler method isn't the most accurate method of position calculation around, but it's simple, easy to understand and it works. Try it first before trying others, such as the (Time Corrected) Verlet Method. But I doubt you need anything more advanced than the Euler method.

Good luck,
Stemic

Share this post


Link to post
Share on other sites
Well, I hate to always cite the same references, but they're really the best ones I can think of for 2D physics. So here goes:

1. Chris Hecker's articles (I should find the url so I can just link to it...) Great overall, but especially useful in that it covers everything in 2D before moving to 3D.

2. 'Physics for Game Developers', by David Bourg. Some people don't like this book, but FWIW it does cover rigid body dynamics in 2D, along with code samples.

For 3D, I again recommend Hecker's articles, along with David Baraff's papers and Dave Eberly's 'Game Physics'.

'Hope that helps.

Share this post


Link to post
Share on other sites
For the purposes of a 2d space shooter trying to immitate physics like those seen in the game: Asteroids, would the Euler method do?

Share this post


Link to post
Share on other sites
Quote:
Original post by Stemic
v += ( F / m ) * dt;
pos += v * dt;
...
But I doubt you need anything more advanced than the Euler method.

Not that the above is wrong, but inevitably somebody will put that into code and then post "Why does my object's motion change with the frame rate?"

The fact is that Euler integration is rarely good enough. The good news is that if you are dealing with constant (piecewise) acceleration, the exact solution is not much more expensive or complicated than the above solution. Here it is:

a = F / m; // This should be constant
v = v0 + a * dt;
s = s0 + v0 * dt + 0.5 * a * dt * dt;
v0 = v;
s0 = s;

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement