2014-01-10 29 views
0

這是我的代碼:剛體衝撞相對速度計算

Vec2 fv = nv.scale(-((1D + aelas) * getLinearVelocity().sub(shape.getLinearVelocity()).dot(nv))); 

aelas是雙,設置爲1D(以後我可以改變一個函數) NV是單位正常矢量

我的問題是,當aelas爲0時碰撞只能與完美的彈性做出恰當的反應,但當它爲1時,它將它看作彈性> 2D(所以更多的能量熄滅然後進入)。

我的想法是,我的相對速度系統是不正確的,導致它不正確的反應。

我徹底調試了我的代碼,並且相當肯定它是我的數學。任何人都在意分享一些見解?

VEC2類別:

package JavaProphet.Profis.Geometry; 

/** 
* Created by JavaProphet on 12/8/13 at 12:28 PM. 
*/ 
public class Vec2 { 
private final double x; 
private final double y; 
public static final Vec2 ZERO_VEC = new Vec2(0D, 0D); 

/** 
* Empty Vector. 
*/ 
public Vec2() { 
    x = 0D; 
    y = 0D; 
} 

public String toString() { 
    return "<" + getX() + ", " + getY() + ">"; 
} 

/** 
* Uses basic trigonometry to create a Vec2 from a radian. 
* 
* @param rad The radian. 
*/ 
public Vec2(double rad) { 
    x = Math.sin(rad); 
    y = Math.cos(rad); 
} 

/** 
* Assigns a vector it's value. 
*/ 
public Vec2(double x, double y) { 
    this.x = x; 
    this.y = y; 
} 

/** 
* Assigns a vector it's value from a double array of length 2. 
*/ 
public Vec2(double v[]) { 
    this.x = v[0]; 
    this.y = v[1]; 
} 

/** 
* Assigns a vector it's value from subtraction of position. 
* 
* @param x x1 
* @param x2 x2 
* @param y y1 
* @param y2 y 
*/ 
public Vec2(double x, double y, double x2, double y2) { 
    this.x = x2 - x; 
    this.y = y2 - y; 
} 

/** 
* @return The x value from the vector. 
*/ 
public double getX() { 
    return x; 
} 

/** 
* @return The y value from the vector. 
*/ 
public double getY() { 
    return y; 
} 

/** 
* Adds two vectors. 
*/ 
public Vec2 add(Vec2 vec) { 
    return new Vec2(getX() + vec.getX(), getY() + vec.getY()); 
} 

/** 
* Subtracts two vectors. 
*/ 
public Vec2 sub(Vec2 vec) { 
    return new Vec2(getX() - vec.getX(), getY() - vec.getY()); 
} 

/** 
* Performs a dot product on two vectors. 
*/ 
public double dot(Vec2 vec) { 
    return getX() * vec.getX() + getY() * vec.getY(); 
} 

/** 
* Scales magnitude of a vector by a scalar. 
* 
* @param sc The scalar. 
*/ 
public Vec2 scale(double sc) { 
    return new Vec2(getX() * sc, getY() * sc); 
} 

/** 
* Scales magnitude of a vector by two scalars. 
* 
* @param x The x scalar. 
* @param y The y scalar. 
*/ 
public Vec2 scale(double x, double y) { 
    return new Vec2(getX() * x, getY() * y); 
} 

public boolean equals(Vec2 vec) { 
    return vec.getX() == getX() && vec.getY() == getY(); 
} 

public double relativeCosine(Vec2 vec) { 
    double d = dot(vec); 
    double rc = d/(getMagnitude() * vec.getMagnitude()); 
    return rc; 
} 

public double relativeRadian(Vec2 vec) { 
    return Math.acos(relativeCosine(vec)); 
} 

public double getRot() { 
    return Math.atan2(getY(), getX()); 
} 

/** 
* Rotates the vector around the origin. 
* 
* @param rad A radian to rotate by. 
* @return The result. 
*/ 
public Vec2 rot(double rad) { 
    double sin = Math.sin(rad); 
    double cos = Math.cos(rad); 
    double t1 = ((cos * getX()) - (sin * getY())); 
    double t2 = ((sin * getX()) + (cos * getY())); 
    return new Vec2(t1, t2); 
} 

/** 
* Retrieves the slope for this vector. 
* 
* @param vec A vector in which is paired to make a line segment. 
* @return A slope. 
*/ 
public double slope(Vec2 vec) { 
    return (vec.getX() - getX())/(vec.getY() - getY()); 
} 

/** 
* Scales magnitude of a vector to a tracable amount. (1/x, 1/y) 
*/ 
public Vec2 tscale() { 
    return new Vec2(1D/getX(), 1D/getY()); 
} 

/** 
* Inverts a vector(-x, -y). 
*/ 
public Vec2 invert() { 
    return new Vec2(-getX(), -getY()); 
} 

private double mag = 0D; // magnitude cannot be 0, so if zero, calc mag. 

/** 
* Return the length, or magnitude of a vector, uses sqrt. 
*/ 
public double getMagnitude() { 
    if(mag == 0D && (getX() != 0D || getY() != 0D)) { 
     mag = Math.sqrt(Math.pow(getX(), 2) + Math.pow(getY(), 2)); 
    } 
    return mag; 
} 

/** 
* Converts a vector to a unit vector (x/length, y/length), uses sqrt. 
*/ 
public Vec2 uscale() { 
    double length = getMagnitude(); 
    return new Vec2(getX()/length, getY()/length); 
} 

/** 
* Converts a vector to a unit vector (x/length, y/length), uses sqrt. 
*/ 
public Vec2 hat() { 
    return uscale(); 
} 
} 
+1

將計算分解成多行,打印出每行,並驗證輸出 – vandale

+0

是的(正如vandale所說),我的物理學已經生鏽,但並不是真正的問題getLinearVelocity()。sub(shape.getLinearVelocity ())。dot(nv)))大於1,它不應該是。你能發佈getLinearVelocity嗎? –

+0

@JimW這裏是我提出的輸出:。 getLinearVelocity()子(shape.getLinearVelocity())點(NV))= 2.0 getLinearVelocity(。)子(shape.getLinearVelocity())= <0.0, 2.0> getLinearVelocity( )= <0.0, 1.0> shape.getLinearVelocity()= <0.0, -1.0> NV = <0.0, 1.0> FV = <-0.0, -4.0> – user2507230

回答

0

對於剛體物理,此代碼行是完全正確的IF FV將是加入到的現有速度,且形狀是一個無限的質量邊界。 如果你的其他代碼試圖使用fv作爲'最終速度',它的計算中不應該有常數1(並且在斜向碰撞中只是錯誤的)。

爲了擴展到有限質量對碰撞,一維常量變成了一個帶符號的加權函數,我將作爲練習給讀者。