Howdy, Stranger!

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

classes collision sdetetction help

edited January 2015 in General Posts: 3

function setup() B={} S={} ballFrequency=3 squareFrequency=1 dropBall() dropSquare() timer=0 timer1=0 x1=CurrentTouch.x y1=CurrentTouch.y end function dropBall() b={} b.x=math.random(50,WIDTH-50) b.y=HEIGHT+250 b.r=55 b.s=math.random(200,400) b.c=color(math.random(100,255),math.random(100,255),math.random(100,200),255) table.insert(B,b) end function dropSquare() s={} s.x=math.random(50,WIDTH-50) s.y=HEIGHT+500 s.w=40 s.h=40 s.s=math.random(100,200) s.c=color(math.random(100,255),math.random(100,255),math.random(100,255),255) table.insert(S,s) end function draw() background(137, 134, 130, 255) timer=timer+DeltaTime timer1=timer1+DeltaTime if timer1>2/squareFrequency then dropSquare() timer1=0 end for i,s in pairs(S) do s.y=s.y-s.s/65 if s.y<-50 then table.remove(S,i) end fill(s.c) rect(s.x,s.y,s.w,s.h) end if timer>1/ballFrequency then dropBall() timer=0 end for i,b in pairs(B) do b.y=b.y-6 if b.y<-50 then table.remove(B,i) end fill(b.c) ellipse(b.x,b.y,b.r) end if CurrentTouch.x >= b.x and CurrentTouch.x <= b.x+b.r -- i cant seem to get collision for the y axis then table.remove(B,i) end end


  • dave1707dave1707 Mod
    Posts: 8,811

    @WillAMParks You're comparing CurrentTouch.x to b.x, which appears to be the value of the last ball created. You need to compare CurrentTouch to the x,y values in the table B.

  • @dave1707 I am a little confused on what you mean. What would that look like.

  • dave1707dave1707 Mod
    Posts: 8,811

    @WillAMParks You're creating tables that hold the square and ball objects. You use those tables when you draw those objects and subtract a value from their y values to move them down the screen. You should also use those tables to check if you touch one of those objects. Create a table loop like when you're drawing them and then in that loop compare the CurrentTouch x,y values to each table x,y values plus or minus the size of those objects. If the touch is close to the object, then delete that object from the table. If you still need help, let me know.

  • It works! Thank you for the help I really appreciate it

Sign In or Register to comment.