UnderwareDESIGN

PlayBASIC => Show Case => Topic started by: slayer93 on October 26, 2008, 03:04:29 PM

Title: Box2D Wrapper
Post by: slayer93 on October 26, 2008, 03:04:29 PM
Hello guys,

Once I got PlayBasic I have been messing around with making a wrapper for Box2D (http://box2d.org). I have been hacking away at it all weekend and I got the core of it done. I also figured a nice and easy way for it to control the sprites for you by creating a command set that wraps the commands I wrapped. So you don't use the functions in the dll (unless you want to) but use the command set that does everything for you.

Right now you can create worlds, bodies, and use forces to move them (or let gravity do its work if you want).

The library will also be free to use but I'm not releasing it yet because I want to add a few more things so it can do more but thought I would show what I have now and see what you guys thought. Hopefully I can release it tonight or tomorrow :)

I will show you a couple screenshots though it does not serve justice to what Box2D can do. And here is the code to show how simple (or complex?) it is.

[pbcode]; PROJECT : Box2D Demo
; AUTHOR  : Christian Ang
; CREATED : 10/19/2008
; EDITED  : 10/26/2008
; ---------------------------------------------------------------------

; Setup
SetFPS 60
CreateCamera 1
RenderToScreen

; Use Box2D
#include "box2D"

; Create a new world
B2DCreateWorld( 1, -100.0, -100.0, 200.0, 200.0, 0.0, 0.5 )
B2DSetWorldStep( 1, 1.0 / 60.0 )
B2DSetWorldVelocity( 1, 1.0 )
B2DSetWorldIterations( 1, 10.0 )
B2DSetWorldMtPRatio( 1, 10.0 )

; Create a ground Body
boxc 0,0,200,10,1,rgb(0,0,120)
getImage 1,0,0,200,10
gSpr=newSprite( 0, 0, 1 )
AutoCenterSpriteHandle gSpr, 1

B2DCreateBody( 1 )
B2DSetBodyPosition( 1, 10.0, 20.0 )
B2DBuildBody( 1 )
B2DCreateShape( 1 )
B2DSetShapeAsBox( 1, 10.0, 0.5 )
B2DAddShapetoBody( 1, 1 )
B2DLinkSpriteToBody( gSpr, 1 )

; Create Boxes


Boxc 0,0,20,20,1,rgb(100,0,0)
getImage 2,0,0,20,20
   
For i=2 to 20
   B2DCreateBody( i )
   B2DSetBodyPosition( i, 10.0, 0.0-i*2.2 )
   B2DBuildBody( i )
   B2DCreateShape( i )
   B2DSetShapeAsBox( i, 1.0, 1.0 )
   B2DSetShapeFriction( i, 0.1 )
   B2DSetShapeDensity( i, 1.0 )
   B2DAddShapetoBody( i, i )
   B2DCalculateBodyMass( i )
   spr=newSprite(0, 0, 2)
   AutoCenterSpriteHandle spr, 1
   B2DLinkSpriteToBody( spr, i )
Next i


; Main Loop
Do
   
   CaptureToScene
   ClsScene
   
   If spacekey()=1 then B2DApplyImpluse( 2, 0.5, 0.5, 0.0, 0.0 )
   
   ; Draw the Box2D World
   B2DDrawWorld( 1 )
   
   ; Update Box2D World
   B2DUpdateWorld( 1 )
   
   DrawCamera 1
   
; Sync
   Sync
Loop
[/pbcode]


Title: Re: Box2D Wrapper
Post by: thaaks on October 26, 2008, 03:19:48 PM
Ooooooh, physics for PlayBasic! That's just great  :D

The wrapper looks pretty cool. Maybe you add some functions like B2DCreateNewWorld or B2DCreateNewBody where a new index value is automatically generated and returned, just like in PlayBasic. Maybe you even modify the creating functions to always return the index of the created object and never receive it as a parameter! Gasp!

I personally think a coder should never decide which index is to be used on creating a new resource - that should always be returned by the called creating function. Everything else is error prone (reusing indices, forgetting increment and so on)...

Looking forward to play with it! Do you also plan to port the testbed application (The GUI to test all the stuff)?
Title: Re: Box2D Wrapper
Post by: slayer93 on October 26, 2008, 03:27:30 PM
Thanks :)

QuoteThe wrapper looks pretty cool. Maybe you add some functions like B2DCreateNewWorld or B2DCreateNewBody where a new index value is automatically generated and returned, just like in PlayBasic. Maybe you even modify the creating functions to always return the index of the created object and never receive it as a parameter! Gasp!


I'll probably do that ;)

QuoteLooking forward to play with it! Do you also plan to port the testbed application (The GUI to test all the stuff)?

Not sure if I will because of how massive testbed is, maybe once the plugin is completely finished but till then I'll probably create demos and examples here and there to show the new things I added and how it works. Wait a few hours and I'll hopefully finish what I want and upload it ;D
Title: Re: Box2D Wrapper
Post by: ATLUS on October 26, 2008, 03:41:57 PM
 :o NICE  :o 2d physic!!!!!!!!!!!!!! :P
Title: Re: Box2D Wrapper
Post by: Green7 on October 26, 2008, 04:38:20 PM
Thanks a lot! now that opens a ot of options to a physic-coding noob like me! ;D
Title: Re: Box2D Wrapper
Post by: kevin on October 26, 2008, 09:11:05 PM

  Nice, good job.   I was wondering when somebody would do this.    Thankfully it's not me :)
 

Title: Re: Box2D Wrapper
Post by: slayer93 on October 26, 2008, 09:54:18 PM
Here is a quick test version you guys can try out (lets call it v0.0.1). No documentation as of yet but all the commands are listed in the Read-me. It is also extremely slow if you use a lot of dynamic objects (you can change the marked piece of code in the example to find out).

