[Resolved] Complex body freezes the simulator when collision occurs

Sorry, I realised my points were in the anti-clockwise winding direction - this has been fixed below…

I have a multi part complex body (a cog) which should drop to the ground and bounce. For some reason, it freezes. Could someone help me debug this please?

To run the code below you’ll need my mathlib.lua from the code exchange:

https://developer.anscamobile.com/code/maths-library

[lua]-- COG

– turn off status display bar
display.setStatusBar(display.HiddenStatusBar)

– load libraries
local physics = require(“physics”)
local mathapi = require(“mathlib”)

– setup world
physics.start()
physics.setGravity(0,10)
physics.setDrawMode(“hybrid”)

– get stage reference
local stage = display.getCurrentStage()

– the basic design of an individual tooth
local toothshape = { 10,-10 , 10,10 , -10,10 , -10,-10 }

– scale the design of a tooth, move it to the edge of the cog and rotate it into position around the cog
function adjustShape(pts,radius,scale,rotation)
local tbl = {}
for i=1, #pts-1, 2 do
local pt = { x=pts[i]*scale, y=(pts[i+1]*scale) + radius }
pt = mathapi.rotatePoint( pt, rotation )
tbl[#tbl+1] = pt.x
tbl[#tbl+1] = pt.y
end
return tbl
end

– make an individual tooth as a graphical object
function makeTooth(s,r,rot)
local tbl = adjustShape(toothshape,r,s,rot)
local line = display.newLine( tbl[1], tbl[2], tbl[3], tbl[4] )
for i=5, #tbl-1, 2 do
line:append( tbl[i], tbl[i+1] )
end
line.width = 5
return line, tbl
end

– collect the teeth and central hub of the code and turn it into a physical object
function makeCog(r,scale)
local group = display.newGroup()

local props = {}

for i=1, 6 do
local tooth, sh = makeTooth(scale, r, (360/6)*i)
group:insert(tooth)
local prop = { friction=.3,density=1,bounce=1,shape=sh }
props[#props+1] = prop
end

local circle = display.newCircle( group, 0, 0, r )
circle.strokeWidth = 5
circle:setFillColor(0,0,0)

props[#props+1] = { friction=1,density=1,bounce=1,radius=r }

physics.addBody( group, “dynamic”, unpack( props ) )

return group
end

– request a new cog
local cog = makeCog(200, 2)
cog.x,cog.y = display.contentCenterX, display.contentCenterY
cog.rotation = 15

– create the floor so the cog doesn’t drop off the screen
local floor = display.newRect( 0,display.contentHeight-50, display.contentWidth, 50)
physics.addBody( floor, “static”, { friction=.5,density=1,bounce=1 })

– handle the drag operation
function cog:touch( event )
if (event.phase == “began”) then
stage:setFocus( event.target )
event.target.touchjoint = physics.newJoint( “touch”, event.target, event.x, event.y )
elseif (event.phase == “moved”) then
event.target.touchjoint:setTarget( event.x, event.y )
else
event.target.touchjoint:removeSelf()
event.target.touchjoint = nil
stage:setFocus( nil )
end
end

– attach a touch handler to the cog
cog:addEventListener( “touch”, cog )[/lua] [import]uid: 8271 topic_id: 22959 reply_id: 322959[/import]

Glad you got it sorted, that was the first thing i was going to suggest. [import]uid: 84637 topic_id: 22959 reply_id: 91732[/import]

Glad you got it sorted, that was the first thing i was going to suggest. [import]uid: 84637 topic_id: 22959 reply_id: 91733[/import]