数学、物理算法ActionScript实现(1)

数学、物理算法ActionScript实现(1)

1.计算基本的三角函数

正弦=对边/斜边 Math.sin(弧度值):Number

余弦=邻边/斜边 Math.cos(弧度值):Number

正切=对边/邻边 Math.tan(弧度值):Number

余切=邻边/对边 1/Math.tan(弧度值):Number

2.转换弧度为度和转换度为弧度

radians = degrees * Math.PI / 180;

degrees = radians * 180 / Math.PI;

3.旋转鼠标位置

dx = mouseX - sprite.x;

dy = mouseY - sprite.y;

sprite.rotation = Math.atan2(dy,dx) * 180 / Math.PI;

4.创建波

public function Active(e:Event)

{

     value = center + Math.sin(angle) * range;

     angle += speed;
}

5.创建圆

public function Active(e:Event)

{

     xpos = centerX + Math.cos(angle) * radius;

     ypos = centerY + Math.sin(angle) * radius;

     angle += speed;

}

6.创建椭圆

if(sprite.x - sprite.width/2 > right)

{

     sprite.x = left - sprite.width/2;

}

else if(sprite.x + sprite.width/2 < left)

{

     sprite.x = right + sprite.width/2;

}

public function Active(e:Event)

{

     xpos += centerX + Math.cos(angle) * radiusA;

     ypos += centerY + Math.sin(angle) * radiusB;

     angle += speed;

}

7.获得两点之间的距离

dx = x2 - x1;

dy = y2 - y1;

distance = Math.sqrt(dx*dx + dy*dy);

8.相互转换16进制和10进制

trace(hexValue);

trace(decimalValue.toString(16));

9.组合组成色

color24 = red<<16 | green<<8 | blue;

color32 = alpha<<24 | red<<16 | green<<8 | blue;

10.提取组成色

red = color24 >>16;

green = color24 >>8 & 0xFF;

blue = color24 & 0xFF;

 

alpha = color32 >>24;

red = color32 >>16 & 0xFF;

green = color32 >>8 & 0xFF;

blue = color32 & 0xFF;

11.通过一点绘制曲线

//xt,yt是要穿过的点  x0,y0和x2,y2是曲线的端点

x1 = xt * 2 -(x0 + x2) /2;

y1 = yt * 2 -(y0 + y2) /2;

moveTo(x0,y0);

curveTo(x1,y1,x2,y2);

12.转换角速度为x,y速度

vx = speed * Math.cos(angle);

vy = speed * Math.sin(angle);

13.转换角加速度为x,y的加速度

ax = force * Math.cos(angle);

ay = force * Math.sin(angle);

14.将加速度加到速度上,将速度加大位移上

vx += ax;

vy += ay;

sprite.x += vx;

sprite.y += vy;

15.移除超过边界的物体

if(sprite.x-sprite.width/2 > right || sprite.x+sprite.width/2 < left || sprite.y-sprite.height/2 > bottom || sprite.y+sprite.height/2 <top)

{

     removeChild(sprite);

}

16.重新生成超出边界的物体

if(sprite.x-sprite.width/2 > right || sprite.x+sprite.width/2 < left || sprite.y-sprite.height/2 > bottom || sprite.y+sprite.height/2 <top)

{

     //重置精灵的位置和速度;

}

17.屏幕反弹折回

if(sprite.x - sprite.width/2 > right)

{

     sprite.x = left - sprite.width/2;

}

else if(sprite.x + sprite.width/2 < left)

{

     sprite.x = right + sprite.width/2;

}

if(sprite.y - sprite.height/2 > bottom)

{

     sprite.y = top - sprite.height/2;

}

else if(sprite.y + sprite.width/2 < top)

{

     sprite.y = bottom + sprite.height/2;

}

18.应用摩擦力

A:正确方法

speed = Math.sqrt(vx*vx +vy*vy);

angle = Math.atan2(dy,dx);

if(speed > friction)

{

     speed -= firction;

}

else

{

     speed = 0;

}

vx = Math.cos(angle) * speed;

vy = Math.sin(angle) * speed;

B:容易方法

vx *= friction;

vy *= friction;

你可能感兴趣的:(actionscript)