日曜日, 11月 23, 2008

forward kinematics

関節みたいな動きのプログラミング。
ActionScript 3.0アニメーションからporting。
PVectorクラスを使ってみた。
Processing Monster作りたい。

walking.pde

Segment s1, s2, s3, s4;
float cycle = 0;
float offset = -PI*0.5;

void setup() {
size(200, 200);
smooth();
frameRate(30);
fill(255);
s1 = new Segment(width*0.5, height*0.5, 30, 10);
s2 = new Segment(s1.getPin().x, s1.getPin().y, 30, 10);
s3 = new Segment(width*0.5, height*0.5, 30, 10);
s4 = new Segment(s3.getPin().x, s3.getPin().y, 30, 10);
}

void draw() {
background(255);
walk(s1, s2, cycle);
walk(s3, s4, cycle+PI);
cycle += .2;
}

void walk(Segment segA, Segment segB, float cyc) {
float angleA = sin(cyc) * HALF_PI*0.25 + HALF_PI;
float angleB = sin(cyc+offset) * HALF_PI*0.17 + HALF_PI*0.12;
segA.rotation = angleA;
segB.rotation = segA.rotation + angleB;
segB.x = segA.getPin().x;
segB.y = segA.getPin().y;
segA.render();
segB.render();
}

Segment.pde

class Segment {
float x;
float y;
float w;
float h;
float rotation;
PVector v;

Segment(float x, float y, float w, float h) {
this.x = x;
this.y = y;
this.w = w;
this.h = h;
v = new PVector();
}

void render() {
pushMatrix();
translate(x, y);
rotate(rotation);
rect(0, -h*0.5, w, h);
ellipse(0, 0, 2, 2);
ellipse(w, 0, 2, 2);
popMatrix();
}

PVector getPin() {
v.x = x + cos(rotation) * w;
v.y = y + sin(rotation) * w;
return v;
}
}

0 件のコメント: