Howdy, Stranger!

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

How to check for collisions?

So, say you had just a circle on screen and you you wanted it to print "touching" when you touch it. What are good methods to do this?

Comments

  • dave1707dave1707 Mod
    edited November 30 Posts: 7,155

    Here’s something I wrote a long time ago.

    function setup()
        parameter.integer("a",1,400,200)    -- size of a axis
        parameter.integer("b",1,400,100)    -- size of b axis
        xc=250    -- center of ellipse x axis
        yc=400    -- center of ellipse y axis
    end
    
    function draw()
        background(40, 40, 50)    
        fill(255)
        ellipse(xc,yc,a*2,b*2)    -- draw ellipse   
        x=CurrentTouch.x    -- touched x value
        y=CurrentTouch.y    -- touched y value        
        fill(255,0,0)
        ellipse(x,y,5)  -- draw point  
        -- check if point is inside of ellipse or circle
        if (x-xc)^2/a^2+(y-yc)^2/b^2 <= 1 then 
            text("point in ellipse",xc,yc)        
        end    
    end
    
  • Posts: 8

    Thanks!

Sign In or Register to comment.