C ++平面球体碰撞检测 [英] C++ Plane Sphere Collision Detection

查看:46
本文介绍了C ++平面球体碰撞检测的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在C ++中实现球面碰撞检测.我有一个Vector3,Plane和Sphere类.

I'm attempting to implement Sphere-Plane collision detection in C++. I have a Vector3, Plane and Sphere class.

#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的值?

I know the equation for a plane is Ax + By = Cz + D = 0 which we can simplify to N.S + 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. How do I calculate the value of d from my Plane and Sphere?

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

推荐答案

对于具有平面方程的点-平面距离,有一个相当简单的公式

There is rather simple formula for point-plane distance with plane equation

Ax + By + Cz + D = 0 ( eq.10这里)

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

其中(x0,y0,z0)是点坐标.如果将飞机的法线向量(A,B,C)归一化(单位),则可以省略分母.

where (x0,y0,z0) are point coordinates. If your plane normal vector (A,B,C) is normalized (unit), then denominator may be omitted.

(距离的迹象通常对于交叉路口并不重要)

(A sign of distance usually is not important for intersection purposes)

这篇关于C ++平面球体碰撞检测的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