FreeBASIC port of Erin Catto's Box2D_Lite physics engine

User projects written in or related to FreeBASIC.
h4tt3n
Posts: 677
Joined: Oct 22, 2005 21:12
Location: Denmark

FreeBASIC port of Erin Catto's Box2D_Lite physics engine

Postby h4tt3n » Sep 28, 2015 9:47

Hello folks, It's been a while since my last post, but after a long break I've gotten around to do some coding again :-)

My current project is a Freebasic port of Erin Catto's tiny physics engine Box2D_Lite, which was originally written in C++. I've wanted to do this for quite a while and have finally found the time to do it. It contains nine demos which demonstrate box stacking and joint constraints using sequential impulses. There are some links at the bottom of this post for downloading code and papers.

The main differences between the original c++ version and this one are:

-glut has been removed (unsupported for over 15 years) and replaced with native fbgfx graphics.
-The content of corresponding .h and .cpp files have been merged into just one .bi file.
-A few things have been moved in order to get rid of circular dependencies.

Links to Box2D related stuff:

Erin Catto's Box2D_Lite C++ version with Visual CPP and Code::Blocks project
https://dl.dropboxusercontent.com/u/389 ... te_Cpp.zip

My own Box2D_Lite FreeBASIC version with FBEdit project
https://dl.dropboxusercontent.com/u/389 ... _fbgfx.zip

Erin Catto's Box2D page with lots of nice papers, slides, and source code
http://box2d.org/

Cheers,
Mike
Last edited by h4tt3n on Sep 29, 2015 17:03, edited 3 times in total.
RockTheSchock
Posts: 218
Joined: Mar 12, 2006 16:25

Re: FreeBASIC port of Erin Catto's Box2D_Lite physics engine

Postby RockTheSchock » Sep 28, 2015 15:23

Maybe you could use mdTypes as Container Lib.
https://www.freebasic-portal.de/downloa ... s-308.html
D.J.Peters
Posts: 7667
Joined: May 28, 2005 3:28

Re: FreeBASIC port of Erin Catto's Box2D_Lite physics engine

Postby D.J.Peters » Sep 28, 2015 17:42

You can implement the C++ std::vector as dynamic array of class pointers.

But the bad news are:
The arbiters are a C++ template of std::map
you have to replace the std::map with your own stuf
(A binary tree with search and sort order features via unique key)
http://www.cplusplus.com/reference/map/map/

but last not least same for C++ std::pair
http://www.cplusplus.com/reference/utility/pair/pair/

good luck

Joshy
Last edited by D.J.Peters on Jan 19, 2016 2:16, edited 1 time in total.
MOD
Posts: 554
Joined: Jun 11, 2009 20:15

Re: FreeBASIC port of Erin Catto's Box2D_Lite physics engine

Postby MOD » Sep 28, 2015 18:15

I didn't check the whole project but simply created some dummy classes and tried to translate it more or less to a mdTypes version:

Code: Select all

#Include "md/util/mdMap.bi"
#Include "md/util/mdList.bi"

Type Body Extends Object
   As Single invMass
End Type

Type Contact Extends Object
   
End Type

Type ArbIter Extends Object
   Declare Constructor (b1 As Body Ptr, b2 As Body Ptr)
   Declare Sub Update(contacts As Contact Ptr, numContacts As Integer)
   
   As Integer numContacts
   As Contact Ptr contacts
End Type

Constructor ArbIter(b1 As Body Ptr, b2 As Body Ptr)
   
End Constructor

Sub ArbIter.Update(contacts As Contact Ptr, numContacts As Integer)
   
End Sub

Type ArbIterKey Extends Object
   Declare Constructor (b1 As Body Ptr, b2 As Body Ptr)
End Type

Constructor ArbIterKey(b1 As Body Ptr, b2 As Body Ptr)
   
End Constructor

mdListDeclarePtr(Body, Ptr)
mdListDeclarePtr(ArbIter, Ptr)
mdMapDeclarePtr(ArbIterKey, Ptr, ArbIter, Ptr)

Type World Extends Object
   Declare Sub BroadPhase()
   
   As mdList(Body, Ptr) bodies
   As mdMapPtr(ArbIterKey, Ptr, ArbIter, Ptr) arbiters
End Type

