Example #1
0
        public void Update(GameTime gameTime)
        {
            var direction = XVector3.Transform(XVector3.UnitZ,
                                               Matrix.CreateRotationX(MathHelper.ToRadians(Game.Client.Pitch)) *
                                               Matrix.CreateRotationY(MathHelper.ToRadians(-(Game.Client.Yaw - 180) + 180)));

            var cast = VoxelCast.Cast(Game.Client.World,
                                      new TRay(Game.Camera.Position, new TVector3(direction.X, direction.Y, direction.Z)),
                                      Game.BlockRepository, TrueCraftGame.Reach, TrueCraftGame.Reach + 2);

            if (cast == null)
            {
                Game.HighlightedBlock = -Coordinates3D.One;
            }
            else
            {
                var provider = Game.BlockRepository.GetBlockProvider(Game.Client.World.GetBlockID(cast.Item1));
                if (provider.InteractiveBoundingBox != null)
                {
                    var box = provider.InteractiveBoundingBox.Value;

                    Game.HighlightedBlock     = cast.Item1;
                    Game.HighlightedBlockFace = cast.Item2;

                    DestructionEffect.World = HighlightEffect.World = Matrix.Identity
                                                                      * Matrix.CreateScale(new XVector3((float)box.Width, (float)box.Height, (float)box.Depth))
                                                                      * Matrix.CreateTranslation(new XVector3((float)box.Min.X, (float)box.Min.Y, (float)box.Min.Z))
                                                                      * Matrix.CreateTranslation(new XVector3(cast.Item1.X, cast.Item1.Y, cast.Item1.Z));
                }
            }
        }
        private void Transform(AstroObject astroObject, Matrix3D baseTransformation, FrameworkElement txt)
        {
            // Transform Model
            if (astroObject == null)
            {
                return;
            }
            var m = Matrix.Identity;

            m *= Matrix.CreateTranslation(0, 60, 0);
            m *= Matrix.CreateRotationX(Microsoft.Xna.Framework.MathHelper.ToRadians(90));
            m *= baseTransformation.ToXnaMatrix();
            astroObject.Transform = m;
            astroObject.IsVisible = true;

            // Transform FrameworkElement
            // Center at origin of the TextBlock
            var centerAtOrigin = Matrix3DFactory.CreateTranslation(-txt.ActualWidth * 0.5, -txt.ActualHeight * 0.5, 0);
            // Swap the y-axis
            var scale = Matrix3DFactory.CreateScale(1, -1, 1);
            // Move a bit away from the center
            var translation = Matrix3DFactory.CreateTranslation(0, 50, 0);
            // Calculate the complete transformation matrix based on the first detection result
            var world = centerAtOrigin * translation * scale * baseTransformation;

            // Calculate the final transformation matrix by using the camera projection matrix
            var vp = Matrix3DFactory.CreateViewportTransformation(Viewport.ActualWidth, Viewport.ActualHeight);
            var mp = Matrix3DFactory.CreateViewportProjection(world, Matrix3D.Identity, arDetector.Projection, vp);

            // Apply the final transformation matrix to the TextBox
            txt.Projection = new Matrix3DProjection {
                ProjectionMatrix = mp
            };
            txt.Visibility = Visibility.Visible;
        }
Example #3
0
        public BasicEntity(ModelDefinition modelbb, MaterialEffect material, Vector3 position, double angleZ, double angleX, double angleY, Vector3 scale, MeshMaterialLibrary library = null, Entity physicsObject = null)
        {
            Id                  = IdGenerator.GetNewId();
            Name                = GetType().Name + " " + Id;
            WorldTransform      = new TransformMatrix(Matrix.Identity, Id);
            ModelDefinition     = modelbb;
            Model               = modelbb.Model;
            BoundingBox         = modelbb.BoundingBox;
            BoundingBoxOffset   = modelbb.BoundingBoxOffset;
            SignedDistanceField = modelbb.SDF;

            Material = material;
            Position = position;
            Scale    = scale;

            RotationMatrix = Matrix.CreateRotationX((float)angleX) * Matrix.CreateRotationY((float)angleY) *
                             Matrix.CreateRotationZ((float)angleZ);

            if (library != null)
            {
                RegisterInLibrary(library);
            }

            if (physicsObject != null)
            {
                RegisterPhysics(physicsObject);
            }

            WorldTransform.World        = Matrix.CreateScale(Scale) * RotationMatrix * Matrix.CreateTranslation(Position);
            WorldTransform.Scale        = Scale;
            WorldTransform.InverseWorld = Matrix.Invert(Matrix.CreateTranslation(BoundingBoxOffset * Scale) * RotationMatrix * Matrix.CreateTranslation(Position));
        }
