示例#1
0
        private int CompareHits(MyPhysics.HitInfo info1, MyPhysics.HitInfo info2)
        {
            System.Type type  = info1.HkHitInfo.GetHitEntity().GetType();
            System.Type type2 = info2.HkHitInfo.GetHitEntity().GetType();
            if (type != type2)
            {
                System.Type type3 = typeof(MyVoxelMap);
                if (type == type3)
                {
                    return(1);
                }
                if (type2 == type3)
                {
                    return(-1);
                }
                System.Type type4 = typeof(MyVoxelPhysics);
                if (type == type4)
                {
                    return(1);
                }
                if (type2 == type4)
                {
                    return(-1);
                }
                System.Type type5 = typeof(MyCubeGrid);
                if (type == type5)
                {
                    return(1);
                }
                if (type2 == type5)
                {
                    return(-1);
                }
            }
            Vector3D vectord  = info1.Position - this.m_rayOrigin;
            Vector3D vectord2 = info2.Position - this.m_rayOrigin;
            int      num3     = Vector3.Dot((Vector3)this.m_rayDirection, Vector3.Normalize(vectord2)).CompareTo(Vector3.Dot((Vector3)this.m_rayDirection, Vector3.Normalize(vectord)));

            if (num3 != 0)
            {
                return(num3);
            }
            int num4 = vectord2.LengthSquared().CompareTo(vectord.LengthSquared());

            return((num4 == 0) ? 0 : num4);
        }
            private void Hammer()
            {
                var   IntersectionStart     = MySector.MainCamera.Position;
                var   IntersectionDirection = MySector.MainCamera.ForwardVector;
                LineD line = new LineD(IntersectionStart, IntersectionStart + IntersectionDirection * 200);

                var m_tmpHitList = new List <MyPhysics.HitInfo>();

                MyPhysics.CastRay(line.From, line.To, m_tmpHitList, MyPhysics.CollisionLayers.ObjectDetectionCollisionLayer);
                // Remove character hits.
                m_tmpHitList.RemoveAll(delegate(MyPhysics.HitInfo hit)
                {
                    return(hit.HkHitInfo.GetHitEntity() == MySession.Static.ControlledEntity.Entity);
                });

                if (m_tmpHitList.Count == 0)
                {
                    return;
                }

                MyEntity closestEntity = null;

                MyPhysics.HitInfo closestHit = default(MyPhysics.HitInfo);

                foreach (var hit in m_tmpHitList)
                {
                    if (hit.HkHitInfo.Body != null)
                    {
                        closestEntity = hit.HkHitInfo.GetHitEntity() as MyEntity;
                        closestHit    = hit;
                        break;
                    }
                }
                if (closestEntity == null)
                {
                    return;
                }
                HkdFractureImpactDetails details = HkdFractureImpactDetails.Create();

                details.SetBreakingBody(closestEntity.Physics.RigidBody);
                details.SetContactPoint(closestEntity.Physics.WorldToCluster(closestHit.Position));
                details.SetDestructionRadius(RADIUS);
                details.SetBreakingImpulse(Sandbox.MyDestructionConstants.STRENGTH * 10);
                if (HammerForce)
                {
                    details.SetParticleVelocity(-line.Direction * 20);
                }
                details.SetParticlePosition(closestEntity.Physics.WorldToCluster(closestHit.Position));
                details.SetParticleMass(1000000);
                //details.ZeroColidingParticleVelocity();
                details.Flag = details.Flag | HkdFractureImpactDetails.Flags.FLAG_DONT_RECURSE;
                if (closestEntity.GetPhysicsBody().HavokWorld.DestructionWorld != null)
                {
                    MyPhysics.FractureImpactDetails destruction = new MyPhysics.FractureImpactDetails();
                    destruction.Details = details;
                    destruction.World   = closestEntity.GetPhysicsBody().HavokWorld;
                    destruction.Entity  = closestEntity;
                    MyPhysics.EnqueueDestruction(destruction);
                    //closestGrid.GetPhysicsBody().HavokWorld.DestructionWorld.TriggerDestruction(ref details);
                }
                //details.RemoveReference();
            }
