Sets the #getYaw() yaw and #getPitch() pitch to point in the direction of the vector. @param vector the direction vector @return the same location
(@NotNull Vector vector)
| 315 | * @return the same location |
| 316 | */ |
| 317 | @NotNull |
| 318 | public Location setDirection(@NotNull Vector vector) { |
| 319 | /* |
| 320 | * Sin = Opp / Hyp |
| 321 | * Cos = Adj / Hyp |
| 322 | * Tan = Opp / Adj |
| 323 | * |
| 324 | * x = -Opp |
| 325 | * z = Adj |
| 326 | */ |
| 327 | final double _2PI = 2 * Math.PI; |
| 328 | final double x = vector.getX(); |
| 329 | final double z = vector.getZ(); |
| 330 | |
| 331 | if (x == 0 && z == 0) { |
| 332 | pitch = vector.getY() > 0 ? -90 : 90; |
| 333 | return this; |
| 334 | } |
| 335 | |
| 336 | double theta = Math.atan2(-x, z); |
| 337 | yaw = (float) Math.toDegrees((theta + _2PI) % _2PI); |
| 338 | |
| 339 | double x2 = NumberConversions.square(x); |
| 340 | double z2 = NumberConversions.square(z); |
| 341 | double xz = Math.sqrt(x2 + z2); |
| 342 | pitch = (float) Math.toDegrees(Math.atan(-vector.getY() / xz)); |
| 343 | |
| 344 | return this; |
| 345 | } |
| 346 | |
| 347 | /** |
| 348 | * Adds the location by another. |