/// <summary> /// Offset the position of the camera in coordinates relative to its current orientation /// </summary> /// <param name="x">Movement along the camera ground (left/right)</param> /// <param name="y">Movement along the camera axis (forward)</param> /// <param name="z">Height to move</param> public void Move(float x, float y, float z) { Vector3 offset = new Vector3(); Vector3 forward = new Vector3((float)Math.Sin((float)Orientation.X), 0, (float)Math.Cos((float)Orientation.X)); Vector3 right = new Vector3(-forward.Z, 0, forward.X); offset += x * right; offset += y * forward; offset.Y += z; offset.NormalizeFast(); offset = Vector3.Multiply(offset, MoveSpeed); Position += offset; }
public void Move(float x, float y) { Vector3 offset = new Vector3(); Vector3 right = new Vector3(1, 0, 0); Vector3 up = new Vector3(0, 1, 0); offset += x * right; offset += y * up; offset.NormalizeFast(); offset = Vector3.Multiply(offset, moveSpeed); position += offset; }
/// <summary> /// This function handles the movement of the camera based on OnKeyPress() method in ACWWindow.cs /// </summary> /// <param name="x">The x axis movement value</param> /// <param name="y">The y axis movement value</param> /// <param name="z">The z axis movement value</param> public void Move(float x, float y, float z) { Vector3 offset = new Vector3(); Vector3 forward = new Vector3((float)Math.Sin((float)Orientation.X), 0, (float)Math.Cos((float)Orientation.X)); // Forward and backward movement on the x and z axis Vector3 right = new Vector3(-forward.Z, 0, forward.X); // Right and left movement on the x and z axis // Actually moves the camera using the parameter values offset += x * right; offset += y * forward; offset.Y += z; offset.NormalizeFast(); offset = Vector3.Multiply(offset, MoveSpeed); // Sets the new position of the camera based on the above calculations Position += offset; }
public Shell(Vector3 position, Vector3 cannonFacing, Vector3 cannonVelocity, float startSpeed, object shellOwner) { int segments = 8; List<Vector3> vertices = new List<Vector3>(segments + 1); //float unitradius = (float)Math.Sqrt(8); float angleStep = (float)(2 * Math.PI / segments); for (int i = 0; i < segments; i++) { vertices.Add(new Vector3((float)Math.Cos(angleStep * i) * radius, (float)Math.Sin(angleStep * i) * radius, 0)); } vertices.Add(new Vector3(radius, 0, 0)); graphics = GraphicsAspect.Create(this, vertices, position, 1, Color.White, Color.Red); // вычисляем вектор полёта снаряда - сумма импульса выстрела и собственной скорости оружия cannonFacing.NormalizeFast(); Vector3 shootVelocity = cannonFacing * startSpeed + cannonVelocity; Vector2 shootDirection = shootVelocity.Xy; shootDirection.NormalizeFast(); //physics = new PhysicsAspect(this, position, shootVelocity.Xy, startSpeed); physics = PhysicsAspect.Create(this, position, shootDirection, shootVelocity.LengthFast); bounds = BoundSetAspect.Create(this, null); BoundsAspect bound = CircleBoundsAspect.Create(bounds, position.Xy, radius); bounds.AddBound(bound); // снаряд будет быстродвижущимся объектом, для него особый алгоритм определения столкновений bounds.SetAttribute(Strings.CollisionDetectionSpeedType, Strings.CollisionDetectionSpeedTypeFast); damage = DamageAspect.Create(this, 1); //physics = new PhysicsAspect(this, position, Vector2.Zero, 0); //timer = DestroyByTimerAspect.Create(this, new TimeSpan(0, 0, 0, 2, 500)); timer = DestroyByTimerAspect.Create(this, new TimeSpan(0, 0, 0, 1, 0)); this.shellOwner = shellOwner; this.name = "shell"; MessageDispatcher.RegisterHandler(typeof(SetPosition), bounds); MessageDispatcher.RegisterHandler(typeof(SetPosition), graphics); MessageDispatcher.RegisterHandler(typeof(DestroyChildrenOf), this); MessageDispatcher.RegisterHandler(typeof(Kill), this); messageHandler.Handlers.Add(typeof(Kill), HandleKill); }
/// <summary> /// Moves the camera in local space /// </summary> /// <param name="x">Distance to move along the screen's x axis</param> /// <param name="y">Distance to move along the axis of the camera</param> /// <param name="z">Distance to move along the screen's y axis</param> public void Move(float x, float y, float z) { /** When the camera moves, we don't want it to move relative to the world coordinates * (like the XYZ space its position is in), but instead relative to the camera's view. * Like the view angle, this requires a bit of trigonometry. */ Vector3 offset = new Vector3(); Vector3 forward = new Vector3((float)Math.Sin((float)Orientation.X), 0, (float)Math.Cos((float)Orientation.X)); Vector3 right = new Vector3(-forward.Z, 0, forward.X); offset += x * right; offset += y * forward; offset.Y += z; offset.NormalizeFast(); offset = Vector3.Multiply(offset, MoveSpeed); Position += offset; }
public bool IsOutOfBounds(Vector3 obj) { Vector3 normalized = new Vector3(obj.X, obj.Y, obj.Z); normalized.NormalizeFast(); float norm = obj.LengthFast; //Vector3 abs = new Vector3(Math.Abs(obj.X), Math.Abs(obj.Y), Math.Abs(obj.Z)); if (norm > _playerLimitRange)//- Math.Abs(normalized.Z*500))//+ normalized.X * _playerLimitEccentricityX + normalized.Z * _playerLimitEccentricityZ) return true; else return false; }
private void MoveDreadnaught() { // ORIENTATION if (InputManager.Mouse.RightClickdown) //Follow cursor position when right click is down or set new orientation if just right click { _savedPlayerCursorPosition = new Vector3(_playerCursor.Position.X, _playerCursor.Position.Y, _playerCursor.Position.Z); } _dreadnaught.AlignTo(_savedPlayerCursorPosition); // POSITION LIMIT CHECK bool commandsEnabled = true; Vector3 command = new Vector3(0, 0, 0); Vector3 repelForce = new Vector3(0, 0, 0); if (!IsOutOfBounds(_dreadnaught.Position)) //if dreadnaught is in bounds, enable controls (player can accelerate spaceship) { commandsEnabled = true; } else { //If out of bounds, disable controls and apply a small "force" towards the origin, until spaceship changes direction if (Vector3.Dot(_dreadnaught.Velocity, _dreadnaught.Position) > 0) { commandsEnabled = false; repelForce = new Vector3(-_dreadnaught.Position.X, -_dreadnaught.Position.Y, -_dreadnaught.Position.Z); repelForce.NormalizeFast(); }//If out of bounds but spaceship has changed direction, enable commands (to allow player to get back) else { commandsEnabled = true; } } // USER COMMANDS CATCH if (commandsEnabled) { if (InputManager.Keyboard.Sdown) if (InputManager.Keyboard.ShiftLeftdown) _camera.Move(_camera.Down, _camera.PositionSpeed * 100); else command.Z -= 1; if (InputManager.Keyboard.Wdown) if (InputManager.Keyboard.ShiftLeftdown) _camera.Move(_camera.Up, _camera.PositionSpeed * 100); else command.Z += 1; if (InputManager.Keyboard.Adown) if (InputManager.Keyboard.ShiftLeftdown) _camera.Move(_camera.Left, _camera.PositionSpeed * 100); else command.X += 1; if (InputManager.Keyboard.Ddown) if (InputManager.Keyboard.ShiftLeftdown) _camera.Move(_camera.Left, _camera.PositionSpeed * 100); else command.X -= 1; } // POSITION command.NormalizeFast(); command += repelForce; //apply repelforce (if any) Vector3 nextVelocity = _dreadnaught.Velocity + (command * _dreadnaught.MaxAcceleration * Game.DeltaTime); //SPEED LAW if (Math.Abs(nextVelocity.X) > _dreadnaught.MaxSpeed) nextVelocity.X = _dreadnaught.Velocity.X; //limit max speed on X if (Math.Abs(nextVelocity.Z) > _dreadnaught.MaxSpeed) nextVelocity.Z = _dreadnaught.Velocity.Z; //limit max speed on Z _dreadnaught.Velocity = nextVelocity; //new speed vector apply _dreadnaught.Position = _dreadnaught.Position + _dreadnaught.Velocity; //POSITION LAW }
/// <summary> /// Applies Gram-Schmitt Ortho-normalization to the given two input Vectro3 objects. /// </summary> /// <param name="vec1">The first Vector3 objects to be ortho-normalized</param> /// <param name="vec2">The secound Vector3 objects to be ortho-normalized</param> public static void OrthoNormalize(ref Vector3 vec1, ref Vector3 vec2) { vec1.NormalizeFast(); vec2 = Vector3.Subtract(vec2, ProjectAndCreate(vec2, vec1)); vec2.NormalizeFast(); }