I want to be able to detect when a sprite touches one another. I don’t want them to collide or anything. I don’t want to setup the physics engine. I just want a simple code to detect when one sprite is touching another. Is that possible?
Sure is. Use an if statement and compare their x,y coords. Dont forget to add in width and height of the image.
function setup()
s1 = {x=WIDTH/2,y=HEIGHT/2,width=101,height=100}
s2 = {x=0,y=0,width=101,height=100}
end
function draw()
background(0, 0, 0, 255)
sprite("Planet Cute:Character Boy",s1.x,s1.y)
sprite("Planet Cute:Character Horn Girl",s2.x,s2.y)
strokeWidth(2)
noFill()
rect(s1.x-s1.width/2,s1.y-s1.height/2,s1.width,s1.height)
rect(s2.x-s2.width/2,s2.y-s2.height/2,s2.width,s2.height)
if colision(s1,s2) then output.clear() print("Touching!") else output.clear() print("Not Touching") end
end
function touched(touch)
s2.x = touch.x
s2.y = touch.y
end
function colision(sA, sB)
local widthA = sA.width/2
local heightA = sA.height/2
local widthB = sB.width/2
local heightB = sB.height/2
return sA.x+widthA > sB.x-widthB and sA.x-widthA < sB.x+widthB and sA.y+heightA > sB.y-heightB and sA.y-heightA < sB.y +heightB
end
Thanks! I got it now.