2D Rigid Body Dynamics

User projects written in or related to FreeBASIC.
coderJeff
Site Admin
Posts: 4313
Joined: Nov 04, 2005 14:23
Location: Ontario, Canada
Contact:

2D Rigid Body Dynamics

Post by coderJeff »

2D rigid body dynamics source code
All in FreeBASIC. No additional libraries needed. Program will compile/run under fb.15, fb.16, win32/linux.

Download Source: rigid_source.zip 19K

It supports the following:
* linear and angular momentum in 2D
* Collision detection/resolution for object/world and object/object interactions
* Scene files to describe shapes and objects to be used in the simulation
* Elastics which can be added realtime during the simulation either object to object or object to world. using the mouse.
* Controls for zooming/panning the scene, restarting, and deleting all eleastics.

All instructions are in the readme.txt, but here's a summary:
make with `mkrigid.bat' or `./mkrigid.sh'
run with `rigid [scenefilename]'
where [scenefilename] is the name of a scene. Defaults to default.ini if not present. I have included four simple scenes to try out in addition to the default.

There are many problems yet to solve in this physics engine, so if you are thinking adding physics to a game, you should also watch the topic Newton Game Dynamics for developments as it is a very powerful physics engine.

Image

Download Source: rigid_source.zip 36K
Last edited by coderJeff on Apr 01, 2006 15:28, edited 1 time in total.
dontmailme
Posts: 10
Joined: Mar 10, 2006 9:46

Post by dontmailme »

Cool :D

Can't wait for your 3D version !!! ;)
rdc
Posts: 1741
Joined: May 27, 2005 17:22
Location: Texas, USA
Contact:

Post by rdc »

Very impressive. I like scene3. :)
Dr_D
Posts: 2451
Joined: May 27, 2005 4:59
Contact:

Post by Dr_D »

Yeah, that's fun isn't it? :)
redcrab
Posts: 623
Joined: Feb 07, 2006 15:29
Location: France / Luxemburg
Contact:

Post by redcrab »

Cool ! I love it !
coderJeff
Site Admin
Posts: 4313
Joined: Nov 04, 2005 14:23
Location: Ontario, Canada
Contact:

Post by coderJeff »

Thanks to everyone who tried it out and sorry I didn't post back sooner.

Getting to the next step is harder than I thought. The simulation mostly works but not always. (Which is just another way of saying it doesn't work). All of the math used so far can be applied to a 3D simulation except for rotation I think I'll make a 3D version after I work out the problems in 2D first.

I have added a 2D BSP model for the world. So collision detection is pretty much perfect. Actually, that part of the simulation is very robust. I can accurately and reliably detect collisions between objects and world. And singular collisions seem to resolve just fine. What I can't do at the moment, is properly resolve multiple simultaneous collisions. So what sometimes happens is that objects get in to a state where the collision resolver can't compute a way out of it and the simulation hangs.

I've read a few more papers and found a few more snippets of code in C/C++ on the subject this week and am going to try and implement something new for multiple collision resolution.
Dr_D
Posts: 2451
Joined: May 27, 2005 4:59
Contact:

Post by Dr_D »

One way to do to is to find the midpoint of the collisions and use that as the point of impact. That's just a cheap way to solve it, which has problems of it's own though. All in all, you've got a nice start to an engine there. ;)
coderJeff
Site Admin
Posts: 4313
Joined: Nov 04, 2005 14:23
Location: Ontario, Canada
Contact:

Post by coderJeff »

Dr_D, I hear what you are saying. It seems that that's the least of my problems at the moment though. I think taking the midpoint helps to reduce the effects caused by one point hitting "first" followed by another point, when actually both are occuring at the same time.

I've been working quite a bit on this project. Thankfully it is interesting to me even if it is not completely working right. It some ways it's now better. There are a few situations that really give me problems. For example the hammer shape, which is thin and can rotate quickly sometimes tunnels in to other shapes. Which means I should be doing a swept test for collision rather than an overlap.

Anyway, I updated the download and the screen image in the first post. The scenes are still basic testing scenes.

New features:
* Added 2D BSP (very basic)
* Many changes to scene files
* right mouse button/drag forces an object to move. Mouse wheel will rotate the object
* Added a caption each scene to explain what it is I'm trying to test:

Download Source: rigid_source.zip 36K

Web page on this project is here: http://frontpage.execulink.com/coder/fr ... /rigid.asp

Also, it seems there is a rigid body dynamics ninja stickman fighting game called 'N' available here: http://www.harveycartel.org/metanet/n.html. NO source code but some very good on-line interactive tutorials about collision detection.
voodooattack
Posts: 605
Joined: Feb 18, 2006 13:30
Location: Alexandria / Egypt
Contact:

Post by voodooattack »

nice work! :)

just found a small error with scene3

when u take the ball and throw it at a wall, it will bounce fine..
but when it hits other dynamic objects, it'll simply "freeze", looks strange :)

very outstanding work in my opinion :D
rdc
Posts: 1741
Joined: May 27, 2005 17:22
Location: Texas, USA
Contact:

Post by rdc »

Looks like it is progressing quite well to me. Nice work.
coderJeff
Site Admin
Posts: 4313
Joined: Nov 04, 2005 14:23
Location: Ontario, Canada
Contact:

Post by coderJeff »

thanks, voodooattack and rdc
voodooattack wrote:when u take the ball and throw it at a wall, it will bounce fine..
but when it hits other dynamic objects, it'll simply "freeze", looks strange
They should be able to still move with the mouse though. That's my fall back plan for now. When objects get in to a situation where the collisions can't be resolved, I just set the velocity for that object to zero. Not very realistic I know, but at least it's better than the simulation hanging up. It should only happen when objects are pinned against the world. I think I can fix it by prioritizing the collisions and resolving them in the "proper" order. (which should also handle with situations like stacking)
mambazo
Posts: 652
Joined: Jul 17, 2005 13:02
Location: Ireland
Contact:

Post by mambazo »

Only scene1.ini will load for me... strange. Other than that, this project is really getting places!
coderJeff
Site Admin
Posts: 4313
Joined: Nov 04, 2005 14:23
Location: Ontario, Canada
Contact:

Post by coderJeff »

Ok, I get an error too here, looks like I didn't test the most recent version with -exx and it dies on startup.

EDIT: Nevermind, that was just FB15 throwing an error on GetMouse. Don't know mambazo. It works here with fb15/16 win32 and fb16 linux.

What happens when you try this:

Code: Select all

rigid scene3.ini
v1ctor
Site Admin
Posts: 3804
Joined: May 27, 2005 8:08
Location: SP / Bra[s]il
Contact:

Post by v1ctor »

I missed the 3d version of Atoms, it's so nice, any plans to extend Rigid to another dimension too? :)
mambazo
Posts: 652
Joined: Jul 17, 2005 13:02
Location: Ireland
Contact:

Post by mambazo »

Ah, it actually works when I run it from the command prompt, I was dragging the ini file onto the exe in WinXP, that has worked for me the last few releases. But strangely not in this release... how odd.
Post Reply