Next Update (most likely next weekend or so) I plan to see if there are any ways to improve speed and what is causing the huge speed decrease, it is most likely me trying to do things quickly. I also am going to finish up the destroy commands because although they are in the command set its not completely finished, so dont use them, then I will add in any of the get state information commands and a couple of joints if I have time.

Quick documentation on making a basic demo in Box2D because I can't leave you with a dll and no information


First Create a world using B2DCreateWorld or B2DCreateNewWorld. Box2D works in meters and not pixels, make sure you know that. The commands I have handle the conversion automatically (from meters to pixels).

B2DSetWorldMtPRatio is a command I made which stands for Meter to Pixel Ratio you specify what ratio you want, 1 meter to X pixel. In the example I made it a 1 m:10px ratio.

B2DSetWorldStep sets the step you want.
B2DSetWorldVelocity sets the velocity of the objects each step
B2DSetWorldIterations sets the iterations each step

Play around with those to see what fits you.

B2DUpdateWorld updates specified world

B2DUseWorld This will set a different world to be actively written to. If you noticed you don't have to specify the world ID in any of the body or shape commands because what you set here is what all the commands will write to. When you create a new world it is set automatically.

For bodies

B2DCreateBody( bID ) Creates a new body and later finalized with B2DBuildBody

B2DSetBodyPosition used before you build the body, you usually don't reposition bodies in the middle of a simulation

Same goes for B2DSetBodyAngle, B2DSetBodyMass, B2DSetBodyLinearDamping, B2DSetBodyAnglularDamping, and B2DSetBodyAsBullet

There is also B2DSetBodySleeping

Shapes are added to bodies, they are what is used for collision.

B2DCreateShape and B2DCreateNewShape create new shapes

B2DSetShapeAsBox uses a box shape for collision, this is the only current collision shape

You may also use B2DSetShapeFriction, B2DSetShapeDensity, and B2DSetShapeRestitution to configure the shape

B2DAddShapetoBody will attach a shape to a body (really when you create a shape it only creates a definition for the shape and when you use this command it creates a new shape under the specified body)

B2DCalculateBodyMass this will automatically calculate the body mass (depending on the shapes) for you

B2DLinkSpriteToBody when you create a body you will need to attach it to a sprite that will represent that body (for the sprite to function correctly you must center the sprite with AutoCenterSpriteHandle or CenterSpriteHandle and set the draw mode to 2 [for rotation] )

B2DDrawWorld this will reposition and rotate any attached sprites to the bodies automatically

You can also try out on your bodies B2DApplyForce, B2DApplyImpluse, and B2DApplyTorque

I think that is the gist of most of the commands you need.

[/tt]

New and Improved Example--

[pbcode]
; PROJECT : Box2D Demo
; AUTHOR  : Christian Ang
; CREATED : 10/19/2008
; EDITED  : 10/26/2008
; ---------------------------------------------------------------------

; Setup
SetFPS 0
CreateCamera 1
RenderToScreen

; Use Box2D
#include "box2D"

; Create a new world
wID=B2DCreateNewWorld( -100.0, -100.0, 200.0, 200.0, 0.0, 0.5 )
B2DSetWorldStep( wID, 1.0 / 60.0 )
B2DSetWorldVelocity( wID, 1.0 )
B2DSetWorldIterations( wID, 10.0 )
B2DSetWorldMtPRatio( wID, 10.0 )

; Create a ground Body
boxc 0,0,200,10,1,rgb(0,0,120)
getImage 1,0,0,200,10

gID = B2DCreateNewBody( )
B2DSetBodyPosition( gID, 15.0, 50.0 )
B2DBuildBody( gID )
sID = B2DCreateNewShape( )
B2DSetShapeAsBox( sID, 10.0, 0.5 )
B2DAddShapetoBody( gID, sID )
gSpr = newSprite( 0, 0, 1 )
AutoCenterSpriteHandle gSpr, 1
B2DLinkSpriteToBody( gSpr, gID )

gID = B2DCreateNewBody( )
B2DSetBodyPosition( gID, 35.0, 45.0 )
B2DBuildBody( gID )
sID = B2DCreateNewShape( )
B2DSetShapeAsBox( sID, 10.0, 0.5 )
B2DAddShapetoBody( gID, sID )
gSpr = newSprite( 0, 0, 1 )
AutoCenterSpriteHandle gSpr, 1
B2DLinkSpriteToBody( gSpr, gID )

; Create Boxes
Boxc 0,0,20,20,1,rgb(100,0,0)
getImage 2,0,0,20,20

; -----------------------------------------------------------
; -----------------------------------------------------------
; Change 20 to different numbers to create more or less boxes
; -----------------------------------------------------------
; -----------------------------------------------------------
For i=2 to 20
   bID = B2DCreateNewBody( )
   B2DSetBodyPosition( bID, rnd(500)/10, rnd(200)/10 )
   B2DBuildBody( bID )
   sID = B2DCreateNewShape( )
   B2DSetShapeAsBox( sID, 1.0, 1.0 )
   B2DSetShapeFriction( sID, 0.1 )
   B2DSetShapeDensity( sID, 1.0 )
   B2DAddShapetoBody( bID, sID )
   B2DCalculateBodyMass( sID )
   spr = newSprite(0, 0, 2)
   AutoCenterSpriteHandle spr, 1
   SpriteDrawMode spr,2
   B2DLinkSpriteToBody( spr, bID )
Next i


; Main Loop
Do
   
   CaptureToScene
   ClsScene
   
   text 0,0,FPS()
   
   ; Draw the Box2D World
   B2DDrawWorld( wID )
   
   ; Update Box2D World
   B2DUpdateWorld( wID )
   
   DrawCamera 1
   
; Sync
   Sync
Loop
[/pbcode]

Download - http://teroton.com/PBbox2D/Box2D.rar (http://teroton.com/PBbox2D/Box2D.rar)

