Hello. I’m developing physics based game and have problem with rectangular dynamic body.
[media]https://www.youtube.com/watch?v=AHK1hGw2FbI[/media]
Here I’m creating world objects
for i = 1, #layers do local layer = layers[i] if layer.type == "objectgroup" then for j = 1, #layer.objects do local object = layer.objects[j] if object.polygon then local backgrougdPointsLenght = #backgroundPoints local points = { } local isBig = true for k = 1, #object.polygon - 1 do isBig = isBig and Vector.len( object.polygon[k].x - object.polygon[k + 1].x, object.polygon[k].y - object.polygon[k + 1].y ) \> 5 end isBig = isBig and Vector.len( object.polygon[1].x - object.polygon[#object.polygon].x, object.polygon[1].y - object.polygon[#object.polygon].y ) \> 5 local clockwiseSum = 0 for k = 1, #object.polygon - 1 do clockwiseSum = clockwiseSum + (object.polygon[k + 1].x - object.polygon[k].x) \* (object.polygon[k + 1].y + object.polygon[k].x) end clockwiseSum = clockwiseSum + (object.polygon[1].x - object.polygon[#object.polygon].x) \* (object.polygon[1].y + object.polygon[#object.polygon].x) local minX = (object.polygon[1].x + object.x) \* mapScale local minY = (object.polygon[1].y + object.y) \* mapScale local maxX = (object.polygon[1].x + object.x) \* mapScale local maxY = (object.polygon[1].y + object.y) \* mapScale for k = 1, #object.polygon do local pointX, pointY = (object.polygon[k].x + object.x) \* mapScale, (object.polygon[k].y + object.y) \* mapScale if isBig then backgroundPoints[#backgroundPoints + 1] = Point(pointX, pointY) end points[2 \* k - 1] = pointX points[2 \* k] = pointY minX = mathMin( minX, pointX ) minY = mathMin( minY, pointY ) maxX = mathMax( maxX, pointX ) maxY = mathMax( maxY, pointY ) end -------------------------------- creating polygon static objects local polygonObject = display.newPolygon( map, (minX + maxX) \* .5, (minY + maxY) \* .5, points ) physics.addBody( polygonObject, "static", { density = 1, friction = 1 } ) ---------------------------------------------------------------------------- local polygon = Polygon(unpack(points)) local polygonTriangles = polygon:splitConvex() for k = 1, #polygonTriangles do local centerX, centerY = polygonTriangles[k]:getCenter() local trianglesLenght = #triangles for l = 1, #polygonTriangles[k].vertices - 1 do triangles[trianglesLenght + l] = Triangle(Point(polygonTriangles[k].vertices[l]:unpack()), Point(polygonTriangles[k].vertices[l + 1]:unpack()), Point(centerX, centerY)) triangles[trianglesLenght + l].p1.z = backgroundDepth() triangles[trianglesLenght + l].p2.z = backgroundDepth() triangles[trianglesLenght + l].p3.z = foregroundDepth() triangles[trianglesLenght + l].type = object.type end triangles[trianglesLenght + #polygonTriangles[k].vertices] = Triangle(Point(polygonTriangles[k].vertices[1]:unpack()), Point(polygonTriangles[k].vertices[#polygonTriangles[k].vertices]:unpack()), Point(centerX, centerY)) triangles[trianglesLenght + #polygonTriangles[k].vertices].p1.z = backgroundDepth() triangles[trianglesLenght + #polygonTriangles[k].vertices].p2.z = backgroundDepth() triangles[trianglesLenght + #polygonTriangles[k].vertices].p3.z = foregroundDepth() triangles[trianglesLenght + #polygonTriangles[k].vertices].type = object.type end elseif object.type == "piece" then if not gameManager.worlds[gameManager.choosedWorld].pieceCollected[pieceIndex] then pieceItems[#pieceItems + 1] = { x = object.x \* mapScale, y = object.y \* mapScale, index = pieceIndex } end pieceIndex = pieceIndex + 1 elseif object.type == "start" then playerStartX, playerStartY = object.x \* mapScale, object.y \* mapScale elseif object.type == "exit" then exitX, exitY = object.x \* mapScale, object.y \* mapScale end end end end
And creating player
player = display.newImageRect( map, gameManager.levelImageSheet, gameManager.levelSheetInfo:getFrameIndex( "heroes/cube/cube\_base" ), playerRadius, playerRadius ) gameManager.playerObject = player player.xScale = 4.5 player.yScale = 4.5 physics.addBody( player, "dynamic", { density = 1, friction = 1, bounce = 0, shape = { -playerRadius, -playerRadius, playerRadius, -playerRadius, playerRadius, playerRadius, -playerRadius, playerRadius } } )
I have no idea what I’m doing wrong
Interesting that player with circular shape works great without bugs