zoukankan      html  css  js  c++  java
  • 游戏编程精粹学习

    参考自《游戏编程精粹1》多边形相交部分,用该方法除了知道是否相交以外还可以得到相交点,从而用于其他判断。

    原文用到了平面方程Ax+By+Cz+D=0,通过三角形求出平面,然后用方程求出线段交点

    这里直接使用平面,然后再拿相交点和平面所占区域进行判断,以下为Unity中的实现代码:

    using UnityEngine;
    
    public class Practice : MonoBehaviour
    {
        public Transform p0;
        public Transform p1;
        public Transform planeLocation;
        public float size = 5f;
    
    
        void OnDrawGizmos()
        {
            if (p0 == null) return;
            if (p1 == null) return;
            if (planeLocation == null) return;
    
            var cacheColor = Gizmos.color;
    
            var r = GetIntersectPoint(planeLocation.forward, planeLocation.position, p0.position, p1.position);
    
            if (r != null)
            {
                var matrix = Matrix4x4.TRS(planeLocation.position, Quaternion.FromToRotation(planeLocation.forward, Vector3.forward), planeLocation.localScale);
                var temp = matrix.inverse.MultiplyPoint(r.Value);
                var sizeHalf = size * 0.5f;
    
                if (temp.x <= sizeHalf
                    && temp.x >= -sizeHalf
                    && temp.y <= sizeHalf
                    && temp.y >= -sizeHalf)
                {
                    Gizmos.color = Color.red;
                }
    
                Gizmos.DrawWireSphere(r.Value, 0.1f);
            }
    
            var cacheMatrix = Gizmos.matrix;
            Gizmos.matrix = Matrix4x4.TRS(planeLocation.position, planeLocation.rotation, Vector3.one);
            Gizmos.DrawWireCube(Vector3.zero, new Vector3(size, size, 0f));
            Gizmos.matrix = cacheMatrix;
            Gizmos.color = cacheColor;
        }
    
        /// <summary>
        /// 检测相交点的原始函数,没有相交点返回空,否则返回相交点。
        /// </summary>
        /// <param name="planeNormal">平面的法线朝向</param>
        /// <param name="planePosition">平面的位置</param>
        /// <param name="p0">线段点0</param>
        /// <param name="p1">线段点1</param>
        Vector3? GetIntersectPoint(Vector3 planeNormal, Vector3 planePosition, Vector3 p0, Vector3 p1)
        {
            var sign1 = Mathf.Sign(Vector3.Dot(planeNormal, planePosition - p0));
            var sign2 = Mathf.Sign(Vector3.Dot(planeNormal, planePosition - p1));
            if (Mathf.Approximately(sign1, sign2)) return null;//同侧异侧.
    
            var a = planeNormal.x;
            var b = planeNormal.y;
            var c = planeNormal.z;
            var d = -a * planePosition.x - b * planePosition.y - c * planePosition.z;
    
            var i0 = a * p0.x + b * p0.y + c * p0.z;
            var i1 = a * p1.x + b * p1.y + c * p1.z;
            var final_t = -(i1 + d) / (i0 - i1);
    
            var finalPoint = new Vector3()
            {
                x = p0.x * final_t + p1.x * (1 - final_t),
                y = p0.y * final_t + p1.y * (1 - final_t),
                z = p0.z * final_t + p1.z * (1 - final_t),
            };
    
            return finalPoint;
        }
    }
  • 相关阅读:
    May 24th 2017 Week 21th Wednesday
    May 23rd 2017 Week 21st Tuesday
    全球顶尖大学的UX课程资源,英文!
    如何设计出一款出色的结账表单
    快速完成网页设计,10个顶尖响应式HTML5网页模板助你一臂之力
    界面设计中如何增强CTA按钮召唤力?
    10个实用的UX设计作品推销小窍门
    UX术语详解:任务流,用户流,流程图以及其它全新术语
    让你不再恋家的9款小众时尚的酒店网站设计
    聊聊原型设计中的团队管理功能
  • 原文地址:https://www.cnblogs.com/hont/p/7467618.html
Copyright © 2011-2022 走看看