Enjoy :)
Title: Re: Box2D Wrapper
Post by: slayer93 on October 26, 2008, 09:59:07 PM
QuoteNice, good job.   I was wondering when somebody would do this.    Thankfully it's not me Smiley

After getting PlayBasic for free along with some other awesome prizes I thought I would try to give something back :P
Title: Re: Box2D Wrapper
Post by: kevin on October 26, 2008, 10:22:07 PM
 hmmm, I can't get it working, it's spitting up an DLL link error on the B2D_CreateWorld function.  Seems to think it doesn't exist. 
Title: Re: Box2D Wrapper
Post by: slayer93 on October 26, 2008, 10:45:40 PM
Err...really, hmm well I don't have to much time tonight but I'll see if I can take a closer look at it tomorrow when I have more free time. Here is the demo exe, see if that works.




(Broken Attachment removed)
Title: Re: Box2D Wrapper
Post by: ATLUS on October 27, 2008, 06:23:02 AM
Quote from: slayer93 on October 26, 2008, 10:45:40 PM
Err...really, hmm well I don't have to much time tonight but I'll see if I can take a closer look at it tomorrow when I have more free time. Here is the demo exe, see if that works.
This Example not work=(
Title: Re: Box2D Wrapper
Post by: kevin on October 27, 2008, 11:20:02 AM
  The example doesn't work here either.    Moreover, in terms of speed, you're making a common mistake (wrong type of image).

  I suggest reading  this (http://www.underwaredesign.com/forums/index.php?topic=1799.msg12982#msg12982)

 Given this is pretty big news, I would like to include a section on it the next edition of our news letter.  So chop, chop :)

Title: Re: Box2D Wrapper
Post by: slayer93 on October 27, 2008, 09:00:51 PM
QuoteThe example doesn't work here either.    Moreover, in terms of speed, you're making a common mistake (wrong type of image). I suggest reading  this

Will have to test it on a different computer I suppose. I'll also give that a read over, thanks :)

QuoteGiven this is pretty big news, I would like to include a section on it the next edition of our news letter.  So chop, chop Smiley

Cool 8). Time to get working
Title: Re: Box2D Wrapper
Post by: slayer93 on October 27, 2008, 10:53:14 PM
I think I got it working, it was the way I was compiling it and took a lot of googling to find it out. Re download from same place - http://teroton.com/PBbox2D/Box2D.rar (http://teroton.com/PBbox2D/Box2D.rar)

Hope it works out
Title: Re: Box2D Wrapper
Post by: kevin on October 27, 2008, 11:05:07 PM
 Yep, works like a charmer.

When you grab images for use sprite rotation,  the image should be converted to FX format.   

Ie.
  getImage 1,0,0,200,10
  preparefximage 1
 

Note: This assume the code is targeted at PB1.63/PB1.64.  PBFX users should use 3D images (Textures)  with sprite rotation.
Title: Re: Box2D Wrapper
Post by: slayer93 on October 27, 2008, 11:07:38 PM
Thanks, I'll be improving the actual plugin this weekend :)

Glad it works
Title: Re: Box2D Wrapper
Post by: thaaks on October 28, 2008, 04:22:33 AM
Did you create your own wrapper dll to call the Box2D dll or is PBBox2d.dll just a renamed version of the Box2D dll?

I hope to give this a test run tonight!

Great stuff for PB, thanks a lot slayer93!
Title: Re: Box2D Wrapper
Post by: slayer93 on October 28, 2008, 06:03:49 PM
QuoteDid you create your own wrapper dll to call the Box2D dll or is PBBox2d.dll just a renamed version of the Box2D dll?

I created my own wrapper.


Have fun! :)
Title: Re: Box2D Wrapper
Post by: kevin on October 28, 2008, 10:05:41 PM
     Had bit of play with this last night, and was a somewhat shocked by just how slow Box2D seems to be.   The processing time seems directly proportional to the object count (ie. recursive)).  So the more objects, the slower the simulation gets.  Takes 30->35 milliseconds to process 50 moving objects,  60->100 milliseconds for 100 objects etc.  Much quicker when they're at rest.  Which is a bit of a worry really.


[pbcode]

; Setup
SetFPS 30
CreateCamera 1
RenderToScreen

; Use Box2D
#include "box2D"

; Create a new world
wID=B2DCreateNewWorld( -100.0, -100.0, 200.0, 200.0, 0.0, 0.5 )
B2DSetWorldStep( wID, 1.0 / 60.0 )
B2DSetWorldVelocity( wID, 1.0 )
B2DSetWorldIterations( wID, 10.0 )
B2DSetWorldMtPRatio( wID, 10.0 )

; Create a ground Body
boxc 0,0,200,10,1,rgb(0,0,120)
getImage 1,0,0,200,10

gID = B2DCreateNewBody( )
B2DSetBodyPosition( gID, 15.0, 50.0 )
B2DBuildBody( gID )
sID = B2DCreateNewShape( )
B2DSetShapeAsBox( sID, 10.0, 0.5 )
B2DAddShapetoBody( gID, sID )
gSpr = newSprite( 0, 0, 1 )
AutoCenterSpriteHandle gSpr, 1
B2DLinkSpriteToBody( gSpr, gID )

gID = B2DCreateNewBody( )
B2DSetBodyPosition( gID, 35.0, 45.0 )
B2DBuildBody( gID )
sID = B2DCreateNewShape( )
B2DSetShapeAsBox( sID, 10.0, 0.5 )
B2DAddShapetoBody( gID, sID )
gSpr = newSprite( 0, 0, 1 )
AutoCenterSpriteHandle gSpr, 1
B2DLinkSpriteToBody( gSpr, gID )

