Exemple #1
0
        private IEnumerable <VoxelBlock> neighbor_nodes(VoxelBlock current)
        {
            for (int i = 0; i < 3; i++)
            {
                var axis = new Point3();
                axis[i] = 1;

                var n1 = terrain.GetVoxelAt(terrain.GetPositionOf(current) + axis);
                var n2 = terrain.GetVoxelAt(terrain.GetPositionOf(current) - axis);

                if (n1 != null)
                {
                    yield return(n1);
                }
                if (n2 != null)
                {
                    yield return(n2);
                }
            }
        }
        public void Simulate()
        {
            if (info.Mode != CameraInfo.CameraMode.FirstPerson)
            {
                return;
            }



            if (TW.Graphics.Keyboard.IsKeyPressed(Key.Space))
            {
                camData.JumpVelocity += 3;
            }

            camData.JumpVelocity = MathHelper.Clamp(camData.JumpVelocity, -100, 30);

            camData.JumpVelocity -= 9.81f * TW.Graphics.Elapsed;

            var voxelDown = terrain.GetVoxelAt(camData.Position - Vector3.UnitY);


            camData.JumpHeight += camData.JumpVelocity * TW.Graphics.Elapsed;



            info.ActiveCamera = cam;

            cam.Update(TW.Graphics.Elapsed, TW.Graphics.Keyboard, TW.Graphics.Mouse);

            var newPos = cam.CameraPosition;

            newPos += camData.JumpVelocity * TW.Graphics.Elapsed * Vector3.UnitY;

            for (int i = 0; i < points.Count; i++)
            {
                var delta = camData.Position - newPos;

                var dir = points[i];



                var voxel = terrain.GetVoxelAt(newPos + dir);

                if (voxel != null)
                {
                    //TW.Graphics.LineManager3D.AddBox(new BoundingBox(voxel.Position + new Vector3(30, 0, 0), voxel.Position + new Vector3(30, 0, 0) + MathHelper.One), new Color4(1, 1, 0));
                }
                if (voxel != null && voxel.Filled)
                {
                    dir = Vector3.Normalize(dir);
                    var axisDelta = Vector3.Dot(delta, dir);

                    if (axisDelta > -0.01)
                    {
                        axisDelta = -0.01f;
                    }

                    newPos += axisDelta * dir;
                    //TW.Graphics.LineManager3D.AddCenteredBox(newPos + dir, 0.1f, new Color4(Color.Green));
                }
                else
                {
                    //TW.Graphics.LineManager3D.AddCenteredBox(newPos + dir, 0.1f, new Color4(Color.Red));
                }
            }

            if (newPos.Y < 1.5f)
            {
                newPos.Y             = 1.5f;
                camData.JumpVelocity = 0;
            }


            if (Math.Abs(newPos.Y - camData.Position.Y) < 0.000001f)
            {
                camData.JumpVelocity = 0;
            }

            camData.Position = newPos;
            camData.LookDir  = cam.CameraDirection;

            cam.CameraPosition = camData.Position;
            cam.UpdateCameraInfo();
        }