//public Vector3 GetDirection() //{ // double pitch = Pitch.ToRadians(); // double yaw = Yaw.ToRadians(); // double y = -Math.Sin(pitch); // double xz = Math.Cos(pitch); // double x = -xz * Math.Sin(yaw); // double z = xz * Math.Cos(yaw); // return new Vector3((float)x, (float)y, (float)z); //} public Vector3 GetDirection() { Vector3 vector = new Vector3(); double pitch = Pitch.ToRadians(); double yaw = Yaw.ToRadians(); vector.X = (float)(-Math.Sin(yaw) * Math.Cos(pitch)); vector.Y = (float)-Math.Sin(pitch); vector.Z = (float)(Math.Cos(yaw) * Math.Cos(pitch)); return(vector); }
/// <summary> /// Applies left/right/forward movement based on the current entity's yaw /// </summary> /// <param name="strafMovement"></param> /// <param name="forwardMovement"></param> /// <param name="friction"></param> protected void ApplyStrafingToVelocity(double strafMovement, double forwardMovement, double friction) { double distance = Math.Sqrt(strafMovement * strafMovement + forwardMovement * forwardMovement); if (distance < 0.01) { return; } if (distance < 1.0) { distance = 1.0; } distance = friction / distance; strafMovement *= distance; forwardMovement *= distance; // Based on current yaw and xMovement and zMovement, determine the velocity along X / Z axis double yawSin = Math.Sin(Yaw.ToRadians()); double yawCos = Math.Cos(Yaw.ToRadians()); this.Velocity.X += strafMovement * yawCos - forwardMovement * yawSin; this.Velocity.Z += forwardMovement * yawCos + strafMovement * yawSin; }