示例#3
0
        private void GetHitEntityAndPosition(LineD line, out IMyEntity entity, out MyHitInfo hitInfoRet, out object customdata)
        {
            entity     = null;
            hitInfoRet = new MyHitInfo();
            customdata = null;

            // 1. rough raycast
            int raycastListIndex = 0;

            do
            {
                if (entity == null)
                {
                    if (raycastListIndex == 0) // cast only the first iteration
                    {
                        ProfilerShort.Begin("MyGamePruningStructure::CastProjectileRay");
                        MyPhysics.CastRay(line.From, line.To, m_raycastResult,
                                          MyPhysics.CollisionLayers.DefaultCollisionLayer);
                        ProfilerShort.End();
                    }

                    if (raycastListIndex < m_raycastResult.Count)
                    {
                        MyPhysics.HitInfo hitInfo = m_raycastResult[raycastListIndex];

                        entity = hitInfo.HkHitInfo.GetHitEntity() as MyEntity;
                        hitInfoRet.Position = hitInfo.Position;
                        hitInfoRet.Normal   = hitInfo.HkHitInfo.Normal;
                        hitInfoRet.ShapeKey = hitInfo.HkHitInfo.GetShapeKey(0);
                    }
                }

                // 2. prevent shooting through characters, retest trajectory between entity and player
                if (!(entity is MyCharacter) || entity == null)
                {
                    // first: raycast, get all entities in line, limit distance if possible
                    LineD lineLimited = new LineD(line.From, entity == null ? line.To : hitInfoRet.Position);
                    if (m_entityRaycastResult == null)
                    {
                        m_entityRaycastResult = new List <MyLineSegmentOverlapResult <MyEntity> >(16);
                    }
                    else
                    {
                        m_entityRaycastResult.Clear();
                    }
                    MyGamePruningStructure.GetAllEntitiesInRay(ref lineLimited, m_entityRaycastResult);
                    // second: precise tests, find best result
                    double    bestDistanceSq = double.MaxValue;
                    IMyEntity entityBest     = null;
                    for (int i = 0; i < m_entityRaycastResult.Count; i++)
                    {
                        if (m_entityRaycastResult[i].Element is MyCharacter)
                        {
                            MyCharacter hitCharacter = m_entityRaycastResult[i].Element as MyCharacter;
                            bool        intersection = hitCharacter.GetIntersectionWithLine(ref line, ref m_charHitInfo);
                            if (intersection)
                            {
                                double distanceSq =
                                    Vector3D.DistanceSquared(m_charHitInfo.Triangle.IntersectionPointInWorldSpace,
                                                             line.From);
                                if (distanceSq < bestDistanceSq && !IsIgnoredEntity(hitCharacter))
                                {
                                    bestDistanceSq      = distanceSq;
                                    entityBest          = hitCharacter;
                                    hitInfoRet.Position = m_charHitInfo.Triangle.IntersectionPointInWorldSpace;
                                    hitInfoRet.Normal   = m_charHitInfo.Triangle.NormalInWorldSpace;
                                    customdata          = m_charHitInfo;
                                }
                            }
                        }
                    }
                    // finally: do we have best result? then return it
                    if (entityBest != null)
                    {
                        entity = entityBest;
                        return; // this was precise result, so return
                    }
                }

                // 3. nothing found in the precise test? then fallback to already found results
                if (entity == null)
                {
                    return;                // no fallback results
                }
                if (entity is MyCharacter) // retest character found in fallback
                {
                    MyCharacter hitCharacter = entity as MyCharacter;
                    bool        intersection = hitCharacter.GetIntersectionWithLine(ref line, ref m_charHitInfo);
                    if (intersection)
                    {
                        hitInfoRet.Position = m_charHitInfo.Triangle.IntersectionPointInWorldSpace;
                        hitInfoRet.Normal   = m_charHitInfo.Triangle.NormalInWorldSpace;
                        customdata          = m_charHitInfo;
                    }
                    else
                    {
                        entity = null; // no hit.
                    }
                }
                else
                {
                    MyCubeGrid grid = entity as MyCubeGrid;
                    if (grid != null)
                    {
                        bool success = grid.GetIntersectionWithLine(ref line, ref m_cubeGridHitInfo);
                        if (success)
                        {
                            hitInfoRet.Position = m_cubeGridHitInfo.Triangle.IntersectionPointInWorldSpace;
                            hitInfoRet.Normal   = m_cubeGridHitInfo.Triangle.NormalInWorldSpace;
                            if (Vector3.Dot(hitInfoRet.Normal, line.Direction) > 0)
                            {
                                hitInfoRet.Normal = -hitInfoRet.Normal;
                            }

                            customdata = m_cubeGridHitInfo;
                        }

                        MyHitInfo info = new MyHitInfo();
                        info.Position = hitInfoRet.Position;
                        info.Normal   = hitInfoRet.Normal;
                    }

                    MyVoxelBase voxel = entity as MyVoxelBase;
                    if (voxel != null)
                    { //get accurate hit because of particles and decals
                        MyIntersectionResultLineTriangleEx?res;
                        if (voxel.GetIntersectionWithLine(ref line, out res, IntersectionFlags.DIRECT_TRIANGLES))
                        {
                            hitInfoRet.Position = res.Value.IntersectionPointInWorldSpace;
                            hitInfoRet.Normal   = res.Value.NormalInWorldSpace;
                            hitInfoRet.ShapeKey = 0;
                        }
                    }
                }
            } while (entity == null && ++raycastListIndex < m_entityRaycastResult.Count);
        }
        private int CompareHits(MyPhysics.HitInfo info1, MyPhysics.HitInfo info2)
        {
            IMyEntity entity1 = info1.HkHitInfo.GetHitEntity();
            IMyEntity entity2 = info2.HkHitInfo.GetHitEntity();

            Type entity1Type = entity1.GetType();
            Type entity2Type = entity2.GetType();

            if (entity1Type != entity2Type)
            {
                // Fix highlighting order on legacy worlds
                Type voxelMapType = typeof(MyVoxelMap);
                if (entity1Type == voxelMapType)
                {
                    return(1);
                }
                if (entity2Type == voxelMapType)
                {
                    return(-1);
                }

                // Fix highlighting order on planets
                Type voxelPhysicsType = typeof(MyVoxelPhysics);
                if (entity1Type == voxelPhysicsType)
                {
                    return(1);
                }
                if (entity2Type == voxelPhysicsType)
                {
                    return(-1);
                }

                Type cubeGridType = typeof(MyCubeGrid);
                if (entity1Type == cubeGridType)
                {
                    return(1);
                }
                if (entity2Type == cubeGridType)
                {
                    return(-1);
                }
            }

            Vector3D deltaPos1 = info1.Position - m_rayOrigin;
            Vector3D deltaPos2 = info2.Position - m_rayOrigin;

            float dot1      = Vector3.Dot(m_rayDirection, Vector3.Normalize(deltaPos1));
            float dot2      = Vector3.Dot(m_rayDirection, Vector3.Normalize(deltaPos2));
            int   dotResult = dot2.CompareTo(dot1);

            if (dotResult != 0)
            {
                return(dotResult);
            }

            int distanceCheck = deltaPos2.LengthSquared().CompareTo(deltaPos1.LengthSquared());

            if (distanceCheck != 0)
            {
                return(distanceCheck);
            }

            return(0);
        }
示例#5
0
 internal bool <UpdatePlacement> b__24_0(MyPhysics.HitInfo hitInfo) =>