So I’ve got an answer, but I’m not sure why it works. Thanks to roaminggamer for starting me off on the right track!
If you start the triangle at the origin, and you make the points clockwise, the collisions seem to work correctly.
If you offset the same triangle by 10,10 in x, y, the collision shape seems offset from the original shape by that much, but works.
If you define the triangle points counterclockwise, the behaviour is wonky and undefinied.
I thought maybe the reference points for the object were being re-defined by physics.addBody(), but as you can see on your terminal, they remain unchanged from before to after it is called.
So it looks like maybe you *can* define poly shapes with display.addLine/display.append, but they have to be clockwise, and they have start at the origin. You can then move them somehwhere else. Here’s the complete code:
–
– main.lua
– include Corona’s “physics” library
local physics = require “physics”
physics.setDrawMode( “hybrid” ) – see how the physic engine sees objects
physics.start();
–physics.pause()
– forward declarations and other locals
local screenW, screenH, halfW = display.contentWidth, display.contentHeight, display.contentWidth*0.5
– BEGINNING OF YOUR IMPLEMENTATION
– triangle starting (not centered) at the origin seems correctly aligned with collision shape shape
local centeredTri = display.newLine( 0,0, 50,0)
centeredTri:append (50,50, 0,0)
centeredTri:setColor( 0, 255, 0, 255 )
centeredTri.x = 50
– centeredTri.y = 100
print (“NewRun”)
print ("centeredTri.xReference: "…centeredTri.xReference)
print ("centeredTri.yReference: "…centeredTri.yReference)
centeredTriShape = { 0,0, 50,0, 50,50} – First and Last vertex should be different - docs
physics.addBody( centeredTri, { density=1.0, friction=0.3, bounce=.1, shape=centeredTriShape })
print ("centeredTri.xReference: "…centeredTri.xReference)
print ("centeredTri.yReference: "…centeredTri.yReference)
– print centeredTriShape.yReference
– triangle not starting at the origin ends up offset from collision shape
local uncenteredTri = display.newLine( 10,10, 60,10)
uncenteredTri:append (60,60, 10,10)
uncenteredTri:setColor( 0, 255, 0, 255 )
uncenteredTri.x = 125
print ("uncenteredTri.xReference: "…uncenteredTri.xReference)
print ("uncenteredTri.yReference: "…uncenteredTri.yReference)
uncenteredTriShape = { 10,10, 60,10, 60,60} – First and Last vertex should be different - docs
physics.addBody( uncenteredTri, { density=1.0, friction=0.3, bounce=.1, shape=uncenteredTriShape })
print ("uncenteredTri.xReference: "…uncenteredTri.xReference)
print ("uncenteredTri.yReference: "…uncenteredTri.yReference)
– I tried altering the reference point below to see if I could correct the offset, but that seems to
– …mess up the collision detection even more. I don’t understand how reference points work yet, though,
– …so there may still be a solution here.
– uncenteredTri:setReferencePoint(uncenteredTri.x+10 , uncenteredTri.y+10)
– triangle with counterclockwise points seems to overlap,but has wonky collision behavior
– “The shape coordinates must be defined in clockwise order, and the resulting shape must be convex-only. The
– first and last vertex must be different.”
– http://docs.coronalabs.com/api/library/physics/addBody.html
local counterclockwiseTri = display.newLine( 0,0, 0,50)
counterclockwiseTri:append (50,50, 0,0)
counterclockwiseTri:setColor( 255, 0, 0, 255 )
counterclockwiseTri.x = 200
print ("counterclockwiseTri.xReference: "…counterclockwiseTri.xReference)
print ("counterclockwiseTri.yReference: "…counterclockwiseTri.yReference)
counterclockwiseTriShape = { 0,0, 0,50, 50,50} – First and Last vertex should be different - docs
physics.addBody( counterclockwiseTri, { density=1.0, friction=0.3, bounce=.1, shape=counterclockwiseTriShape })
print ("counterclockwiseTri.xReference: "…counterclockwiseTri.xReference)
print ("counterclockwiseTri.yReference: "…counterclockwiseTri.yReference)
– Make a box to Contain our Enthusiasm
local bouncebox = display.newRect( 0,0, screenW, 82 )
bouncebox:setReferencePoint( display.BottomLeftReferencePoint )
bouncebox.x, bouncebox.y = 0, display.contentHeight
physics.addBody( bouncebox, “static”, { friction=0.3} )
local bounceboxleft = display.newRect( 0,0, 5, screenH)
bounceboxleft:setReferencePoint( display.BottomLeftReferencePoint )
bounceboxleft.x, bounceboxleft.y = 0, display.contentHeight
physics.addBody( bounceboxleft, “static”, { friction=0.3} )
local bounceboxright = display.newRect( 0,0, 5, screenH )
bounceboxright:setReferencePoint( display.BottomLeftReferencePoint )
bounceboxright.x, bounceboxright.y = display.contentWidth-5, display.contentHeight
physics.addBody( bounceboxright, “static”, { friction=0.3} )
for loop = 1 , 50, 1 do – make a bouncyBall (off-screen), position it, and rotate slightly
local bouncyBall = display.newCircle( 90, 90, 10 )
bouncyBall.x, bouncyBall.y = 160+math.random(0,50) , -100 bouncyBall.rotation = 15
physics.addBody( bouncyBall, { density=1.0, friction=0.3, bounce=0.9, radius = 10} )
end