Polygon physics bodies

Why Can’t we set the mass of Polygon physics bodies. Like in this code, codea crashes if you run it after uncommenting the fifth line.

function setup()
    Mbody=physics.body(POLYGON,vec2(0,0),vec2(10,0),vec2(10,50),vec2(0,50))
    Mbody.x=WIDTH/2
    Mbody.y=HEIGHT/2
    --Mbody.mass=1
end

@Saurabh, most likely has something to do with your vertices. Mass is allowed on the default example polygon represented by:

physics.body(POLYGON,vec2(-10,10),vec2(-10,-10),vec2(10,-10),vec2(10,10))

Edit: your same shape dimension, but written with 0,0 remaining in the middle of the polygon instead of being one of the corners does work with mass.

physics.body(POLYGON,vec2(-5,-25),vec2(5,-25),vec2(5,25),vec2(-5,25))

Try with density and gravityScale;
http://twolivesleft.com/Codea/Reference/Physics.html#physics.body

Thanks @Slashin8r and @juaxix. But why doesn’t it work with 0,0 just shifted the vertices, right??

@Saurabh, I’m not exactly sure how mass is causing these crashes, and after messing around with many vertex combinations I’m even more confused on what would be causing these crashes. It doesn’t look like 0,0 is significant to mass in any way. I’ve been able to get it to run using all negative vertices and all positive vertices. It seems like it doesn’t like 0,0 being on an edge or close to an edge. Whenever I make 0,0 within a few coordinates of the edge, it crashes.

Weird…

One more question regarding physics bodies. Try this code out.

function setup()
    edge=physics.body(EDGE,vec2(0,0),vec2(0,HEIGHT/2))
    edge.x=WIDTH/2
    edge.y=HEIGHT/2
    edge.type=KINEMATIC
    edge.linearVelocity=vec2(0,-20)
    ball=physics.body(CIRCLE,20)
    ball.x=WIDTH/2
    ball.y=HEIGHT/2-50
    floor=physics.body(EDGE,vec2(0,0),vec2(WIDTH,0))
    floor.x=0
    floor.y=HEIGHT/2-100
    floor.type=KINEMATIC
end

function draw()
    background(0, 0, 0, 255)
    stroke(255, 255, 255, 255)
    fill(255, 255, 255, 255)
    strokeWidth(4)
    line()
    
    ellipse(ball.x,ball.y,40)
    line(edge.x,edge.y,edge.x,edge.y+HEIGHT/2)
    line(0,HEIGHT/2-100,WIDTH,HEIGHT/2-100)
end

Why isn’t the upper Edge’s velocity becoming zero when it collides with the ball?

@Saurabh, change edge.type to DYNAMIC and it should then work as expected.

Yeah that’s right, but why doesn’t a KINEMATIC body’s velocity become zero? Is it that unless we set the velocity of a KINEMATIC body to zero it won’t become zero?

@Saurabh, yep you are absolutely correct. Here is the exact definition provided in the link that @juaxix posted above:

This constant specifies the kinematic body type. Kinematic bodies are unaffected by forces and collisions. Unlike static bodies, kinematic bodies are meant to be moved, usually by setting linear velocity directly. They also do not collide with other static or kinematic bodies. Also note that you cannot attach two static/kinematic bodies together with a joint.

But that post also says that we can’t make edges as dynamic bodies. Then how is it working if we set the edge type to dynamic?

Nice, are you experiencing bugs with box2d::getBody() function? it is a OBJC error that make Codea crash…

Ah, It would be nice to have a function to delete all the bodies and joints, like this:
physics.clear()

agree with a function to destroy all physics bodies, else we have to store all in a table and then destroy them.

.Saurabh to use things like EDGE but dynamics, use physics.body → CHAIN with false parameter to not close the shape.

@juaxix @Saurabh, chain also states it cannot be set to dynamic, but I think that more so applies to the use of joints while it is set to dynamic. If you aren’t going to be connecting these edges or chains to any other body, then you can set them to dynamic and it all seems to work out. Let me know if you run across any issues using dynamic edges or chains.

Ah, you are right, I was thinking in another thing :confused:
if you want lines you have to use POLYGON and close them with a little distance between the points…