2016-08-16 10 views
1

キャンバス上に2つの点(x/y)があり、requestanimationframeまたはsetintervalを使用して直線の間で画像を移動できます。JS - 2Dベクトルをカーブに変換する

:?!

しかし、代わりに私は、このJSBinを作成して、何とか、したいと曲がったアニメーションでオブジェクトを移動、速度X/Y、特にS(ステップ)のベクトルの組み合わせによって う

http://jsbin.com/furomu/edit?html,js,console,output - (クリックしてください)。

この「ベクトル」をなんらかの形でカーブにして滑らかな動きを描くことは可能ですか? もしそうでなければ、私はそれを曲がった動きに変えるのに何か他の値を必要としますか?

//start at 50,50 
    //move to 150,125 
// Vector is 100/75 or v +1/+0.75 at 100 steps 


function Move(ox, oy, x, y){ 
    this.ox = ox; 
    this.oy = oy; 
    this.x = x 
    this.y = y 
    this.p; 
    this.v = {x: x - ox, y: y - oy}; 

    this.setup = function(){ 

    var p = {}; 
    var v = this.v; 

    if (this.x > this.y){ 
     p.s = v.y; 
     p.y = 1; 
     p.x = v.x/v.y; 
    } 
    else { 
     p.s = v.x; 
     p.x = 1; 
     p.y = v.y/v.x; 
    } 

    this.p = p; 
    } 

    this.setup(); 
} 


function Ship(x, y){ 
    this.x = x; 
    this.y = y; 

    this.moves = []; 

    this.draw = function(){ 
    this.drawSelf(); 
    this.drawTarget(); 
    } 

    this.create = function(){ 
    var move = new Move(this.x, this.y, 150, 125); 
    this.moves.push(move); 
    } 

    this.update = function(){ 
    var m = this.moves[0]; 
    var self = this; 

    anim = setInterval(function(){   
     ctx.clearRect(0, 0, res, res); 
     self.x += m.p.x; 
     self.y += m.p.y; 
     m.p.s--; 

    self.draw(); 

     if (m.p.s == 0){ 
     clearInterval(anim); 
     } 
    }, 30); 
    } 

    this.create(); 
    this.draw(); 
    this.update(); 
} 

function init(){ 
    var ship = new Ship(50, 50); 
     } 
+0

曲線を定義するには3点以上が必要です。 Googleのスプライン。ベロシティを持っている場合は、スタートポイントから最初のベロシティ方向に前方に突き出して3番目のポイントを定義することができます。 – samgak

答えて

2

このトピックhere

に関するいくつかの有用な情報があります基本的には、カーブにあなたのベクトルを「曲げ」するために、1つ以上のコントロールポイントを必要とします。

enter image description hereenter image description here

これは、単一の制御点については、以下の式を用いて達成することができる。 [x,y]=(1–t)²P0+2(1–t)tP1+t2²P2

t=0、右手側は、第1の制御点と等しい - 線分の開始。 t=1の場合、点P1、第2の制御点、および線分の終点が得られます。

関連する問題