Results 1 to 3 of 3

Thread: Breakout help?!

  1. #1
    Jason21m
    Guest

    Question Breakout help?!

    I am trying to get a ball to bounce off of a few things. As of now I have the ball bouncing off of the sides and bottom of the form. One question I have is
    Can anyone tell me how to get the ball to stop from going off of the top of the screen? It gets to the title bar and then disappears. The second one is how do I get the ball to bounce off of the paddle? Any help will be greatl appreciated.

  2. #2
    Addicted Member
    Join Date
    Dec 2001
    Location
    Great White North, ey?
    Posts
    202

    Try the bounding box collsioion detection!

    Try this, it works for me every time. You can use this to make the ball bounce off of the paddles AND prevent it from going off the screen. I assume that you know how I got the ball.x and ball.y,
    I just used a Public Type.

    picball- is the picturebox on your form
    paddle - is the paddle picturebox on your form.

    If ball.x + picball.Width >= Paddle.Left And ball.x <= Paddle.Left + Paddle.Width And ball.y + picball.Height >= Paddle.Top And ball.y <= Paddle.Top + Paddle.Height Then ball.y = ball.y - 5

    "ball.y = ball.y - 5" change this to ball.x= ball.x - 5 if your game is horizontal.


    Hope this helps.

  3. #3
    NOMADMAN
    Guest

    API TO THE RESCUE

    I recommend API if you're comfortable with it.

    There is a command IntersectRect that can be called. This is very fast and very effective.



    VB Code:
    1. Declare Function IntersectRect Lib "user32" Alias "IntersectRect" _
    2.  (lpDestRect As RECT, lpSrc1Rect As RECT, lpSrc2Rect As RECT) As Long
    3.  
    4. Type RECT
    5.      Left as Long
    6.      Top as Long
    7.      Right as Long
    8.      Bottom as Long
    9. End Type

    Now use a line similar to this to see if the two intersect:

    VB Code:
    1. If IntersectRect(TEMPRECT, BALLRECT, PADDLERECT) <> 0 Then
    2.      'RUN COLLISION CODE
    3. End If

    You must declare all those varible in the finction call as RECT. And to set values to both the ball and the paddle you do something like this:

    VB Code:
    1. With BALLRECT
    2.      .Left = 50
    3.      .Right = .Left + 10
    4.      .Top = 50
    5.      .Bottom = .Top + 10     'HEIGHT OF BALL
    6. End With
    7.  
    8. With PADDLERECT
    9.      .Left = 150
    10.      .Right = .Left + 100     'WIDTH OF PADDLE
    11.      .Top = 50
    12.      .Bottom = .Top + 50
    13. End With

    All those numbers are of coarse arbitrary, so do what you must. This explanation is pretty over simplified, but if you have any questions ask'em away!

    NOMAD

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