Sub World.BroadPhase()
   ' O(n^2) broad-phase
   For i As Integer = 0 To i + 1 < bodies.size()
      Dim As Body Ptr bi = bodies.get(i)
      For j As Integer = i + 1 To j + 1 < bodies.size()
         Dim As Body Ptr bj = bodies.get(j)
         
         If bi->invMass = 0.0f AndAlso bj->invMass = 0.0f Then Continue For
         
         Dim As ArbIter newArb = ArbIter(bi, bj)
         Dim As ArbIterKey key = ArbIterKey(bi, bj)
         
         If newArb.numContacts > 0 Then
            Dim As ArbIter Ptr iter = arbiters.get(@key)
            Dim As ArbIter Ptr lastElement = Type<mdListArbIterPtr>(arbiters.values()).get(arbiters.size() - 1)
            If iter = lastElement Then
               arbiters.put(@key, @newArb)
            Else
               iter->Update(newArb.contacts, newArb.numContacts)
            EndIf
         Else
            arbiters.remove(@key)
         EndIf
      Next
   Next
End Sub

I'm sure this will not work but maybe it's enough to get the idea. ;)
h4tt3n
Posts: 677
Joined: Oct 22, 2005 21:12
Location: Denmark

Re: FreeBASIC port of Erin Catto's Box2D_Lite physics engine

Postby h4tt3n » Sep 29, 2015 6:13

*Download link updated*

Okay, I got it working. Now the port works as it should, including collision. Now for some code cleanup...

Cheers
D.J.Peters
Posts: 7667
Joined: May 28, 2005 3:28

Re: FreeBASIC port of Erin Catto's Box2D_Lite physics engine

Postby D.J.Peters » Sep 29, 2015 6:42

@h4tt3n don't spend too much time for the light version.
The object penetration (collision for ever) will never ending.
Because the body sleep feature is missing and much more.
Curently I study the latest version of Box2D.
A really nice pice of code.

Anyway good job your FreeBASIC port.

Joshy
D.J.Peters
Posts: 7667
Joined: May 28, 2005 3:28

Re: FreeBASIC port of Erin Catto's Box2D_Lite physics engine

Postby D.J.Peters » Sep 29, 2015 9:47

Do you know the book Game Physics Engine Development
download: pdf
The source code from the book CD are hosted on github.
https://github.com/idmillington/cyclone-physics

Joshy
h4tt3n
Posts: 677
Joined: Oct 22, 2005 21:12
Location: Denmark

Re: FreeBASIC port of Erin Catto's Box2D_Lite physics engine

Postby h4tt3n » Sep 29, 2015 17:11

D.J.Peters wrote:@h4tt3n don't spend too much time for the light version.
The object penetration (collision for ever) will never ending.
Because the body sleep feature is missing and much more.
Curently I study the latest version of Box2D.
A really nice pice of code.

Anyway good job your FreeBASIC port.

Joshy


Yes, I realise that the lite version does not have any fancy stuff in it, and I have just begun to look into the Box2D engine, which I find a bit daunting because of its size. I have always struggled to comprehend the math behind rigid constraints, so I thought Erin's small engine would be a good start. With a working rigid body simulator in freebasic I have a reasonably simple sandbox to play around with while attempting to understand the underlying math. I'll also be looking into the book you linked to, thanks :-)

Cheers,
MIke
D.J.Peters
Posts: 7667
Joined: May 28, 2005 3:28

Re: FreeBASIC port of Erin Catto's Box2D_Lite physics engine

Postby D.J.Peters » Sep 30, 2015 3:49

Hello MIke, do you use codeblocks ?
I created a cyclone codeblocks workspace (in build/codeblocks)
and build cyclone and freeglut as static lib for win32 and X86_64. (libWindows32 /64)

The executable binaries are in bin/Windows32/64 folder.

download: cyclone-src.7z

A year ago I read the complete book today I study all lines of code and learned much more than before.

The docu inside the all *.h files are the key for me. :-)

The most important part for me are how contact points are build and collected
to solve all kinds of joint and body penetration in 3D space.

Another point are how the volume bounding sphere binary tree works.

Ther cyclone engine is tiny and limited many kinds of joints and konvex hul bodies are missed
but thjer solver is really fast for boxes, spheres, planes, particels, rods, sticks and simple joints and more.

May be a good starting point for a physics engine in pure FreeBASIC ;-)

Joshy
Last edited by D.J.Peters on Oct 03, 2017 4:09, edited 1 time in total.
h4tt3n
Posts: 677
Joined: Oct 22, 2005 21:12
Location: Denmark

Re: FreeBASIC port of Erin Catto's Box2D_Lite physics engine

Postby h4tt3n » Sep 30, 2015 12:34

Hello Joshy,

Yes, I actually do use Code::Blocks, and I really like it. Nice little physics examples, I'll lok into the cyclone engine. Do you by any chance have a C::B workspace for one of the newer versions of Box2D? I think the latest is 2.3.1 now using GLFW instead of freeglut, but Erin has stopped including builds in his updates.