; Create Boxes
Boxc 0,0,20,20,1,rgb(100,0,0)
getImage 2,0,0,20,20
preparefximage 1
preparefximage 2
; -----------------------------------------------------------
; -----------------------------------------------------------
; Change 20 to different numbers to create more or less boxes
; -----------------------------------------------------------
; -----------------------------------------------------------
For i=2 to 100
   bID = B2DCreateNewBody( )
   B2DSetBodyPosition( bID, rnd(500)/10, rnd(200)/10 )
   B2DBuildBody( bID )
   sID = B2DCreateNewShape( )
   B2DSetShapeAsBox( sID, 1.0, 1.0 )
   B2DSetShapeFriction( sID, 0.1 )
   B2DSetShapeDensity( sID, 1.0 )
   B2DAddShapetoBody( bID, sID )
   B2DCalculateBodyMass( sID )
   spr = newSprite(0, 0, 2)
   AutoCenterSpriteHandle spr, 1
   SpriteDrawMode spr,2
   B2DLinkSpriteToBody( spr, bID )
Next i



; Main Loop
Do
   CLS 0   
   
   ; Update Box2D World
   t=timer()
   B2DUpdateWorld( wID )
   t=timer()-t

   if T>Max then  Max=t
   
   print FPS()
   print t   
   print max
   

   Sync
Loop

[/pbcode]

Title: Re: Box2D Wrapper
Post by: slayer93 on October 28, 2008, 10:40:38 PM
Yeah, I'm going to look a bit harder at my code over the weekend and see if I can speed this up before I add more features...apparently it seems that right now I'm making myself known to make really slow programs :P.

But after a real quick google just now I happen to come across this - http://www.box2d.org/forum/viewtopic.php?f=8&t=1474 (http://www.box2d.org/forum/viewtopic.php?f=8&t=1474). So I think Box2D shouldn't update every sync but update on a timer.

I think I have to implement this into the wrapper, so I'll do this on the weekend (unfortunately I can't do it now). I'll also look for other things that might be slowing it down.

Hopefully I can get it to simulate 100 bodies easily over the weekend.

Thanks for the test Kevin, it really showed me the problem :)

Edit: Quick Test, this seemed to work well

[pbcode]
; Setup
SetFPS 0
CreateCamera 1
RenderToScreen

; Use Box2D
#include "box2D"

; Create a new world
wID=B2DCreateNewWorld( -100.0, -100.0, 200.0, 200.0, 0.0, 0.5 )
B2DSetWorldStep( wID, 1.0 / 60.0 )
B2DSetWorldVelocity( wID, 1.0 )
B2DSetWorldIterations( wID, 10.0 )
B2DSetWorldMtPRatio( wID, 10.0 )

; Create a ground Body
boxc 0,0,200,10,1,rgb(0,0,120)
getImage 1,0,0,200,10
preparefximage 1

gID = B2DCreateNewBody( )
B2DSetBodyPosition( gID, 15.0, 50.0 )
B2DBuildBody( gID )
sID = B2DCreateNewShape( )
B2DSetShapeAsBox( sID, 10.0, 0.5 )
B2DAddShapetoBody( gID, sID )
gSpr = newSprite( 0, 0, 1 )
AutoCenterSpriteHandle gSpr, 1
B2DLinkSpriteToBody( gSpr, gID )

gID = B2DCreateNewBody( )
B2DSetBodyPosition( gID, 35.0, 45.0 )
B2DBuildBody( gID )
sID = B2DCreateNewShape( )
B2DSetShapeAsBox( sID, 10.0, 0.5 )
B2DAddShapetoBody( gID, sID )
gSpr = newSprite( 0, 0, 1 )
AutoCenterSpriteHandle gSpr, 1
B2DLinkSpriteToBody( gSpr, gID )

; Create Boxes
Boxc 0,0,20,20,1,rgb(100,0,0)
getImage 2,0,0,20,20
preparefximage 2

; -----------------------------------------------------------
; -----------------------------------------------------------
; Change 20 to different numbers to create more or less boxes
; -----------------------------------------------------------
; -----------------------------------------------------------
For i=2 to 100
   bID = B2DCreateNewBody( )
   B2DSetBodyPosition( bID, rnd(500)/10, rnd(200)/10 )
   B2DBuildBody( bID )
   sID = B2DCreateNewShape( )
   B2DSetShapeAsBox( sID, 1.0, 1.0 )
   B2DSetShapeFriction( sID, 0.1 )
   B2DSetShapeDensity( sID, 1.0 )
   B2DAddShapetoBody( bID, sID )
   B2DCalculateBodyMass( sID )
   spr = newSprite(0, 0, 2)
   AutoCenterSpriteHandle spr, 1
   SpriteDrawMode spr,2
   B2DLinkSpriteToBody( spr, bID )
Next i

   t=timer()

; Main Loop
Do
   
   CaptureToScene
   ClsScene
   
   text 0,0,FPS()

   ; Draw the Box2D World
   B2DDrawWorld( wID )
   
   ; Update Box2D World
   if (Timer()-t) > 16
      B2DUpdateWorld( wID )
      t=timer()
   endif
   
   DrawCamera 1
   
; Sync
   Sync
Loop
[/pbcode]
Title: Re: Box2D Wrapper
Post by: kevin on November 11, 2008, 09:56:43 AM

Any progress with this ?

 
Title: Re: Box2D Wrapper
Post by: slayer93 on November 11, 2008, 10:29:45 AM
Yeah, I just want to make it timer based(which should fix the frame rate problems) and rearrange some things so it is a little easier for me. I should be able to show a few things sometime this week.
Title: Re: Box2D Wrapper
Post by: kevin on November 12, 2008, 08:38:25 AM
 Having not really looked at Box2D library,  I'm not sure I follow that, I assumed the simulation was built in.  But anyway,  What I would like to see :) is something up and running prior to the next TGC news letter.    Chop Chop :)

