Bug on physics shape, crash Corona SDK!

In the new assemblies Corona SDK there is an error has crashes that leads to an simulator crashes.

Check the code:

require "physics" physics.start() physics.setDrawMode("hybrid") lines = display.newLine(100, 100, 200, 200) lines:setColor(255, 255, 255) lines.width = 5 physics.addBody(lines, "static", { shape = {0,0,100,100,0,0} } ) 

For example in the build 1142 code works, in new builds leads to an simulator crashes.

Please correct this error.

You have created a completely flat object. It is not even 2-dimensional. A line which is 5 pixels wide renders as a rectangle, so you need to create a rectangular physics body to be accurate. Creating a 1-dimensional physics body will definitely crash Box2D. Do this:

local rect = display.newRect( 0, 0, 100, 5 ) rect.rotation = 45 rect.x, rect.y = 150, 150 physics.addBody( rect, "static" )

That’s just off the top of my head, but it should create a thin rectangle, angled at 45 degrees, with a static physics body. This is what I think you’re trying to do with your line.

But in the old assembly it worked. In our game so draw the lines of with physics, this is the most simple solution.

Maybe it did, but it should not have. The official Box2D documentation states that because Box2D is a real world simulation objects must have some dimension to them. A triangle with 2 points at the same location is not a triangle and as such is not a 2D object, therefore it breaks down. I suspect that even in the old build it would have crashed eventually.

Not everything worked. how then do make physics for the line knowing the coordinates of the two dots?

And, this code does not work:

require "physics" physics.start() physics.setDrawMode("hybrid") lines = display.newLine(100, 100, 200, 200) lines:setColor(255, 255, 255) lines.width = 5 physics.addBody(lines, "static", { shape = {2,20,10,50,12,5} } )

Although the in old version is created  the physics triangle.

As Matt has alluded to, you can’y/shouldn’t add a physics body to a line display object.

As the Docs outline:

NOTE: You should not create physics bodies from objects created using display.newLine().

http://docs.coronalabs.com/api/library/physics/addBody.html

Your code works for me, but it does not create a body around the line because you have not specified a physics shape which encompasses the line. You really should just create a rectangle and apply a body to it because Corona will work out its size automatically. This code works for me:

require "physics" physics.start() physics.setDrawMode("hybrid") local rect = display.newRect( 0, 0, 100, 5 ) rect.rotation = 45 rect.x, rect.y = 150, 150 physics.addBody( rect, "static" )

Yes, but it cannot be done do to draw the line with physics draw a mouse.

In this code you need to know two coordinates, and it works in the old version. Lines with the physics so can draw a mouse.

Here’s an example:

local physics = require "physics" physics.start() physics.setDrawMode( "hybrid" ) local lines = {} local lineGroup = display.newGroup() local prevX,prevY local isDrawing = false local i = 0 local x1 = 0 local x2 = 0 local y1 = 0 local y2 = 0 local function distanceBetween(x1, y1, x2, y2) local dist\_x = x2 - x1 local dist\_y = y2 - y1 local distanceBetween = math.sqrt((dist\_x\*dist\_x) + (dist\_y\*dist\_y)) return distanceBetween end local function drawLine(e) if(e.phase == "began") then prevX = e.x prevY = e.y isDrawing = true i = i + 1 print(i) elseif(e.phase == "moved") then local distance = distanceBetween(prevX, prevY, e.x, e.y) if(isDrawing and distance \< 100) then if(lines[i]) then lineGroup:remove(lines[i]) end lines[i] = display.newLine(prevX, prevY, e.x, e.y) lines[i]:setColor(255, 255, 0) lines[i].width = 5 local dist\_x = e.x - prevX local dist\_y = e.y - prevY physics.addBody(lines[i], "static", { density = 1, friction = 0.5, bounce = 1.6, shape = {0, 0, dist\_x, dist\_y, 0, 0} } ) lineGroup:insert(lines[i]) end elseif(e.phase == "ended") then isDrawing = false end end Runtime:addEventListener("touch",drawLine)

Here is how to draw a 5px wide line with a touch listener and turn it into a physics object using a rectangle:

