Results 1 to 10 of 10

Thread: Bouncing Ball

  1. #1

    Thread Starter
    Lively Member MileOut's Avatar
    Join Date
    Nov 2001
    Location
    Glasgow
    Posts
    83

    Bouncing Ball

    What's the simplest - yet best- way to create a bouncing ball in the Breakout mould?

    I have made a ball graphic and want to ensure that I can bounce it all around an arena (basically a rectangle shape) before I add other stuff.

  2. #2
    Not NoteMe SLH's Avatar
    Join Date
    Mar 2002
    Location
    192.168.0.1 Preferred Animal: Penguin Reason for errors: Line#38
    Posts
    3,051
    Basically, there are 2 widely used methods. Giving the ball an angle, and a speed, or using a left/right (x-axis) vector, and an up/down vector.

    I have used both, and prefer the second method (just my preference, as i find it easier to work with, and manipulate).



    Making a ball bounce off the form using the vector method of ball movement.

    I suggest setting up a ball type, so you can easily manipulate it's various properties. Here is a basic one for you.

    VB Code:
    1. Private Type typBall
    2.     X as Double
    3.     Y as Double
    4.     a as Double
    5.     b as Double
    6.     Radius as Integer 'Does not need to be too precise (nor do the others really...)
    7. End Type

    Of course you could have many others such as....

    VB Code:
    1. Private Type typBall
    2.     Mass As Double              'How Heavy is 1 unit (120 by 120) of this object?
    3.     Height As Double            'The height of the object
    4.     Width As Double             'The width of the object
    5.     X As Double                 'X coordinate of the ball
    6.     Y As Double                 'Y coordinate of the ball
    7.     Speed(1 To 2) As Double     'Speed of the ball as a vector in X and Y (1=x, 2=y)
    8.     Accel(1 To 2) As Double     'Accelleration of the ball as a vector in X and Y (1=x, 2=y)
    9.     CoeffRestitution As Double  'General coefficient of restitution (different for each surface - based on contact with the form edges) (0-1)
    10.     CoeffFriction As Double     'General coefficient of friction (different for each surface - based on contact with the form edges) (0-1)
    11.     AirResistance As Double     'How much wind effects this object 0=none, 1 = fully
    12.     State As String             'Used in code, do decide what physics to apply
    13. End Type

    We'll stick to the simple ones

    Ok, the x value is the position of the ball relative to the left side of the form, and the y value is relative to the top (0 being at the very top).

    A is the veolcity vector for the x axis. So if a = 100 then the ball would move left...

    B is the velocity vector for the y axis. So if b = 100 then the ball would move DOWN (as measurements are made from the top of the form).

    Enough of the lecture, to get the ball rolling (hehe) put a timer on a form, with a shape (easy to change to a picturebox).

    Call the shape shpBall, and put in a timer called Timer1

    VB Code:
    1. Option Explicit
    2. Private Type typBall
    3.     x As Double
    4.     y As Double
    5.     a As Double
    6.     b As Double
    7.     radius As Integer
    8. End Type
    9. Dim Ball As typBall
    10. Private Sub Form_Activate()
    11.     Ball.x = shpBall.Left
    12.     Ball.y = shpBall.Top
    13.     Ball.a = 60
    14.     Ball.b = 30
    15.     Ball.radius = (shpBall.Height / 2)
    16. End Sub
    17. Private Sub Timer1_Timer()
    18.     shpBall.Left = Ball.x + (Ball.radius)
    19.     shpBall.Top = Ball.y + (Ball.radius)
    20.     Ball.x = Ball.x + Ball.a
    21.     Ball.y = Ball.y + Ball.b
    22. End Sub

    OOOO, the ball moves, but it goes off the edge of the screen. Lets deal with that now.

    In the timer1_timer before ball.x = ball.x + ball.a Put

    VB Code:
    1. Call FormCollisionDetection

    Now, we'll make the collision detection routine.

    It basiclly works like this. If the ball was going right (i.e. ball.a was +ve) then when it hits the form's edge we want it to go left (i.e. ball.a is -ve). It is similar for ball.y

    If this was collision detection for an object, instead of the edge of the form, we would have to check that the balls path didn't pass over that object, otherwise if the ball was going fast enough it could 'jump' the object. I won't deal with that now though, as we are just dealing with the form's edges (and it doesn't have to be very realistic).

    VB Code:
    1. Private Sub FormCollisionDetection()
    2.     If Ball.x + Ball.radius > Form1.Width Then 'The ball hit the right edge
    3.         Ball.a = -Abs(Ball.a)
    4.     End If
    5.     If Ball.x - Ball.radius < 0 Then 'The ball hit the left edge
    6.         Ball.a = Abs(Ball.a)
    7.     End If
    8.     If Ball.y + Ball.radius > Form1.Height Then 'The ball hit the bottom edge
    9.         Ball.b = -Abs(Ball.b)
    10.     End If
    11.     If Ball.y - Ball.radius < 0 Then 'The ball hit the top edge
    12.         Ball.b = Abs(Ball.b)
    13.     End If
    14. End Sub

    Now run the program and see what happens.

    Success, the ball bounces (you can see that i've only checked the location of the ball, not it's path, as the ball goes off the screen sometimes).

    Go ahead and have a play with that code. Try this for example, all i did was add 1 line.

    VB Code:
    1. Option Explicit
    2. Private Type typBall
    3.     x As Double
    4.     y As Double
    5.     a As Double
    6.     b As Double
    7.     radius As Integer
    8. End Type
    9. Dim Ball As typBall
    10. Private Sub Form_Activate()
    11.     Ball.x = shpBall.Left
    12.     Ball.y = shpBall.Top
    13.     Ball.a = 60
    14.     Ball.b = 30
    15.     Ball.radius = (shpBall.Height / 2)
    16. End Sub
    17. Private Sub Timer1_Timer()
    18.     shpBall.Left = Ball.x + (Ball.radius)
    19.     shpBall.Top = Ball.y + (Ball.radius)
    20.     Call FormCollisionDetection
    21.    
    22.     If Ball.b < 120 Then Ball.b = Ball.b + 1 'A crude simulation of gravity
    23.    
    24.     Ball.x = Ball.x + Ball.a
    25.     Ball.y = Ball.y + Ball.b
    26. End Sub
    27. Private Sub FormCollisionDetection()
    28.     If Ball.x + Ball.radius > Form1.Width Then 'The ball hit the right edge
    29.         Ball.a = -Abs(Ball.a)
    30.     End If
    31.     If Ball.x - Ball.radius < 0 Then 'The ball hit the left edge
    32.         Ball.a = Abs(Ball.a)
    33.     End If
    34.     If Ball.y + Ball.radius > Form1.Height Then 'The ball hit the bottom edge
    35.         Ball.b = -Abs(Ball.b)
    36.     End If
    37.     If Ball.y - Ball.radius < 0 Then 'The ball hit the top edge
    38.         Ball.b = Abs(Ball.b)
    39.     End If
    40. End Sub

    Next you could try changing this...

    VB Code:
    1. If Ball.y + Ball.radius > Form1.Height Then 'The ball hit the bottom edge
    2.     Ball.b = -Abs(Ball.b)
    3. End If
    ...so that the player had to be under the ball for it to bounce up.


    Have fun
    Last edited by SLH; Apr 18th, 2002 at 03:43 PM.
    Quotes:
    "I am getting better then you guys.." NoteMe, on his leet english skills.
    "And I am going to meat her again later on tonight." NoteMe
    "I think you should change your name to QuoteMe" Shaggy Hiker, regarding NoteMe
    "my sweet lord jesus. I've decided never to have breast implants" Tom Gibbons
    Have I helped you? Please Rate my posts.


  3. #3
    Hyperactive Member Ambivalentiowa's Avatar
    Join Date
    Apr 2002
    Location
    Coming soon to a store near you!
    Posts
    375
    I have seen some games SLH has made with this method and it works very well.
    -Show me on the doll where the music touched you.

  4. #4
    Not NoteMe SLH's Avatar
    Join Date
    Mar 2002
    Location
    192.168.0.1 Preferred Animal: Penguin Reason for errors: Line#38
    Posts
    3,051
    Originally posted by Ambivalentiowa
    I have seen some games SLH has made with this method and it works very well.
    Thanks Ambivalentiowa, i'll pay you later
    Quotes:
    "I am getting better then you guys.." NoteMe, on his leet english skills.
    "And I am going to meat her again later on tonight." NoteMe
    "I think you should change your name to QuoteMe" Shaggy Hiker, regarding NoteMe
    "my sweet lord jesus. I've decided never to have breast implants" Tom Gibbons
    Have I helped you? Please Rate my posts.


  5. #5
    Hyperactive Member Ambivalentiowa's Avatar
    Join Date
    Apr 2002
    Location
    Coming soon to a store near you!
    Posts
    375
    LMAO
    -Show me on the doll where the music touched you.

  6. #6
    Fanatic Member alkatran's Avatar
    Join Date
    Apr 2002
    Location
    Canada
    Posts
    860

    I've tried this before...

    use what ppl above me posted, but to check if it is at edge, make it

    if ballpositionx + speedx > form1.width then ballpositionx = form1.width: speedx = 0 - speedx

    change according to spot... basicly the ball can be going 9999999 and it will never go outside of form.
    Don't pay attention to this signature, it's contradictory.

  7. #7
    Not NoteMe SLH's Avatar
    Join Date
    Mar 2002
    Location
    192.168.0.1 Preferred Animal: Penguin Reason for errors: Line#38
    Posts
    3,051
    Alkatran, that method of collision detection is a better way, but i don't know about how you moved the ball...

    Look at the attached pic.

    With your method, alkatran, the ball would be moved to the red position. While at low speeds this would be un-noticable, but if it was going 9999999 (in your example ), then it would be very noticable.

    What you should do, is test various percentages of the velocity of the ball, then when you detect a collision, move the ball to ball.x + (ball.a * Percent/100) and ball.y = ball.y + (ball.b * Percent/100).

    This, if done correctly, would place the ball in the exact position that it would collide with the form.

    NB: This method would have to be used for detection of small objects, such as a line, as otherwise the ball would go straight over it (at a relativly low speed).

    Attached Images Attached Images  
    Quotes:
    "I am getting better then you guys.." NoteMe, on his leet english skills.
    "And I am going to meat her again later on tonight." NoteMe
    "I think you should change your name to QuoteMe" Shaggy Hiker, regarding NoteMe
    "my sweet lord jesus. I've decided never to have breast implants" Tom Gibbons
    Have I helped you? Please Rate my posts.


  8. #8
    Not NoteMe SLH's Avatar
    Join Date
    Mar 2002
    Location
    192.168.0.1 Preferred Animal: Penguin Reason for errors: Line#38
    Posts
    3,051
    Also, if you want to be really clever, you could add the remainder of the ball's velocity on to the new ball position (in the correct direction of course). If you did use this method, you would have to check for collision on the new velocity as well and if that collided, then you could have to check again.....

    Attached Images Attached Images  
    Quotes:
    "I am getting better then you guys.." NoteMe, on his leet english skills.
    "And I am going to meat her again later on tonight." NoteMe
    "I think you should change your name to QuoteMe" Shaggy Hiker, regarding NoteMe
    "my sweet lord jesus. I've decided never to have breast implants" Tom Gibbons
    Have I helped you? Please Rate my posts.


  9. #9
    Hyperactive Member Ambivalentiowa's Avatar
    Join Date
    Apr 2002
    Location
    Coming soon to a store near you!
    Posts
    375
    again, i've seen this, and it works well.
    -Show me on the doll where the music touched you.

  10. #10
    Not NoteMe SLH's Avatar
    Join Date
    Mar 2002
    Location
    192.168.0.1 Preferred Animal: Penguin Reason for errors: Line#38
    Posts
    3,051
    Quotes:
    "I am getting better then you guys.." NoteMe, on his leet english skills.
    "And I am going to meat her again later on tonight." NoteMe
    "I think you should change your name to QuoteMe" Shaggy Hiker, regarding NoteMe
    "my sweet lord jesus. I've decided never to have breast implants" Tom Gibbons
    Have I helped you? Please Rate my posts.


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