Title: Re: Box2D Wrapper
Post by: slayer93 on November 12, 2008, 08:29:59 PM
QuoteHaving not really looked at Box2D library,  I'm not sure I follow that, I assumed the simulation was build in.

Box2D is handling the simulation, it was my fault that it was running slow.
The reason I think the frame rate was so low was because I used a for/next loop to update Box2D 60 times(internally). So each time you called B2DUpdate it would update the simulation 60 times. Why did I do that? Well for some reason I thought since it had to be updated 60 times for a complete step I should loop through it 60 times to make that complete step. The time step for each world is in Hz per second(frequency it is called), so when I used B2DSetWorldStep and put 1/60 it just said it is at 60 Hz per second. But instead of that I called it 60 times each sync and setting that to 60 FPS basically called it 3600 times each second, no doubt making the FPS dive. But it's a bit strange right now because if I update it once per sync (at 60 FPS) it moves incredibly slow, so I still have to figure that out as I sure it is another silly mistake I am making. Calling it to few times maybe, not sure? Anyway I plan to internally setup a timer based system (according to this article - http://gafferongames.wordpress.com/game-physics/fix-your-timestep/ (http://gafferongames.wordpress.com/game-physics/fix-your-timestep/) -because people seem to agree it is a good way to do it) and it should solve my update problems because it doesn't rely on the frame rate.

Hope I didn't sound like I was spouting nonsense. I'm still far from understanding everything about Box2D still.

QuoteBut anyway,  What I would like to see Smiley is something up and running prior to the next TGC news letter.

I can manage that :)
Title: Re: Box2D Wrapper
Post by: slayer93 on November 14, 2008, 11:18:00 PM
Ok, finally an update!

Sorry it took so long and I only added a few things too :-\. What's new?

- Turn shapes into sensors
- Changed some of the setBody commands to setBodyDef because of new commands
- Added get State commands
- Added filters (allows bodies to collide with one thing but not the other)
- Set and Get Linear and Angular Velocity

This is untested thus far and I plan on testing it while I make my new demo but might as well put it out there for any one who wants to try. New Commands are in the Read-Me.

I was also able to find out why things were so slow and I'm pretty ashamed that I didn't find out sooner. I put the gravity so low(before because it was so fast with 3600 calls), it was at 0.5 and once I put it at 9.8 it was looking a lot better. I also added in a temporary timer system which calls the update command every few milliseconds. I plan to put in a more advanced one later based on the advice from the article I posted earlier but for now it will have to do.

But my plan for now is as follows...

- Create a section on my website for the PlayBasic Box2D Port to host documentation and demos(later on) so you can have it as a reference(will post once ready).
- Make a new demo that shows off Box2D a lot better.
- Fix anything that doesn't work correctly.
- Add Joint Commands(this probably won't be till next week).

Download - [plink]http://teroton.com/PBbox2D/Box2D.rar (http://teroton.com/PBbox2D/Box2D.rar)[/plink]

Here is an updated demo to try with the updated library.

[pbcode]; PROJECT : Box2D Demo
; AUTHOR  : Christian Ang
; CREATED : 10/19/2008
; EDITED  : 11/14/2008
; ---------------------------------------------------------------------

; Setup
SetFPS 0
CreateCamera 1
RenderToScreen

; Use Box2D
#include "box2D"

; Create a new world
wID=B2DCreateNewWorld( -100.0, -100.0, 200.0, 200.0, 0.0, 9.8 )
B2DSetWorldStep( wID, 1.0/60.0 )
B2DSetWorldVelocity( wID, 10.0 )
B2DSetWorldIterations( wID, 10.0 )
B2DSetWorldMtPRatio( wID, 10.0 )

; Create a ground Body
boxc 0,0,200,10,1,rgb(0,0,120)
getImage 1,0,0,200,10
preparefximage 1

gID = B2DCreateNewBody( )
B2DSetBodyDefPosition( gID, 15.0, 50.0 )
B2DBuildBody( gID )
sID = B2DCreateNewShape( )
B2DSetShapeAsBox( sID, 10.0, 0.5 )
B2DAddShapetoBody( gID, sID )
gSpr = newSprite( 0, 0, 1 )
AutoCenterSpriteHandle gSpr, 1
B2DLinkSpriteToBody( gSpr, gID )

gID = B2DCreateNewBody( )
B2DSetBodyDefPosition( gID, 35.0, 45.0 )
B2DBuildBody( gID )
sID = B2DCreateNewShape( )
B2DSetShapeAsBox( sID, 10.0, 0.5 )
B2DAddShapetoBody( gID, sID )
gSpr = newSprite( 0, 0, 1 )
AutoCenterSpriteHandle gSpr, 1
B2DLinkSpriteToBody( gSpr, gID )

; Create Boxes
Boxc 0,0,20,20,1,rgb(100,0,0)
getImage 2,0,0,20,20
preparefximage 2

; -----------------------------------------------------------
; -----------------------------------------------------------
; Change 20 to different numbers to create more or less boxes
; -----------------------------------------------------------
; -----------------------------------------------------------
For i=2 to 100
   bID = B2DCreateNewBody( )
   B2DSetBodyDefPosition( bID, rnd(500)/10, rnd(200)/10 )
   B2DBuildBody( bID )
   sID = B2DCreateNewShape( )
   B2DSetShapeAsBox( sID, 1.0, 1.0 )
   B2DSetShapeFriction( sID, 0.1 )
   B2DSetShapeDensity( sID, 1.0 )
   B2DAddShapetoBody( bID, sID )
   B2DCalculateBodyMass( sID )
   spr = newSprite(0, 0, 2)
   AutoCenterSpriteHandle spr, 1
   SpriteDrawMode spr,2
   B2DLinkSpriteToBody( spr, bID )
Next i

; Main Loop
Do
   
   CaptureToScene
   ClsScene
   
   text 0,0,FPS()

   ; Draw the Box2D World
   B2DDrawWorld( wID )
   
   ; Update Box2D World
   B2DUpdateWorld( wID, 16 )
   DrawCamera 1
   
; Sync
   Sync
Loop
[/pbcode]
Title: Re: Box2D Wrapper
Post by: kevin on November 15, 2008, 12:58:21 AM

Yep, that one works much smoother than before..  Well done ! 

  One concern I is the world creation can really chew through some time.  Ie. if I bump the count 500 it eats over second it create the objects. Any ideas ?
Title: Re: Box2D Wrapper
Post by: slayer93 on November 15, 2008, 01:23:32 AM
It is probably because of my find ID functions because it has to go through an entire array to find the next free id. Which was probably a bad idea to begin with. I'll change those set of functions to not do that and find an ID a quicker way.
Title: Re: Box2D Wrapper
Post by: kevin on November 21, 2008, 11:05:49 PM

  Any luck coming up with a demo ? :)
