コード例 #1
0
        protected override void ReadEntitiesInRange()
        {
            m_entitiesInRange.Clear();

            BoundingSphereD bsphere = new BoundingSphereD(Center, m_radius);
            var             res     = MyEntities.GetEntitiesInSphere(ref bsphere);

            for (int i = 0; i < res.Count; ++i)
            {
                var rootEntity = res[i].GetTopMostParent();
                if (!IgnoredEntities.Contains(rootEntity))
                {
                    m_entitiesInRange[rootEntity.EntityId] = new DetectionInfo(rootEntity, FrontPoint);
                }
            }
            res.Clear();
        }
コード例 #2
0
        protected override void ReadEntitiesInRange()
        {
            m_entitiesInRange.Clear();

            MyOrientedBoundingBox bbox = new MyOrientedBoundingBox(Center, m_halfExtents, m_orientation);
            var aabb = bbox.GetAABB();
            var res  = MyEntities.GetEntitiesInAABB(ref aabb);

            for (int i = 0; i < res.Count; ++i)
            {
                var rootEntity = res[i].GetTopMostParent();
                if (!IgnoredEntities.Contains(rootEntity))
                {
                    m_entitiesInRange[rootEntity.EntityId] = new DetectionInfo(rootEntity, FrontPoint);
                }
            }
            res.Clear();
        }
コード例 #3
0
        protected override void ReadEntitiesInRange()
        {
            m_entitiesInRange.Clear();
            m_hits.Clear();
            MyPhysics.CastRay(m_origin, FrontPoint, m_hits, MyPhysics.ObjectDetectionCollisionLayer);

            DetectionInfo value = new DetectionInfo();

            foreach (var hit in m_hits)
            {
                var hitInfo = hit.HkHitInfo;
                if (hitInfo.Body == null)
                {
                    continue;
                }
                var entity = hitInfo.GetHitEntity();

                if (entity == null)
                {
                    continue;
                }
                var rootEntity = entity.GetTopMostParent();
                if (!IgnoredEntities.Contains(rootEntity))
                {
                    Vector3D detectionPoint = hit.Position;

                    MyCubeGrid grid = rootEntity as MyCubeGrid;
                    if (grid != null)
                    {
                        var shape    = hitInfo.Body.GetShape();
                        int shapeIdx = 0;
                        if (grid.Physics.IsWelded || grid.Physics.WeldInfo.Children.Count != 0)
                        {
                            if (shape.IsContainer())
                            {
                                shape    = shape.GetContainer().GetShape(hitInfo.GetShapeKey(0));
                                shapeIdx = 1;
                            }
                        }
                        if (!GetShapeCenter(shape, hitInfo.GetShapeKey(shapeIdx), grid, ref detectionPoint))
                        {
                            if (grid.GridSizeEnum == Common.ObjectBuilders.MyCubeSize.Large)
                            {
                                detectionPoint += hit.HkHitInfo.Normal * -0.08f;
                            }
                            else
                            {
                                detectionPoint += hit.HkHitInfo.Normal * -0.02f;
                            }
                        }
                    }

                    if (m_entitiesInRange.TryGetValue(rootEntity.EntityId, out value))
                    {
                        var oldDistance = Vector3.DistanceSquared(value.DetectionPoint, m_origin);
                        var newDistance = Vector3.DistanceSquared(detectionPoint, m_origin);
                        if (oldDistance > newDistance)
                        {
                            m_entitiesInRange[rootEntity.EntityId] = new DetectionInfo(rootEntity as MyEntity, detectionPoint);
                        }
                    }
                    else
                    {
                        m_entitiesInRange[rootEntity.EntityId] = new DetectionInfo(rootEntity as MyEntity, detectionPoint);
                    }
                }
            }

            LineD line = new LineD(m_origin, FrontPoint);

            using (m_raycastResults.GetClearToken())
            {
                MyGamePruningStructure.GetAllEntitiesInRay(ref line, m_raycastResults);
                foreach (var segment in m_raycastResults)
                {
                    if (segment.Element == null)
                    {
                        continue;
                    }
                    var rootEntity = segment.Element.GetTopMostParent();
                    if (!IgnoredEntities.Contains(rootEntity))
                    {
                        MyCubeBlock block = segment.Element as MyCubeBlock;
                        if (block == null)
                        {
                            continue;
                        }

                        Vector3D point = new Vector3D();

                        if (block.SlimBlock.HasPhysics == false)
                        {
                            MatrixD  blockWorldMatrixNormalizedInv = block.PositionComp.WorldMatrixNormalizedInv;
                            Vector3D localOrigin = Vector3D.Transform(m_origin, ref blockWorldMatrixNormalizedInv);
                            Vector3D localFront  = Vector3D.Transform(FrontPoint, ref blockWorldMatrixNormalizedInv);
                            Ray      ray         = new Ray(localOrigin, Vector3.Normalize(localFront - localOrigin));
                            //MyRenderProxy.DebugDrawAABB(block.WorldAABB, Color.Red.ToVector3(), 1.0f, 1.0f, false);
                            float?dist = ray.Intersects(block.PositionComp.LocalAABB);
                            dist += 0.01f;
                            if (dist.HasValue && dist <= m_rayLength)
                            {
                                point = m_origin + Vector3D.Normalize(FrontPoint - m_origin) * dist.Value;
                            }
                            else
                            {
                                continue;
                            }
                        }
                        else
                        {
                            // This entity was caught by Havok raycast
                            continue;
                        }

                        if (m_entitiesInRange.TryGetValue(rootEntity.EntityId, out value))
                        {
                            if (Vector3.DistanceSquared(value.DetectionPoint, m_origin) > Vector3.DistanceSquared(point, m_origin))
                            {
                                m_entitiesInRange[rootEntity.EntityId] = new DetectionInfo(rootEntity, point);
                            }
                        }
                        else
                        {
                            m_entitiesInRange[rootEntity.EntityId] = new DetectionInfo(rootEntity, point);
                        }
                    }
                }
            }
        }