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)); }
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 _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)); }); } }
private void TranslateRightButton_Click(object sender, EventArgs e) { ArcBallCamera cam = CameraManager.ActiveCamera as ArcBallCamera; XnaMatrix rotationmatrix = XnaMatrix.CreateRotationY(MathHelper.ToRadians(cam.Yaw)); Vector3 dfvect = new Vector3(1, 0, 0); //Shift the camera Right Vector3 dfTransVect = Vector3.Transform(dfvect, rotationmatrix); cam.Target += dfTransVect; }
/// <summary> /// Initializes World matrix of this location on the 3d world /// </summary> private void InitializeWorldMatrix() { if (VectorPoint.Z > 0) { WorldMatrix = Matrix.CreateRotationY((float)Math.Atan(VectorPoint.X / VectorPoint.Z)); } else { WorldMatrix = Matrix.CreateRotationY(-MathHelper.Pi + (float)Math.Atan(VectorPoint.X / VectorPoint.Z)); } WorldMatrix = WorldMatrix * Matrix.CreateTranslation(VectorPoint); }
public virtual void Render(TimeSpan time) { GraphicsDevice.Clear(Color.SteelBlue); GraphicsDevice.RasterizerState = RasterizerState.CullNone; GraphicsDevice.SetVertexBuffer(_vertexBuffer); // Rotate cube around up-axis. _basicEffect.World = Matrix.CreateRotationY((float)time.Milliseconds / 1000 * MathHelper.TwoPi) * _worldMatrix; foreach (var pass in _basicEffect.CurrentTechnique.Passes) { pass.Apply(); GraphicsDevice.DrawPrimitives(PrimitiveType.TriangleList, 0, 12); } }
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 OnCustomPresent(object sender, CustomPresentEventArgs e) { var time = e.Time; if (!_isDragging) { _worldMatrix *= Matrix.CreateRotationY(-(float)time.ElapsedGameTime.Milliseconds / 1000f); } Clear(); Model model; lock (_modelLock) model = _model; if (model != null) { // Draw the model. A model can have multiple meshes, so loop. foreach (var mesh in model.Meshes) { // This is where the mesh orientation is set, as well // as our camera and projection. foreach (BasicEffect effect in mesh.Effects) { effect.EnableDefaultLighting(); effect.World = _worldMatrix; effect.View = _viewMatrix; effect.Projection = _projectionMatrix; } // Draw the mesh, using the effects set above. mesh.Draw(); } } e.Handled = true; }
public void Draw() { GraphicsDevice device = SharedGraphicsDeviceManager.Current.GraphicsDevice; device.BlendState = BlendState.Opaque; device.RasterizerState = RasterizerState.CullCounterClockwise; device.DepthStencilState = DepthStencilState.Default; // Store the old viewport and set the desired viewport Viewport oldViewport = device.Viewport; device.Viewport = new Viewport(state.ViewportRect); // Calculate the world-view-projection matrices for the tank and camera Matrix world = Matrix.CreateScale(.002f) * Matrix.CreateRotationY(MathHelper.ToRadians((float)state.TankRotationY)); Matrix view = Matrix.CreateLookAt(state.CameraPosition, state.CameraTarget, Vector3.Up); Matrix projection = Matrix.CreatePerspectiveFieldOfView(1, device.Viewport.AspectRatio, .01f, 100f); // Draw the sky sky.Draw(view, projection); // Draw the ground groundEffect.World = Matrix.Identity; groundEffect.View = view; groundEffect.Projection = projection; device.SetVertexBuffer(groundVertices); foreach (var pass in groundEffect.CurrentTechnique.Passes) { pass.Apply(); device.DrawPrimitives(PrimitiveType.TriangleStrip, 0, 2); } // Draw the tank tank.Draw(world, view, projection, state); // Reset the viewport device.Viewport = oldViewport; }
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); }
private void Present() { var now = Environment.TickCount; if (_lastTime != 0L && !_isDragging) { _worldMatrix *= Matrix.CreateRotationY(-(float)(now - _lastTime) / 1000); } _lastTime = now; if (_device.GraphicsDeviceStatus != GraphicsDeviceStatus.Normal) { while (_device.GraphicsDeviceStatus == GraphicsDeviceStatus.Lost) { Thread.Sleep(1); } Reset(); return; } // Do resize if (_needResize) { _needResize = false; var backupFrontBuffer = _frontBuffer; var backupBackBuffer = _backBuffer; var backupDepthStencilBuffer = _depthStencilBuffer; CreateBuffers(); backupFrontBuffer.Dispose(); backupBackBuffer.Dispose(); backupDepthStencilBuffer.Dispose(); return; } _device.SetRenderTarget(0, _backBuffer); _device.DepthStencilBuffer = _depthStencilBuffer; Clear(); Model model; lock (_modelLock) model = _model; if (model != null) { // Draw the model. A model can have multiple meshes, so loop. foreach (var mesh in model.Meshes) { // This is where the mesh orientation is set, as well // as our camera and projection. foreach (BasicEffect effect in mesh.Effects) { effect.EnableDefaultLighting(); effect.World = _worldMatrix; effect.View = _viewMatrix; effect.Projection = _projectionMatrix; } // Draw the mesh, using the effects set above. mesh.Draw(); } } _copySurfaceMethod.Invoke(_device, new object[] { _backBufferPointer, _frontBufferPointer }); _device.SetRenderTarget(0, null); _device.DepthStencilBuffer = _deviceDepthStencilBuffer; try { _device.Present(); } catch (DeviceLostException) {} }
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); }
/// <summary> /// This is the method that is called to move our avatar /// </summary> /// <code> /// // create the class that does translations /// GiveHelpTransforms ght = new GiveHelpTransforms(); /// // have it load our XML into the SourceXML property /// ght.LoadXMLFromFile( /// "E:\\Inetpub\\wwwroot\\GiveHelp\\GiveHelpDoc.xml"); /// </code> void UpdateAvatarPosition() { var keyboardState = Keyboard.GetState(); var currentState = GamePad.GetState(PlayerIndex.One); var mouseState = Mouse.GetState(); //if (Console.IsOpen()) return; if (keyboardState.IsKeyDown(Keys.P)) { System.Console.WriteLine("Open!"); } if (keyboardState.IsKeyDown(Keys.A) || (currentState.DPad.Left == ButtonState.Pressed)) { // move left //avatarYaw += RotationSpeed; var forwardMovement = Matrix.CreateRotationY(avatarYaw); var v = new Vector3(1, 0, 0); v = Vector3.Transform(v, forwardMovement); avatarPosition.X += v.X; avatarPosition.Y += v.Y; avatarPosition.Z += v.Z; } if (keyboardState.IsKeyDown(Keys.D) || (currentState.DPad.Right == ButtonState.Pressed)) { // move right //avatarYaw -= RotationSpeed; var forwardMovement = Matrix.CreateRotationY(avatarYaw); var v = new Vector3(1, 0, 0); avatarPosition -= Vector3.Transform(v, forwardMovement); } if (keyboardState.IsKeyDown(Keys.W) || (currentState.DPad.Up == ButtonState.Pressed)) { //var forwardMovement = Matrix.CreateRotationY(avatarYaw); //var v = new Vector3(0, 0, ForwardSpeed); //v = Vector3.Transform(v, forwardMovement); //avatarPosition.Z += v.Z; //avatarPosition.X += v.X; var horizontal = avatarPitch.Cos(); var vertical = -avatarPitch.Sin(); var v = new Vector3(avatarYaw.Sin() * horizontal, vertical, avatarYaw.Cos() * horizontal); v.Normalize(); avatarPosition += v * ForwardSpeed; } if (keyboardState.IsKeyDown(Keys.S) || (currentState.DPad.Down == ButtonState.Pressed)) { var horizontal = avatarPitch.Cos(); var vertical = -avatarPitch.Sin(); var v = new Vector3(avatarYaw.Sin() * horizontal, vertical, avatarYaw.Cos() * horizontal); v.Normalize(); avatarPosition -= v * ForwardSpeed; } if (keyboardState.IsKeyDown(Keys.F)) { avatarPosition.Y = avatarPosition.Y - 1; } if (keyboardState.IsKeyDown(Keys.R) || keyboardState.IsKeyDown(Keys.Space)) { avatarPosition.Y = avatarPosition.Y + 1; } if (keyboardState.IsKeyDown(Keys.T)) { _thirdPersonReference.Y = _thirdPersonReference.Y - 0.25f; } if (keyboardState.IsKeyDown(Keys.G)) { _thirdPersonReference.Y = _thirdPersonReference.Y + 0.25f; } // adjust speed if (keyboardState.IsKeyDown(Keys.OemPlus)) { ForwardSpeed = Math.Min(ForwardSpeed + 0.1f, 15); } else if (keyboardState.IsKeyDown(Keys.OemMinus)) { ForwardSpeed = Math.Max(ForwardSpeed - 0.1f, 0.1f); } if (mouseState.LeftButton == ButtonState.Pressed && !mouseLeftButtonDown) { // select polygon mouseLeftButtonDown = true; var width = _graphics.GraphicsDevice.Viewport.Width; var height = _graphics.GraphicsDevice.Viewport.Height; var x = mouseState.X; var y = mouseState.Y; if (x < 0 || y < 0 || x > width || y > height) { return; } var add = (keyboardState.IsKeyDown(Keys.LeftShift) || keyboardState.IsKeyDown(Keys.RightShift)); var startPos = new Vector3(x, y, 0); var endPos = new Vector3(x, y, 1); var near = _graphics.GraphicsDevice.Viewport.Unproject(startPos, _proj, _view, Matrix.Identity).ToWCell(); var far = _graphics.GraphicsDevice.Viewport.Unproject(endPos, _proj, _view, Matrix.Identity).ToWCell(); var dir = (far - near).NormalizedCopy(); var ray = new Ray(near, dir); TerrainProgram.TerrainManager.SelectedTriangleManager.UpdateSelectedTriangle(ray, add); } if (mouseState.LeftButton == ButtonState.Released && mouseLeftButtonDown) { mouseLeftButtonDown = false; } }
private void HandleGesture() { while (TouchPanel.IsGestureAvailable) { GestureSample gestureSample = TouchPanel.ReadGesture(); switch (gestureSample.GestureType) { case GestureType.FreeDrag: _gestureTimer.Stop(); _gestureState = GestureStateEnum.OnFreeDrag; float rotateRate = 0.01f; float translateRate = 0.1f; _cameraMatrix = _cameraMatrix * Matrix.CreateRotationY(-gestureSample.Delta.X * rotateRate); _cameraMatrix = _cameraMatrix * Matrix.CreateTranslation(0f, -gestureSample.Delta.Y * translateRate, 0f); //rotate radar Dispatcher.BeginInvoke(delegate() { (radarCanvas.RenderTransform as System.Windows.Media.RotateTransform).Angle += MathHelper.ToDegrees((gestureSample.Delta.X * rotateRate)); }); break; case GestureType.DragComplete: _gestureTime = 0; _gestureTimer.Start(); break; case GestureType.Tap: _gestureState = GestureStateEnum.OnMotion; if (myVM.ClickedNearbyLocation != null) { AnimateLocationDeselection(myVM.ClickedNearbyLocation); myVM.ClickedNearbyLocation.IsClickedOnView = false; if (OnLocationReleased != null) { OnLocationReleased(this, myVM.ClickedNearbyLocation); } } if (_gestureState != GestureStateEnum.OnPressHold) { _gestureState = GestureStateEnum.OnPressHold; float mouseX = gestureSample.Position.X; float mouseY = gestureSample.Position.Y; Vector3 nearsource = new Vector3((float)mouseX, (float)mouseY, 0f); Vector3 farsource = new Vector3((float)mouseX, (float)mouseY, -1f); Vector3 nearPoint = _device.Viewport.Unproject(nearsource, _projectionMatrix, _cameraMatrix, _phoneWorld); Vector3 farPoint = _device.Viewport.Unproject(farsource, _projectionMatrix, _cameraMatrix, _phoneWorld); Vector3 direction = -farPoint + nearPoint; direction.Normalize(); Ray pickRay = new Ray(nearPoint, direction); if (myVM.ClickedNearbyLocation != null) { AnimateLocationDeselection(myVM.ClickedNearbyLocation); myVM.ClickedNearbyLocation.IsClickedOnView = false; if (OnLocationReleased != null) { OnLocationReleased(this, myVM.ClickedNearbyLocation); } } foreach (LocationsVM.Location l in myVM.SelectedNearbyLocations) { float?result = pickRay.Intersects(l.PlacemarkBoundingSphere); if (result.HasValue) { AnimateLocationSelection(l); l.IsClickedOnView = true; if (OnLocationSelected != null) { OnLocationSelected(this, l); } break; } } } break; } } }
public override void Update(GameTime gameTime) { var delta = Delta; var gamePad = GamePad.GetState(PlayerIndex.One); // TODO: Can this stuff be done effectively in the GamePadHandler? if (gamePad.IsConnected && gamePad.ThumbSticks.Left.Length() != 0) { delta = new XVector3(gamePad.ThumbSticks.Left.X, 0, gamePad.ThumbSticks.Left.Y); } var digging = Digging; if (gamePad.IsConnected && gamePad.Triggers.Right > 0.5f) { digging = true; } if (gamePad.IsConnected && gamePad.Triggers.Left > 0.5f && GamePadState.Triggers.Left < 0.5f) { var item = Game.Client.Inventory.Hotbar[Game.Client.HotbarSelection]; Game.Client.QueuePacket(new PlayerBlockPlacementPacket( Game.HighlightedBlock.X, (sbyte)Game.HighlightedBlock.Y, Game.HighlightedBlock.Z, Game.HighlightedBlockFace, item.ID, item.Count, item.Metadata)); } if (gamePad.IsConnected && gamePad.ThumbSticks.Right.Length() != 0) { var look = -(gamePad.ThumbSticks.Right * 8) * (float)(gameTime.ElapsedGameTime.TotalSeconds * 30); Game.Client.Yaw -= look.X; Game.Client.Pitch -= look.Y; Game.Client.Yaw %= 360; Game.Client.Pitch = MathHelper.Clamp(Game.Client.Pitch, -89.9f, 89.9f); } if (digging) { if (Game.StartDigging == DateTime.MinValue) // Would like to start digging a block { var target = Game.HighlightedBlock; if (target != -Coordinates3D.One) { BeginDigging(target); } } else // Currently digging a block { var target = Game.HighlightedBlock; if (target == -Coordinates3D.One) // Cancel { Game.StartDigging = DateTime.MinValue; Game.EndDigging = DateTime.MaxValue; Game.TargetBlock = -Coordinates3D.One; } else if (target != Game.TargetBlock) // Change target { BeginDigging(target); } } } else { Game.StartDigging = DateTime.MinValue; Game.EndDigging = DateTime.MaxValue; Game.TargetBlock = -Coordinates3D.One; } if (delta != XVector3.Zero) { var lookAt = XVector3.Transform(-delta, Matrix.CreateRotationY(MathHelper.ToRadians(-(Game.Client.Yaw - 180) + 180))); lookAt.X *= (float)(gameTime.ElapsedGameTime.TotalSeconds * 4.3717); lookAt.Z *= (float)(gameTime.ElapsedGameTime.TotalSeconds * 4.3717); var bobbing = Game.Bobbing; Game.Bobbing += Math.Max(Math.Abs(lookAt.X), Math.Abs(lookAt.Z)); Game.Client.Velocity = new TVector3(lookAt.X, Game.Client.Velocity.Y, lookAt.Z); if ((int)bobbing % 2 == 0 && (int)Game.Bobbing % 2 != 0) { PlayFootstep(); } } else { Game.Client.Velocity *= new TVector3(0, 1, 0); } if (Game.EndDigging != DateTime.MaxValue) { if (NextAnimation < DateTime.UtcNow) { NextAnimation = DateTime.UtcNow.AddSeconds(0.25); Game.Client.QueuePacket(new AnimationPacket(Game.Client.EntityID, AnimationPacket.PlayerAnimation.SwingArm)); } if (DateTime.UtcNow > Game.EndDigging && Game.HighlightedBlock == Game.TargetBlock) { Game.Client.QueuePacket(new PlayerDiggingPacket( PlayerDiggingPacket.Action.StopDigging, Game.TargetBlock.X, (sbyte)Game.TargetBlock.Y, Game.TargetBlock.Z, Game.HighlightedBlockFace)); Game.EndDigging = DateTime.MaxValue; } } GamePadState = gamePad; }