フォワードキネマティクス:歩行
/**
* Copyright _wonder ( http://wonderfl.net/user/_wonder )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/z28L
*/
// forked from _wonder's フォワードキネマティクス(2)
// forked from _wonder's フォワードキネマティクス基礎
package {
import flash.display.Sprite;
import flash.events.Event;
import flash.geom.Point;
public class SingleSegment extends Sprite {
private var segment0:Segment;
private var segment1:Segment;
private var segment2:Segment;
private var segment3:Segment;
private var speedSlider:SimpleSlider;
private var thighRangeSlider:SimpleSlider;
private var thighBaseSlider:SimpleSlider;
private var calfRangeSlider:SimpleSlider;
private var calfOffsetSlider:SimpleSlider;
private var gravitySlider:SimpleSlider;
private var cycle:Number = 0;
private var offset:Number = -1.7;
private var vx:Number = 0;
private var vy:Number = 0;
public function SingleSegment() {
init();
}
private function init():void {
segment0 = new Segment(50, 15);
addChild( segment0 );
segment0.x = 200;
segment0.y = 200;
segment1 = new Segment(50, 15);
addChild( segment1 );
segment1.x = segment0.getPin().x;
segment1.y = segment0.getPin().y;
segment2 = new Segment(50, 15);
addChild( segment2 );
segment2.x = 200;
segment2.y = 200;
segment3 = new Segment(50, 15);
addChild( segment3 );
segment3.x = segment2.getPin().x;
segment3.y = segment2.getPin().y;
speedSlider = new SimpleSlider( 0, 0.3, 0.12 );
addChild( speedSlider );
speedSlider.x = 10;
speedSlider.y = 10;
thighRangeSlider = new SimpleSlider( 0, 90, 45 );
addChild( thighRangeSlider );
thighRangeSlider.x = 30;
thighRangeSlider.y = 10;
thighBaseSlider = new SimpleSlider( 0, 180, 90 );
addChild( thighBaseSlider );
thighBaseSlider.x = 50;
thighBaseSlider.y = 10;
calfRangeSlider = new SimpleSlider( 0, 90, 45 );
addChild( calfRangeSlider );
calfRangeSlider.x = 70;
calfRangeSlider.y = 10;
calfOffsetSlider = new SimpleSlider( -3.14, 3.14, -1.57 );
addChild( calfOffsetSlider );
calfOffsetSlider.x = 90;
calfOffsetSlider.y = 10;
gravitySlider = new SimpleSlider( 0, 1, 0.2 );
addChild( gravitySlider );
gravitySlider.x = 110;
gravitySlider.y = 10;
addEventListener(Event.ENTER_FRAME, onEnterFrame);
}
private function onEnterFrame(e:Event):void {
doVelocity();
cycle += speedSlider.value;
walk(segment0, segment1, cycle);
walk(segment2, segment3, cycle + Math.PI);
checkFloor( segment1 );
checkFloor( segment3 );
checkWalls();
}
private function doVelocity():void {
vy += gravitySlider.value;
segment0.x += vx;
segment0.y += vy;
segment2.x += vx;
segment2.y += vy;
}
private function checkFloor(seg:Segment):void {
var yMax:Number = seg.getBounds(this).bottom;
if( yMax > stage.stageHeight ){
var dy:Number = yMax - stage.stageHeight;
segment0.y -= dy;
segment1.y -= dy;
segment2.y -= dy;
segment3.y -= dy;
vx -= seg.vx;
vy -= seg.vy;
}
}
private function checkWalls():void {
var w:Number = stage.stageWidth + 200;
if( segment0.x > stage.stageWidth + 100 ){
segment0.x -= w;
segment1.x -= w;
segment2.x -= w;
segment3.x -= w;
} else if ( segment0.x < -100 ){
segment0.x += w;
segment1.x += w;
segment2.x += w;
segment3.x += w;
}
}
private function walk(segA:Segment, segB:Segment, cyc:Number):void {
var foot:Point = segB.getPin();
var angleA:Number = Math.sin( cyc ) * thighRangeSlider.value + thighBaseSlider.value;
var angleB:Number = Math.sin( cyc+calfOffsetSlider.value ) * calfRangeSlider.value + calfRangeSlider.value;
segA.rotation = angleA;
segB.rotation = segA.rotation + angleB;
segB.x = segA.getPin().x;
segB.y = segA.getPin().y;
segB.vx = segB.getPin().x - foot.x;
segB.vy = segB.getPin().y - foot.y;
}
}
}
import flash.display.Sprite;
import flash.geom.Point;
class Segment extends Sprite {
private var color:uint;
private var segmentWidth:Number;
private var segmentHeight:Number;
public var vx:Number = 0;
public var vy:Number = 0;
public function Segment(segmentWidth:Number,segmentHeight:Number,color:uint=0xffffff){
this.segmentWidth = segmentWidth;
this.segmentHeight = segmentHeight;
this.color = color;
init();
}
public function init():void {
//セグメントの描画
graphics.lineStyle( 0 );
graphics.beginFill( color );
graphics.drawRoundRect( -segmentHeight / 2, -segmentHeight / 2, segmentWidth+segmentHeight, segmentHeight, segmentHeight, segmentHeight );
graphics.endFill();
//ピン
graphics.drawCircle( 0, 0, 2 );
graphics.drawCircle( segmentWidth, 0, 2 );
}
public function getPin():Point {
var angle:Number = rotation * Math.PI / 180;
var xPos:Number = x + Math.cos( angle ) * segmentWidth;
var yPos:Number = y + Math.sin( angle ) * segmentWidth;
return new Point( xPos, yPos );
}
}
import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.geom.Rectangle;
import flash.events.Event;
class SimpleSlider extends Sprite
{
private var _width:Number = 16;
private var _height:Number = 100;
private var _value:Number;
private var _max:Number = 100;
private var _min:Number = 0;
private var _handle:Sprite;
private var _back:Sprite;
private var _backWidth:Number = 4;
private var _handleHeight:Number = 6;
private var _backColor:uint = 0xcccccc;
private var _backBorderColor:uint = 0x999999;
private var _handleColor:uint = 0xeeeeee;
private var _handleBorderColor:uint = 0xcccccc;
private var _handleRadius:Number = 2;
private var _backRadius:Number = 2;
public function SimpleSlider(min:Number=0, max:Number=100, value:Number=100)
{
_min = min;
_max = max;
_value = Math.min(Math.max(value, min), max)
init();
}
private function init():void
{
_back = new Sprite();
addChild(_back);
_handle = new Sprite();
addChild(_handle);
_handle.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
draw();
updatePosition();
}
private function draw():void
{
drawBack();
drawHandle();
}
private function drawBack():void
{
_back.graphics.clear();
_back.graphics.beginFill(_backColor);
_back.graphics.lineStyle(0, _backBorderColor);
_back.graphics.drawRoundRect(0, 0, _backWidth, _height, _backRadius, _backRadius);
_back.graphics.endFill();
_back.x = _width / 2 - _backWidth / 2;
}
private function drawHandle():void
{
_handle.graphics.clear();
_handle.graphics.beginFill(_handleColor);
_handle.graphics.lineStyle(0, _handleBorderColor);
_handle.graphics.drawRoundRect(0, 0, _width, _handleHeight, _handleRadius, _handleRadius);
_handle.graphics.endFill();
}
private function updatePosition():void
{
var handleRange:Number = _height - _handleHeight;
var valueRange:Number = _max - _min;
_handle.y = handleRange - (_value - _min) / valueRange * handleRange;
}
private function updateValue():void
{
var handleRange:Number = _height - _handleHeight;
var valueRange:Number = _max - _min;
_value = (handleRange - _handle.y) / handleRange * valueRange + _min;
dispatchEvent(new Event(Event.CHANGE));
}
private function onMouseUp(event:MouseEvent):void
{
stage.removeEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
stage.removeEventListener(MouseEvent.MOUSE_UP, onMouseUp);
_handle.stopDrag();
}
private function onMouseDown(event:MouseEvent):void
{
stage.addEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
stage.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
_handle.startDrag(false, new Rectangle(0, 0, 0, _height - _handleHeight));
}
private function onMouseMove(event:MouseEvent):void
{
updateValue();
}
public function invalidate():void
{
draw();
}
public function move(x:Number, y:Number):void
{
this.x = x;
this.y = y;
}
public function setSize(w:Number, h:Number):void
{
_width = w;
_height = h;
draw();
}
public function set backBorderColor(n:uint):void
{
_backBorderColor = n;
draw();
}
public function get backBorderColor():uint
{
return _backBorderColor;
}
public function set backColor(n:uint):void
{
_backColor = n;
draw();
}
public function get backColor():uint
{
return _backColor;
}
public function set backRadius(n:Number):void
{
_backRadius = n;
}
public function get backRadius():Number
{
return _backRadius;
}
public function set backWidth(n:Number):void
{
_backWidth = n;
draw();
}
public function get backWidth():Number
{
return _backWidth;
}
public function set handleBorderColor(n:uint):void
{
_handleBorderColor = n;
draw();
}
public function get handleBorderColor():uint
{
return _handleBorderColor;
}
public function set handleColor(n:uint):void
{
_handleColor = n;
draw();
}
public function get handleColor():uint
{
return _handleColor;
}
public function set handleRadius(n:Number):void
{
_handleRadius = n;
draw();
}
public function get handleRadius():Number
{
return _handleRadius;
}
public function set handleHeight(n:Number):void
{
_handleHeight = n;
draw();
updatePosition();
}
public function get handleHeight():Number
{
return _handleHeight;
}
override public function set height(n:Number):void
{
_height = n;
draw();
}
override public function get height():Number
{
return _height;
}
public function set max(n:Number):void
{
_max = n;
updatePosition();
}
public function get max():Number
{
return _max;
}
public function set min(n:Number):void
{
_min = n;
updatePosition();
}
public function get min():Number
{
return _min;
}
public function set value(n:Number):void
{
_value = n;
_value = Math.min(_max, Math.max(_value, _min));
updatePosition();
}
public function get value():Number
{
return _value;
}
override public function set width(n:Number):void
{
_width = n;
draw();
}
override public function get width():Number
{
return _width;
}
}