Hittest

Hi,

maybe I missed it, is there a collision function available - couldn’t find anything :frowning:

If not can somebody post a few lines how to do it manually ?

THX

M [import]uid: 6553 topic_id: 1040 reply_id: 301040[/import]

Hey, the internet is full of code for this. But here is my stuff:

[lua]local mAbs = math.abs

–****************************************************************
local ColCheck_Box2Box = function( source, target )
– Here I check against a radius property which I added when the
– object was created
–****************************************************************
local rad = source.radius + target.radius
if mAbs(source.y - target.y) < rad then
if mAbs(source.x - target.x) < rad then
return true
end
end
return false
end

–****************************************************************
local ColCheck_Circle2Circle = function( source, target )
– Here I check against a radius property which I added when the
– object was created
–****************************************************************
local xf = (source.x - target.x)
xf = xf * xf
local yf = (source.y - target.y)
yf = yf * yf

local rf = (source.radius*source.xScale) + (target.radius*target.xScale)
rf = rf * rf
if (xf+yf) < rf then
return true
end
return false
end[/lua] [import]uid: 5712 topic_id: 1040 reply_id: 2548[/import]

Mike,

Thanks for posting your sample code but I had one question. I went through some examples and it looks like it only detects objects that overlap and not objects that are touching. Is this correct? Changing the “if” tests from "
Thanks,
Tom [import]uid: 6119 topic_id: 1040 reply_id: 2549[/import]

Hi Tom,

sure, only if they overlap. but like anything else… it depends on the needs of a project. :slight_smile:

Cheers
Michael [import]uid: 5712 topic_id: 1040 reply_id: 2550[/import]

Hi Michael,

Thanks, I just wanted to verify my understanding of your code sample.

These code samples are very useful for learning Lua and the Corona SDK and I would like to thank you and the others who post the code and answer questions here on the forum. It makes a big difference knowing there is support from both Ansca and the forum community.

Tom [import]uid: 6119 topic_id: 1040 reply_id: 2551[/import]

Thanks for your kind words. [import]uid: 5712 topic_id: 1040 reply_id: 2557[/import]

THX mate :slight_smile:

M [import]uid: 6553 topic_id: 1040 reply_id: 2721[/import]