NooNooPhysics - A 2D physics engine

Community Forums/Showcase/NooNooPhysics - A 2D physics engine

Noobody(Posted 2009) [#1]
For my final thesis for school I worked on a verlet physics engine during the last half year.

The result is NooNooPhysics - a 2D physics engine for B3D (haven't tested it in BlitzPlus, but should work too).
It was made in C++ and BlitzBasic and is open source; for further information read the License.txt in the source folder.

It features collision for all convex objects, friction and soft bodies.
The approach is based on the verlet integration, using the separating axis theorem for collision detection and an own method for collision response and friction.

Installation is quite easy, just copy the NooNooPhysics.dll and the NooNooPhysics.decls in the userlibs folder of your B3D directory, include the NooNooPhysics.bb in your project and you're done.

The examples show the basic commands on how to use the engine; more information about the functions and types can be found in the attached documentation.

Have some screenshots of some of the examples :)

Example 1:


Example 4:


The download contains the DLL, the userlib, the include, examples, a small documentation as well as the code of the DLL:
Download

The examples are also available as .exes, since it's always quite a bother to copy all of these files into the right folder just to test something out
Download


BlitzSupport(Posted 2009) [#2]
Wow, very nice, and under a very generous licence too. Thanks for sharing this!


Nate the Great(Posted 2009) [#3]
nice job.. looks kindof like what I just got my physics engine for bmax to do. is it 3d compatible? This is certaily interesting.


Noobody(Posted 2009) [#4]
is it 3d compatible?

Basic verlet integration can easily be extended to 3D, however, collision detection and response in 3D is much more difficult than in 2D.
Using the same approach as I did in 2D, one would need the separating plane theorem, which is a real performance killer.
Also, verlet integration in 3D needs much more iterations to maintain the integrity of the physics bodies, eating the rest of the FPS up.

For these reasons, I have discarded the 3D part a while ago, since a rigid body system is much more adequate in 3D.
In fact, there's only one 3D - game that uses verlet integration, Hitman: Codename 47, where it has been used for the ragdolls.


Nate the Great(Posted 2009) [#5]
ok so after running it a few times I get an unknown runtime error :( maybe it is a divided by 0 error because thats what happens to my programs that have a /0 error.


Noobody(Posted 2009) [#6]
It says 'Unkown Runtime Error'?
Never heard of that one, I must admit :)

Is it one of the examples that doesn't work or the whole library?
It would be quite helpful if you could start the affected example in debug mode to specify the critical code section (if you have B3D, that is).

Seems to be a rather system-specific error, since it used to work fine on the german forums (or I did a mistake while I translated the code into English :) ).


Nate the Great(Posted 2009) [#7]
ok I ran the first one in debug and it said abnormal program termination. it looks like a result of a divided by 0 error I have seen so many times.

Here is the code I ran.. I modified the numbers a little.

Graphics 800, 600, 0, 2
SetBuffer BackBuffer()

Include "NooNooPhysics.bb"

NNP_CreatePhysics 512, 1024, 1024 ;Initialize physics

NNP_SetGravity 0, 0.1 ;Set gravity

For X = 0 To 10
	For Y = 0 To 10
		NNP_CreateBox 100 + X*60, 100 + Y*60, 50, 50 ;Create lots of cubes
	Next
Next

Local Bottom.TEntity = NNP_CreateBox( 400, 600, 800, 10 ) ;Create the ground
	Bottom\Locked = True ;Lock it


Timer = CreateTimer( 60 )

While Not KeyHit( 1 )
	Cls
	
	NNP_UpdateForces  ;Calculate forces
	NNP_UpdatePhysics ;Calculate physics
	NNP_DebugRender DEBUG_EDGES + DEBUG_VERTICES + DEBUG_CENTER ;Render objects
	
	Flip 0
	WaitTimer Timer
Wend
End


The error happens randomly so you must run it a few times before you get it


Nate the Great(Posted 2009) [#8]
ok here is the error... I messed around with the code a bit to see what it could handle and thats what allowed me to see the error more clearly.



As you can see, there is a crushed box at the moment of the error


Noobody(Posted 2009) [#9]
Okay, that is quite helpful.
This is a general problem with the collision detection, which allows only convex objects. At the moment the box is crushed, one of the outer vertices pops to the inside, making the object concave and therefore confusing the collision detection.
That produces this strange error, even though it always said 'Memory Access Violation' on my machine :)

Since the verlet integration allows only soft bodies, one must be particularly careful with the engine - if great force or acceleration is taking effect on an object, it will most certainly be compressed in the next collision and there is a high propability that it is concave afterwards.

That is a bit of a problem, I know, but shouldn't happen under normal circumstances.
I'm working on it.


Noobody(Posted 2009) [#10]
I wrote an article for GameDev describing the method I used for this engine.

If anyone is interested in implementing physics into their games, they might want to give it a look: Link


slenkar(Posted 2009) [#11]
thanks