Title: Re: Box2D Wrapper
Post by: slayer93 on November 22, 2008, 02:16:42 AM
Sorry, not to much has been updated, I can only work on the weekends but I did a bit today, will do more this weekend or at least over Thanksgiving (if I don't pig out and get to lazy :P).

Put some things into the dll internally (finding unused IDs) which actually made creating new things much faster, but I will probably use a more efficient method later on. So no new commands have been put in yet.

Same Download link...

Download - [plink]http://teroton.com/PBbox2D/Box2D.rar (http://teroton.com/PBbox2D/Box2D.rar)[/plink]

and you can also get it as a zip...

Download - [plink]http://teroton.com/PBbox2D/Box2D.zip (http://teroton.com/PBbox2D/Box2D.zip)[/plink]

Here is a demo I quickly made based on an example I saw in the testbed, not super interesting but more interesting than boxes falling.

[pbcode]; PROJECT : Verticle Stack
; AUTHOR  : Christian Ang
; CREATED : 11/21/2008
; EDITED  : 11/22/2008
; ---------------------------------------------------------------------


;
; Setup
SetFPS 0
CreateCamera 1
RenderToScreen

;
; Include the Box2D library for use
#include "box2D"

;
; Create a box2d world to handle all the physics simulation
wID=B2DCreateNewWorld( -100.0, -100.0, 150.0, 150.0, 0.0, 9.8 )
B2DSetWorldStep( wID, 1.0/60.0 )
B2DSetWorldVelocity( wID, 10.0 )
B2DSetWorldIterations( wID, 10.0 )
B2DSetWorldMtPRatio( wID, 10.0 )

;
; Create the ground on which all the bodies will rest on
boxc 0,0,2500,20,1,rgb(0,0,120)
getImage 1,0,0,1500,10
preparefximage 1

gID = B2DCreateNewBody( )
B2DSetBodyDefPosition( gID, 75.0, 60.0 )
B2DBuildBody( gID )
sID = B2DCreateNewShape( )
B2DSetShapeAsBox( sID, 125.0, 0.1 )
B2DAddShapetoBody( gID, sID )
gSpr = newSprite( 0, 0, 1 )
AutoCenterSpriteHandle gSpr, 1
B2DLinkSpriteToBody( gSpr, gID )

;
; Create 8 columns of 10 bodies on top of each other

; Create Boxes
Boxc 0,0,19,19,0,rgb(100,0,0)
getImage 2,0,0,20,20
preparefximage 2

for y=1 to 8
   for x=1 to 10
      bID = B2DCreateNewBody( )
      B2DSetBodyDefPosition( bID, y*8.0, 60.0-x*2.0 )
      B2DBuildBody( bID )
      sID = B2DCreateNewShape( )
      B2DSetShapeAsBox( sID, 1.0, 1.0 )
      B2DSetShapeFriction( sID, 0.1 )
      B2DSetShapeDensity( sID, 1.0 )
      B2DAddShapetoBody( bID, sID )
      B2DCalculateBodyMass( sID )
      spr = newSprite(0, 0, 2)
      AutoCenterSpriteHandle spr, 1
      SpriteDrawMode spr,2
      B2DLinkSpriteToBody( spr, bID )
   next x   
next y

;
; Create a bullet to shoot
Boxc 0,0,19,19,0,rgb(0,200,0)
getImage 3,0,0,20,20
preparefximage 3

bID = B2DCreateNewBody( )
B2DSetBodyDefPosition( bID, -50.0,50.0 )
B2DSetBodyDefSleeping( bID, 1)
B2DBuildBody( bID )
sID = B2DCreateNewShape( )
B2DSetShapeAsBox( sID, 1.0, 1.0 )
B2DSetShapeFriction( sID, 0.1 )
B2DSetShapeDensity( sID, 1.0 )
B2DAddShapetoBody( bID, sID )
B2DCalculateBodyMass( sID )
spr = newSprite(0, 0, 3)
AutoCenterSpriteHandle spr, 1
SpriteDrawMode spr,2
B2DLinkSpriteToBody( spr, bID )


;
; Main Loop
Do
   
   CaptureToScene
   ClsScene
   
   text 0,0,FPS()
   text 0,20,"Verticle Stack Demo - Press Spacekey to launch bullet"
   
   if SpaceKey()=1
      fire=1
      B2DApplyForce( bID, 0.0, 0.0, 0.0, 0.0 )
      firetime=timer()
   endif
   
   if fire=1
      B2DSetBodyLinearVelocity( bID, speed#, 0.0)
      inc speed#
      if timer()-firetime > 500 then fire=0
   endif

   ; Draw the Box2D World
   B2DDrawWorld( wID )
   
   ; Update Box2D World
   B2DUpdateWorld( wID, 16 )
   DrawCamera 1
   
; Sync
   Sync
Loop
[/pbcode]

Edit: Forgot to show a nice little screenshot
Title: Re: Box2D Wrapper
Post by: kevin on November 23, 2008, 04:50:46 AM

   Nice,  I can see this library becoming part of a PB release one day..
Title: Re: Box2D Wrapper
Post by: slayer93 on November 23, 2008, 11:24:50 AM
Thanks, I'd be nice to see this part of PB one day, when it's finished.

Right now I'm adding in Contacts, which give collision information of everything, hopefully I can get it finished today otherwise It'll be finished before the end of the week along with some other things(may create another demo). :)
Title: Re: Box2D Wrapper
Post by: kevin on November 23, 2008, 11:52:18 AM
   Sounds good, the problem will be educating people on it's use.  Which can be lot trickier than what it might sound.  Moreover, I'd be tempted to completely detach it from the sprites, and let the use pick choose how they wish to represent the objects visually.

   Can certainly see there being a huge need for some type character designer to go with it also, wouldn't have to be pretty.. Just so user can get something on screen and get familiar with the terminology .  Including me.   I was thinking about using this in a little retro remake later this week actually. 
Title: Re: Box2D Wrapper
Post by: slayer93 on November 23, 2008, 03:37:50 PM
QuoteSounds good, the problem will be educating people on it's use.  Which can be lot trickier than what it might sound.  Moreover, I'd be tempted to completely detect it from the sprites, andlet the use pick choose how they wish to represent the objects visually.

Yeah, probably the reason why not many people would use this yet is because there are no documentation to show them how to use it, I may work on writing up some docs before I add anything else. But I'm trying to make it as easy to use while also giving as much flexibility as possible.

Quote
   Can certainly see there being a huge need for some type character designer to go with it also, wouldn't have to be pretty.. Just so user can get something on screen and get familiar with the terminology .  Including me.   I was thinking about using this in a little retro remake later this week actually.

Would be interesting to make, also looking forward to your retro remake too ;).
Title: Re: Box2D Wrapper
Post by: kevin on November 23, 2008, 08:26:16 PM
Quote
Yeah, probably the reason why not many people would use this yet is because there are no documentation to show them how to use it, I may work on writing up some docs before I add anything else. But I'm trying to make it as easy to use while also giving as much flexibility as possible.

  Are their any (related such as the Dbpro one) Wiki's perhaps that could be used in the time being ?


