lua - Corona/Box2D detect collision with non-moving static objects -


for posting reasons here's simple version of i'm trying do.

on screen have simple circle object static , doesn't move. user can drag , drop straight line. if line passes through circle i'm hoping collision event triggered.

it appears unless 1 of objects moving collision never detected. can detect collision when line drawn?

collision event

function onhit(e)     print("hit"); end runtime:addeventlistener("collision", onhit) 

touch event

local startx = 0; local starty = 0; local endx = 0; local endy = 0;  function ontouch(e)     if(e.phase == "began")         startx = e.x         starty = e.y     elseif(e.phase == "moved")         endx = e.x         endy = e.y     elseif(e.phase == "ended")         local line = display.newline(startx, starty, endx, endy)         line:setcolor(100, 100, 100)         line.width = 2         physics.addbody(line, "static", {   })     end end runtime:addeventlistener("touch", ontouch) 

create circle

local c = display.newcircle(50, 50, 24) physics.addbody(c, "static", { radius = 24 }) 

this page corona sdk docs describes bodytype property halfway down page. when describing "static" bodies, says (my emphasis):

static bodies don't move, , don't interact each other; examples of static objects include ground, or walls of pinball machine.

that means 1 of objects has other static.

here's idea, although haven't tried myself: (see update below.) make line dynamic when first create it. set static few milliseconds later using timer.performwithdelay function. if collision event occurs in meantime, know you've got overlap, , can set bodytype static immediately. if don't collision event, bodytype still dynamic in delayed routine, , you'll know didn't have overlap. in case, you'll still need set line static in delayed routine.


update: tested this, using code starting point

i changed collision event set both objects' bodytype static:

function onhit(e)     print("hit")     e.object1.bodytype = "static"     e.object2.bodytype = "static" end 

then changed addbody call line add dynamic body, new code setup timer.performwithdelay function check after short time:

physics.addbody(line, "dynamic", {   })  timer.performwithdelay(10,      function()         if line.bodytype == "dynamic"             print ("no overlap")             line.bodytype = "static"         end     end) 

the results were, unfortunately, mixed. works of time, perhaps 95%, fails when drawing line starts outside circle , ends inside, should overlap, reported no overlap. wasn't able figure out why happening. i'm posting anyway, hoping gets going, , thinking may able figure out inconsistent behavior , educate both.

failing that, add additional check "no overlap" case check if either endpoint of line closer radius of circle away center. make things work, suppose misses whole point of letting physics engine work.

anyway, luck!


Comments

Popular posts from this blog

objective c - Change font of selected text in UITextView -

php - Accessing POST data in Facebook cavas app -

c# - Getting control value when switching a view as part of a multiview -