Example #1
0
        protected override bool TestVisibility(VisualAspect aspect)
        {
            //if not in cylinder, can not see it
            var halfHeight = _height / 2.0f;
            var rod        = this.Rod;
            var center     = this.GetCenterInWorldSpace();
            var otherPos   = aspect.transform.position;

            if (!Cylinder.ContainsPoint(center - (rod * halfHeight),
                                        center + (rod * halfHeight),
                                        _radius,
                                        _innerRadius,
                                        otherPos))
            {
                return(false);
            }

            if (this._angle < 360.0f)
            {
                var v = VectorUtil.SetLengthOnAxis(otherPos - center, rod, 0f);
                var a = Vector3.Angle(this.transform.forward, v);
                if (a > this._angle / 2.0f)
                {
                    return(false);
                }
            }

            if (this.RequiresLineOfSight)
            {
                var v = otherPos - center;
                //RaycastHit[] hits = Physics.RaycastAll(this.transform.position, v, v.magnitude, this.LineOfSightMask);
                //foreach (var hit in hits)
                //{
                //    //we ignore ourself
                //    var r = hit.collider.FindRoot();
                //    if (r != aspect.entityRoot && r != this.entityRoot) return false;
                //}
                using (var lst = com.spacepuppy.Collections.TempCollection.GetList <RaycastHit>())
                {
                    int cnt = PhysicsUtil.RaycastAll(this.transform.position, v, lst, v.magnitude, this.LineOfSightMask);
                    for (int i = 0; i < cnt; i++)
                    {
                        //we ignore ourself
                        var r = lst[i].collider.FindRoot();
                        if (r != aspect.entityRoot && r != this.entityRoot)
                        {
                            return(false);
                        }
                    }
                }
            }

            return(true);
        }
        protected override bool TestVisibility(VisualAspect aspect)
        {
            //if not in cylinder, can not see it
            if (!Cylinder.ContainsPoint(this.transform.position,
                                        this.transform.position + this.Direction * _range,
                                        _radius,
                                        aspect.transform.position))
            {
                return(false);
            }

            if (this.RequiresLineOfSight)
            {
                var v = aspect.transform.position - this.transform.position;
                //RaycastHit[] hits = Physics.RaycastAll(this.transform.position, v, v.magnitude, this.LineOfSightMask);
                //foreach (var hit in hits)
                //{
                //    //we ignore ourself
                //    var r = hit.collider.FindRoot();
                //    if (r != aspect.entityRoot && r != this.entityRoot) return false;
                //}
                using (var lst = com.spacepuppy.Collections.TempCollection.GetList <RaycastHit>())
                {
                    int cnt = PhysicsUtil.RaycastAll(this.transform.position, v, lst, v.magnitude, this.LineOfSightMask);
                    for (int i = 0; i < cnt; i++)
                    {
                        //we ignore ourself
                        var r = lst[i].collider.FindRoot();
                        if (r != aspect.entityRoot && r != this.entityRoot)
                        {
                            return(false);
                        }
                    }
                }
            }

            return(true);
        }
Example #3
0
        protected override bool TestVisibility(VisualAspect aspect)
        {
            //if not in cylinder, can not see it
            var halfHeight = _height / 2.0f;
            var rod        = this.Rod;
            var center     = this.GetCenterInWorldSpace();
            var otherPos   = aspect.transform.position;

            float aspRad = aspect.Radius;

            if (aspRad > MathUtil.EPSILON)
            {
                if (!Cylinder.ContainsSphere(center - (rod * halfHeight),
                                             center + (rod * halfHeight),
                                             _radius,
                                             _innerRadius,
                                             otherPos,
                                             aspRad))
                {
                    return(false);
                }

                if (this._angle < 360.0f)
                {
                    var   v = VectorUtil.SetLengthOnAxis(otherPos - center, rod, 0f);
                    var   a = Vector3.Angle(this.transform.forward, v);
                    float k = 2f * Mathf.Asin(aspRad / (Mathf.Sqrt(v.sqrMagnitude + (aspRad * aspRad) / 4f))) * Mathf.Rad2Deg;
                    if (a > (_angle / 2.0f) - k)
                    {
                        return(false);
                    }
                }
            }
            else
            {
                if (!Cylinder.ContainsPoint(center - (rod * halfHeight),
                                            center + (rod * halfHeight),
                                            _radius,
                                            _innerRadius,
                                            otherPos))
                {
                    return(false);
                }

                if (this._angle < 360.0f)
                {
                    var v = VectorUtil.SetLengthOnAxis(otherPos - center, rod, 0f);
                    var a = Vector3.Angle(this.transform.forward, v);
                    if (a > this._angle / 2.0f)
                    {
                        return(false);
                    }
                }
            }

            if (this.LineOfSightMask.value != 0)
            {
                var v = otherPos - center;
                using (var lst = com.spacepuppy.Collections.TempCollection.GetList <RaycastHit>())
                {
                    int cnt = PhysicsUtil.RaycastAll(this.transform.position, v, lst, v.magnitude, this.LineOfSightMask);
                    for (int i = 0; i < cnt; i++)
                    {
                        //we ignore ourself
                        var r = lst[i].collider.FindRoot();
                        if (r != aspect.entityRoot && r != this.entityRoot)
                        {
                            return(false);
                        }
                    }
                }
            }

            return(true);
        }