예제 #1
0
        public Particle CreateParticle()
        {
            Particle particle = null;

            if (particlesPool.Count() > 0)
            {
                particle = particlesPool[0];
                particlesPool.RemoveAt(0);

                particle.Position  = Vector3.Zero;
                particle.Direction = Vector3.Zero;
                particle.Force     = Vector3.Zero;
            }
            else
            {
                particle = new Particle();
            }

            // Create mesh for the particle
            InstancedMesh <VertexData> .Instance instance = instancedMesh.AppendInstance(Matrix.Identity);
            particle.Instance = instance;

            particles.Add(particle);

            return(particle);
        }
예제 #2
0
        public void AddBlock(String blockType, Matrix transform)
        {
            // Obtain instanced mesh for the block
            InstancedMesh instancedMesh = blockInstancedMeshes[blockType];

            int instanceIndex = instancedMesh.AppendInstance(transform);
        }
예제 #3
0
        public void AddBlock(String blockType, int x, int y, int z)
        {
            // Obtain compound child for the block
            int           index = x + y * GAME_FIELD_SIZE + z * GAME_FIELD_SIZE * GAME_FIELD_SIZE;
            CompoundChild child = compoundBody.CollisionInformation.Children[index];

            child.CollisionInformation.CollisionRules.Group = null;

            // Obtain instanced mesh for the block
            InstancedMesh instancedMesh = blockInstancedMeshes[blockType];

            // Create instance of the block in instanced mesh
            Matrix transform     = child.Entry.LocalTransform.Matrix; //Matrix.CreateTranslation(x + BLOCK_SIZE * 0.5f, y + BLOCK_SIZE * 0.5f, z + BLOCK_SIZE * 0.5f);
            int    instanceIndex = instancedMesh.AppendInstance(transform);

            // Store new instance to the list
            Block block = new Block(instancedMesh, instanceIndex, child);

            blocks.Add(block);
        }
예제 #4
0
        public void ChangleBlockType(Block block, String newBlockTypeName)
        {
            // Obtain new block type for the block
            BlockType newBlockType = null;

            try
            {
                newBlockType = blockTypes[newBlockTypeName];
            }
            catch (KeyNotFoundException exc)
            {
                // New block type does not exists in the game, try to load it
                newBlockType = LoadBlockType(newBlockTypeName, Blocks.Count);
            }

            if (newBlockType == null)
            {
                // If not able to load new block type, just do nothing.
                // TODO: handle this error properly
                return;
            }

            // Remove graphical representation of the block
            String blockTypeName = block.BlockType.Name;
            InstancedMesh <VertexData> instancedMesh = blockInstancedMeshes[blockTypeName];

            instancedMesh.RemoveInstance(block.Instance);

            // Add new graphical representation of the block
            InstancedMesh <VertexData> newInstancedMesh = blockInstancedMeshes[newBlockTypeName];

            Matrix localPosTransform = Matrix.CreateTranslation(compoundBody.CollisionInformation.LocalPosition);

            InstancedMesh <VertexData> .Instance instance = newInstancedMesh.AppendInstance(Matrix.CreateScale(block.Scale) * block.Entity.Entry.LocalTransform.Matrix * localPosTransform);

            // Make changes in block instance
            block.ChangeBlockType(newBlockType, instance);
        }
예제 #5
0
        public void LoadLevel(String fileName)
        {
            // Load level data
            GameLevelContent levelData = Game.Content.Load <GameLevelContent>(fileName);
            int blocksCount            = levelData.BlocksCount();

            // List of blocks chapes for compound body
            List <CompoundShapeEntry> shapes = new List <CompoundShapeEntry>();

            // Create block groups and block physics
            foreach (BlockGroupData blockGroup in levelData.BlockGroups)
            {
                // Create block group
                LoadBlockType(blockGroup.BlockTypeName, blocksCount);

                // Create physical representation of blocks in this group
                Vector3    scale;
                Quaternion rotation;
                Vector3    translation;
                foreach (BlockData blockData in blockGroup.Blocks)
                {
                    // Extract size, position and orientation values from block data transform
                    blockData.Transform.Decompose(out scale, out rotation, out translation);
                    blockData.Scale = scale;

                    // Create physical shape of the block to be part of compound body of blocks
                    BoxShape blockShape = new BoxShape(scale.X, scale.Y, scale.Z);

                    // Create compound shape entry for compund body of blocks
                    CompoundShapeEntry entry = new CompoundShapeEntry(blockShape, new RigidTransform(translation, rotation));
                    shapes.Add(entry);
                }
            }

            // Create compound body
            compoundBody = new CompoundBody(shapes, COMPOUND_BODY_MASS);

            compoundBody.PositionUpdateMode = BEPUphysics.PositionUpdating.PositionUpdateMode.Continuous;
            compoundBody.AngularDamping     = COMPOUND_BODY_ANGULAR_DAMPING;

            // Compound body has Position and LocalPosition (in Collision information)
            // Position property is position of mass center in global space - it is calculated automatically.
            // LocalPosition property is position of geometry of the body in its local space.
            // So in order to create compound body which is rotated around desired position ((0,0,0) for now)
            // We should switch Position and LocalPosition properties of our compound body.
            compoundBody.CollisionInformation.LocalPosition = compoundBody.Position;
            compoundBody.Position = Vector3.Zero;

            // Add constraint prevents compound body from moving
            constraint = new MaximumLinearSpeedConstraint(compoundBody, 0.0f);

            // Create collision group for removed blocks
            removedBlocksGroup = new CollisionGroup();

            // Create blocks
            int childCollidableIndex = 0;

            foreach (BlockGroupData blockGroup in levelData.BlockGroups)
            {
                Matrix localPosTransform = Matrix.CreateTranslation(compoundBody.CollisionInformation.LocalPosition);

                foreach (BlockData blockData in blockGroup.Blocks)
                {
                    // Obtain block type and instanced mesh for the block
                    BlockType blockType = blockTypes[blockGroup.BlockTypeName];
                    InstancedMesh <VertexData> instancedMesh = blockInstancedMeshes[blockGroup.BlockTypeName];

                    // Obtain physics body (a part of compound body) for the block
                    CompoundChild child = compoundBody.CollisionInformation.Children[childCollidableIndex];

                    // Create instance of the block in instanced mesh
                    InstancedMesh <VertexData> .Instance instance = instancedMesh.AppendInstance(Matrix.CreateScale(blockData.Scale) * child.Entry.LocalTransform.Matrix * localPosTransform);

                    // Store new block instance to the list
                    Block block = new Block(blockType, instance, child);
                    blocks.Add(block);

                    block.Scale = blockData.Scale;

                    childCollidableIndex++;
                }
            }

            // Add compound body and its constraints to physics space
            space.Add(compoundBody);
            space.Add(constraint);
        }