QuoteWould be interesting to make, also looking forward to your retro remake too

   Tinkering with the idea of another Shamus remake, but running out of time.   The end of NOV is fast approaching and the news letter stuff will have to done a few days prior to the 30th.   

Title: Re: Box2D Wrapper
Post by: slayer93 on November 23, 2008, 10:12:09 PM
Quote
  Are their any (related such as the Dbpro one) Wiki's perhaps that could be used in the time being ?

The only wiki I know of is the Box2D one but that is mostly on C++, Flash, etc. Nothing really basic but I suppose the general idea is there. Here is a link - http://box2d.org/wiki/index.php?title=Main_Page (http://box2d.org/wiki/index.php?title=Main_Page). Although I may start up a wiki for Box2D on my website. At first I was going to make a few html docs because I'm probably going to write up the help files myself anyway, but maybe it would be easier to just look it up on a wiki and it would be much nicer looking. It doesn't matter to me either way but anyone have any sort of preference?

Quote
   Tinkering with the idea of another Shamus remake, but running out of time.   The end of NOV is fast approaching and the news letter stuff will have to done a few days prior to the 30th.

I'll be happy to help out with a newsletter article of Box2D. I could make a simple demo or two.
Title: Re: Box2D Wrapper
Post by: kevin on November 23, 2008, 10:47:37 PM

QuoteThe only wiki I know of is the Box2D one but that is mostly on C++, Flash, etc. Nothing really basic but I suppose the general idea is there. Here is a link - http://box2d.org/wiki/index.php?title=Main_Page. Although I may start up a wiki for Box2D on my website. At first I was going to make a few html docs because I'm probably going to write up the help files myself anyway, but maybe it would be easier to just look it up on a wiki and it would be much nicer looking. It doesn't matter to me either way but anyone have any sort of preference?

    A wiki would be cool, even although i'm not huge fan of them.  But they're prolly the best way to get such things done quickly though, that's assuming people are willing to help out.  Initially, I feel this is going to be a difficult sell.   People generally tend to sit back and wait, until somebody else does it for them :)

    Yeah.  doc's are a hard one.  The current PB help is in HTML, but it's not created by an editor, they're built with a custom (rough as guts) database tool.   You might be better going down this road yourself, then when we finalize the new PBDOC format, you could convert your data files across to Pb format,  then they'd can be plugged directly into the IDE.


QuoteI'll be happy to help out with a newsletter article of Box2D. I could make a simple demo or two.

    Go for it.  The deadline for the TGC stuff is generally 3 or 4 days before they publish it.  That one (the TGC news letter) is the most pressing thing in the short term,   So if you could slap a few paragraphs together on what it does, where your at. Sorta stuff, I'll just plug this into my monthly dribble.   

Title: Re: Box2D Wrapper
Post by: slayer93 on November 23, 2008, 11:12:55 PM
QuoteA wiki would be cool, even although i'm not huge fan of them.  But they're prolly the best way to get such things done quickly though, that's assuming people are willing to help out.  Initially, I feel this is going to be a difficult sell.   People generally tend to sit back and wait, until somebody else does it for them Smiley

    Yeah.  doc's are a hard one.  The current PB help is in HTML, but it's not created by an editor, they're built with a custom (rough as guts) database tool.   You might be better going down this road yourself, then when we finalize the new PBDOC format, you could convert your data files across to Pb format,  then they'd can be plugged directly into the IDE.

Alright, I'll most likely be making it in html till your new format comes out.

QuoteGo for it.  The deadline for the TGC stuff is generally 3 or 4 days before they publish it.  That one (the TGC news letter) is the most pressing thing in the short term,   So if you could slap a few paragraphs together on what it does, where your at. Sorta stuff, I'll just plug this into my monthly dribble.   

