Ejemplo n.º 1
0
        public void Close()
        {
            Debug.Assert(this.rigidBody != null);
            MyPhysicsObjects physobj = AppCode.Physics.MyPhysics.physicsSystem.GetPhysicsObjects();

            physobj.DestroyRigidBody(this.rigidBody);
        }
        static void PreallocateObjects()
        {
            if (m_objectPool == null)
            {
                m_objectPool = new MyObjectsPool <MyExplosionDebrisModel>(MyExplosionsConstants.MAX_EXPLOSION_DEBRIS_OBJECTS);
            }
            m_objectPool.DeallocateAll();

            List <MyRBElementDesc> collisionPrimitives = new List <MyRBElementDesc>();

            MyPhysicsObjects      physobj    = MyPhysics.physicsSystem.GetPhysicsObjects();
            MyRBSphereElementDesc sphereDesc = physobj.GetRBSphereElementDesc();

            sphereDesc.SetToDefault();
            sphereDesc.m_RBMaterial = MyMaterialsConstants.GetMaterialProperties(MODEL_DEBRIS_MATERIAL_TYPE).PhysicsMaterial;

            collisionPrimitives.Add(sphereDesc);

            int counter = 0;

            foreach (var item in m_objectPool.GetPreallocatedItemsArray())
            {
                MyModelsEnum   modelEnum      = (MyModelsEnum)((int)MyModelsEnum.Debris1 + counter % 31);
                MyModel        model          = MyModels.GetModelOnlyData(modelEnum);
                BoundingSphere boundingSphere = model.BoundingSphere;

                sphereDesc.m_Radius             = boundingSphere.Radius;
                sphereDesc.m_Matrix.Translation = boundingSphere.Center;

                item.Value.Init(collisionPrimitives, modelEnum);
                counter++;
            }
        }
        static void PreallocateObjects()
        {
            if (m_objectPool == null)
            {
                m_objectPool = new MyObjectsPool <MyExplosionDebrisVoxel>(MyExplosionsConstants.MAX_EXPLOSION_DEBRIS_OBJECTS);
            }
            m_objectPool.DeallocateAll();

            //  This collision primitive is used by every instance of this class
            MyModel                model               = MyModels.GetModelOnlyData(VOXEL_DEBRIS_MODEL_ENUM);
            BoundingSphere         boundingSphere      = model.BoundingSphere;
            List <MyRBElementDesc> collisionPrimitives = new List <MyRBElementDesc>();

            MyPhysicsObjects      physobj      = MyPhysics.physicsSystem.GetPhysicsObjects();
            MyRBSphereElementDesc sphereDesc   = physobj.GetRBSphereElementDesc();
            MyMaterialType        materialType = MyMaterialType.ROCK;

            sphereDesc.SetToDefault();
            sphereDesc.m_RBMaterial         = MyMaterialsConstants.GetMaterialProperties(materialType).PhysicsMaterial;
            sphereDesc.m_Radius             = boundingSphere.Radius;
            sphereDesc.m_Matrix.Translation = model.BoundingSphere.Center;

            collisionPrimitives.Add(sphereDesc);


            int counter = 0;

            foreach (LinkedListNode <MyExplosionDebrisVoxel> item in m_objectPool.GetPreallocatedItemsArray())
            {
                item.Value.Init(collisionPrimitives);
                counter++;
            }
        }
