检测射线与边界盒AABB相交(C#代码Unity环境下测试通过)

本文详细解释了在Unity中使用Bounds.IntersectRay方法进行碰撞检测的算法,并指出有优化空间,建议根据实际场景进行调整。
摘要由CSDN通过智能技术生成

        说明:如果仅仅是再Unity环境下使用的话,直接使用Bounds.IntersectRay方法就好了,这里仅仅探索其算法。

        上代码:

bool RayCrossBounds(Ray ray, Bounds bounds)
{
	if (bounds.Contains(ray.origin)) return true;
	//
	float tMinX = float.MinValue;
	float tMaxX = float.MaxValue;
	if (Mathf.Approximately(ray.direction.x, 0))
	{
		if (ray.origin.x <= bounds.min.x || ray.origin.x >= bounds.max.x) return false;
	}
	else
	{
		tMinX = (bounds.min.x - ray.origin.x) / ray.direction.x;
		if (tMinX < 0) tMinX = 0;
		tMaxX = (bounds.max.x - ray.origin.x) / ray.direction.x;
		if (tMaxX < 0) tMaxX = 0;
		if (tMinX < 0 && tMaxX < 0) return false;
	}

	if (tMinX > tMaxX)
	{
		float temp = tMinX;
		tMinX = tMaxX;
		tMaxX = temp;
	}
	//
	float tMinY = float.MinValue;
	float tMaxY = float.MaxValue;
	if (Mathf.Approximately(ray.direction.y, 0))
	{
		if (ray.origin.y <= bounds.min.y || ray.origin.y >= bounds.max.y) return false;
	}
	else
	{
		tMinY = (bounds.min.y - ray.origin.y) / ray.direction.y;
		if (tMinY < 0) tMinY = 0;
		tMaxY = (bounds.max.y - ray.origin.y) / ray.direction.y;
		if (tMaxY < 0) tMaxY = 0;
		if (tMinY < 0 && tMaxY < 0) return false;
	}

	if (tMinY > tMaxY)
	{
		float temp = tMinY;
		tMinY = tMaxY;
		tMaxY = temp;
	}
	//
	float tMinZ = float.MinValue;
	float tMaxZ = float.MaxValue;
	if (Mathf.Approximately(ray.direction.z, 0))
	{
		if (ray.origin.z <= bounds.min.z || ray.origin.z >= bounds.max.z) return false;
	}
	else
	{
		tMinZ = (bounds.min.z - ray.origin.z) / ray.direction.z;
		if (tMinZ < 0) tMinZ = 0;
		tMaxZ = (bounds.max.z - ray.origin.z) / ray.direction.z;
		if (tMaxZ < 0) tMaxZ = 0;
		if (tMinZ < 0 && tMaxZ < 0) return false;
	}

	if (tMinZ > tMaxZ)
	{
		float temp = tMinZ;
		tMinZ = tMaxZ;
		tMaxZ = temp;
	}
	//Get Max min value
	float tMin = tMinX;
	if (tMin < tMinY) tMin = tMinY;
	if (tMin < tMinZ) tMin = tMinZ;
	//Get Min max value
	float tMax = tMaxX;
	if (tMax > tMaxY) tMax = tMaxY;
	if (tMax > tMaxZ) tMax = tMaxZ;

	if (tMax < tMin) return false;

	return true;
}

        该算法应该还有很多优化的余地,使用前应该考虑根据实际情况优化。

原理参考链接

  • 9
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值