Crash with physics and mass

In my setup() I am initializing an object that has the following code:

local defPoints = {vec2(0,5), vec2(3,0), vec2(-3,0), vec2(0,5)}
self.body = physics.body(POLYGON, unpack(defPoints))
self.body.mass = 1

It seems that the last line causes the crash as soon as I try to run the project (I disabled everything in my draw() to make sure it wasn’t something else causing the crash - commenting out the last line does in fact fix the crash). I get the following on my device’s console output:

Mar 18 10:52:53 MA-iPad-mini Codea[13454] <Error>: libMobileGestalt copySystemVersionDictionaryValue: Could not lookup ReleaseType from system version dictionary
??Mar 18 10:52:53 MA-iPad-mini Codea[13454] <Warning>: (
??Mar 18 10:52:53 MA-iPad-mini Codea[13454] <Warning>: (

I was able to recreate the crash also. I was able to make it work with the POLYGON if I had a vec2 in each of the 4 quadrants. Basically a square around 0,0. If I shifted the points so they were in 1 quadrant, Codea crashed.

-- create a square with a point in each of the 4 quadrants
local defPoints = {vec2(-5,5),vec2(-5,-5),vec2(5,-5),vec2(5,5)}   -- this works

-- shift the square by 5 in x and y direction, square in 1st quadrant
local defPoints = {vec2(0,10),vec2(0,0),vec2(10,0),vec2(10,10)}  -- this crashes

While you’re waiting for an answer, please tell me how you get to see the console output?

@Ignatz - Go to Settings>General>About>Diagnostics & Usage>Diagnostics & Usage Data>Codea-%date%_%iPad name%.
Make sure your are not automatically sending Diagnostics & Usage to apple to view it.
Hope that helps!

You can view console output in Xcode (in the Organizer). Really useful.

@zoyt - ta, that is useful, it should be in the FAQ!

@Cplr - What Organizer?

If running Xcode on your computer and the iPad is connected you can select windows → organizer

Just a quick follow up, you can work around this crash by setting the density instead of mass.

Whatever is causing the crash seems to depend on the polygon’s size (update: when it is less than 1 square meter, given the scale of 32 pixels per meter?), based on experiments with the following rig:

function setup()
    parameter.boolean("isSettingMass", false)
    parameter.integer("size", 10, 20, 16)
    parameter.integer("dx", -200, 200, 0)
    parameter.integer("dy", -200, 200, 0)

function draw()
    stroke(0, 255, 0)
    line(0, HEIGHT / 2, WIDTH, HEIGHT / 2)
    line(WIDTH / 2, 0, WIDTH / 2, HEIGHT)
    fill(255, 255, 0)
    text("Touch Viewer to trigger physics.body", WIDTH / 2, HEIGHT - 50)
    translate(WIDTH / 2, HEIGHT / 2)
    rect(dx, dy, size * 2, size * 2)

function touched(touch)
    if touch.state ~= ENDED then return end
    local dp = vec2(dx, dy)
    print("Setting physics.body!")
    b = physics.body(POLYGON,
    vec2(size, size) + dp, vec2(-size, size) + dp,
    vec2(-size, -size) + dp, vec2(size, -size) + dp)
    if isSettingMass then
        print("Setting mass!")
        b.mass = 1 -- Will crash Codea if size < 16
                   -- and (0, 0) not within polygon


This is a known issue with Box 2D regarding small objects with their origin too far away from the center of mass (rounding errors causing negative rotational inertia). I will look into trying to stop Codea from crashing as a result.