読もう!!曲がるコミックビーム
とぎれるのかっこわるいなあ。
雷電レーザーのアルゴリズムをどなたか教えてください。。
/*
*
とぎれるのかっこわるいなあ。
雷電レーザーのアルゴリズムをどなたか教えてください。。
*/
package{
import flash.display.*;
import flash.events.*;
public class Beam05 extends Sprite{
private var spring:Spring;
private var head:BeamHead;
private function onEnterFrame(e:Event):void {
spring.draw();
head.x = mouseX;
head.y = mouseY;
}
public function Beam05() {
spring = new Spring;
head = new BeamHead;
addChild(spring);
addChild(head);
addEventListener(Event.ENTER_FRAME,onEnterFrame );
}
}
}
import flash.display.Sprite;
import flash.filters.GlowFilter;
import flash.display.Graphics;
import flash.events.Event;
import flash.geom.Point;
class BeamHead extends Sprite{
private var pts1:Array = new Array;
private var pts2:Array = new Array;
public function BeamHead(){
addEventListener(Event.ENTER_FRAME,update);
var i:int ;
var len:Number;
var r:Number;
for(i=0;i<30;i++){
len = Math.random()*10+5;
r = Math.PI*2/30;
pts1.push(new Point(Math.cos(r*i)*len,Math.sin(r*i)*len));
}
for(i=0;i<30;i++){
len = Math.random()*20+10;
r = Math.PI*2/30;
pts2.push(new Point(Math.cos(r*i)*len,Math.sin(r*i)*len));
}
filters = [new GlowFilter()];
}
public function update(e:Event):void{
graphics.clear();
var r:Number = Math.PI*2/30;
var i:int;
var len:Number;
graphics.lineStyle(1,0xff0000);
graphics.moveTo(pts1[0].x,pts1[0].y);
for(i=1;i<30;i++){
graphics.lineTo(pts1[i].x,pts1[i].y);
len = Math.random()*10+5;
pts1[i].x = Math.cos(r*i)*len;
pts1[i].y =Math.sin(r*i)*len;
}
graphics.lineTo(pts1[0].x,pts1[0].y);
pts1[0].x = Math.cos(r*i)*len;
pts1[0].y =Math.sin(r*i)*len;
graphics.lineStyle(1,0xffff00);
graphics.moveTo(pts2[0].x,pts2[0].y);
for(i=1;i<30;i++){
graphics.lineTo(pts2[i].x,pts2[i].y);
len = Math.random()*20+10;
pts2[i].x = Math.cos(r*i)*len;
pts2[i].y =Math.sin(r*i)*len;
}
graphics.lineTo(pts2[0].x,pts2[0].y);
pts2[0].x = Math.cos(r*i)*len;
pts2[0].y =Math.sin(r*i)*len;
}
}
class Spring extends Sprite{
//private var grav:Number = .3;
private var dist:Number = 10;//25;
private var k:Number = .80;//.55;
private var damp:Number = .5//.99;
private var cx:Number = 270;
private var cy:Number = 200;
// private var points:Array = [];
private var beams:Array = [];
private var numPoints:int = 15;
private var oneSlice:Number = Math.PI * 2 / numPoints;
private var radius:Number = 100;
public function Spring(){
for (var i:int = 0; i < numPoints; i++)
{
var angle:Number = oneSlice * i;
var x:Number = Math.cos(angle) * radius + cx;
var y:Number = Math.sin(angle) * radius + cy;
var b:BeamNode = new BeamNode;
b.x = x;
b.y = y;
b.a = angle - Math.PI / 2
b.vx = 0;
b.vy = 0;
beams[i] = b//{x:x, y:y, a:angle - Math.PI / 2,vx:0,vy:0};
addChild(b);
}
filters = [new GlowFilter()];
}
private const unit:Number = 4.0;
public function draw():void{
graphics.clear();
beams[numPoints-1].x = mouseX;
beams[numPoints-1].y = mouseY;
for (var i:int = 1; i < numPoints; i++){
beams[i].Draw();
}
var totalx:int = 0;
var totaly:int = 0;
for (i = 0; i < numPoints - 1; i++)
{
var point:BeamNode = beams[i];
spring(point, beams[i + 1]);
}
for (i = 1; i < numPoints; i++)
{
point = beams[i];
point.vx *= damp;
point.vy *= damp;
point.x += point.vx;
point.y += point.vy;
if (point.y > 380)
{
point.y = 380;
point.vy = 0;
}
if(point.x<20)
{
point.x=20;
point.vx=0;
}else if(point.x>520)
{
point.x=520;
point.vx=0;
}
}
}
public function spring(p0:BeamNode, p1:BeamNode):void
{
var dx:Number = p0.x - p1.x;
var dy:Number = p0.y - p1.y;
var rad:Number = Math.atan2(dy,dx)+Math.PI/2;
p0.rotation = rad/Math.PI*180;
p0.length = Math.sqrt(dx*dx+dy*dy);
var angle:Number = p0.a;
var tx:Number = p1.x + dist * Math.cos(angle);
var ty:Number = p1.y + dist * Math.sin(angle);
var ax:Number = (tx - p0.x) * k;
var ay:Number = (ty - p0.y) * k;
p0.vx += ax * .5;
p0.vy += ay * .5;
p1.vx -= ax * .5;
p1.vy -= ay * .5;
}
}
class BeamNode extends Sprite{
public var a:Number;
public var vx:Number;
public var vy:Number;
public var length:Number;
public function Draw():void{
graphics.clear();
graphics.lineStyle(1,0xff);
graphics.moveTo(0,0);
graphics.lineTo(0,length);
for(var i:int = 0;i<beams.length;i++){
beams[i].Draw(graphics,length);
}
}
private var beams:Array = new Array;
public function BeamNode(){
beams.push(new Beam(0xff,20));
beams.push(new Beam(0xffff00,5));
filters = [new GlowFilter()];
}
}
class Beam {
private var length:Number = 100;
private var unit:Number = 5;
private var col:uint;
private var thick:Number;
public function Beam(col:uint,thick:Number){
this.col = col;
this.thick = thick;
}
public function Draw(g:Graphics,length:int):void{
g.lineStyle(1,col);
g.moveTo(Math.random()*thick-thick/2,0);
for(var i:int = 0;i<length/unit;i++){
g.lineTo(Math.random()*thick-thick/2,i*unit);
}
}
}