public override void DoWork()
        {
            Matrix sensorMatrix  = m_SensorElement.GetGlobalTransformation();
            Matrix elementMatrix = m_RBElement.GetGlobalTransformation();

            MySphereSensorElement sphereSensorElement = (MySphereSensorElement)m_SensorElement;

            float sradius = sphereSensorElement.Radius;
            float eradius = ((MyRBSphereElement)m_RBElement).Radius;

            Vector3 dv = sensorMatrix.Translation - elementMatrix.Translation;

            float distance = dv.LengthSquared();

            if (distance <= (sradius + eradius) * (sradius + eradius))
            {
                if (sphereSensorElement.SpecialDetectingAngle == null || distance <= ((sradius + eradius) / 2f) * ((sradius + eradius) / 2f))
                {
                    m_IsInside = true;
                }
                else if (sphereSensorElement.SpecialDetectingAngle != null)
                {
                    Vector3 normalizeDirectionToRBElement = Vector3.Normalize(elementMatrix.Translation - sensorMatrix.Translation);
                    float   cosAngle = Vector3.Dot(normalizeDirectionToRBElement, sensorMatrix.Forward);
                    m_IsInside = Math.Abs(cosAngle) >= sphereSensorElement.SpecialDetectingAngle.Value;
                }
            }
            else
            {
                m_IsInside = false;
            }
        }
        public MySensorElement CreateSensorElement(MySensorElementDesc desc)
        {
            switch (desc.GetElementType())
            {
            case MySensorElementType.ET_SPHERE:
            {
                MySphereSensorElement element = m_SphereSensorElementPool.Allocate();

                MyCommonDebugUtils.AssertDebug(element != null);

                if (element.LoadFromDesc(desc))
                {
                    return(element);
                }
                else
                {
                    m_SphereSensorElementPool.Deallocate(element);
                    return(null);
                }
            }
            break;

            default:
                return(null);

                break;
            }
        }
        public override void DoWork()
        {
            MySphereSensorElement sphere = (MySphereSensorElement)m_SensorElement;

            BoundingSphere seSphere = new BoundingSphere(sphere.GetGlobalTransformation().Translation, sphere.Radius);
            BoundingBox    oeAABB   = m_RBElement.GetWorldSpaceAABB();

            seSphere.Intersects(ref oeAABB, out m_IsInside);
        }
        public override void DoWork()
        {
            MyRBBoxElement        box    = (MyRBBoxElement)m_RBElement;
            MySphereSensorElement sphere = (MySphereSensorElement)m_SensorElement;

            Matrix  boxMatrix    = box.GetGlobalTransformation();
            Vector3 sphereCenter = sphere.GetGlobalTransformation().Translation;

            Matrix invBoxMatrix = Matrix.Invert(boxMatrix);

            Vector3 boxLocalsphereCenter = Vector3.Transform(sphereCenter, invBoxMatrix);

            bool    penetration = false;
            Vector3 normal      = new Vector3();
            Vector3 closestPos  = new Vector3();
            uint    customData  = 0;

            box.GetClosestPoint(boxLocalsphereCenter, ref closestPos, ref normal, ref penetration, ref customData);

            if (penetration)
            {
                m_IsInside = true;
                return;
            }

            closestPos = Vector3.Transform(closestPos, boxMatrix);

            float vLength = (sphereCenter - closestPos).LengthSquared();

            if (vLength <= sphere.Radius * sphere.Radius)
            {
                if (vLength <= (sphere.Radius / 2f) * (sphere.Radius / 2f) || sphere.SpecialDetectingAngle == null)
                {
                    m_IsInside = true;
                }
                else if (sphere.SpecialDetectingAngle != null)
                {
                    Vector3 normalizeDirectionToRBElement = Vector3.Normalize(boxMatrix.Translation - sphereCenter);
                    float   cosAngle = Vector3.Dot(normalizeDirectionToRBElement, sphere.GetGlobalTransformation().Forward);
                    m_IsInside = Math.Abs(cosAngle) >= sphere.SpecialDetectingAngle.Value;
                }
            }
            else
            {
                m_IsInside = false;
            }
        }
        protected override void InitPrefab(string displayName, Vector3 relativePosition, Matrix localOrientation, MyMwcObjectBuilder_PrefabBase objectBuilder, MyPrefabConfiguration prefabConfig)
        {
            MyPrefabConfigurationKinematic prefabKinematicConfig = (MyPrefabConfigurationKinematic)prefabConfig;
            MyMwcObjectBuilder_PrefabKinematic kinematicBuilder = objectBuilder as MyMwcObjectBuilder_PrefabKinematic;                        

            MyModel model = MyModels.GetModelOnlyDummies(m_config.ModelLod0Enum);

            for (int i = 0; i < prefabKinematicConfig.KinematicParts.Count; i++)
            {
                MyPrefabConfigurationKinematicPart kinematicPart = prefabKinematicConfig.KinematicParts[i];
                MyModelDummy open, close;
                if (model.Dummies.TryGetValue(kinematicPart.m_open, out open) && model.Dummies.TryGetValue(kinematicPart.m_close, out close))
                {
                    float? kinematicPartHealth = kinematicBuilder.KinematicPartsHealth[i];
                    float? kinematicPartMaxHealth = kinematicBuilder.KinematicPartsMaxHealth[i];
                    uint? kinematicPartEntityId = kinematicBuilder.KinematicPartsEntityId[i];

                    // if health is not set or not destroyed, then create part
                    if (kinematicPartHealth == null || kinematicPartHealth != 0)
                    {
                        MyPrefabKinematicPart newPart = new MyPrefabKinematicPart(m_owner);
                        if (kinematicPartEntityId.HasValue)
                        {
                            newPart.EntityId = new MyEntityIdentifier(kinematicPartEntityId.Value);
                        }
                        Parts[i] = newPart;
                        newPart.Init(this, kinematicPart, prefabKinematicConfig.m_openTime, prefabKinematicConfig.m_closeTime, (MyModelsEnum)kinematicPart.m_modelMovingEnum, open.Matrix, close.Matrix, prefabKinematicConfig.MaterialType, prefabKinematicConfig.m_soundLooping, prefabKinematicConfig.m_soundOpening, prefabKinematicConfig.m_soundClosing/*, m_groupMask*/, kinematicPartHealth, kinematicPartMaxHealth, Activated);
                    }
                }
            }                        

            //make handler
            m_sensorHandler = new MyPrefabKinematicSensor(this);
            MySphereSensorElement sensorEl = new MySphereSensorElement();
            sensorEl.Radius = DETECT_RADIUS;
            sensorEl.LocalPosition = new Vector3(0, 0, 0);
            sensorEl.DetectRigidBodyTypes = MyConstants.RIGIDBODY_TYPE_SHIP;            
            sensorEl.SpecialDetectingAngle = DETECTION_ANGLE;
            MySensorDesc senDesc = new MySensorDesc();
            senDesc.m_Element = sensorEl;
            senDesc.m_Matrix = WorldMatrix;
            senDesc.m_SensorEventHandler = m_sensorHandler;
            m_sensor = new MySensor();
            m_sensor.LoadFromDesc(senDesc);
            MyPhysics.physicsSystem.GetSensorModule().AddSensor(m_sensor);

            GetOwner().UpdateAABB();

            UseProperties = new MyUseProperties(MyUseType.FromHUB | MyUseType.Solo, MyUseType.FromHUB);
            if (kinematicBuilder.UseProperties == null)
            {
                UseProperties.Init(MyUseType.FromHUB, MyUseType.FromHUB, 3, 4000, false);
            }
            else
            {
                UseProperties.Init(kinematicBuilder.UseProperties);
            }
            UpdateHudAndCloseStatus();
        }