Example #4
0
        private void _motion_CurrentValueChanged(object sender, SensorReadingEventArgs <MotionReading> e)
        {
            double motionDelta = 0.01;

            if (!_motion.IsDataValid)
            {
                return;
            }

            if (Math.Abs(e.SensorReading.Attitude.Yaw - _yaw) > motionDelta)
            {
                _yaw = e.SensorReading.Attitude.Yaw;
            }

            if (Math.Abs(e.SensorReading.Attitude.Roll - _roll) > motionDelta)
            {
                _roll = e.SensorReading.Attitude.Roll;
            }

            if (Math.Abs(e.SensorReading.Attitude.Pitch - _pitch) > motionDelta)
            {
                _pitch = e.SensorReading.Attitude.Pitch;
            }

            Dispatcher.BeginInvoke(delegate()
            {
                if (myVM.ClickedNearbyLocation != null)
                {
                    myVM.ClickedNearbyLocation.CalculateHeadingAngle(MathHelper.ToDegrees(_yaw));
                }
            });

            if (_onDragEndAnimation)
            {
                return;
            }

            if (_gestureState == GestureStateEnum.OnFreeDrag)
            {
                _cameraMatrixBehindDrag  = Matrix.CreateLookAt(new Vector3(0, 0, 0), new Vector3(0, 0, -1), Vector3.Up);
                _cameraMatrixBehindDrag *= Matrix.CreateRotationY(-_yaw);
                _cameraMatrixBehindDrag *= Matrix.CreateRotationZ(_roll);
                _cameraMatrixBehindDrag *= Matrix.CreateRotationX(-_pitch + MathHelper.PiOver2);
            }
            else
            {
                _cameraMatrix  = Matrix.CreateLookAt(new Vector3(0, 0, 0), new Vector3(0, 0, -1), Vector3.Up);
                _cameraMatrix *= Matrix.CreateRotationY(-_yaw);
                _cameraMatrix *= Matrix.CreateRotationZ(_roll);
                _cameraMatrix *= Matrix.CreateRotationX(-_pitch + MathHelper.PiOver2);

                //rotate radar
                Dispatcher.BeginInvoke(delegate()
                {
                    (radarCanvas.RenderTransform as System.Windows.Media.RotateTransform).Angle = MathHelper.ToDegrees((_yaw));
                });
            }
        }
