Need Help with Collisions
Posted: Fri Apr 21, 2017 12:30 am
Hey everyone!
I'm a big noob (just started today), so pardon me if I say something stupid.
I was trying to make a collision happen between a rectangle and a moving circle. I know the way I set it up surely isn't the right one, but I can't understand why it won't work.
So, imagine the circle starting in the middle of the window and a rectangle at the same height, but left. I have a function updating the circle which makes it move left in the beginning. When the left side of the circle meets the right side of the rectangle, it should start moving to the right. This won't happen though: it just goes through it. Something even stranger happens when changing "b_left = a_right" with "b_left < a_right".
Sorry for my bad English, I hope I made it understandable enough.
I'm a big noob (just started today), so pardon me if I say something stupid.
I was trying to make a collision happen between a rectangle and a moving circle. I know the way I set it up surely isn't the right one, but I can't understand why it won't work.
So, imagine the circle starting in the middle of the window and a rectangle at the same height, but left. I have a function updating the circle which makes it move left in the beginning. When the left side of the circle meets the right side of the rectangle, it should start moving to the right. This won't happen though: it just goes through it. Something even stranger happens when changing "b_left = a_right" with "b_left < a_right".
Sorry for my bad English, I hope I made it understandable enough.
Code: Select all
function Circle:update(dt)
i = 1
if Circle.checkCollision(r1,c) then
i = i + 1
end
if (i%2 == 0) then
self.x = self.x + self.speed * dt
else
self.x = self.x - self.speed * dt
end
end
function Circle.checkCollision(a,b)
a_left = a.x;
b_left = b.x - b.radius;
if b_left = a_right then
return true
else
return false
end
end