Ejemplo n.º 4
0
        public override void Init(string displayName, Microsoft.Xna.Framework.Vector3 relativePosition, Microsoft.Xna.Framework.Matrix localOrientation, MyMwcObjectBuilder_PrefabBase objectBuilder, MyPrefabConfiguration prefabConfig)
        {
            m_config = prefabConfig;
            MyPrefabConfigurationKinematicRotating config = (MyPrefabConfigurationKinematicRotating)prefabConfig;

            base.Init(displayName, relativePosition, localOrientation, objectBuilder, prefabConfig);
            Physics.RemoveAllElements();

            // create the box
            MyPhysicsObjects            physobj          = MyPhysics.physicsSystem.GetPhysicsObjects();
            MyRBTriangleMeshElementDesc trianglemeshDesc = physobj.GetRBTriangleMeshElementDesc();

            trianglemeshDesc.SetToDefault();
            trianglemeshDesc.m_Model      = ModelLod0;
            trianglemeshDesc.m_RBMaterial = MyMaterialsConstants.GetMaterialProperties(config.MaterialType).PhysicsMaterial;;


            MyRBTriangleMeshElement trEl = (MyRBTriangleMeshElement)physobj.CreateRBElement(trianglemeshDesc);

            this.Physics = new MyPhysicsBody(this, 1.0f, RigidBodyFlag.RBF_RBO_STATIC)
            {
                MaterialType = config.MaterialType
            };
            this.Physics.Enabled        = true;
            this.Physics.CollisionLayer = MyConstants.COLLISION_LAYER_PREFAB_KINEMATIC;
            this.Physics.AddElement(trEl, true);

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

            foreach (var dummyKVP in model.Dummies)
            {
                if (dummyKVP.Key.StartsWith("Dummy"))
                {
                    MyModelDummy dummy                         = dummyKVP.Value;
                    MyModelsEnum rotatingPartModel             = MyModels.GetModelEnumByAssetName(dummy.CustomData["LINKEDMODEL"].ToString());
                    MyPrefabKinematicRotatingPart rotatingPart = new MyPrefabKinematicRotatingPart(this.GetOwner());
                    rotatingPart.Init(this, rotatingPartModel, config.MaterialType, dummy.Matrix, config.RotatingVelocity, true, config.SoundLooping, config.SoundOpening, config.SoundClosing);
                    m_parts.Add(rotatingPart);
                }
            }

            AppCode.Physics.MyPhysics.physicsSystem.GetRigidBodyModule().EnableCollisionInLayers(MyConstants.COLLISION_LAYER_PREFAB_KINEMATIC, MyConstants.COLLISION_LAYER_MISSILE, true);
            AppCode.Physics.MyPhysics.physicsSystem.GetRigidBodyModule().EnableCollisionInLayers(MyConstants.COLLISION_LAYER_PREFAB_KINEMATIC, MyConstants.COLLISION_LAYER_ALL, true);
            AppCode.Physics.MyPhysics.physicsSystem.GetRigidBodyModule().EnableCollisionInLayers(MyConstants.COLLISION_LAYER_PREFAB_KINEMATIC, MyConstants.COLLISION_LAYER_MODEL_DEBRIS, true);
            AppCode.Physics.MyPhysics.physicsSystem.GetRigidBodyModule().EnableCollisionInLayers(MyConstants.COLLISION_LAYER_PREFAB_KINEMATIC, MyConstants.COLLISION_LAYER_VOXEL_DEBRIS, true);
            AppCode.Physics.MyPhysics.physicsSystem.GetRigidBodyModule().EnableCollisionInLayers(MyConstants.COLLISION_LAYER_PREFAB_KINEMATIC, MyConstants.COLLISION_LAYER_PREFAB_KINEMATIC_PART, false);

            NeedsUpdate = false;
            EnabledChanged();
            //Enabled = true;
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Initializes a new instance of the <see cref="MyPhysicsBody"/> class.
        /// </summary>
        /// <param name="entity">The entity.</param>
        public MyPhysicsBody(MyEntity entity, float mass, RigidBodyFlag flags)
        {
            Debug.Assert(entity != null);

            this.m_enabled = false;
            this.Entity    = entity;

            MyPhysicsObjects physobj = AppCode.Physics.MyPhysics.physicsSystem.GetPhysicsObjects();
            MyRigidBodyDesc  rboDesc = physobj.GetRigidBodyDesc();

            rboDesc.SetToDefault();
            rboDesc.m_Mass   = mass;
            rboDesc.m_Matrix = entity.WorldMatrix;
            rboDesc.m_Flags |= flags;

            this.rigidBody            = physobj.CreateRigidBody(rboDesc);
            this.rigidBody.m_UserData = this;
        }
Ejemplo n.º 6
0
        //  IMPORTANT: This class isn't realy inicialized by constructor, but by Start()
        //  So don't initialize members here, do it in Start()
        public virtual void Init(MyModelsEnum modelLod0Enum, MyModelsEnum?modelLod1Enum, MyMaterialType materialType, float scale, List <MyRBElementDesc> collisionPrimitives, float mass)
        {
            MyPhysicsObjects physobj = MyPhysics.physicsSystem.GetPhysicsObjects();

            base.Init(null, modelLod0Enum, modelLod1Enum, null, scale, null);

            m_maxLifeTimeInMiliseconds = MyMwcUtils.GetRandomInt(MyExplosionsConstants.EXPLOSION_DEBRIS_LIVING_MIN_IN_MILISECONDS, MyExplosionsConstants.EXPLOSION_DEBRIS_LIVING_MAX_IN_MILISECONDS);
            m_randomizedDiffuseTextureColorMultiplier = MyMwcUtils.GetRandomFloat(0.4f, 0.6f);
            m_initialOrientation = Matrix.CreateRotationX(MyMwcUtils.GetRandomRadian()) * Matrix.CreateRotationY(MyMwcUtils.GetRandomRadian()) * Matrix.CreateRotationZ(MyMwcUtils.GetRandomRadian());

            // create physics
            this.Physics = new MyPhysicsBody(this, mass, 0)
            {
                MaterialType = materialType
            };

            for (int i = 0; i < collisionPrimitives.Count; i++)
            {
                MyRBSphereElement sphereEl = (MyRBSphereElement)physobj.CreateRBElement(collisionPrimitives[i]);
                //
                sphereEl.Radius *= scale;
                this.Physics.AddElement(sphereEl, true);
            }
        }
Ejemplo n.º 7
0
        public virtual void Init(string hudLabelText, MyMwcObjectBuilder_PrefabContainer objectBuilder, Matrix matrix)
        {
            m_intializing = true;
            StringBuilder hudLabelTextSb = (string.IsNullOrEmpty(hudLabelText) ? null : new StringBuilder(hudLabelText));

            base.Init(hudLabelTextSb, null, null, null, null, objectBuilder);

            DisplayName = objectBuilder.DisplayName;

            SetWorldMatrix(matrix);

            Flags |= EntityFlags.EditableInEditor;

            this.Faction = objectBuilder.Faction;

            //during container initialization, it is not necessary to check if prefab is outside boundaries, because it cannot be saved that way
            foreach (MyMwcObjectBuilder_PrefabBase prefabBuilder in objectBuilder.Prefabs)
            {
                CreateAndAddPrefab(null, prefabBuilder);
            }

            // we must initialize inventory after prefabs, because some prefabs are registered on OnInventoryContentChanged event
            if (objectBuilder.Inventory != null)
            {
                Inventory.Init(objectBuilder.Inventory, MyMwcUtils.GetRandomFloat(1.1f, 2f));
            }

            //Commit();

            UpdateAABBHr();

            // ----- THIS PHYSICS IS NEEDED BECAUSE ENTITY DETECTOR -----
            this.Physics = new MyPhysicsBody(this, 1.0f, UseKinematicPhysics ? RigidBodyFlag.RBF_KINEMATIC : RigidBodyFlag.RBF_RBO_STATIC)
            {
                MaterialType = MyMaterialType.METAL
            };
            MyPhysicsObjects   physobj = MyPhysics.physicsSystem.GetPhysicsObjects();
            MyRBBoxElementDesc boxDesc = physobj.GetRBBoxElementDesc();

            boxDesc.SetToDefault();
            boxDesc.m_RBMaterial     = MyMaterialsConstants.GetMaterialProperties(MyMaterialType.METAL).PhysicsMaterial;
            boxDesc.m_Size           = UseKinematicPhysics ? WorldAABBHr.Size() : Vector3.One;
            boxDesc.m_CollisionLayer = UseKinematicPhysics ? MyConstants.COLLISION_LAYER_DEFAULT : MyConstants.COLLISION_LAYER_UNCOLLIDABLE;
            MyRBBoxElement boxEl = (MyRBBoxElement)physobj.CreateRBElement(boxDesc);

            this.Physics.AddElement(boxEl, true);
            this.Physics.Enabled = true;
            this.Physics.RigidBody.KinematicLinear = false;
            // ----- THIS PHYSICS IS NEEDED BECAUSE ENTITY DETECTOR -----

            /*
             * MyRBBoxElementDesc boxDesc = physobj.GetRBBoxElementDesc();
             * MyMaterialType materialType = MyMaterialType.METAL;
             * boxDesc.SetToDefault();
             * boxDesc.m_Size = Vector3.One;
             * boxDesc.m_CollisionLayer = MyConstants.COLLISION_LAYER_UNCOLLIDABLE;
             * boxDesc.m_RBMaterial = MyMaterialsConstants.GetMaterialProperties(materialType).PhysicsMaterial;
             */
            //m_selectionBox = (MyRBBoxElement)physobj.CreateRBElement(boxDesc);
            m_selectionBox = new BoundingBox(-Vector3.One, Vector3.One);

            // to be selectable in editor
            //this.Physics.AddElement(m_selectionBox, true);

            m_userID  = objectBuilder.UserOwnerID;
            m_faction = objectBuilder.Faction;

            VisibleInGame = false;
            // set here later on user id when firstly created object inserted into scene

            //m_selectionBox = new BoundingBox(m_worldAABB.Min, m_worldAABB.Max);

            if (OnPrefabContainerInitialized != null)
            {
                OnPrefabContainerInitialized(this);
            }

            //StringBuilder displayName;
            //if (!string.IsNullOrEmpty(DisplayName))
            //{
            //    MyHud.ChangeText(this, new StringBuilder(DisplayName), null, 0, MyHudIndicatorFlagsEnum.SHOW_TEXT | MyHudIndicatorFlagsEnum.SHOW_BORDER_INDICATORS | MyHudIndicatorFlagsEnum.SHOW_ONLY_IF_DETECTED_BY_RADAR);
            //}
            m_intializing = false;

            UpdateGenerators();

            UseProperties = new MyUseProperties(MyUseType.FromHUB, MyUseType.FromHUB);
            if (objectBuilder.UseProperties == null)
            {
                UseProperties.Init(MyUseType.FromHUB, MyUseType.FromHUB, 3, 4000, false);
            }
            else
            {
                UseProperties.Init(objectBuilder.UseProperties);
            }

            AlarmOn    = objectBuilder.AlarmOn;
            RefillTime = objectBuilder.RefillTime;

            // check possible values
            if (Inventory.TemplateType != null && RefillTime == null)
            {
                RefillTime = DEFAULT_REFILL_TIME_IN_SEC;
            }
            else if (Inventory.TemplateType == null && RefillTime != null)
            {
                RefillTime = null;
            }
        }