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()

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


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.


Try with density and gravityScale;

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.


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

function setup()

function draw()
    background(0, 0, 0, 255)
    stroke(255, 255, 255, 255)
    fill(255, 255, 255, 255)

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:

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…