Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Detect Touching of Sprites without Collision?

edited June 2013 in Questions Posts: 7

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?

Comments

  • BriarfoxBriarfox Mod
    edited June 2013 Posts: 1,542

    Sure is. Use an if statement and compare their x,y coords. Dont forget to add in width and height of the image.

  • BriarfoxBriarfox Mod
    Posts: 1,542
    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
    
    
    
  • Posts: 7

    Thanks! I got it now.

Sign In or Register to comment.