Example #5
0
        private void OnTargetImageMouseMove(object sender, MouseEventArgs e)
        {
            if (!_isDragging)
            {
                return;
            }

            var mousePosition = e.GetPosition(_targetImage);

            var dX = mousePosition.X - _lastMousePosition.X;
            var dY = mousePosition.Y - _lastMousePosition.Y;

            _lastMousePosition = mousePosition;

            _worldMatrix *= Matrix.CreateRotationX(0.01f * (float)dY);
            _worldMatrix *= Matrix.CreateRotationY(0.01f * (float)dX);
        }
        private void CurrentValueChanged(MotionReading reading)
        {
            if (viewport.Width == 0)
            {
                InitializeViewport();
            }


            // Get the RotationMatrix from the MotionReading.
            // Rotate it 90 degrees around the X axis to put it in xna coordinate system.
            Matrix attitude = Matrix.CreateRotationX(MathHelper.PiOver2) * reading.Attitude.RotationMatrix;

            // Loop through the points in the list
            for (int i = 0; i < points.Count; i++)
            {
                // Create a World matrix for the point.
                Matrix world = Matrix.CreateWorld(points[i], new Vector3(0, 0, 1), new Vector3(0, 1, 0));

                // Use Viewport.Project to project the point from 3D space into screen coordinates.
                Vector3 projected = viewport.Project(Vector3.Zero, projection, view, world * attitude);


                if (projected.Z > 1 || projected.Z < 0)
                {
                    // If the point is outside of this range, it is behind the camera.
                    // So hide the TextBlock for this point.
                    textBlocks[i].Visibility = Visibility.Collapsed;
                }
                else
                {
                    // Otherwise, show the TextBlock
                    textBlocks[i].Visibility = Visibility.Visible;

                    // Create a TranslateTransform to position the TextBlock.
                    // Offset by half of the TextBlock's RenderSize to center it on the point.
                    TranslateTransform tt = new TranslateTransform();
                    tt.X = projected.X - (textBlocks[i].RenderSize.Width / 2);
                    tt.Y = projected.Y - (textBlocks[i].RenderSize.Height / 2);
                    textBlocks[i].RenderTransform = tt;
                }
            }
        }
        /*
         * private void NameTextBox_LostFocus(object sender, RoutedEventArgs e)
         * {
         *  // When the TextBox loses focus. Hide the Canvas containing it.
         *  TextBoxCanvas.Visibility = Visibility.Collapsed;
         * }
         */
        private void NameTextBox_KeyUp(object sender, KeyEventArgs e)
        {
            // If the key is not the Enter key, don't do anything.
            if (e.Key != Key.Enter)
            {
                return;
            }

            // When the TextBox loses focus. Hide the Canvas containing it.
            TextBoxCanvas.Visibility = Visibility.Collapsed;

            // If any of the objects we need are not present, exit the event handler.
            if (NameTextBox.Text == "" || pointOnScreen == null || motion == null)
            {
                return;
            }

            // Translate the point before projecting it.
            System.Windows.Point p = pointOnScreen;
            p.X  = LayoutRoot.RenderSize.Width - p.X;
            p.Y  = LayoutRoot.RenderSize.Height - p.Y;
            p.X *= .5;
            p.Y *= .5;


            // Use the attitude Matrix saved in the OnMouseLeftButtonUp handler.
            // Rotate it 90 degrees around the X axis to put it in xna coordinate system.
            attitude = Matrix.CreateRotationX(MathHelper.PiOver2) * attitude;

            // Use Viewport.Unproject to translate the point on the screen to 3D space.
            Vector3 unprojected = viewport.Unproject(new Vector3((float)p.X, (float)p.Y, -.9f), projection, view, attitude);

            unprojected.Normalize();
            unprojected *= -10;

            // Call the helper method to add this point
            AddPoint(unprojected, NameTextBox.Text);

            // Clear the TextBox
            NameTextBox.Text = "";
        }
Example #8
0
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            float amount = 0.5f + (float)Math.Sin((double)gameTime.TotalGameTime.TotalSeconds / 4.0) / 2f;

            effect.World = Matrix.CreateRotationX(MathHelper.Lerp(-(float)Math.PI, (float)Math.PI, amount)) *
                           Matrix.CreateRotationY(MathHelper.Lerp(-(float)Math.PI, (float)Math.PI, amount)) *
                           Matrix.CreateRotationZ(MathHelper.Lerp(-(float)Math.PI, (float)Math.PI, amount));

            effect.View       = camera.view;
            effect.Projection = camera.projection;

            // Begin effect and draw for each pass
            foreach (EffectPass pass in effect.CurrentTechnique.Passes)
            {
                pass.Apply();
                for (int i = 0; i < quad.Length; i++)
                {
                    GraphicsDevice.DrawUserIndexedPrimitives <VertexPositionNormalTexture>(PrimitiveType.TriangleList, quad[i].Vertices, 0, 4, quad[i].Indexes, 0, 2);
                }
            }
            base.Draw(gameTime);
        }
