Box2D 3.1 Ported to Solar2D for iOS and Android

We have successfully ported Box2D 3.1 to Solar2D, which now supports iOS, Android, osx, windows. Compared to Box2D v2, Box2D 3.1 has seen a significant performance improvement.

You can download and test it here: Box2D 3.1 for Solar2D

We hope to merge this version into the official release, but we are currently unsure of how to proceed to allow Box2D v2 and Box2D v3 to coexist.

13 Likes

Great! The result is spectacular. I wasnā€™t familiar with Box2D, but its contribution to physics-based games is impressive.

Iā€™ve been reading the Box2D documentation and noticed this warning:

ā€˜Caution: Box2D is tuned for MKS units. Keep the size of moving objects larger than 1cm. Youā€™ll need to use some scaling system when you render your environment and actors. The Box2D samples application does this by using an OpenGL viewport transform. Do not use pixel units unless you understand the implications.ā€™

How does this warning affect the adaptation you made?

Wow! The difference is amazing.

About co-existing, maybe the same approach with the Graphics v1 compatibility can be taken. Default Box2D version would be the newer one and the older one can be used in that compatibility mode. After a year, it can be deprecated completely.

This looks amazing. Can you please provide a few more details on what I need to do to try this out. The github page has a number of assets and Iā€™m not sure if I need to download them all and then how to install them (ideally without uninstalling my current solar2d installation).

Oh wow, Iā€™m amazed at how much better the performance is.

Thanks a lot for your work.

It has improved performance a lot.

It seems that preCollision and postCollision have been removed, is there an alternative way for me to get Physics Contact?

To enable the ā€˜preCollisionā€™ event, you need to manually call the API ā€˜body:setPreSolveEventsEnabled(true)ā€™.
And Disable the specific collision by return false in preCollisionEvent.

local platform = display.newRect( 0, 0, 280, 30 )
platform.x, platform.y = display.contentCenterX, display.contentCenterY+80
platform.collType = "passthru"
physics.addBody( platform, "static", { bounce=0.0, friction=0.3 } )
 
local function preCollisionEvent( self, event )
   local collideObject = event.other
   if ( collideObject.collType == "passthru" ) then
      -- event.contact.isEnabled = false -- box2d v2
      return false -- box2d v3
   end
   return true
end
 
local ball = display.newCircle( display.contentCenterX, display.contentCenterY-40, 15 )
physics.addBody( ball, "dynamic", { bounce=0.0, radius=20 } )
 
ball.preCollision = preCollisionEvent
body:setPreSolveEventsEnabled(true)
ball:addEventListener( "preCollision", ball )

Weā€™re replacing ā€˜postCollisionā€™ with ā€˜hitCollisionā€™.
You can get detail from this doc.
And also you need to manually call the API ā€˜body:setPreSolveEventsEnabled(true)ā€™ to enable this event.

local function hitCollisionEvent( self, event )
	if e.approachSpeed > 10 then
		-- do something
	else
		-- ..
	end
end

local ball = display.newCircle( display.contentCenterX, display.contentCenterY-40, 15 )
physics.addBody( ball, "dynamic", { bounce=0.0, radius=20 } )

ball.hitCollision = hitCollisionEvent
body:setHitEventsEnabled(true)
ball:addEventListener( "hitCollision", ball )
1 Like
  • For macOS users: Download the DMG file, open it, and drag the app folder into the Applications folder. This will not interfere with your current installation.
  • For Windows users: Download the MSI file. Iā€™m unsure if changing the installation destination will prevent uninstallation.
1 Like

Solar2d use physics.setScale( value ) for scaling.
You can find some information in this document.

1 Like

Got it!
Thank you very much!

HTML5 supported!

5 Likes

Fantastic! Would you be open to merging your Emscripten port into our repo?

Yeah, of course!
I will create a PR soon, when I have free time.

Hi,
Thank you very much for sharing this!

Iā€™m testing it, and Iā€™m having some difficulties. Should I enable something or edit anything that works differently from the old version?

Iā€™ve noticed two major issues so far:

  1. Something seems wrong with physics filters and raycasting (it looks like some maskBits arenā€™t being hit)

  2. dynamic bodies can slowly pass through static bodies if I move them by changing x/y cordinates (maybe I need to set a specific scale or something? Iā€™m using the default settings)

For your first problem, yes, there is a filter parameter in v3 raycasting. The default rayCast filter is {categoryBits = 0x0001, maskBits = 0xFFFF} (actually v3 supports 64-bit category and mask bits). So if your bodyā€™s maskBits do not include category 0x0001, it will not have a raycast result. You can find more details in this document.

In the next update, we will change the default filter to cast everything by default and expose the filter parameter to the Solar2D API as physic.rayCast(fromX, fromY, toX, toY, behavior, filter).

For your second issue, I found some information in box2d v3 doc.

Caution:
Generally you should not set the transform on bodies after creation.
Box2D treats this as a teleport and may result in undesirable behavior.

Okay, it makes sense. Thank you!

So there are small breaking challenges, and I need to change some approaches.
Itā€™s fine, but since Iā€™m working on a new project Iā€™m a bit worried about timing.

Should I use V3 since it will be added to Solar2D ā€œsoonā€ or should I stick to V2 because this transition will happen slowly?
(Iā€™d start using V3 now personally)

There are no indications that it will be added to Solar2D soon. If your project does not have high requirements for physics performance, it is recommended to stick with the official V2.
Some of our apps, such as Labo Mechanical Studio and Labo Marble Ball Race, have high requirements for physics performance, and we will replace Box2D v2 with v3 in the future.

3 Likes