@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
Brent , after the user “finalizes” the drawing of the polygon.
Brent correct me if I’m wrong, but physics polygon shapes have to be convex. So you would need to make sure all your points are convex or you would have to have multiple physics bodies. If you have a list of your vertices, you should be able to create a physics shape that works out for you instead of depending on physics to auto-shape it.
Rob
Well, how to make that a polygon created correctly?
I haven’t run this actual code, but @horacebury (Corona utility guru ) has created some modules which may do exactly what you need:
http://developer.coronalabs.com/code/shape-creation-use-addbody
http://developer.coronalabs.com/code/simple-polygon-triangulation
And yes, custom physics shapes must be convex at all point bends, no exceptions.
Brent
Brent , it will not work with polygon because I did not know where his center. This will only work with a shape property. Shape of the object relative to the center does not shift automatically as opposed to the type of object poligon.
The only thing you can do is to disable the automatic alignment of form object type poligon, or how to calculate the coordinates of the center of that object.
Thanks
Hi @GamingStudio17,
Can you gather the points of the drawn polygon, calculate the center using center-finding math on an irregular polygon, and then shift those vertices relative to the new center point?
Brent, no
Hi @GamingStudio17,
Can you explain in more detail why not? If you look at the attached diagram (theoretical use case), the blue triangle would be the one that the user “draws”. The center point would be approximately where the blue “X” is. But, of course, when you draw the bounding rectangle around the object (pink rectangle), the center point of that bounding rectangle is at a different point compared to the triangle. However, if you calculate the actual center point of the blue triangle using the proper math, you know the relation between that point (in coordinates) versus the center point of the bounding rectangle. So, you can use that offset X/Y on each of the corner points of the physics shape, which should put the polygon body correctly oriented around the object.
Brent