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.

12 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!