Ok, great I'll send you an pm sometime this weekend then.
Title: Re: Box2D Wrapper
Post by: kevin on December 19, 2008, 10:22:17 PM


so what's the latest news ? :)
Title: Re: Box2D Wrapper
Post by: slayer93 on December 20, 2008, 08:41:25 PM
Ahh, sorry Kevin I haven't been working on this to much. I've been working on another project of mine, but since I have a nice long vacation in front of me I'll be working on this.
Title: Re: Box2D Wrapper
Post by: kevin on December 30, 2008, 06:04:40 AM

    Well,  if you don't have time you should consider releasing the source so somebody else can continue it.

Title: Re: Box2D Wrapper
Post by: slayer93 on December 30, 2008, 03:25:01 PM
Yeah sorry I haven't given any attention to this at all, although I didn't update it I didn't drop the project. But I think it would be better if I did released the source, although I must forewarn I really only started learning C++ this year and self taught so I guess I could of forgotten to do things or the way I did something looks strange.

For anyone wanting to continue, I stopped off on Contacts, it was suppose to add a message into the vector which could be read by PB and then discarded so PB could know about every contact and what to do about contacts. Without it really a lot of the things would be incomplete such as sensors.

I haven't given up on this project though, just made it open-source, I'll update it someday...before the next Ice Age hoepfully... :-\

I've put it on an SVN respository...

[plink]https://svn.xp-dev.com/svn/Slayer93_PBBox2D/trunk/ (https://svn.xp-dev.com/svn/Slayer93_PBBox2D/trunk/) [/plink] (SSL)

[plink]http://svn.xp-dev.com/svn/Slayer93_PBBox2D/trunk/ (http://svn.xp-dev.com/svn/Slayer93_PBBox2D/trunk/) [/plink](No SSL)

Title: Re: Box2D Wrapper
Post by: kevin on January 02, 2009, 11:23:23 AM

Thanks for uploading the code. While  I downloaded it the other day, I haven't really had a good chance to have at it.  What compiler are using BTW ?
Title: Re: Box2D Wrapper
Post by: slayer93 on January 02, 2009, 11:57:12 AM
I've been using Visual C++ Express 2008.
Title: Re: Box2D Wrapper
Post by: slayer93 on February 09, 2009, 08:11:44 PM
Okay, I've started some work on this project. Sorry to everyone that wanted to use it :'(! I unfortunately have to many projects for my own good.

Anyway I figured out the problem with contacts and I found out it was extremely simple, I made a derived class to send the messages but used the original one (doesn't make any sense but even the smallest things get overlooked sometimes). Anyway I'm going to clean up my code and organize it before I release the next update. I'll see if I can get another demo out too.

@Kevin:

Sorry to bother you about a small matter, but PlayBasic doesn't seem to recognize some commands, it has been like that for a while but I just rem it out and moved on. Thought it might be time to figure it out.

If you still have the library on your comp than could you check it out. If you open up the PBBox2D.PBA (links the dll) you'll see some remed out commands and if you check the decorated name of the remed commands in the dll you'll see it is there. I can't figure out if this is something I overlooked or if PlayBasic can't see the command for some reason.

No problem if you don't want to check though. ;)

Thanks Kevin.
Title: Re: Box2D Wrapper
Post by: kevin on February 11, 2009, 05:14:20 AM

works here.
Title: Re: Box2D Wrapper
Post by: slayer93 on February 11, 2009, 03:43:13 PM
Wierd, but I did find out that it wasn't the decorated name it couldn't find but the function name I gave it was something it didn't like  ::).

Any ideas about that?

Edit: I suppose it doesn't matter, adding in an underscore to the front is a quick enough workaround.
Title: Re: Box2D Wrapper
Post by: slayer93 on February 15, 2009, 09:05:12 PM
Release...


I got contacts working (thats about it) but I'll be testing them to make sure they work correctly and add or fix anything I need to. Hopefully I'll get up a demo too. I also fixed some of the commands that didn't work, I don't have time to look them up but just know what ever I remed out before now works.


The commands are in the read me, but I really need to get some documentation on this...

Download Links -
[plink]Box2D.rar (http://teroton.com/PBbox2D/Box2D.rar)[/plink]
[plink]Box2D.zip (http://teroton.com/PBbox2D/Box2D.zip)[/plink]
Title: Re: Box2D Wrapper
Post by: micky4fun on December 21, 2009, 08:19:37 AM
Hi

been looking at this again , see its on wiki , so some help there for me with commands ,
but both the latest links here are dead , is there someware i can download this PB version and if so how do i include the file in PB

mick :)
Title: Re: Box2D Wrapper
Post by: kevin on December 23, 2009, 07:16:59 AM
 
  I might have an old version of it somewhere, although i don't think it's ever been updated anyway   
Title: Re: Box2D Wrapper
Post by: micky4fun on December 23, 2009, 07:41:01 AM
Hi kevin

thanks , its just BlinkOk , as asked me a few times to look at box2d , so i thought i maight have a look again , as first time i didnt have a clue what was happing , but i see on wiki there was some sort of intsructions for it , but i think its proberly beyond me ,

im struggling again with normal PB let a lone any add on's

im hoping you maybe how to help me as ive been on a bit of my game for 3days now and can not get it to work , tried a number of ways
so ile post link in compo forum maybe you can point me in the right direction or tell me why the code is wrong , ile post in next 5 mins

thanks
mick ;D
Title: Re: Box2D Wrapper
Post by: kevin on December 23, 2009, 08:12:11 PM
Box 2D Library

 Attached are a couple of versions of the Box2D wrapper,  the newest file i've got is from September 2009 (download date).   As for what state it's in, I've absolutely no idea...  


Downloads