Example #9
0
        public virtual void Initialize()
        {
            float tilt = MathHelper.ToRadians(0);  // 0 degree angle

            // Use the world matrix to tilt the cube along x and y axes.
            _worldMatrix = Matrix.CreateRotationX(tilt) * Matrix.CreateRotationY(tilt);
            _viewMatrix  = Matrix.CreateLookAt(new Vector3(5, 5, 5), Vector3.Zero, Vector3.Up);

            _projectionMatrix = Matrix.CreatePerspectiveFieldOfView(
                MathHelper.ToRadians(45),  // 45 degree angle
                (float)GraphicsDevice.Viewport.Width /
                (float)GraphicsDevice.Viewport.Height,
                1.0f, 100.0f);

            _basicEffect = new BasicEffect(GraphicsDevice);

            _basicEffect.World      = _worldMatrix;
            _basicEffect.View       = _viewMatrix;
            _basicEffect.Projection = _projectionMatrix;

            // primitive color
            _basicEffect.AmbientLightColor = new Vector3(0.1f, 0.1f, 0.1f);
            _basicEffect.DiffuseColor      = new Vector3(1.0f, 1.0f, 1.0f);
            _basicEffect.SpecularColor     = new Vector3(0.25f, 0.25f, 0.25f);
            _basicEffect.SpecularPower     = 5.0f;
            _basicEffect.Alpha             = 1.0f;

            _basicEffect.LightingEnabled = true;
            if (_basicEffect.LightingEnabled)
            {
                _basicEffect.DirectionalLight0.Enabled = true; // enable each light individually
                if (_basicEffect.DirectionalLight0.Enabled)
                {
                    // x direction
                    _basicEffect.DirectionalLight0.DiffuseColor = new Vector3(1, 0, 0); // range is 0 to 1
                    _basicEffect.DirectionalLight0.Direction    = Vector3.Normalize(new Vector3(-1, 0, 0));
                    // points from the light to the origin of the scene
                    _basicEffect.DirectionalLight0.SpecularColor = Vector3.One;
                }

                _basicEffect.DirectionalLight1.Enabled = true;
                if (_basicEffect.DirectionalLight1.Enabled)
                {
                    // y direction
                    _basicEffect.DirectionalLight1.DiffuseColor  = new Vector3(0, 0.75f, 0);
                    _basicEffect.DirectionalLight1.Direction     = Vector3.Normalize(new Vector3(0, -1, 0));
                    _basicEffect.DirectionalLight1.SpecularColor = Vector3.One;
                }

                _basicEffect.DirectionalLight2.Enabled = true;
                if (_basicEffect.DirectionalLight2.Enabled)
                {
                    // z direction
                    _basicEffect.DirectionalLight2.DiffuseColor  = new Vector3(0, 0, 0.5f);
                    _basicEffect.DirectionalLight2.Direction     = Vector3.Normalize(new Vector3(0, 0, -1));
                    _basicEffect.DirectionalLight2.SpecularColor = Vector3.One;
                }
            }

            vertexDeclaration = new VertexDeclaration(new[]
            {
                new VertexElement(0, VertexElementFormat.Vector3, VertexElementUsage.Position, 0),
                new VertexElement(12, VertexElementFormat.Vector3, VertexElementUsage.Normal, 0),
                new VertexElement(24, VertexElementFormat.Vector2, VertexElementUsage.TextureCoordinate, 0)
            });

            Vector3 topLeftFront     = new Vector3(-1.0f, 1.0f, 1.0f);
            Vector3 bottomLeftFront  = new Vector3(-1.0f, -1.0f, 1.0f);
            Vector3 topRightFront    = new Vector3(1.0f, 1.0f, 1.0f);
            Vector3 bottomRightFront = new Vector3(1.0f, -1.0f, 1.0f);
            Vector3 topLeftBack      = new Vector3(-1.0f, 1.0f, -1.0f);
            Vector3 topRightBack     = new Vector3(1.0f, 1.0f, -1.0f);
            Vector3 bottomLeftBack   = new Vector3(-1.0f, -1.0f, -1.0f);
            Vector3 bottomRightBack  = new Vector3(1.0f, -1.0f, -1.0f);

            Vector2 textureTopLeft     = new Vector2(0.0f, 0.0f);
            Vector2 textureTopRight    = new Vector2(1.0f, 0.0f);
            Vector2 textureBottomLeft  = new Vector2(0.0f, 1.0f);
            Vector2 textureBottomRight = new Vector2(1.0f, 1.0f);

            Vector3 frontNormal  = new Vector3(0.0f, 0.0f, 1.0f);
            Vector3 backNormal   = new Vector3(0.0f, 0.0f, -1.0f);
            Vector3 topNormal    = new Vector3(0.0f, 1.0f, 0.0f);
            Vector3 bottomNormal = new Vector3(0.0f, -1.0f, 0.0f);
            Vector3 leftNormal   = new Vector3(-1.0f, 0.0f, 0.0f);
            Vector3 rightNormal  = new Vector3(1.0f, 0.0f, 0.0f);

            var cubeVertices = new VertexPositionNormalTexture[36];

            // Front face.
            cubeVertices[0] = new VertexPositionNormalTexture(topLeftFront, frontNormal, textureTopLeft);
            cubeVertices[1] = new VertexPositionNormalTexture(bottomLeftFront, frontNormal, textureBottomLeft);
            cubeVertices[2] = new VertexPositionNormalTexture(topRightFront, frontNormal, textureTopRight);
            cubeVertices[3] = new VertexPositionNormalTexture(bottomLeftFront, frontNormal, textureBottomLeft);
            cubeVertices[4] = new VertexPositionNormalTexture(bottomRightFront, frontNormal, textureBottomRight);
            cubeVertices[5] = new VertexPositionNormalTexture(topRightFront, frontNormal, textureTopRight);

            // Back face.
            cubeVertices[6]  = new VertexPositionNormalTexture(topLeftBack, backNormal, textureTopRight);
            cubeVertices[7]  = new VertexPositionNormalTexture(topRightBack, backNormal, textureTopLeft);
            cubeVertices[8]  = new VertexPositionNormalTexture(bottomLeftBack, backNormal, textureBottomRight);
            cubeVertices[9]  = new VertexPositionNormalTexture(bottomLeftBack, backNormal, textureBottomRight);
            cubeVertices[10] = new VertexPositionNormalTexture(topRightBack, backNormal, textureTopLeft);
            cubeVertices[11] = new VertexPositionNormalTexture(bottomRightBack, backNormal, textureBottomLeft);

            // Top face.
            cubeVertices[12] = new VertexPositionNormalTexture(topLeftFront, topNormal, textureBottomLeft);
            cubeVertices[13] = new VertexPositionNormalTexture(topRightBack, topNormal, textureTopRight);
            cubeVertices[14] = new VertexPositionNormalTexture(topLeftBack, topNormal, textureTopLeft);
            cubeVertices[15] = new VertexPositionNormalTexture(topLeftFront, topNormal, textureBottomLeft);
            cubeVertices[16] = new VertexPositionNormalTexture(topRightFront, topNormal, textureBottomRight);
            cubeVertices[17] = new VertexPositionNormalTexture(topRightBack, topNormal, textureTopRight);

            // Bottom face.
            cubeVertices[18] = new VertexPositionNormalTexture(bottomLeftFront, bottomNormal, textureTopLeft);
            cubeVertices[19] = new VertexPositionNormalTexture(bottomLeftBack, bottomNormal, textureBottomLeft);
            cubeVertices[20] = new VertexPositionNormalTexture(bottomRightBack, bottomNormal, textureBottomRight);
            cubeVertices[21] = new VertexPositionNormalTexture(bottomLeftFront, bottomNormal, textureTopLeft);
            cubeVertices[22] = new VertexPositionNormalTexture(bottomRightBack, bottomNormal, textureBottomRight);
            cubeVertices[23] = new VertexPositionNormalTexture(bottomRightFront, bottomNormal, textureTopRight);

            // Left face.
            cubeVertices[24] = new VertexPositionNormalTexture(topLeftFront, leftNormal, textureTopRight);
            cubeVertices[25] = new VertexPositionNormalTexture(bottomLeftBack, leftNormal, textureBottomLeft);
            cubeVertices[26] = new VertexPositionNormalTexture(bottomLeftFront, leftNormal, textureBottomRight);
            cubeVertices[27] = new VertexPositionNormalTexture(topLeftBack, leftNormal, textureTopLeft);
            cubeVertices[28] = new VertexPositionNormalTexture(bottomLeftBack, leftNormal, textureBottomLeft);
            cubeVertices[29] = new VertexPositionNormalTexture(topLeftFront, leftNormal, textureTopRight);

            // Right face.
            cubeVertices[30] = new VertexPositionNormalTexture(topRightFront, rightNormal, textureTopLeft);
            cubeVertices[31] = new VertexPositionNormalTexture(bottomRightFront, rightNormal, textureBottomLeft);
            cubeVertices[32] = new VertexPositionNormalTexture(bottomRightBack, rightNormal, textureBottomRight);
            cubeVertices[33] = new VertexPositionNormalTexture(topRightBack, rightNormal, textureTopRight);
            cubeVertices[34] = new VertexPositionNormalTexture(topRightFront, rightNormal, textureTopLeft);
            cubeVertices[35] = new VertexPositionNormalTexture(bottomRightBack, rightNormal, textureBottomRight);

            _vertexBuffer = new VertexBuffer(GraphicsDevice, vertexDeclaration, cubeVertices.Length, BufferUsage.None);
            _vertexBuffer.SetData(cubeVertices);
        }