Advertisement Jump to content
Sign in to follow this  

How to do unit tests for a physics engine?

This topic is 4910 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

The topic just about explains it. I really like tests but how would I do some for a physics engine? Just set up some random data and check? Doesn't sound very.... groovy...

Share this post

Link to post
Share on other sites

While unfortunately not groovy :) you're probably best off. Supply the engine with known inital conditions then run for 'a time' before checking it produced the expected results.

Some observations (that might ease the pain):

- Your data shouldn't be random, I'd start with data that is simple to calculate the expected result (by hand) And move onto data that is near edge conditions (very high/low masses and velocities, unusual coefficients, multiple objects colliding at the same time).

- Depending where you intend to use your engine (PhD physic sim vs real time game) You might decide that the tests are intended to show the engine does 'about' the right thing, allowing your expected data checks to be fairly vague (after colliding velocity is within 20% of expect value - allowing your expected values to be approximate)

The only other thing that springs to mind is the possiblity of using another physics engine to generate test data, but I'll leave that as an exercise for the reader.



Share this post

Link to post
Share on other sites
Also, keep in mind that testing dosn't always have to be or the "foo == bar" variety. You can design fuzzier tests than that that are still useful.

For instance if you create an object, apply a downward force on it (say... gravity?), you should then give it a single timestep and then just check that it is indeed lower.

Although in that particular case it should be fairly simple to do a "foo == bar" type test if you make your "equals" a "almost equals" (particularly to avoid floating-point errors).

Another example might be a "motor" object that rotates another object. A suitable test would be for a change in rotation but not position. This may be suitable if calculation of actual results is not possible.

Yet another example might be testing for the simulation of friction. Simulate something once with it off, once with it on, and then test the resultant movement with friction is smaller than the one without friction.

Share this post

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

  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!