示例#6
0
        private void InitSensor(MyMwcObjectBuilder_EntityDetector_TypesEnum type, Vector3 size)
        {
            MySensorElement sensorElement;

            if (type == MyMwcObjectBuilder_EntityDetector_TypesEnum.Sphere)
            {
                MySphereSensorElement sphereSensorElement = new MySphereSensorElement();
                sphereSensorElement.Radius = size.Length() / 2f;
                sensorElement = sphereSensorElement;
                EntityDetectorType = MyEntityDetectorType.Sphere;
                m_radius = sphereSensorElement.Radius;
            }
            else if (type == MyMwcObjectBuilder_EntityDetector_TypesEnum.Box)
            {
                MyBoxSensorElement boxSensorElement = new MyBoxSensorElement();
                boxSensorElement.Size = size;
                sensorElement = boxSensorElement;
                EntityDetectorType = MyEntityDetectorType.Box;
                m_extent = size / 2f;
            }
            else
            {
                throw new MyMwcExceptionApplicationShouldNotGetHere();
            }

            sensorElement.LocalPosition = Vector3.Zero;
            MySensorDesc senDesc = new MySensorDesc();
            senDesc.m_Element = sensorElement;
            senDesc.m_Matrix = Matrix.Identity;
            senDesc.m_SensorEventHandler = this;
            m_sensor.Inserted = false;
            m_sensor.LoadFromDesc(senDesc);
            m_sensor.Active = true;
            m_size = size;           
        }