A Flash Developer Resource Site

Results 1 to 20 of 29

Thread: Ball inside Ball collision - stop bouncing

Threaded View

  1. #1
    bibuti. nolen's Avatar
    Join Date
    Sep 2002
    Location
    az.
    Posts
    191

    Ball inside Ball collision - stop bouncing

    I'm currently wrapping my head around vectors and have tried my hand at ball vs ball collisions.


    My problem comes when the ball has bounced enough and should finally be coming to rest.

    Code below:
    PHP Code:
    import com.nolentabner.Vector2D;

    // initialization of the balls
    var smallBall:Sprite = new Sprite();
    var 
    bigBall:Sprite = new Sprite();
    // bigger ball
    bigBall.graphics.lineStyle(10x000000);
    bigBall.graphics.drawCircle(00120);
    bigBall.stage.stageWidth/2;
    bigBall.stage.stageHeight/2;
    addChild(bigBall);
    // smaller ball
    smallBall.graphics.beginFill(0xFF0000);
    smallBall.graphics.drawCircle(0,040);
    smallBall.graphics.endFill();
    smallBall.stage.stageWidth/50;
    smallBall.stage.stageHeight/2;
    addChild(smallBall);

    // initialization of the vectors
    // main movement vector for smallBall
    var movement:Vector2D = new Vector2D(0, -2);
    // vector between ball centers
    var ballDist:Vector2D = new Vector2D(0,0);
    // used for collision reaction
    var projection:Vector2D = new Vector2D(0,0);
    var 
    proj2:Vector2D = new Vector2D(0,0);

    // step per frame. will eventually be replaced with a timer
    stage.addEventListener(Event.ENTER_FRAMEonFrame);

    // enterframe function
    function onFrame(e:Event):void
    {
        
    movement.+= 0.8;
        
        
    ballDist.bigBall.smallBall.x;
        
    ballDist.bigBall.smallBall.y;
        
        
    // if the smaller ball is outside the bigger ball
        // perform reaction
        
    if(ballDist.len+smallBall.width/bigBall.width 2)
        {    
            var 
    wall:Vector2D ballDist.leftNormal;
            var 
    dp:Number dot(movementwall.normal);
            var 
    dp2:Number dot(movementwall.leftNormal.normal);
            
    projection.dp*wall.normal.x;
            
    projection.dp*wall.normal.y;
            
    proj2.dp2*(wall.leftNormal.x/wall.len);
            
    proj2.dp2*(wall.leftNormal.y/wall.len);
            
    proj2.*= -1;
            
    proj2.*= -1;
            
    movement.projection.proj2.x;
            
    movement.projection.proj2.y;
            
    movement.*= 0.5;
            
    movement.*= 0.5;
            
            
    /*if(movement.len < 0.4)
            {
                movement.x = 0;
                movement.y = 0;
            }*/
        
    }
        
        
    smallBall.x+=movement.x;
        
    smallBall.y+=movement.y;
    }
    // finds the dot product between two vectors
    function dot(v1:Vector2Dv2:Vector2D):Number
    {
        return (
    v1.x*v2.v1.y*v2.y);

    PHP Code:
    package com.nolentabner
    {    
        public class 
    Vector2D extends Object
        
    {
            
    // x
            
    private var __x:Number;
            
    // y
            
    private var __y:Number;
            
    // length of our vector
            
    private var __len:Number;
            
    // our normal
            
    private var __normal:Vector2D;
            
    // our lefthand normal
            
    private var __leftNormal:Vector2D;
            
    // our righthand normal
            
    private var __rightNormal:Vector2D;
            
            
    // tracking variables
            
    private var lenUndefined:Boolean;
            
            public function 
    Vector2D(x:Number=0y:Number=0)
            {
                
    this.__x x;
                
    this.__y y;
                
                
    lenUndefined true;
            }
            
            
    /*** GETTERS/SETTERS ***/
            // x
            
    public function get x():Number
            
    {
                return 
    __x;
            }
            
            public function 
    set x(num:Number):void
            
    {
                
    __x num;
            }
            
            
    // y
            
    public function get y():Number
            
    {
                return 
    __y;
            }
            
            public function 
    set y(num:Number):void
            
    {
                
    __y num;
            }
            
            
    // length (read only)
            
    public function get len():Number
            
    {
                
    __len Math.sqrt(__x __x __y __y);
                
                return 
    __len;
            }
            
    // normal (read only)
            
    public function get normal():Vector2D
            
    {
                if(!
    __normal)
                {
                    
    //normalized unit-sized vector
                    
    __normal = new Vector2D();
                    
                    if (
    this.len>0)
                    {
                        
    __normal.__x/__len;
                        
    __normal.__y/__len;
                    } else
                    {
                        
    __normal.0;
                        
    __normal.0;
                    }
                }
                return 
    __normal;
            }
            
            
    // left hand normal (read only)
            
    public function get leftNormal():Vector2D
            
    {
                return new 
    Vector2D(__y__x * -1);
            }
            
            
    // right hand normal (read only)
            
    public function get rightNormal():Vector2D
            
    {
                return new 
    Vector2D(__y * -1__x);
            }
            
            
    /*** UTILITIES ***/
            
    public function normalize():void
            
    {
                if (
    this.len == 0)
                {
                    
    __x 0;
                    
    __y 0;
                }
                else 
                {
                    
    __x /= this.len;
                    
    __y /= this.len;
                }
            }
            
            public function 
    toString():String
            
    {
                var 
    st:String "Vector2D: x=" __x.toString() + " y=" __y.toString();
                
    st += " length="+len.toString();
                
                return 
    st;
            }
        }

    You can paste this into any version of Flash CS3 or greater.

    edit: I posted the Vector2D class as well. It's nothing amazing but works for now.

    You can see where i commented out a check where if the length of the movement vector is less than a specific amount, to set the x/y properties of the vector to 0. It somewhat worked but it didn't look natural.

    Can anyone take a look at this and help point me in the right direction? I just want a mostly-realistic collision reaction between two balls where one is inside the other.

    Here is an example of what happens for those who don't want to create their own .swf. I know how lazy some of you are out there .
    http://www.nolentabner.com/projects/ball/test.swf

    Thanks!
    Last edited by nolen; 03-02-2009 at 06:40 PM.
    i'm obsessed with video games.

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