private void UpdateItemInHand(KeyboardState state) { if (state.IsKeyDown(Keys.I)) { inHand = new SampleCube(Vector3.Zero); } if (inHand != null) { Matrix matrix = Matrix.CreateRotationX(-target.Y)*Matrix.CreateRotationY(-target.X) * Matrix.CreateTranslation(new Vector3(Position.X, Position.Y + 2, Position.Z)); inHand.Position = Vector3.Transform(new Vector3(0.25f, -0.18f - clickingCount / 170f, -0.5f - clickingCount / 40f), matrix); inHand.RotationY = -target.X; inHand.RotationX = -target.Y - clickingCount / 70f; inHand.Scale = 0.2f; } }
/// <summary> /// Funkcja sprawdzająca, który klocek został kliknięty. /// </summary> /// <param name="x"></param> /// <param name="y"></param> /// <param name="objects"></param> /// <param name="graphics"></param> private Object3D CheckClickedModel(Object3D[] objects) { Vector3 nearsource = new Vector3(game.GraphicsDevice.Viewport.Width / 2, game.GraphicsDevice.Viewport.Height / 2, 0f); Vector3 farsource = new Vector3(game.GraphicsDevice.Viewport.Width / 2, game.GraphicsDevice.Viewport.Height / 2, 1f); Vector3 nearPoint = game.GraphicsDevice.Viewport.Unproject(nearsource, fppCamera.Projection, fppCamera.View,Matrix.CreateTranslation(0, 0, 0)); Vector3 farPoint = game.GraphicsDevice.Viewport.Unproject(farsource, fppCamera.Projection, fppCamera.View, Matrix.CreateTranslation(0, 0, 0)); Vector3 direction = farPoint - nearPoint; direction.Normalize(); Ray pickRay = new Ray(nearPoint, direction); float selectedDistance = 2.5f; int selectedIndex = -1; for (int i = 0; i < objects.Length; i++) { if (objects[i].Exists) { float? result = pickRay.Intersects(objects[i].BBox); if (result.HasValue) { if (result.Value < selectedDistance) { selectedIndex = i; selectedDistance = result.Value; } } } } if (selectedIndex > -1) { return objects[selectedIndex]; } return null; }