2016-11-27 2 views
0

私はゲームをデザインしており、うまく動作しています。私の1つの条件は、スプライトをあるポイントから別のポイントに移動する現在の方法がうまくいくことです...しかし、方向はやや不正確です。スプライトがそのパスの最後の場所に「スナップ」していないと、それほど悪くはありません。ここで私の正規化されたベクトルの方向が間違っているのはなぜですか?

誰かが a)は私の方向 Bの精度を向上させる助けどちらかにする可能性のある提案があります)スプライトが緩やか

場所に来るのに役立つが、点Aから運動を計算するための私のコードですBへ:ここ

if (!mMoving) { 
     mMoving = true; 
     mVecStart = new Vec(mPos.getX(),mPos.getY()); 
     mVecEnd = new Vec(target.getX(), target.getY()); 

     mDistanceToDestination = Vec.distanceBetween(mVecEnd, mVecStart); 

     mDirection = mVecStart.directionTo(mVecEnd, mDistanceToDestination); 

     mVelocityX = mDirection.mDX * mSpeed; 
     mVelocityY = mDirection.mDY * mSpeed; 
    } 


    if (mMoving == true) { 
     // Move along the x and y axis at given velocity, scaled by deltaTime. 
     this.mPos.x += mVelocityX * deltaTime; 
     this.mPos.y += mVelocityY * deltaTime; 

     // If the distance traveled exceeds the original distance computed, snap the sprite 
     // into place immediately. 
     if (mMoving && Vec.distanceBetween(mVecStart, new Vec(this.mPos.x, this.mPos.y)) 
       >= mDistanceToDestination) { 
      this.mPos.x = (int) target.getX(); 
      this.mPos.y = (int) target.getY(); 
      mMoving = false; 
      mMoveComplete = true; 
     } 
    } 

は方向が計算されている方法です。

public Vec directionTo(Vec vecEnd, double distance) { 

    return new Vec((vecEnd.mDX - this.mDX)/distance, 
      ((vecEnd.mDY - this.mDY)/distance)); 
} 
+1

あなたの方向機能は私には意味がありません。開始点引数と終了点引数、または単一のベクトルを持つ必要があります。正規化は渡されません。ベクトル自体から計算する必要があります。ベクトルが間違っているか、大きさが正しくありません。あなたはテストケースが何であるか、あるいはあなたが得る答えを示さない。どちらもあなたが間違っていたことを理解するのに役立ちます。デバッガはここで尋ねるよりも速く伝えます。 – duffymo

答えて

1

あなたはトンを計算しますそれぞれの成分をその大きさで除算することによって与えられたベクトルの単位ベクトルです。

は、ここで私は直交座標系における平面ベクトルの大きさを計算したい方法は次のとおりです。

package vector; 

import java.awt.geom.Point2D; 

/** 
* Created by Michael 
* Creation date 11/26/2016. 
* @link 
*/ 
public class VectorUtils { 

    public static double magnitude(Point2D beg, Point2D end) { 
     double magnitude = 0.0; 
     if ((beg != null) && (end != null)) { 
      double dx = Math.abs(end.getX()-beg.getX()); 
      double dy = Math.abs(end.getY()-beg.getY()); 
      if ((dx == 0.0) && (dy == 0.0)) { 
       magnitude = 0.0; 
      } else { 
       if (dx > dy) { 
        double r = dy/dx; 
        magnitude = dx*Math.sqrt(1.0+r*r); 
       } else { 
        double r = dx/dy; 
        magnitude = dy*Math.sqrt(1.0+r*r); 
       } 
      } 
     } 
     return magnitude; 
    } 
} 

あなたのクラスをテストするJUnitの使い方を学ぶ必要があります。デバッガで時間を浪費したり、欠陥を頭で掻き集めることはありません。

package vector; 

import org.junit.Assert; 
import org.junit.Test; 

import java.awt.geom.Point2D; 

/** 
* Created by Michael 
* Creation date 11/26/2016. 
* @link 
*/ 
public class VectorUtilsTest { 

    public static final double TOLERANCE = 1.0E-16; 

    @Test 
    public void testMagnitude_NullArguments() { 
     // setup 
     Point2D beg = null; 
     Point2D end = null; 
     // exercise and assert 
     Assert.assertEquals(0.0, VectorUtils.magnitude(beg, end), TOLERANCE); 
    } 

    @Test 
    public void testMagnitude_ZeroVector() { 
     // setup 
     Point2D beg = new Point2D.Double(0.0, 0.0); 
     Point2D end = new Point2D.Double(0.0, 0.0); 
     // exercise and assert 
     Assert.assertEquals(0.0, VectorUtils.magnitude(beg, end), TOLERANCE); 
    } 

    @Test 
    public void testMagnitude_UnitX() { 
     // setup 
     Point2D beg = new Point2D.Double(0.0, 0.0); 
     Point2D end = new Point2D.Double(2.0, 0.0); 
     // exercise and assert 
     Assert.assertEquals(2.0, VectorUtils.magnitude(beg, end), TOLERANCE); 
    } 

    @Test 
    public void testMagnitude_UnitY() { 
     // setup 
     Point2D beg = new Point2D.Double(0.0, 0.0); 
     Point2D end = new Point2D.Double(0.0, 2.0); 
     // exercise and assert 
     Assert.assertEquals(2.0, VectorUtils.magnitude(beg, end), TOLERANCE); 
    } 

    @Test 
    public void testMagnitude() { 
     // setup 
     Point2D beg = new Point2D.Double(0.0, 0.0); 
     Point2D end = new Point2D.Double(1.0, 1.0); 
     // exercise and assert 
     Assert.assertEquals(Math.sqrt(2.0), VectorUtils.magnitude(beg, end), TOLERANCE); 
    } 
} 
+0

単体テストのレッスンをありがとう、それはまた私に恩恵を与えます。そして、大胆なプロフィール記述のための笑。 –

関連する問題