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?


  • dave1707dave1707 Mod
    edited November 2018 Posts: 8,445

    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
    function draw()
        background(40, 40, 50)    
        ellipse(xc,yc,a*2,b*2)    -- draw ellipse   
        x=CurrentTouch.x    -- touched x value
        y=CurrentTouch.y    -- touched y value        
        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)        
  • Thanks!

Sign In or Register to comment.