Brian Chia
       
 
categories
actionscript
actionscript 1.0
actionscript 2.0
misc
 
processing
 
links
brian chia
kelvin zhao
ronald leong
 
 
Rebounce v02
 

Seen the bit-101 post and it seriously wake me up. Sometime thing are so simple and yet i didnt realise it. Here the main script which bit-101 had written that woke me up:

// rotate the scene to make it as if wall was lying flat
// -angle is the amount we need to rotate it.
// first rotate ball's position:
x1=Math.cos(angle)*x+Math.sin(angle)*y;
y1=Math.cos(angle)*y-Math.sin(angle)*x;

To do a rebounce off an angle surface simply mean rotate the surface to 0 degree (a flat horizontal angle), perform the rebounce and lastly rotate the angle back.

Took bit-101 script in the post and modify it using my Vector Class. Make the script look neater and shorter.


import Vector;

var v:Vector=new Vector(0,0);
var gravity:Vector=new Vector(0,.3);
var friction:Number=-(f_mc._x-fl_mc._x)/fl_mc._width;

// rotate line to an angle
line._rotation=25;

this.onEnterFrame=function():Void{

bounce();

}

function bounce():Void{

// add gravity to velocity to create acceleration
v.plus(gravity);
ball._x+=v.vx;
ball._y+=v.vy;

// get the position of ball in relation to wall
var v1:Vector=new Vector(ball._x-line._x,ball._y-line._y);

// rotate the line to a flat horizontal angle
v1.rotate(line._rotation);

// check ball is behind line
if(v1.vy>-ball._height/2){

// rotate velocity
v.rotate(line._rotation);

// position ball, reverse the y velocity and lose momentum
v1.reset(v1.vx,-ball._height/2);
v.reset(v.vx,v.vy*friction);

// rotate everything back
v1.rotate(-line._rotation);
v.rotate(-line._rotation);

// position ball position
ball._x=line._x+v1.vx;
ball._y=line._y+v1.vy;

}

}



suggestion

 
 
 
         
  © copyright 2007 THINKLUNATIC.    
Brian Chia Jonathan Ng