9 Replies Latest reply on Mar 16, 2007 7:49 AM by mclem

    Rounded object bounce

    Mike912
      I posted before about a game I'm making and how I can't get the ball to bounce off of a rounded object correctly.

      I used the code you gave me and it's working better, but the ball still doesn't seem to really pick up the object correctly still.

      Here is the basics of the game:
      http://img297.imageshack.us/my.php?image=99330798ks1.swf

      Here is the full code on the ball:

        • 1. Re: Rounded object bounce
          mclem Level 1
          you need to account for the height and width of the ball, my suggestion is to open the movieclip for the vall and place the ball directly in the center of the object. If you open the clip the cross that indicates the origin of the movieclip should be centered on top of the circle for the ball. If you do this then you can use the modified version of you code pasted below. the other option would be to place the ball at 0,0 and then you would only have to account for the width and height on the right and bottom detection.

          Mark C.

          • 2. Re: Rounded object bounce
            Mike912 Level 1
            Okay, this code works for picking up the ball, but the problem is that there is a delay on the ball when it bounces off the object.

            Like in the game example I gave you, the ball seems to slowly change direction.

            http://oneslime.net/ << This is somewhat what I'm looking for.
            • 3. Re: Rounded object bounce
              mclem Level 1
              This is a pretty unsophisticated fix, There are much more elegant solutions. I use the speed of the player paddle to calculate the x direction adjustment, i calculated the speed of the paddle by figuring out how much the paddle has moved in one frame.


              • 4. Re: Rounded object bounce
                Mike912 Level 1
                Okay, thats a bit better. Still a few issues with the ball going through the object (bouncing around in it?) and sometimes it gets stuck on the edge and shakes around.

                http://img472.imageshack.us/my.php?image=slimeballhx1.swf

                I used your code on the ball, but maybe there is some type of issue with the object.

                Here is the obect (player) code:

                • 5. Rounded object bounce
                  brad9376 Level 1
                  It looks like even though the ball has changed direction, due to the speed the ball it is too far into the players boundary. To fix this you may be able to move the ball back to the location it was before hitting the player.

                  • 6. Re: Rounded object bounce
                    Mike912 Level 1
                    Right, that works for if it hits the top of the ball, but what if it hits the side?

                    I think it's something with cos or sin, not really sure. I'm not very familiar with actionscript.
                    • 7. Re: Rounded object bounce
                      mclem Level 1
                      To calculate the new vector of the ball you need the angle of the ball at impact with player and the angle between the player and the ball.

                      1. Take the position of the ball and then position of the ball plus it's xspeed and yspeed, this gives you a line along the balls path to generate that vector,
                      2. Use the position of the ball and the position of the player and calculate that angle.
                      3. Add the angles together and you should have your new vector, take the speed of the ball ( the distance between the ball and it's next position)
                      4. Multiply it by the new vector,
                      5. You might have to account for which side of the player you are on and adjust the angle based on that.

                      f that sounds confusing, it's because it is, try reading up on trajectory or adding vectors.

                      I've attached the code to calculated the ball speed and the angle between two points.

                      Good Luck
                      Mark C.
                      • 8. Re: Rounded object bounce
                        haymeadow Level 1
                        I have a ball bounce i was messing with a while ago heres some of the code i used, maybe it will fill some gaps..

                        first key frame//,\

                        _x=10;
                        _y=200;
                        xspeed=Math.random()*60-30;
                        yspeed=Math.random()*60-30;
                        rightedge=550;
                        leftedge=0;
                        topedge=0;
                        bottomedge=370;
                        gravity=2;
                        drag=.98;
                        bounce=.9;

                        second frame//

                        if (!dragging) {
                        _x = _x+xspeed;
                        if (_x+_width/2>rightedge) {
                        _x = rightedge-_width/2;
                        xspeed = -xspeed*bounce;
                        }
                        if (_x-_width/2<leftedge) {
                        _x = leftedge+_width/2;
                        xspeed = -xspeed*bounce;
                        }
                        _y = _y+yspeed;
                        if (_y+_height/2>bottomedge) {
                        _y = bottomedge-_height/2;
                        yspeed = -yspeed*bounce;
                        }
                        if (_y-_height/2<topedge) {
                        _y = topedge+_height/2;
                        yspeed = -yspeed*bounce;
                        }
                        yspeed = yspeed*drag+gravity;
                        xspeed = xspeed*drag;
                        } else {
                        xspeed=_x-oldx;
                        yspeed=_y-oldy;
                        oldx=_x;
                        oldy=_y;
                        }

                        third frame//

                        gotoAndPlay(2);

                        I have it so i can grab and fling it and the ball bounces all around and then comes to a stop... I have to try hitting something with it next...

                        • 9. Re: Rounded object bounce
                          mclem Level 1
                          I also found this great tutorial on realistic ball movement, check it out and see what you think http://www.actionscript.org/resources/articles/562/1/Realistic-ball-movement/Page1.html