角度がついたものへの反射
forked from base (diff: 70)
ActionScript3 source code
/**
* Copyright _wonder ( http://wonderfl.net/user/_wonder )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/Opgl
*/
// forked from _wonder's base
package {
import flash.display.Sprite;
import flash.events.Event;
import flash.geom.Rectangle;
import flash.geom.Rectangle;
public class FlashTest extends Sprite {
private var ball:Ball;
private var line:Sprite;
private var gravity:Number = 0.5;
private var bounce:Number = -0.8;
public function FlashTest() {
init();
}
private function init():void {
ball = new Ball(10);
addChild( ball );
ball.x = 100;
ball.y = 50;
line = new Sprite();
line.graphics.lineStyle(1);
line.graphics.lineTo( 200,0 );
addChild( line );
line.x = 50;
line.y = 100;
line.rotation = 10;
addEventListener(Event.ENTER_FRAME, onEnterFrame);
}
private function onEnterFrame(e:Event):void {
line.rotation = ( stage.stageWidth / 2 - mouseX ) * 0.1;
ball.vy += gravity;
ball.x += ball.vx;
ball.y += ball.vy;
var bounds:Rectangle = line.getBounds(this);
if( ball.x > bounds.left && ball.x < bounds.right ){
var angle:Number = line.rotation * Math.PI / 180;
var cos:Number = Math.cos( angle );
var sin:Number = Math.sin( angle );
var x1:Number = ball.x - line.x;
var y1:Number = ball.y - line.y;
var y2:Number = cos * y1 - sin * x1;
if( y2 > -ball.height / 2 ){
var x2:Number = cos * x1 + sin * y1;
var vx1:Number = cos * ball.vx + sin * ball.vy;
var vy1:Number = cos * ball.vy - sin * ball.vx;
y2 = -ball.height / 2;
vy1 *= bounce;
x1 = cos * x2 - sin * y2;
y1 = cos * y2 + sin * x2;
ball.vx = cos * vx1 - sin * vy1;
ball.vy = cos * vy1 + sin * vx1;
ball.x = line.x + x1;
ball.y = line.y + y1;
}
}
}
}
}
import flash.display.Sprite;
class Ball extends Sprite {
public var radius:Number;
public var color:uint;
public var vx:Number = 0;
public var vy:Number = 0;
public function Ball(radius:Number=40, color:uint=0Xff0000){
this.radius = radius;
this.color = color;
init();
}
public function init():void {
graphics.beginFill(color);
graphics.drawCircle(0, 0, radius);
graphics.endFill();
}
}
