示例#1
0
        private MotionEntity CreateUnit(Vector3 position)
        {
            var unit = new MotionEntity
            {
                MainGraphic = new MetaModel
                {
                    SkinnedMesh = new SkinnedMeshFromFile("goblin_blade_master1.x"),
                    Texture     = new TextureFromFile("goblin_blade_master1.png"),
                    World       = SkinnedMesh.InitSkinnedMeshFromMaya * Matrix.Scaling(0.1f, 0.1f, 0.1f),
                    IsBillboard = false,
                },

                /*Animation = "idle1",
                 * AnimationLoop = true,*/
                Translation = position
            };

            var mo = motionSimulation.CreateUnit();
            var lb = new Common.Bounding.Cylinder(Vector3.Zero, 1f, 0.5f);

            mo.LocalBounding  = lb;
            mo.Position       = position;
            unit.MotionObject = mo;

            unit.VisibilityLocalBounding = new MetaBoundingBox
            {
                Mesh           = ((MetaModel)unit.MainGraphic).SkinnedMesh,
                Transformation = ((MetaModel)unit.MainGraphic).World
            };

            Graphics.Renderer.Renderer.EntityAnimation ea = scene.View.Content.Peek <Graphics.Renderer.Renderer.EntityAnimation>(unit.MetaEntityAnimation);
            ea.PlayAnimation(new AnimationPlayParameters("idle1", true));

            return(unit);
        }
 public MotionEntity CreateBlock(Vector3 position, Vector3 boxSize, String textureFilename)
 {
     var block = new MotionEntity
     {
         MainGraphic = new MetaModel
         {
             XMesh = new MeshConcretize
             {
                 MeshDescription = new Graphics.Software.Meshes.BoxMesh
                 {
                     Min = Vector3.Zero,
                     Max = new Vector3(boxSize.X, boxSize.Y, boxSize.Z)
                 },
                 Layout = Graphics.Software.Vertex.PositionNormalTexcoord.Instance
             },
             Texture = new TextureFromFile(textureFilename),
         },
         Translation = position
     };
     var mo = motionSimulation.CreateStatic();
     mo.Position = position;
     mo.LocalBounding = new Common.Bounding.Chain
     {
         Shallow = true,
         Boundings = new object[]
         {
             new Graphics.MetaBoundingBox { Mesh = ((MetaModel)block.MainGraphic).XMesh },
             new Graphics.BoundingMetaMesh { Mesh = ((MetaModel)block.MainGraphic).XMesh }
         }
     };
     block.MotionObject = mo;
     return block;
 }
示例#3
0
        public MotionEntity CreateBlock(Vector3 position, Vector3 boxSize, String textureFilename)
        {
            var block = new MotionEntity
            {
                MainGraphic = new MetaModel
                {
                    XMesh = new MeshConcretize
                    {
                        MeshDescription = new Graphics.Software.Meshes.BoxMesh
                        {
                            Min = Vector3.Zero,
                            Max = new Vector3(boxSize.X, boxSize.Y, boxSize.Z)
                        },
                        Layout = Graphics.Software.Vertex.PositionNormalTexcoord.Instance
                    },
                    Texture = new TextureFromFile(textureFilename),
                },
                Translation = position
            };
            var mo = motionSimulation.CreateStatic();

            mo.Position      = position;
            mo.LocalBounding = new Common.Bounding.Chain
            {
                Shallow   = true,
                Boundings = new object[]
                {
                    new Graphics.MetaBoundingBox {
                        Mesh = ((MetaModel)block.MainGraphic).XMesh
                    },
                    new Graphics.BoundingMetaMesh {
                        Mesh = ((MetaModel)block.MainGraphic).XMesh
                    }
                }
            };
            block.MotionObject = mo;
            return(block);
        }
