Flash/Flex学习笔记(47)：反向运动学(上)

package {
import flash.display.Sprite;
import flash.events.Event;

public class OneSegmentDrag extends Sprite {
private var segment0:Segment;
public function OneSegmentDrag() {
init();
}

private function init():void {
segment0=new Segment(100,20);
segment0.x=stage.stageWidth/2;
segment0.y=stage.stageHeight/2;
}

private function EnterFrameHandler(event:Event):void {
var dx:Number=mouseX-segment0.x;
var dy:Number=mouseY-segment0.y;
var angle:Number=Math.atan2(dy,dx);
segment0.rotation=angle*180/Math.PI;
var w:Number=segment0.getPin().x-segment0.x;
var h:Number=segment0.getPin().y-segment0.y;
segment0.x=mouseX-w;
segment0.y=mouseY-h;
}

}
}


var seg1:Segment = new Segment(100,20);
seg1.x = 100;
seg1.y = 50;

var seg2:Segment = new Segment(100,10);
seg2.x = seg1.getPin().x;
seg2.y = seg1.getPin().y;

function EnterFrameHandler(e:Event):void{
//seg2跟着鼠标动
var dx1:Number = mouseX - seg2.x;
var dy1:Number = mouseY - seg2.y;
var angle1:Number = Math.atan2(dy1,dx1);
seg2.rotation = angle1*180/Math.PI;
var w1:Number = seg2.getPin().x - seg2.x;
var h1:Number = seg2.getPin().y - seg2.y;
seg2.x = mouseX-w1;
seg2.y = mouseY-h1;

//seg1跟着seg2动
var dx2:Number = seg2.x - seg1.x;
var dy2:Number = seg2.y - seg1.y;
var angle2:Number = Math.atan2(dy2,dx2);
seg1.rotation = angle2*180/Math.PI;
var w2:Number = seg1.getPin().x - seg1.x;
var h2:Number = seg1.getPin().y - seg1.y;
seg1.x = seg2.x -w2;
seg1.y = seg2.y -h2;
}


var segNum:Number=10;
var segs:Array=new Array(segNum);

function init():void {
for (var i:int=0; i<segNum; i++) {
segs[i]=new Segment(40,12);
segs[i].x=stage.stageWidth/2;
segs[i].y=stage.stageHeight/2;
}
}

function EnterFrameHandler(e:Event):void {
drag(segs[segNum-1],mouseX,mouseY);
for (var i:int=segNum-2; i>=0; i--) {
drag(segs[i],segs[i+1].x,segs[i+1].y);
}
}

//拖动的封装
function drag(seg:Segment,targetX:Number,targetY:Number):void {
var dx:Number=targetX-seg.x;
var dy:Number=targetY-seg.y;
var angle:Number=Math.atan2(dy,dx);
seg.rotation=angle*180/Math.PI;
var w:Number=seg.getPin().x-seg.x;
var h:Number=seg.getPin().y-seg.y;
seg.x=targetX-w;
seg.y=targetY-h;
}

init();


ok，如果感觉还不是太晕的话，再来小结下“正向运动”与“反向运动”的区别：

1.手臂seg1，先绕着自己的固定点(x,y)，朝鼠标所在点(mousex,mousey)旋转

2.得到一个新的点(tx,ty)--红色的实线三角形，然后胳膊seg2以(tx,ty)为目标旋转

3.重新将seg1挂到seg2的自由端

4.重复以上处理，直到二个关节的姿态不再有任何变化为止

var seg1:Segment=new Segment(80,10);
seg1.x=130;
seg1.y=160;

var seg2:Segment=new Segment(60,20);
seg2.x=seg1.getPin().x;
seg2.y=seg1.getPin().y;

function init():void {
}

function EnterFrameHandler(e:Event):void {
var dx:Number=mouseX-seg1.x;
var dy:Number=mouseY-seg1.y;
var angle:Number=Math.atan2(dy,dx);
seg1.rotation=angle*180/Math.PI;
var w:Number=seg1.getPin().x-seg1.x;
var h:Number=seg1.getPin().y-seg1.y;
var tx:Number=mouseX-w;
var ty:Number=mouseY-h;

dx=tx-seg2.x;
dy=ty-seg2.y;

angle=Math.atan2(dy,dx);
seg2.rotation=angle*180/Math.PI;
seg1.x=seg2.getPin().x;
seg1.y=seg2.getPin().y;
}

init();


function EnterFrameHandler(e:Event):void {
var dx:Number=mouseX-seg1.x;
var dy:Number=mouseY-seg1.y;
var angle:Number=Math.atan2(dy,dx);
seg1.rotation=angle*180/Math.PI;
var w:Number=seg1.getPin().x-seg1.x;
var h:Number=seg1.getPin().y-seg1.y;
var tx:Number=mouseX-w;
var ty:Number=mouseY-h;

dx=tx-seg2.x;
dy=ty-seg2.y;

angle=Math.atan2(dy,dx);
seg2.rotation=angle*180/Math.PI;
seg1.x=seg2.getPin().x;
seg1.y=seg2.getPin().y;

//新增的画线部分，以方便观察
graphics.clear();
graphics.lineStyle(1,0xff0000,0.5);
graphics.moveTo(mouseX,mouseY);
graphics.lineTo(seg1.getPin().x,seg1.getPin().y);

graphics.lineStyle(1,0x0000ff,0.5);
graphics.moveTo(mouseX,mouseY);
graphics.lineTo(seg2.getPin().x,seg2.getPin().y);

}


var segNum:uint=10;
var segs:Array=new Array(segNum);
var i:int=0;

function init():void {
for (i=0; i<segNum; i++) {
segs[i]=new Segment(50,10+2*i);
segs[i].x=30;
segs[i].y=30;
}
}

