A Flash Developer Resource Site

Results 1 to 17 of 17

Thread: Simple Breakout Physics

  1. #1
    Member
    Join Date
    May 2006
    Posts
    31

    Simple Breakout Physics

    I am working on a game where you control a bat that goes left and right. There are 4 bats. The top ones are controlled with left and right and the side ones are controlled with up and down. I need help on making the bounce physics. Ie:

    When ball hits bat, it reverses direction and stays same speed
    When ball hits bat (when bat is moving) ball may bounce differently and perhaps gain speed.

    I have looked everywhere for this code, but can't find it and I have no idea on how to create it. I am only beginning to use actionscript.

  2. #2
    Member
    Join Date
    Nov 2006
    Posts
    69
    Need more information to make any code for that...

    1) in what direction off the bat do you want the ball to move (if you hit the left of the bat do you want the ball to go to the right, or to the left)? Do you want the release angle to be relative to the distance from center the ball hits the bat.

    2) in which instances do you want the ball to increase speed, ie:
    a) if the bat is moving over a given speed the ball increases.
    b) if the ball hits the bat closer to the edge will it go faster? and closer to center it slows down.
    c) do you want to use some sort of a bonus to alter the speed dynamics.

    I'm sure you see what I mean. If you supply it in english it should be simple to make the code, after all it is just angles.

  3. #3
    Member
    Join Date
    May 2006
    Posts
    31
    1) I am not sure I understand you there. When I say bat, I mean a platform that translates from side to side or up and down. (The ones on the side go up and down.) I want the ball to bounce off of it like it would in a normal breakout game.

    2) I want it to increase speed when the bat is moving and it hits the bat
    I also want it to slightly increase every time it is hit just to make it harder. The goal of the game is to keep the ball in the screen. You have four platforms to control to keep it in the level.

  4. #4
    Member
    Join Date
    Nov 2006
    Posts
    69
    Ok for clarification... you have four bats on a two dimentional baord?;

    One at the bottom?
    One at the top?
    One at the left?
    One at the right?

    There are no obsticles in the center?

    When the ball hits the bat, the angle remains constant?
    When the ball hits the bat and it is moving the ball goes faster?
    When the ball hits the bat and it isn't moving, it remains at the same speed?

  5. #5
    Member
    Join Date
    May 2006
    Posts
    31
    One at the bottom? Controlled with left and right.One at the top? Controlled with left and right.
    One at the left? Controlled with up and down.
    One at the right? Controlled with up and down.

    There are no obsticles in the center? Correct

    When the ball hits the bat, the angle remains constant? What do you mean angle?
    When the ball hits the bat and it is moving the ball goes faster? Yes
    When the ball hits the bat and it isn't moving, it remains at the same speed? This one is harder to explain. It gets a little faster every time it gets hit regardless of anything else. If the bat is moving and it hits the bat, it will get a little faster. The ball will also get a little smaller if hit by the bat. Eventually it will dissapear and give the user points, but I can work out that part.

    Here is a link to the game: See what the bats look like: http://www.ethland.com/Flash/BunnyQuest2.html

  6. #6
    Member
    Join Date
    Nov 2006
    Posts
    69
    When the ball hits the bat, the angle remains constant?

    What do you mean angle?

    I have added a gif picture.. hope this helps to explain.
    Attached Images Attached Images

  7. #7
    Member
    Join Date
    May 2006
    Posts
    31
    I like the opposite spin.

  8. #8
    Member
    Join Date
    Nov 2006
    Posts
    69
    Writing the basics of the code at the moment... will nede a few mins.

  9. #9
    Member
    Join Date
    Nov 2006
    Posts
    69
    I'll do this in a few installments cos there is a lot of it.

    Right, to give you a chance to write the code yourself this is everything you need to take into account...

    You need a speed variable and direction handlers.

    _global.speed = 1 // this would be the start speed.
    _global.leftRight = 0 // start direction. (-89 to 89)
    _global.upDown = 0 // start direction. (-89 to 89)

    You don't need to act on every bat seperatley as they are all the same size from the edge.. as i don't know that size I have used batWidth.

    batWidth = 20; // just a guess

    function batCheck () { // every time the ball moves this needs to be called
    if (ball._x == batWidth) {
    checkLeftBat();
    }
    if (ball._x == Stage.width - batWidth) {
    checkRightBat();
    }
    if (ball._y == batWidth) {
    checkTopBat();
    }
    if (ball._y == Stage.height - batwidth) {
    checkBottomBat()
    }
    }

  10. #10
    Member
    Join Date
    Nov 2006
    Posts
    69
    batHeight = 100; // just another guess.

    function checkLeftBat() {
    if (leftBat._y >= ball._y) { //check if the bat is touching the ball
    if (leftBat._y + batHeight <= ball._y) { //same again
    temp = ball._y - leftBat.Y; // get the difference between the bat top and the ball.
    _global.updown = Math.round(temp / batHeight * 188) - 89; // This sets the angle of the ball.
    _global.leftRight = -_global.leftRight; // reverses the direction.
    }
    }
    else {
    killplayer();
    }
    }

    you will need to do this for each of the bats, changing the xs and ys respectivly.
    Last edited by deldelfarrom; 12-10-2006 at 03:10 PM. Reason: adding code

  11. #11
    Member
    Join Date
    Nov 2006
    Posts
    69
    Are you with me so far?

  12. #12
    Member
    Join Date
    May 2006
    Posts
    31
    I am sorry but I am a noob at this. Here is the code I have for the bats:
    onClipEvent (enterFrame)
    {
    moveSpeed=8;
    {
    if (this._y<320)
    if(Key.isDown(Key.DOWN))
    {
    this._y += moveSpeed;
    }
    }
    {
    if (this._y>80)
    if(Key.isDown(Key.UP))
    {
    this._y -= moveSpeed;
    }
    }
    }

  13. #13
    Member
    Join Date
    May 2006
    Posts
    31
    When you say direction handelers, do you mean the ball's starting direction? I would like that to be random.

  14. #14
    Member
    Join Date
    Nov 2006
    Posts
    69
    You can make the starting direction random, I was just declaring the variables at the start to let you know what they were.

  15. #15
    Member
    Join Date
    Nov 2006
    Posts
    69
    As you are new to this, if you upload your .fla, I can add code to it.

  16. #16
    Member
    Join Date
    May 2006
    Posts
    31
    And I put those global variables in the frame's actions, right?

  17. #17
    Member
    Join Date
    Nov 2006
    Posts
    69
    Quote Originally Posted by eth123
    And I put those global variables in the frame's actions, right?
    Everything I have written is for frame 1 of the main layer.

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

HTML5 Development Center