i need a reverse function for an Brakeout clone

Discussion in 'Indie Basics' started by Daikrys, Aug 8, 2005.

  1. Daikrys

    Original Member

    Joined:
    Jul 3, 2005
    Messages:
    24
    Likes Received:
    0
    hi there :)

    iam looking for an math function to calculate the moving direction after
    the ball hit an wall or so

    can you help me?
    i dont find anything usefull with google :(

    Thanks

    sorry if its a silly question, the wrong place and for my bad english :rolleyes:
     
  2. revve

    Original Member

    Joined:
    Jul 4, 2005
    Messages:
    219
    Likes Received:
    0
    I've never done a breakout clone myself, but I'd say the easiest way to do this is to simply reverse the x and y direction the ball is travelling. This does not take into account gravity or anything, but I don't think any breakout clone does.

    I think the only thing still to do (optional) is to see if it hit the paddle, and increase the x speed slightly in the direction the paddle was moving, if the paddle was moving faster than a certian threshhold.

    Edit: I've created a small prog last night when demonstrating something to someone - it's simply an object that bounces around on the screen - I think it's "bouncing" routine is exactly what you are looking for (excluding the paddle). I'm at work at the moment, so I can't post it now (the code is at home), but I can post it tonight if you are interested. It is very basic, however. I doubt anybody will find anything useful in it. I had to demonstrate something to a person that was completely code-illiterate.
     
    #2 revve, Aug 8, 2005
    Last edited: Aug 9, 2005
  3. Pallav Nawani

    Indie Author

    Joined:
    Aug 13, 2004
    Messages:
    371
    Likes Received:
    0
    Here is some psuedo code:
    Code:
    if(ball_hits_left_wall())
      Ball_x_velocity = - Ball_x_velocity;
    if(ball_hits_right_wall())
      Ball_x_velocity = - Ball_x_velocity;
    if(ball_hits_top_wall())
      Ball_y_velocity = - Ball_y_velocity;
    
    You have to do a similar thing when ball hits bricks, but there it is slightly more complicated to determine which face of the brick was hit first by the ball. Btw, this question is probably more appropriate in Game Development & Technical

    Pallav
     
    #3 Pallav Nawani, Aug 8, 2005
    Last edited: Aug 8, 2005
  4. Bad Sector

    Original Member

    Joined:
    May 28, 2005
    Messages:
    2,742
    Likes Received:
    5
    The algorithm is very simple. There are two cases actually:

    1. If the ball hits the brick at the left or at the right side, reverse it's velocity vector's x component.
    2. If the ball hits the brick at the top or at the bottom side, reverse it's velocity vector's y component.
    3. Move the ball to the contact point between the brick and the ball (see below).

    Knowing which side the ball hits can be done like this (assuming that you know that the ball has hit a brick):

    1. Split the brick in four line segments, one for each side as shown in brick1.png
    2. Check if the velocity vector intersects any of these line segments - it may intersect one or two. Also find the intersection points of these line segments (the red dots in the images).
    3. If it intersects one, then you know which side the ball hitted, so use this
    4. If it intersects two, as shown in images brick2.png to brick4.png, then check which of the intersection points is the nearest to the vector's base (the previous position of the ball). The side that belongs to the nearest point is the side that the ball hitted.

    Images 2 to 4 shows different cases:
    * In brick2.png the side is left
    * In brick3.png the side is right
    * In brick4.png the side is bottom

    To find the contact point, after swapping one of the velocity vector's components, make a new vector from it and normalize it. Then multiply it by the ball's radius. Lastly move the ball to the intersection point of the side you hitted and move it along that new vector. In other "words":

    Code:
    ball.position = intesectionPoint + normalized(newVelocity)
    

    I hope this helped you :)
     

    Attached Files:

Share This Page

  • About Indie Gamer

    When the original Dexterity Forums closed in 2004, Indie Gamer was born and a diverse community has grown out of a passion for creating great games. Here you will find over 10 years of in-depth discussion on game design, the business of game development, and marketing/sales. Indie Gamer also provides a friendly place to meet up with other Developers, Artists, Composers and Writers.
  • Buy us a beer!

    Indie Gamer is delicately held together by a single poor bastard who thankfully gets help from various community volunteers. If you frequent this site or have found value in something you've learned here, help keep the site running by donating a few dollars (for beer of course)!

    Sure, I'll Buy You a Beer