function EnterFrameHandler(e:Event):void {
var target:Point=expand(segs[0],mouseX,mouseY);

//记住：第一个为伸展末端，最后一个为固定端
for (i=1; i<segNum; i++) {
target=expand(segs[i],target.x,target.y);
}

//倒挂所有Segment
for (i = segNum - 1; i > 0; i--) {
hang(segs[i-1],segs[i]);
}

}

function expand(seg:Segment,targetX:Number,targetY:Number):Point {
var dx:Number=targetX-seg.x;
var dy:Number=targetY-seg.y;
var angle:Number=Math.atan2(dy,dx);
seg.rotation=angle*180/Math.PI;
var w:Number=seg.getPin().x-seg.x;
var h:Number=seg.getPin().y-seg.y;
var tx:Number=targetX-w;
var ty:Number=targetY-h;
return new Point(tx,ty);
}

function hang(segA:Segment,segB:Segment) {
segA.x=segB.getPin().x;
segA.y=segB.getPin().y;
}

init();


var segNum:uint=9;
var segs:Array=new Array(segNum);
var i:int=0;
var ball:Ball;
var gravity:Number=0.3;
var bounce:Number=-0.95;

function init():void {
ball = new Ball(15);
ball.vx=10;

for (i=0; i<segNum; i++) {
segs[i]=new Segment(45,10+1*i);
segs[i].x=30;
segs[i].y=30;
}

}

function EnterFrameHandler(e:Event):void {
moveBall();//让小球动起来

var target:Point=expand(segs[0],ball.x,ball.y);//最后一个跟着小球跑

for (i=1; i<segNum; i++) {
target=expand(segs[i],target.x,target.y);
}

for (i = segNum - 1; i > 0; i--) {
hang(segs[i-1],segs[i]);
}
}

function expand(seg:Segment,targetX:Number,targetY:Number):Point {
var dx:Number=targetX-seg.x;
var dy:Number=targetY-seg.y;
var angle:Number=Math.atan2(dy,dx);
seg.rotation=angle*180/Math.PI;
var w:Number=seg.getPin().x-seg.x;
var h:Number=seg.getPin().y-seg.y;
var tx:Number=targetX-w;
var ty:Number=targetY-h;
return new Point(tx,ty);
}

function hang(segA:Segment,segB:Segment) {
segA.x=segB.getPin().x;
segA.y=segB.getPin().y;
}

function moveBall():void {
ball.vy+=gravity;
ball.x+=ball.vx;
ball.y+=ball.vy;
ball.vx*=bounce;
} else if (ball.x - ball.radius < 0) {
ball.vx*=bounce;
}
ball.vy*=bounce;
} else if (ball.y - ball.radius < 0) {
ball.vy*=bounce;
}
}

init();


var segNum:uint=4;
var segs:Array=new Array(segNum);
var i:int=0;
var ball:Ball;
var gravity:Number=0.2;
var bounce:Number=-0.95;

function init():void {
ball=new Ball(15);
ball.vx=10;

for (i=0; i<segNum; i++) {
segs[i]=new Segment(45,10+1*i);
segs[i].x=300;
segs[i].y=385;
}

}

function EnterFrameHandler(e:Event):void {
moveBall();

var target:Point=expand(segs[0],ball.x,ball.y);

for (i=1; i<segNum; i++) {
target=expand(segs[i],target.x,target.y);
}

for (i = segNum - 1; i > 0; i--) {
hang(segs[i-1],segs[i]);
}

checkHit();//碰撞检测
}

function expand(seg:Segment,targetX:Number,targetY:Number):Point {
var dx:Number=targetX-seg.x;
var dy:Number=targetY-seg.y;
var angle:Number=Math.atan2(dy,dx);
seg.rotation=angle*180/Math.PI;
var w:Number=seg.getPin().x-seg.x;
var h:Number=seg.getPin().y-seg.y;
var tx:Number=targetX-w;
var ty:Number=targetY-h;
return new Point(tx,ty);
}

function hang(segA:Segment,segB:Segment) {
segA.x=segB.getPin().x;
segA.y=segB.getPin().y;
}

function moveBall():void {
ball.vy+=gravity;
ball.x+=ball.vx;
ball.y+=ball.vy;
ball.vx*=bounce;
} else if (ball.x - ball.radius < 0) {
ball.vx*=bounce;
}
ball.vy*=bounce;
} else if (ball.y - ball.radius < 0) {
ball.vy*=bounce;
}
}

function checkHit():void {
var segment:Segment=segs[0];
var dx:Number=segment.getPin().x-ball.x;
var dy:Number=segment.getPin().y-ball.y;
var dist:Number=Math.sqrt(dx*dx+dy*dy);
ball.vx+=Math.random()*2-1;//给小球一个很小的随机x轴向速度(这样看起来小球会在x轴上突然慢下来)
ball.vy-=1.001;//y轴速度反向(这样看上去，会被反扔出去了)
}
}

init();


|

flash/flex 之 自定义MXML组件
1. 新建 mxml组件 代码如下                         [Bindable]            private var textstr:String = "字符串按钮";            public function set BTNtext(par_...
986 0
|

Flash/Flex学习笔记(57)：实用技巧

766 0
|

Flash/Flex学习笔记(56)：矩阵变换

858 0
|

Flash/Flex学习笔记(48)：反向运动学(下)

934 0
|

Flash/Flex学习笔记(52)：使用TweenLite
932 0
|
Java 索引 内存技术
Flash/Flex学习笔记(49)：3D基础

834 0
|
Java 内存技术 Spring
Flash/Flex学习笔记(44)：万有引力与粒子系统

808 0
Flash/Flex学习笔记(54)：迷你滚动条ScrollBar

944 0
|

Flash/Flex学习笔记(24)：粒子效果

999 0

DDNS