A Flash Developer Resource Site

Results 1 to 6 of 6

Thread: Ball game with boundary troubles.

  1. #1

    Unhappy Ball game with boundary troubles.

    I have a ball game where the ball is suppesed to bounce off of the boundaries to get to the finish(the player draws a set of boundaries and then lets the ball fall and see if it makes it to the finish) similair to paintball at addicting games. It usually works but sometimes the ball goes through the boundaries. It is really bad when i have moving terrain because if the ball is going the opposite dirrection than the terrain it usually goes right through it. Also, I don't know if this can be fixed but sometimes the ball gets inside a line making up the terrain and starts bouncing around inside of the line. I have tried everything from increasing the frame rate to changing the variable values. please help.

    heres the code for the ball ( sorry it's so long it repeats once so that the ball bounces off the players terrain as well as the premade terrain):

    PHP Code:
    onClipEvent (load) {
        
    yspeed 0;
        
    xspeed 0;
        
    gravity 0.2;
        
    radius 5;
        
    friction 0.90;
        
    precision 420;
        
    bounces 0;
    }
    onClipEvent (enterFrame) {
        if (
    _root.go == true) {
            
    collisions 0;
            
    sum_x 0;
            
    sum_y 0;
            
    yspeed yspeed+gravity;
            for (
    x=1x<precisionx++) {
                
    spot_x _x+radius*Math.sin(x*360/precision);
                
    spot_y _y-radius*Math.cos(x*360/precision);
                if (
    _root.terrain.hitTest(spot_xspot_ytrue)) {
                    
    collisions++;
                    
    sum_x += spot_x;
                    
    sum_y += spot_y;
                }
            }
            if (
    collisions>0) {
                
    _root.last_hit._x old_x;
                
    _root.last_hit._y old_y;
                
    bounces++;
                
    _root.collisions.text "Bounces: "+bounces;
                
    ball_dir Math.atan(yspeed/(xspeed*-1))/(Math.PI/180);
                if ((
    xspeed*-1)<0) {
                    
    ball_dir += 180;
                }
                if ((
    xspeed*-1)>=&& yspeed<0) {
                    
    ball_dir += 360;
                }
                
    spot_x sum_x/collisions;
                
    spot_y sum_y/collisions;
                
    x_cat spot_x-_x;
                
    y_cat spot_y-_y;
                
    ball_coll Math.atan(y_cat/x_cat)/(Math.PI/180);
                if (
    x_cat<0) {
                    
    ball_coll += 180;
                }
                if (
    x_cat>=&& y_cat<0) {
                    
    ball_coll += 360;
                }
                
    ground_rotation ball_coll-90;
                if (
    ground_rotation<0) {
                    
    ground_rotation += 180;
                }
                
    bounce_angle 180-ball_dir-2*(ground_rotation);
                if (
    bounce_angle<0) {
                    
    bounce_angle += 360;
                }
                
    speed Math.sqrt((yspeed*yspeed)+(xspeed*xspeed));
                
    xspeed speed*Math.cos(bounce_angle*Math.PI/180)*friction;
                
    yspeed = (speed*Math.sin(bounce_angle*Math.PI/180))*-1*friction;
                
    _x old_x;
                
    _y old_y;
            }
            else{
                
    old_x _x;
                
    old_y _y;
            }
            
    _y _y+yspeed;
            
    _x _x+xspeed;
        }
    }
    onClipEvent (enterFrame) {
        if (
    _root.go == true) {
            
    collisions 0;
            
    sum_x 0;
            
    sum_y 0;
            
    yspeed yspeed+gravity;
            for (
    x=1x<precisionx++) {
                
    spot_x _x+radius*Math.sin(x*360/precision);
                
    spot_y _y-radius*Math.cos(x*360/precision);
                if (
    _root.terrain1.hitTest(spot_xspot_ytrue)) {
                    
    collisions++;
                    
    sum_x += spot_x;
                    
    sum_y += spot_y;
                }
            }
            if (
    collisions>0) {
                
    _root.last_hit._x old_x;
                
    _root.last_hit._y old_y;
                
    bounces++;
                
    _root.collisions.text "Bounces: "+bounces;
                
    ball_dir Math.atan(yspeed/(xspeed*-1))/(Math.PI/180);
                if ((
    xspeed*-1)<0) {
                    
    ball_dir += 180;
                }
                if ((
    xspeed*-1)>=&& yspeed<0) {
                    
    ball_dir += 360;
                }
                
    spot_x sum_x/collisions;
                
    spot_y sum_y/collisions;
                
    x_cat spot_x-_x;
                
    y_cat spot_y-_y;
                
    ball_coll Math.atan(y_cat/x_cat)/(Math.PI/180);
                if (
    x_cat<0) {
                    
    ball_coll += 180;
                }
                if (
    x_cat>=&& y_cat<0) {
                    
    ball_coll += 360;
                }
                
    ground_rotation ball_coll-90;
                if (
    ground_rotation<0) {
                    
    ground_rotation += 180;
                }
                
    bounce_angle 180-ball_dir-2*(ground_rotation);
                if (
    bounce_angle<0) {
                    
    bounce_angle += 360;
                }
                
    speed Math.sqrt((yspeed*yspeed)+(xspeed*xspeed));
                
    xspeed speed*Math.cos(bounce_angle*Math.PI/180)*friction;
                
    yspeed = (speed*Math.sin(bounce_angle*Math.PI/180))*-1*friction;
                
    _x old_x;
                
    _y old_y;
            }
            else{
                
    old_x _x;
                
    old_y _y;
            }
            
    _y _y+yspeed;
            
    _x _x+xspeed;
        }
    }
    onClipEvent (enterFrame) {
        if (
    _root.go == true) {
            
    collisions 0;
            
    sum_x 0;
            
    sum_y 0;
            
    yspeed yspeed+gravity;
            for (
    x=1x<precisionx++) {
                
    spot_x _x+radius*Math.sin(x*360/precision);
                
    spot_y _y-radius*Math.cos(x*360/precision);
                if (
    _root.terrain0.hitTest(spot_xspot_ytrue)) {
                    
    collisions++;
                    
    sum_x += spot_x;
                    
    sum_y += spot_y;
                }
            }
            if (
    collisions>0) {
                
    _root.last_hit._x old_x;
                
    _root.last_hit._y old_y;
                
    bounces++;
                
    _root.collisions.text "Bounces: "+bounces;
                
    ball_dir Math.atan(yspeed/(xspeed*-1))/(Math.PI/180);
                if ((
    xspeed*-1)<0) {
                    
    ball_dir += 180;
                }
                if ((
    xspeed*-1)>=&& yspeed<0) {
                    
    ball_dir += 360;
                }
                
    spot_x sum_x/collisions;
                
    spot_y sum_y/collisions;
                
    x_cat spot_x-_x;
                
    y_cat spot_y-_y;
                
    ball_coll Math.atan(y_cat/x_cat)/(Math.PI/180);
                if (
    x_cat<0) {
                    
    ball_coll += 180;
                }
                if (
    x_cat>=&& y_cat<0) {
                    
    ball_coll += 360;
                }
                
    ground_rotation ball_coll-90;
                if (
    ground_rotation<0) {
                    
    ground_rotation += 180;
                }
                
    bounce_angle 180-ball_dir-2*(ground_rotation);
                if (
    bounce_angle<0) {
                    
    bounce_angle += 360;
                }
                
    speed Math.sqrt((yspeed*yspeed)+(xspeed*xspeed));
                
    xspeed speed*Math.cos(bounce_angle*Math.PI/180)*friction;
                
    yspeed = (speed*Math.sin(bounce_angle*Math.PI/180))*-1*friction;
                
    _x old_x;
                
    _y old_y;
            }
            else{
                
    old_x _x;
                
    old_y _y;
            }
            
    _y _y+yspeed;
            
    _x _x+xspeed;
        }

    terrain1 is the existing background and terrain is drawn by the player.
    i can upload the .fla if that helps.
    Any tips or pieces of code would be greatly appreciated. Thank You

  2. #2
    I think it might help if you could see the game so hear is my website. It is the on the magic lines page. I haven't made a menu yet but i will. the red circle is there to skip levels for testing. Just click go on the first level to see what i mean by goining inside the line. Any advice, tips or opinions would be greatly appreciated.

    http://www.freewebs.com/mygamesrawsom/

    if the link doesn't work here is the address: http://www.freewebs.com/mygamesrawsom/
    Last edited by bob58; 09-09-2008 at 07:40 PM.

  3. #3
    Senior Member tonypa's Avatar
    Join Date
    Jul 2001
    Location
    Estonia
    Posts
    8,223
    Its the basic idea of using coordinates of a point to detect collision with line. Once the point is moving fast enough, it can start moving from one side of the line and end up on other side once the speed value is added. You could set up collision loop where you move the point by 1 pixel, check collisions, move again by 1 pixel until you have reached the speed value. Increased accuracy of course comes with the price of lower speed.

  4. #4
    How do you do that? Is there a specfic line of code for that or do you know of a tutorial that i could use. By the way your games are awsome.
    Last edited by bob58; 09-10-2008 at 07:25 AM.

  5. #5

    Angry Help

    Please, somebody help i really need to get this fixed. Can anyone help with the collision loop that tonya mentioned. I really have no idea how to do that. If anyone can help please do . I will upload the .fla.
    Attached Files Attached Files

  6. #6
    sorry i messed up the .fla here is the actual one
    Attached Files Attached Files

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