Problem with polygon based physic body position

Hi all!

i’m trying create a body based on polygon.

-- main.lua display.setStatusBar(display.HiddenStatusBar) local physics = require("physics") physics.start() physics.setGravity(0, 10) physics.setDrawMode("hybrid") display.setDefault( "anchorX", 0 ) display.setDefault( "anchorY", 0 ) local vertices = { 0,0,200,0,250,50,200,100,100,150,0,100 } local o = display.newPolygon( 0, 300, vertices ) o.x=0 o.y=300   o.strokeWidth = 2 o:setStrokeColor( 0, 255, 255 )   physics.addBody(o, "static", {friction=0.0, bounce=0.4})  

but i see wrong physic body starting from the center of polygon.

0cba051886362be79e1336895c820662c54439b2

and the second issue is polygon has wrong Y coordinate.

i’m using last public release 2013.2076.

This looks like a bug.  Can you file a bug report using the “Report a bug” link above.  The code above will suffice as the demo project needed.  This way the engineer’s can take a look at it.

Thanks

Rob

Thank you Rob. I reported.

Can you post the bug ID back here for later reference.

Thanks

Rob

where can i find my bug ID?

It should have been in the email you got after filing it.  It would have also been on the last screen after submitting the bug I think.

Its 28033 for reference. 

Thanks

Rob

Confirm that it is a serious mistake. Even if in the example with a star, turn on physics, it will be seen that it does not correspond to objects. Please correct.

And another bug: WARNING: Polygon could not be generated. The polygon outline is invalid, possibly due to holes or self-intersection.

Should be created because the physical form is created.

@GamingStudio17 can you produce an example that creates this problem?

Thanks

Rob

Yeah, that’s a standard example with the addition of physics http://docs.coronalabs.com/api/library/display/newPolygon.html

local physics = require("physics") physics.start() physics.setDrawMode("hybrid") local halfW = display.contentWidth\*0.5; local halfH = display.contentHeight\*0.5; local vertices = { 0,-110, 27,-35, 105,-35, 43,16, 65,90, 0,45, -65,90, -43,15, -105,-35, -27,-35, } local o = display.newPolygon( halfW, halfH, vertices ) physics.addBody(o, "static", { density = 0.1, friction = 0.8, bounce = 0} ) o.strokeWidth = 10 o:setStrokeColor( 0, 255, 255 )

Thanks

Anton

What version of Corona SDK are you using?  This does not seem to be a problem in 2087.

Rob

Corona SDK version 2088, the same mistake about whom wrote rustam.

You can use the code

display.setStatusBar(display.HiddenStatusBar) local physics = require("physics") physics.start() physics.setGravity(0, 10) physics.setDrawMode("hybrid") display.setDefault( "anchorX", 0 ) display.setDefault( "anchorY", 0 ) local vertices = { 0,0,200,0,250,50,200,100,100,150,0,100 } local o = display.newPolygon( 0, 300, vertices ) o.x=0 o.y=300 o.strokeWidth = 2 o:setStrokeColor( 0, 255, 255 ) physics.addBody(o, "static", {friction=0.0, bounce=0.4})

There is a filed bug report on it. 

Rob

Thank you for a fix, but not until the end. Object still moves if changing its shape. Object must stand still.

here is the code demonstrates the bug:

display.setStatusBar(display.HiddenStatusBar) local physics = require("physics") physics.start() physics.setGravity(0, 10) physics.setDrawMode("hybrid") display.setDefault( "anchorX", 0 ) display.setDefault( "anchorY", 0 ) local vertices = { 0,0,200,0,250,50,200,100,100,150,0,100 } local o = display.newPolygon( 0, 300, vertices ) o.x=100 o.y=300 physics.addBody(o, "static", {friction=0.0, bounce=0.4}) -- touch polygon local function touch\_polygon(e) if(e.phase == "moved") then display.remove(o) local xx = e.x-o.x local yy = e.y-o.y local vertices = { 0,0,xx,yy,250,50,200,100,100,150,0,100 } o = display.newPolygon( 0, 300, vertices ) o.x=100 o.y=300 physics.addBody(o, "static", {friction=0.0, bounce=0.4}) end end Runtime:addEventListener("touch", touch\_polygon)

I don’t know that this is a bug.  As you resize the polygon, the bounding box of the polygon changes because it’s center mass has changed.  And of course if you drag it enough, eventually the vertices are not in clockwise order at which points the results are undefined. 

Rob

There was also a check in for 2090 regarding shapes and centering, but I don’t think it changes this behavior, which seems to be correct.

Rob

Need to add the ability to disable re-centered about the center of the polygon. Because of this, it is impossible to draw a polygon. Because I do not know where is the center of the object. Without the auto centering, center can assign the first point and then will add a polygon in the right place.

What are you trying to accomplish? 

I’m trying to draw a polygon in real time on the points. From the fact that I do not know where the center, I can not properly draw a polygon.
Here is an example of what I want to do. Set points and closes the figure and it is drawn.
 

display.setStatusBar(display.HiddenStatusBar) local physics = require("physics") physics.start() physics.setDrawMode("hybrid") local n\_point = 0 local tohk = {} function ptInside(obj, x, y) local inside = false local objx = obj.x - (obj.width / 2) local objy = obj.y - (obj.height / 2) if (x \>= objx) and (x \<= (objx + obj.width)) and (y \>= objy) and (y \<= (objy + obj.height)) then inside = true end return inside end -- Touch local function onTouch(e) if(e.phase == "began") then startX = e.x startY = e.y bloc\_add = display.newLine( startX,startY, startX,startY ) if n\_point ~= 0 then startX = save\_x startY = save\_y end tohk[n\_point] = display.newCircle( startX, startY, 10 ) tohk[n\_point]:setFillColor(255,0,0) elseif(e.phase == "moved") then display.remove(bloc\_add) bloc\_add = display.newLine( startX,startY, e.x,e.y ) bloc\_add.strokeWidth = 3 if ptInside(tohk[0], e.x, e.y) then tohk[0]:setFillColor(0,255,0) else tohk[0]:setFillColor(255,0,0) end elseif(e.phase == "ended") then bloc\_add = display.newLine( startX,startY, e.x,e.y ) bloc\_add.strokeWidth = 3 save\_x = e.x save\_y = e.y n\_point = n\_point + 1 if ptInside(tohk[0], e.x, e.y) then local vertices = {} local n = 1 for i=0,n\_point-1 do vertices[n] = tohk[i].x-tohk[0].x print(n,vertices[n].." i "..tohk[i].x) n = n + 1 vertices[n] = tohk[i].y-tohk[0].y vertices[2] = -90 print(n,vertices[n]) n = n + 1 end local new\_x = tohk[0].x local new\_y = tohk[0].y o = display.newPolygon( new\_x, new\_y, vertices ) physics.addBody(o, "static", {friction=0.0, bounce=0.4}) end end end Runtime:addEventListener("touch", onTouch)

Hi @GamingStudio17,

I’m jumping into this thread a bit late, so let me get caught up. You basically want to draw a polygon in real time, and then accurately trace a physics body around it? But since it’s unknown where the “center” of that object will be, you can’t build the body correctly?

By “real time”, do you mean, the physics body must be constantly updated (effectively removed and re-created) on each phase of movement? Or can the application of the body occur after the user “finalizes” the drawing of the polygon?

Brent