【发布时间】:2014-04-01 08:55:28
【问题描述】:
我正在尝试在 C++ 中实现 Sphere-Plane 碰撞检测。我有一个 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
我知道平面的方程是Ax + By = Cz + D = 0,我们可以简化为N.S + d < r,其中 N 是平面的法向量,S 是球体的中心,r 是球体的半径,d 是到原点的距离。如何从我的平面和球体计算 d 的值?
bool Sphere::intersects(const Plane& other) const
{
// return other.getNormal() * this->currentPosition + other.getDistance() < this->radius;
}
【问题讨论】:
-
基本上你想比较球心到平面的距离和球体的半径。寻找关于点到平面的距离的数学。在平面上投影该点也可以为您提供一个计算与平面距离的好位置。叉积和点积可以帮助计算。
标签: c++ geometry collision-detection