require("physics")physics.start() physics.setDrawMode("hybrid") function lengthOf( a, b ) &nbsp; &nbsp; local width, height = b.x-a.x, b.y-a.y &nbsp; &nbsp; return (width\*width + height\*height)^0.5 end local PI = (4\*math.atan(1)) local quickPI = 180 / PI function angleOf( a, b ) &nbsp;&nbsp;&nbsp;&nbsp;return math.atan2( b.y - a.y, b.x - a.x ) \* quickPI end function touch(e) &nbsp; &nbsp; if (e.phase == "began") then &nbsp; &nbsp; &nbsp; &nbsp; local grp = display.newGroup() &nbsp; &nbsp; &nbsp; &nbsp; grp.rect = display.newRect(grp,e.x,e.y,1,1) &nbsp; &nbsp; &nbsp; &nbsp; display.getCurrentStage():setFocus(grp) &nbsp; &nbsp; &nbsp; &nbsp; grp.hasFocus = true &nbsp; &nbsp; &nbsp; &nbsp; grp.start = {x=e.xStart,y=e.yStart} &nbsp; &nbsp; &nbsp; &nbsp; display.getCurrentStage():setFocus(grp) &nbsp; &nbsp; &nbsp; &nbsp; grp:addEventListener("touch",touch) &nbsp; &nbsp; &nbsp; &nbsp; return true &nbsp; &nbsp; elseif (e.target.hasFocus) then &nbsp; &nbsp; &nbsp; &nbsp; local grp = e.target &nbsp; &nbsp; &nbsp; &nbsp; grp.rect:removeSelf() &nbsp; &nbsp; &nbsp; &nbsp; grp.rect = display.newRect( 0, 0, lengthOf( grp.start, e ), 5 ) &nbsp; &nbsp; &nbsp; &nbsp; grp.rect.rotation = angleOf( grp.start, e ) &nbsp; &nbsp; &nbsp; &nbsp; grp.rect.x, grp.rect.y = (grp.start.x+e.x)/2, (grp.start.y+e.y)/2 &nbsp; &nbsp; &nbsp; &nbsp; if (e.phase ~= "moved") then &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; display.getCurrentStage():setFocus(nil) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; grp.hasFocus = false &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; grp:removeEventListener("touch",touch) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; physics.addBody(grp.rect,"static") &nbsp; &nbsp; &nbsp; &nbsp; end &nbsp; &nbsp; &nbsp; &nbsp; return true &nbsp; &nbsp; end &nbsp; &nbsp; return false end Runtime:addEventListener("touch",touch)

horacebury

This code does not work. 

You should find that it does now.

Easier to make a new build as it was before. Corona developers please fix the bug, we do not get our game in the new build program.

What bug?

horacebury,  bug about which I wrote at the beginning of.

As SegaBoy and I have explained, this is not a bug, it is a problem with the way you are building your physics object. If you really believe there is a bug, collect the evidence you have and submit it to the bug reporting system:

http://developer.coronalabs.com/content/bug-submission

I went back through the daily builds to 1142 and there have been three physics related changes that were tagged as such:

1143: Core: Add physics.setDebugErrorsEnabled() for extra run-time debugging information in Box2D.

1147: Core: Fix the physics debug drawing scale.

1178: Core: Enable physics.setDebugErrorsEnabled() by default (Mac simulator only).

It sounds to me like we turned on more error detection and perhaps your app was erroring and you did not know it and now we explicitly tell you that this is not allowed. 

If you need this behavior, you can still build with 1142, but as the folks above posted, you should not use lines for this, or adapt your physics body to be a rectangle. 

Rob Miracle,  Thank you.
Possibly do to physics for the line was made automatically correct just such a code? It would be great.

require "physics" physics.start() physics.setDrawMode("hybrid") lines = display.newLine(100, 100, 200, 200) lines:setColor(255, 255, 255) lines.width = 5 physics.addBody(lines, "static" )

You can go to http://feedback.coronalabs.com and put in a feature request for this.  I also don’t know if the Box2D group has a feature request system.  This isn’t so much a Corona SDK request as it is a Box2D.