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.
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?
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).
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 )
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.
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:
Something seems wrong with physics filters and raycasting (it looks like some maskBits arenāt being hit)
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.
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.