【问题标题】:Collision Angle Detection碰撞角度检测
【发布时间】:2016-10-27 00:32:14
【问题描述】:

我有一些关于碰撞角度的问题。我正在尝试为游戏编写物理代码,我不想使用任何第三方库,实际上我想自己编写每一件事。我知道如何检测两个球体之间的碰撞,但我不知道如何找到两个球体之间的碰撞/排斥角度。我试过反转物体的方向,但没有运气。如果您将我链接到一个有趣的 .pdf 文件来教授物理编程,那就太好了。

【问题讨论】:

  • 基本上你希望你的对象相互“反弹”?
  • 这是 3D 吗?然后没有一个角度。也许您根本不需要角度,可以使用碰撞点上的法向量,这只是两个球体中心之间的差异。 (而在 2D 中,这两个分量将为您提供角度 a = atan2(n.y, n.x))。
  • @Guiroux 不,我希望他们互相排斥。
  • 是的,我的意思是你希望它们“碰撞”

标签: physics game-physics


【解决方案1】:

有很多方法可以处理碰撞

冲动

要模拟一个冲动,你可以直接作用于每个物体的速度,利用反射定律,你可以用“冲击的法线”“反射”每个速度

所以 : v1 = v1 - 2 x ( v1 . n2 ) x n2

和 v2 = v2 - 2 x ( v2 . n1 ) x n1

球体s1和s2的v1和v2速度

n1和n2在碰撞点法线

处罚

在这里,我们有 2 个相互穿透的对象,我们模拟了它们不再相互穿透的事实,因此您可以使用弹簧力创建一个与穿透成正比的力

我没有说所有的方法,但这是我所知道的最简单的两种

【讨论】:

  • 我想你不明白我的问题,我问的是一个物体与另一个物体的排斥/碰撞角度。 (球形)
  • 在我看来,我的答案的第一部分回答了你的问题,你不需要计算角度(你实际上是使用 dot prod 做的),你真正想要计算的是正确的方向?
  • 是的,它是方向。
  • 撞击后的速度方向是您感兴趣的?那么我的回答提供了你所需要的
【解决方案2】:

在 2D 或 3D 坐标空间中两个对象之间的角度可以通过 A * B = |A||B|cosɵ A 和 B 都是向量,而 ɵ 是两个向量之间的角度。 下面的类可以用来解决游戏中的基本向量计算

    class 3Dvector

{

private:

    float x, y, z;

public:

    // purpose:     Our constructor

    // input:       ex- our vector's i component

    //               why- our vector's j component

    //               zee- our vector's k component

    // output:      no explicit output

    3Dvector(float ex = 0, float why = 0, float zee = 0)

{

    x = ex;  y = why; z = zee;

}



// purpose:    Our destructor

    // input:    none

    // output:   none

~3Dvector() { }



// purpose:    calculate the magnitude of our invoking vector

// input:      no explicit input

// output:     the magnitude of our invoking object

float getMagnitude()

{

    return sqrtf(x * x + y * y + z * z);

}



// purpose:  multiply our vector by a scalar value

// input:    num - the scalar value being multiplied

// output:   our newly created vector

3Dvector operator*(float num) const

{

    return 3Dvector(x * num, y * num, z * num);

}



// purpose:    multiply our vector by a scalar value

// input:      num - the scalar value being multiplied

//              vec - the vector we are multiplying to

// output:     our newly created vector

friend 3Dvector operator*(float num, const 3Dvector &vec)

{

    return 3Dvector(vec.x * num, vec.y * num, vec.z * num);

}



// purpose:     Adding two vectors

// input:       vec - the vector being added to our invoking object

// output:      our newly created sum of the two vectors

3Dvector operator+(const 3Dvector &vec) const

{

    return 3Dvector(x + vec.x, y + vec.y, z + vec.z);

}



// purpose:     Subtracting two vectors

// input:       vec - the vector being subtracted from our invoking object

// output:      our newly created difference of the two vectors

3Dvector operator-(const 3Dvector &vec) const

{

    return 3Dvector(x - vec.x, y - vec.y, z - vec.z);

}



// purpose:    Normalize our invoking vector *this changes our vector*

// input:      no explicit input

// output:     none

void normalize3Dvector(void)

{

    float mag = sqrtf(x * x + y * y + z * z);

    x /= mag;  y /= mag; z /= mag

}



// purpose:     Dot Product two vectors

// input:       vec - the vector being dotted with our invoking object

// output:       the dot product of the two vectors

float dot3Dvector(const 3Dvector &vec) const

{

    return x * vec.x + y * vec.y + z * vec.z;

}



// purpose:  Cross product two vectors

// input:    vec- the vector being crossed with our invoking object

// output:   our newly created resultant vector

3Dvector cross3Dvector(const 3Dvector &vec) const

{

    return 3Dvector( y * vec.z – z * vec.y,

                 z * vec.x – x * vec.z,

                 x * vec.y – y * vec.x);

}



};

【讨论】:

    【解决方案3】:

    我不应该回答我自己的问题,但我想我找到了我需要的东西。它也可以帮助其他人。我只是在指指维基百科的物理部分,我明白了。

    This link solves my question

    【讨论】:

      【解决方案4】:

      笛卡尔坐标系中的角度可以这样求:

      arctan((Ya-Yb)/(Xa-Xb))

      因为这是一个重角三角形,您可以在其中知道 catets(高度和宽度的差异)。这将计算切线。所以 arctan 会计算出这个切线的角度。

      希望对你有所帮助。

      【讨论】:

        猜你喜欢
        • 2016-06-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-09-05
        • 2019-04-30
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多