示例#4
0
        //private float timestep = 0.1f;
        public override void Update(float dtime)
        {
            //dtime = timestep;
            base.Update(dtime);

            Random r = new Random();

            if (InputHandler != null)
            {
                InputHandler.ProcessMessage(MessageType.Update, new UpdateEventArgs {
                    Dtime = dtime
                });
            }

            if (droppingUnits)
            {
                if (DateTime.Now.Subtract(lastDrop).TotalMilliseconds >= 6000)
                {
                    int nUnits = units.Count;
                    foreach (var u in units)
                    {
                        u.Remove();
                    }
                    units.Clear();
                    for (int i = 0; i < nUnits; i++)
                    {
                        InsertUnit(r);
                    }
                    lastDrop = DateTime.Now;
                }
            }
            else
            {
                if (DateTime.Now.Subtract(lastJump).TotalMilliseconds >= 3000)
                {
                    foreach (var u in units)
                    {
                        var umo = (Common.IMotion.IUnit)u.MotionObject;
                        if (r.NextDouble() > 0 && umo.IsOnGround)
                        {
                            umo.VelocityImpulse(new Vector3(8f * (float)r.NextDouble() - 4f, 8f * (float)r.NextDouble() - 4f, 3f * (float)r.NextDouble() + 2f));
                        }
                        //umo.VelocityImpulse(new Vector3(0, 0, 3f * (float)r.NextDouble() + 2f));
                    }
                    lastJump = DateTime.Now;
                }

                if (DateTime.Now.Subtract(lastDirectionChange).TotalMilliseconds >= 3000)
                {
                    foreach (var u in units)
                    {
                        var umo = (Common.IMotion.IUnit)u.MotionObject;
                        umo.RunVelocity = new Vector2(8f * (float)r.NextDouble() - 4f, 8f * (float)r.NextDouble() - 4f);
                    }
                    lastDirectionChange = DateTime.Now;
                    if (lastJump == DateTime.MinValue)
                    {
                        lastJump = DateTime.Now.AddMilliseconds(750);
                    }
                }
            }

            if (motionSimulation.Running)
            {
                motionSimulation.Step(dtime);
            }

            if (recordingHeight)
            {
                float height = ((Common.Motion.Simulation)motionSimulation).DebugReturnQuadtree.DebugCheckGroundHeight(controlledUnit.MotionObject.Position);
                recordedHeight.Add(new Vector3(controlledUnit.MotionObject.Position.X, controlledUnit.MotionObject.Position.Y, height));
            }

            if (renderingScene)
            {
                Graphics.Renderer.Renderer renderer = (Graphics.Renderer.Renderer) this.renderer;

                if (sceneQuadtree != null)
                {
                    sceneRendererConnector.CullScene(sceneQuadtree);
                }

                sceneRendererConnector.UpdateAnimations(dtime);

                renderer.PreRender(dtime);
                Device9.Clear(SlimDX.Direct3D9.ClearFlags.Target | SlimDX.Direct3D9.ClearFlags.ZBuffer, Color.DarkSlateGray, 1.0f, 0);

                Device9.BeginScene();

                renderer.Render(dtime);

                if (drawingUnitBBs)
                {
                    foreach (var u in units)
                    {
                        Vector3 min, max;
                        UnitBoxCoords(u.MotionObject.Position, out min, out max);
                        Draw3DAABB(scene.Camera, Matrix.Identity, min, max, Color.Red);
                        UnitBoxCoords(u.MotionObject.InterpolatedPosition, out min, out max);
                        Draw3DAABB(scene.Camera, Matrix.Identity, min, max, Color.Blue);
                    }
                }
                if (controlledUnit != null)
                {
                    Vector3 min, max;
                    UnitBoxCoords(controlledUnit.MotionObject.Position, out min, out max);
                    Draw3DAABB(scene.Camera, Matrix.Identity, min, max, Color.Red);
                    UnitBoxCoords(controlledUnit.MotionObject.InterpolatedPosition, out min, out max);
                    Draw3DAABB(scene.Camera, Matrix.Identity, min, max, Color.Blue);
                }

                if (drawingGroundLines)
                {
                    int   xPieces  = xSize;
                    int   yPieces  = xSize;
                    float stepx    = size.Width / (float)(heightMap[0].Length - 1);
                    float stepy    = size.Height / (float)(heightMap.Length - 1);
                    int   nxPieces = heightMap[0].Length / xPieces;
                    int   nyPieces = heightMap.Length / yPieces;
                    for (int yp = 0; yp < yPieces; yp++)
                    {
                        for (int xp = 0; xp < xPieces; xp++)
                        {
                            for (int y = 0; y < nyPieces; y++)
                            {
                                for (int x = 0; x < nxPieces; x++)
                                {
                                    //Vector3 pos0 = new Vector3((xp * nxPieces + x) * stepx, (yp * nyPieces + y) * stepy, heightMap[yp * (nyPieces - 1) + y][xp * (nxPieces - 1) + x]);
                                    Vector3 pos0 = GetPosOrSmth(x, xp, nxPieces, stepx, y, yp, nyPieces, stepy);
                                    Vector3 pos1 = GetPosOrSmth(x + 1, xp, nxPieces, stepx, y, yp, nyPieces, stepy);
                                    Vector3 pos2 = GetPosOrSmth(x, xp, nxPieces, stepx, y + 1, yp, nyPieces, stepy);

                                    Draw3DLines(scene.Camera, Matrix.Identity, new Vector3[] { pos0, pos1 }, (xp == xPieces - 1 ? Color.Red : Color.Green));
                                    Draw3DLines(scene.Camera, Matrix.Identity, new Vector3[] { pos0, pos2 }, Color.Green);
                                }
                            }
                        }
                    }
                }

                Device9.EndScene();
            }

            //var unit = ValidateUnitPositions();
            MotionEntity unit = null;

            if (motionSimulation.Running && unit != null)
            {
                motionSimulation.Running = false;
                ((MetaModel)unit.MainGraphic).Texture = new TextureConcretizer
                {
                    Texture = global::Graphics.Software.ITexture.SingleColorTexture(Color.Red)
                };

                var umo = ((Common.IMotion.IUnit)unit.MotionObject);
                tag = "Position: " + unit.MotionObject.Position + ". RunVelocity: " + umo.RunVelocity +
                      ". OnGround: " + umo.IsOnGround;
                FocusCamera(umo.Position);
            }

            var forRemoval = new List <MotionEntity>();

            foreach (var u in units)
            {
                if (u.MotionObject.Position.Z < -18 + heightOffset)
                {
                    forRemoval.Add(u);
                }
            }
            foreach (var u in forRemoval)
            {
                units.Remove(u);
                u.Remove();
                InsertUnit(r);   // Keep balance
            }

            if (motionSimulation.Running)
            {
                Application.MainWindow.Text = "FPS: " + FPS + ". #Static: " +
                                              ". #ListUnits: " + units.Count + (controlledUnit != null ? ". UnitPos: " + Common.Math.ToVector2(controlledUnit.Translation).ToString() : "") +
                                              ". CamPos: " + scene.Camera.Position;
            }
            else
            {
                Application.MainWindow.Text = tag + ". CamPos: " + scene.Camera.Position;
            }
        }
        private MotionEntity CreateUnit(Vector3 position)
        {
            var unit = new MotionEntity
            {
                MainGraphic = new MetaModel
                {
                    SkinnedMesh = new SkinnedMeshFromFile("goblin_blade_master1.x"),
                    Texture = new TextureFromFile("goblin_blade_master1.png"),
                    World = SkinnedMesh.InitSkinnedMeshFromMaya * Matrix.Scaling(0.1f, 0.1f, 0.1f),
                    IsBillboard = false,
                },
                /*Animation = "idle1",
                AnimationLoop = true,*/
                Translation = position
            };

            var mo = motionSimulation.CreateUnit();
            var lb = new Common.Bounding.Cylinder(Vector3.Zero, 1f, 0.5f);
            mo.LocalBounding = lb;
            mo.Position = position;
            unit.MotionObject = mo;

            unit.VisibilityLocalBounding = new MetaBoundingBox
            {
                Mesh = ((MetaModel)unit.MainGraphic).SkinnedMesh,
                Transformation = ((MetaModel)unit.MainGraphic).World
            };

            Graphics.Renderer.Renderer.EntityAnimation ea = scene.View.Content.Peek<Graphics.Renderer.Renderer.EntityAnimation>(unit.MetaEntityAnimation);
            ea.PlayAnimation(new AnimationPlayParameters("idle1", true));

            return unit;
        }
        protected override void OnKeyDown(System.Windows.Forms.KeyEventArgs e)
        {
            base.OnKeyDown(e);

            if (e.KeyCode == System.Windows.Forms.Keys.U)
            {
                InsertUnit(new Random());
            }
            if (e.KeyCode == System.Windows.Forms.Keys.R)
            {
                renderingScene = !renderingScene;
            }
            if (e.KeyCode == System.Windows.Forms.Keys.L)
            {
                drawingGroundLines = !drawingGroundLines;
            }
            if (e.KeyCode == System.Windows.Forms.Keys.K)
            {
                drawingUnitBBs = !drawingUnitBBs;
            }
            if (e.KeyCode == System.Windows.Forms.Keys.B)
            {
                recordingHeight = !recordingHeight;
                if (recordingHeight)
                    recordedHeight = new List<Vector3>();
            }
            if (e.KeyCode == System.Windows.Forms.Keys.H)
            {
                ((Common.Motion.Simulation)motionSimulation).SetHeightMap(heightMap, new Vector2(size.Width, size.Height), Vector2.Zero);
            }
            if (e.KeyCode == System.Windows.Forms.Keys.D1)
            {
                var unit = CreateUnit(new Vector2(10, 26));
                ((Common.Motion.Unit)unit.MotionObject).RunVelocity = new Vector2(0, 0);
                scene.Add(unit);
            }
            if (e.KeyCode == System.Windows.Forms.Keys.D2)
            {
                //Vector2 offset = new Vector2(13.5f, -6.5f);
                Vector2 offset = new Vector2(-8f, 3f);
                Vector2 position = new Vector2(25f, 25f) + offset;
                controlledUnit = CreateUnit(position);
                scene.Add(controlledUnit);
                scene.Camera.Position = new Vector3(scene.Camera.Position.X + offset.X, scene.Camera.Position.Y + offset.Y, scene.Camera.Position.Z);
                ((LookatCamera)scene.Camera).Lookat = new Vector3(position, 0);
            }
            if (e.KeyCode == System.Windows.Forms.Keys.D3)
            {
                if (controlledUnit != null)
                {
                    controlledUnit.Remove();
                    controlledUnit = null;
                }
            }
            if (e.KeyCode == System.Windows.Forms.Keys.D7)
            {
                angle -= (float)System.Math.PI / 2f;
                if (angle < 0) angle += (float)System.Math.PI * 2f;
                controlledUnit.MotionObject.Rotation = Quaternion.RotationAxis(Vector3.UnitZ, angle);
            }
            if (e.KeyCode == System.Windows.Forms.Keys.D8)
            {
                angle += (float)System.Math.PI / 2f;
                if (angle >= System.Math.PI * 2f) angle -= (float)System.Math.PI * 2f;
                controlledUnit.MotionObject.Rotation = Quaternion.RotationAxis(Vector3.UnitZ, angle);
            }
            if (e.KeyCode == System.Windows.Forms.Keys.D0)
            {
                System.Windows.Forms.Clipboard.SetText(((Common.Motion.Simulation)motionSimulation).DebugQuadTree());
            }

            if (e.KeyCode == System.Windows.Forms.Keys.Up)
            {
                if (controlledUnit != null)
                    ((Common.IMotion.IUnit)controlledUnit.MotionObject).RunVelocity = new Vector2(((Common.IMotion.IUnit)controlledUnit.MotionObject).RunVelocity.X, -2);
            }
            if (e.KeyCode == System.Windows.Forms.Keys.Down)
                if (controlledUnit != null)
                    ((Common.IMotion.IUnit)controlledUnit.MotionObject).RunVelocity = new Vector2(((Common.IMotion.IUnit)controlledUnit.MotionObject).RunVelocity.X, 2);
            if (e.KeyCode == System.Windows.Forms.Keys.Left)
                if (controlledUnit != null)
                    ((Common.IMotion.IUnit)controlledUnit.MotionObject).RunVelocity = new Vector2(-2, ((Common.IMotion.IUnit)controlledUnit.MotionObject).RunVelocity.Y);
            if (e.KeyCode == System.Windows.Forms.Keys.Right)
                if (controlledUnit != null)
                    ((Common.IMotion.IUnit)controlledUnit.MotionObject).RunVelocity = new Vector2(2, ((Common.IMotion.IUnit)controlledUnit.MotionObject).RunVelocity.Y);

            if (e.KeyCode == System.Windows.Forms.Keys.ControlKey)
                if (controlledUnit != null)
                {
                    var u = (Common.Motion.Unit)controlledUnit.MotionObject;
                    if (u.IsOnGround)
                        u.VelocityImpulse(new Vector3(u.Velocity.X, u.Velocity.Y, 4));
                }
            if (e.KeyCode == System.Windows.Forms.Keys.F1)
            {
                ((Common.Motion.ThreadSimulationProxy)motionSimulation).Shutdown();
                System.Threading.Thread.Sleep(300);
                System.Windows.Forms.Application.Exit();
            }
            if (e.KeyCode == System.Windows.Forms.Keys.Enter)
            {
                motionSimulation.Running = true;
            }
        }