dcsimg
A Flash Developer Resource Site

Results 1 to 9 of 9

Thread: Reversing momentum with collissions

  1. #1
    Junior Member
    Join Date
    Apr 2009
    Posts
    21

    Question Reversing momentum with collissions

    I'm having problems getting my player to switch directions and just bounces off of the walls of the stage. I tried reversing the rate but it still shoots off into the distance and this is what I have now, it just stops the guy at the edge and nothing more. Does anyone know how I can properly make him bounce?

    Code:
    function collide(e:Event):void{
    	if(player.x > stage.stageWidth || player.x < 0){
    			player.x -= player_velocityX;
    			player.y += player_velocityY;
    		
    	}
    	if(player.y > stage.stageHeight || player.y <0){
    			player.y -= player_velocityY;
    			player.x += player_velocityX;
    	}
    	
    }
    I attached the code that starts him moving below:

    Code:
    	if(point_array.length == 4){
    			distance_x = point_array[2] - point_array[0];
    
    			distance_y = point_array[3] - point_array[1];
    
    		//get total distance to get rate
    		total_distance = distance_x + distance_y;
    
    		//Find the angle to move it along then get the X and Y velocity
    		var target_angle:Number = Math.atan2(distance_y,distance_x);
    		player_velocityX = Math.cos(target_angle)*rate;
    		player_velocityY = Math.sin(target_angle)*rate;
    
    	}//end of top if
    	
    	player.x += player_velocityX;
    	player.y += player_velocityY;
    		
    	if(rate > 0){
    		rate -= friction;
    		rate -= friction;
    	}else{
    		rate = 0;
    	}

  2. #2
    Ө_ө sleepy mod
    Join Date
    Mar 2003
    Location
    Oregon, USA
    Posts
    2,441
    Velocity would stay the same, you need to flip your angle of travel: target_angle += pi

  3. #3
    Junior Member
    Join Date
    Apr 2009
    Posts
    21
    Quote Originally Posted by neznein9 View Post
    Velocity would stay the same, you need to flip your angle of travel: target_angle += pi
    Thanks I'm gonna try that right now. I should probably switch it to a global variable too.


    I have one question though my math is kinda bad, wouldn't I do +=180 because wouldn't pi just at 3.whatever to the angle? Or do I have that mixed up?

  4. #4
    Ө_ө sleepy mod
    Join Date
    Mar 2003
    Location
    Oregon, USA
    Posts
    2,441
    Since you're splitting the velocity with sin/cos, the angle should be in radians right? (180 = PI radians)

  5. #5
    Junior Member
    Join Date
    Apr 2009
    Posts
    21
    Quote Originally Posted by neznein9 View Post
    Since you're splitting the velocity with sin/cos, the angle should be in radians right? (180 = PI radians)
    Wow I spaced on that one, yeah it's in radians. I tried to do it like this


    Code:
    function collide(e:Event):void{
    	if(player.x > stage.stageWidth || player.x < 0){
    			target_angle += Math.PI;
    
    		
    	}
    	if(player.y > stage.stageHeight || player.y <0){
    			target_angle += Math.PI;
    	}
    	
    }
    But no luck it just goes off screen.


    Edit: Figured it'd be a positive/negative thing so I tried adding or subtracting with an if else. Didn't work either. I'm really confused.

    Code:
    function collide(e:Event):void{
    	if(player.x > stage.stageWidth || player.x < 0){
    		if(target_angle > 0){
    			target_angle += Math.PI;
    		}else{
    			target_angle -= Math.PI;
    		}
    		
    	}
    	if(player.y > stage.stageHeight || player.y <0){
    			target_angle += Math.PI;
    	}
    	
    }
    Last edited by lawmas; 06-15-2009 at 03:26 AM.

  6. #6
    Ө_ө sleepy mod
    Join Date
    Mar 2003
    Location
    Oregon, USA
    Posts
    2,441
    Try this:

    PHP Code:
    function collide(e:Event):void{
        if(
    player.stageWidth || player.0player_velocityX = -playerVelocityX;
        if(
    player.stage.stageHeight || player.0player_velocityY = -playerVelocityY;
        
    player.+= player_velocityX;
        
    player.+= player_velocityY;


  7. #7
    Junior Member
    Join Date
    Apr 2009
    Posts
    21
    Yeah that's how I had it before but that's a cool way to write and if statement I didn't know you could do it like that.


    Now it'll just wedge itself on the side of the stage, not going off it but just wedged there.

    Code:
    function collide(e:Event):void{
        if(player.x > stage.stageWidth || player.x < 0){
    		player_velocityX = -player_velocityX
    	}
        if(player.y > stage.stageHeight || player.y < 0){
    		player_velocityY = -player_velocityY
    	}
        player.x += player_velocityX;
        player.y += player_velocityY;
    }

  8. #8
    Ө_ө sleepy mod
    Join Date
    Mar 2003
    Location
    Oregon, USA
    Posts
    2,441
    Crap - I am super slow on the uptake on this one! Everything is correct but when you get to the edge you're actually past it so things keep flipping back and forth (you never get back in bounds) -

    PHP Code:
    function collide(e:Event):void{
        if(
    player.stage.stageWidth){
            
    player_velocityX = -(Math.abs(player_velocityX));
            
    player.stage.stageWidth;
        }
        
        if(
    player.0){
            
    player_velocityX Math.abs(player_velocityX);
            
    player.0;
        }

        
    player.+= player_velocityX;


        
    //  repeat for y
        //  ... 
    If your velocity is a constant you could just use that instead of monkeying around with the absolute value stuff...or if optimization is important you can optimize it with some logic, although it's tougher to decipher

    PHP Code:
        if(player.stage.stageWidth){
            
    player_velocityX = (playerVelocityX 0) ? playerVelocityX : -playerVelocityX;
            
    player.stage.stageWidth;
        }
        
        if(
    player.0){
            
    player_velocityX = (playerVelocityX 0) ? playerVelocityX : -playerVelocityX;
            
    player.0;
        } 

  9. #9
    Junior Member
    Join Date
    Apr 2009
    Posts
    21
    Ok I swapped everything around for the first one. I couldn't really figure out the second one it's slightly above my head. Now it'll go onto the edge of the stage then bounce back some but then go back in the same direction and repeat till I pull it off.

    Does this mean we'd have to change the angle too on top of changing the velocity?


    Code:
    function collide(e:Event):void{
        if(player.x > stage.stageWidth){
            player_velocityX = -(Math.abs(player_velocityX));
            player.x = stage.stageWidth;
        }
        
        if(player.x < 0){
            player_velocityX = Math.abs(player_velocityX);
            player.x = 0;
        }
    
        player.x += player_velocityX; 
    	
    	if(player.y > stage.stageWidth){
            player_velocityY = -(Math.abs(player_velocityY));
            player.y = stage.stageWidth;
        }
        
        if(player.y < 0){
            player_velocityY = Math.abs(player_velocityY);
            player.y = 0;
        }
    
        player.y += player_velocityY; 
    	
    }

    Edit: Tried it with Math.PI, same effect:

    Code:
    	if(player.y > stage.stageWidth){
            player_velocityY = -(Math.abs(player_velocityY));
            player.y = stage.stageWidth;
    		target_angle += Math.PI;
    		}
        
        if(player.y < 0){
            player_velocityY = Math.abs(player_velocityY);
            player.y = 0;
    		target_angle += Math.PI;
        }
    Last edited by lawmas; 06-15-2009 at 05:58 PM.

Tags for this Thread

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