简体   繁体   English

C ++平面球体碰撞检测

[英]C++ Plane Sphere Collision Detection

I'm attempting to implement Sphere-Plane collision detection in C++. 我正在尝试在C ++中实现球面碰撞检测。 I have a Vector3, Plane and Sphere class. 我有一个Vector3,Plane和Sphere类。

#include "Vector3.h"

#ifndef PLANE_H
#define PLANE_H

class Plane
{
public:
    Plane(Vector3, float);
    Vector3 getNormal() const;
protected:
    float d;
    Vector3 normal;
};

#endif

I know the equation for a plane is Ax + By = Cz + D = 0 which we can simplify to NS + d < r where N is the normal vector of the plane, S is the center of the sphere, r is the radius of the sphere and d is the distance from the origin point. 我知道一个平面的方程是Ax + By = Cz + D = 0 ,我们可以简化为NS + d < r ,其中N是平面的法向向量,S是球体的中心,r是半径球体,d是距原点的距离。 How do I calculate the value of d from my Plane and Sphere? 如何从“平面”和“球体”计算d的值?

bool Sphere::intersects(const Plane& other) const
{
    // return other.getNormal() * this->currentPosition + other.getDistance() < this->radius;
}

I needed the same computation in a game I made. 在我制作的游戏中,我需要相同的计算。 This is the minimum distance from a point to a plane: 这是从点到平面的最小距离:

distance = (q - plane.p[0])*plane.normal;

Except distance , all variables are 3D vectors (I use a simple class I made with operator overload). 除了distance以外,所有变量都是3D向量(我使用通过运算符重载制作的简单类)。

distance : minimum distance from a point to the plane (scalar). distance :从点到平面的最小距离(标量)。

q : the point (3D vector), in your case is the center of the sphere. q :点(3D矢量),在您的情况下为球体的中心。

plane.p[0] : a point (3D vector) belonging to the plane. plane.p[0] :属于平面的一个点(3D矢量)。 Note that any point belonging to the plane will work. 请注意,属于该平面的任何点都将起作用。

plane.normal : normal to the plane. plane.normal :垂直于飞机。

The * is a dot product between vectors. *是向量之间的点积。 Can be implemented in 3D as a*b = ax*bx + ay*by + az*bz and yields a scalar. 可以在3D中实现为a*b = ax*bx + ay*by + az*bz并产生标量。

Explanantion Explanantion

The dot product is defined: 点积定义如下:

a*b = |a| * |b| * cos(angle)

or, in our case: 或者,就我们而言:

a = q - plane.p[0]
a*plane.normal = |a| * |plane.normal| * cos(angle)

As plane.normal is unitary ( |plane.normal| == 1 ): 由于plane.normal是单一的( |plane.normal| == 1 ):

a*plane.normal = |a| * cos(angle)

在此处输入图片说明

a is the vector from the point q to a point in the plane. a是从点q到平面中一个点的向量。 angle is the angle between a and the normal to the plane. anglea与平面法线之间的角度。 Then, the cosinus is the projection over the normal, which is the vertical distance from the point to the plane. 然后,余弦是法线上方的投影,法线是从点到平面的垂直距离。

There is rather simple formula for point-plane distance with plane equation 点平面之间的距离与平面方程相当简单

Ax+By+Cz+D=0 ( eq.10 here ) Ax+By+Cz+D=0此处为等式10

Distance = (A*x0+B*y0+C*z0+D)/Sqrt(A*A+B*B+C*C)

where (x0,y0,z0) are point coordinates. 其中(x0,y0,z0)是点坐标。 If your plane normal vector (A,B,C) is normalized (unit), then denominator may be omitted. 如果将您的平面法线向量(A,B,C)标准化(单位),则可以省略分母。

(A sign of distance usually is not important for intersection purposes) (距离的迹象通常对于交叉路口并不重要)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM