刚体碰撞相对速度计算

时间:2014-01-10 00:50:13

标签: java math vector geometry physics

这是我的代码:

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 个答案:

答案 0 :(得分:0)

对于刚体物理,如果 fv 添加 的现有速度,此代码行完全正确,并且形状是一个无限质量边界。 如果你的其他代码试图将fv用作'最终速度',那么它的计算中不应该有常数1(并且在倾斜碰撞中也是错误的。)

对于有限质量对碰撞的扩展,1D常数变为有符号加权函数,我将留给读者作为练习。