hello
in my post : https://forums.coronalabs.com/topic/72956-bug-with-2d-box-collision/
i was talking about a bug involve during the collision between the ball and the horizontal bar, and i found that it was because I used ball.x in the collision process.
But I have one question that I was unabled to solve :
when the ball arrives to the horizontal bar from the right side, and touch the left part of the bar , the ball simply bounce and continue to the left side.
But when the ball touch the right part of the bar, we want that the ball go back to the right side.
And at that point, I am block, because I need to calculate the difference between ball.x and bar.x, to check if it’s positive or negative.
function ball:collision(event) local ph=event.phase local x,y=event.x,event.y local other=event.other if ph=="began" then if other.myName=="bloc" then other.delete() end // the brick to break elseif ph=="ended" then local vx,vy=self:getLinearVelocity() if other.myName=="bar" then local dx = self.x-bar.x // here is the problem : I need to calculate this, but this create bug ! if dx\*vx\<0 then vx=-vx end // changing direction self:setLinearVelocity( vx,vy ) end end end ball:addEventListener("collision",ball)
thanks you