Results 1 to 2 of 2

Thread: collisions (sort of)

  1. #1

    Thread Starter
    Hyperactive Member dflw's Avatar
    Join Date
    Apr 2001
    Location
    ct, usa
    Posts
    412

    collisions (sort of)

    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?
    Visual Basic 6, HTML, JavaScript, learning C++

  2. #2
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    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:
    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
    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.
    Harry.

    "From one thing, know ten thousand things."

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width