hello,
does anyone know how to make a moving image stop at a certain point like a floor(you can't go through floors ya know!) or a wall?
Printable View
hello,
does anyone know how to make a moving image stop at a certain point like a floor(you can't go through floors ya know!) or a wall?
What you're talking about is collision detection. You have to check if the position of the object is outside of the area it is allowed to be in. If it's not, there is a collision, and you take appropriate action (like changing direction).
If you're using rectangular objects, then you can check the x and y axes seperately. As an example, here's a bit of collision detection code from a pool game I started last night:
Basically that makes sure that the ball bounces of the cushions of the table. It's a bit more complicated than just changing the velocity of the ball, as it predicts where the ball would have been if the collision had been detected as early as possible, and moves it to that position.Code:If Ball.vPos.x + Ball.dRadius > Table.vSize.x Then
Ball.vVel.x = -Ball.vVel.x
Ball.vPos.x = Ball.vPos.x - 2 * (Ball.vPos.x - (Table.vSize.x - Ball.dRadius))
End If
If Ball.vPos.y + Ball.dRadius > Table.vSize.y Then
Ball.vVel.y = -Ball.vVel.y
Ball.vPos.y = Ball.vPos.y - 2 * (Ball.vPos.y - (Table.vSize.y - Ball.dRadius))
End If
If Ball.vPos.x - Ball.dRadius < 0 Then
Ball.vVel.x = -Ball.vVel.x
Ball.vPos.x = 2 * Ball.dRadius - Ball.vPos.x
End If
If Ball.vPos.y - Ball.dRadius < 0 Then
Ball.vVel.y = -Ball.vVel.y
Ball.vPos.y = 2 * Ball.dRadius - Ball.vPos.y
End If