Regarding the physics engine book, I just read the spring section, and I must say that his arguments for not using damped springs in games is pure bollocks. Everyone in the game physcis scene keeps flaming damped springs, they are a very misunderstood concept. I hope the sections on rigid constraints are better, the book as a whole looks solid enough :-)

If you are interested, I wrote a small article on damped springs for Gamedev.net, which you can read here:
http://www.gamedev.net/page/resources/_ ... ring-r3227

Cheers,
Mike
D.J.Peters
Posts: 7667
Joined: May 28, 2005 3:28

Re: FreeBASIC port of Erin Catto's Box2D_Lite physics engine

Postby D.J.Peters » Oct 05, 2015 4:28

h4tt3n wrote:... Do you by any chance have a C::B workspace for one of the newer versions of Box2D? I think the latest is 2.3.1 now using GLFW instead of freeglut ...

Here are the latest box2d git master brunch from: https://github.com/erincatto/Box2D

Box2D Version 2.3.2
Complete codeblocks workspace for Win/Lin 32/64-bi.

dowmload: Box2D-master.zip

codeblock workspace and projects for box2d,glwe,glfw
Box2D-master\Box2D\Build\codeblocks\box2d.workspace
Box2D-master\Box2D\Build\codeblocks\box2d.cbp
Box2D-master\Box2D\Build\codeblocks\glwe.cbp
Box2D-master\Box2D\Build\codeblocks\glfw.cbp
Box2D-master\Box2D\Build\codeblocks\helloworld.cbp

precompiled static libraries
Box2D-master\Box2D\lib\WindosX86\libbox2d.a
Box2D-master\Box2D\lib\WindosX86\libglwe.a
Box2D-master\Box2D\lib\WindosX86\libglfw.a
Box2D-master\Box2D\lib\WindosX86_64\libbox2d.a
Box2D-master\Box2D\lib\WindosX86_64\libglwe.a
Box2D-master\Box2D\lib\WindosX86_64\libglfw.a

helloworld test
Box2D-master\Box2D\win32\helloworld.exe
Last edited by D.J.Peters on Oct 03, 2017 4:04, edited 1 time in total.
h4tt3n
Posts: 677
Joined: Oct 22, 2005 21:12
Location: Denmark

Re: FreeBASIC port of Erin Catto's Box2D_Lite physics engine

Postby h4tt3n » Oct 07, 2015 6:01

This is great, compiled without error. I will take a very good look at this.
D.J.Peters
Posts: 7667
Joined: May 28, 2005 3:28

Re: FreeBASIC port of Erin Catto's Box2D_Lite physics engine

Postby D.J.Peters » Feb 09, 2016 19:59

Hello Mike, I found two usefull books look at the doc folder at analisis-numerico
Computational Geometry In C, 2nd edition,
Computational Geometry Algorithms and Applications, 3rd Edition
(homepage: http://www.cs.uu.nl/geobook)

how ever any news about your physics engine ?

Joshy
h4tt3n
Posts: 677
Joined: Oct 22, 2005 21:12
Location: Denmark

Re: FreeBASIC port of Erin Catto's Box2D_Lite physics engine

Postby h4tt3n » Oct 03, 2017 11:54

D.J.Peters wrote:dowmload: Box2D-master.zip


Hello Joshy,

Did you just update the Code::Blocks build for Box2D? The forum messaged me about an update because you have quoted me in the post with the download link. I can see that the box2d-master is updated today, but I can't see any changes at first glance, and Erin doesn't appear to have released a new version. Anyway, I appreciate that you keep things up-to-date.

Regarding my own physics engine, I do have something to show by now although it's far from done. It's a physics engine that combines deformable and pressurized soft body physics and "space physics", aka. orbital mechanics and rocket physics. I know that's an odd mix, but it's what I need for my game. Also, the world doesn't really need another rigid body physics lib. I wouldn't mind showing it to you, but I'm not ready to go full open source here on the forums.

Cheers, Mike
D.J.Peters
Posts: 7667
Joined: May 28, 2005 3:28

Re: FreeBASIC port of Erin Catto's Box2D_Lite physics engine

Postby D.J.Peters » Oct 03, 2017 17:45

h4tt3n I moved near 1GB of FB stuff (including all libs and images I posted on this forum)
from: alice-dsl.net/d.j.peters to my new shiny3d.de/public server
and I changes ~1000 links via hand (what a pain)

that's all (nothing new)

Joshy

Return to “Projects”

Who is online

Users browsing this forum: MrSwiss and 2 guests