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)
| 307 | * @return the same location |
| 308 | */ |
| 309 | @NotNull |
| 310 | public Location setDirection(@NotNull Vector vector) { |
| 311 | /* |
| 312 | * Sin = Opp / Hyp |
| 313 | * Cos = Adj / Hyp |
| 314 | * Tan = Opp / Adj |
| 315 | * |
| 316 | * x = -Opp |
| 317 | * z = Adj |
| 318 | */ |
| 319 | final double _2PI = 2 * Math.PI; |
| 320 | final double x = vector.getX(); |
| 321 | final double z = vector.getZ(); |
| 322 | |
| 323 | if (x == 0 && z == 0) { |
| 324 | pitch = vector.getY() > 0 ? -90 : 90; |
| 325 | return this; |
| 326 | } |
| 327 | |
| 328 | double theta = Math.atan2(-x, z); |
| 329 | yaw = (float) Math.toDegrees((theta + _2PI) % _2PI); |
| 330 | |
| 331 | double x2 = NumberConversions.square(x); |
| 332 | double z2 = NumberConversions.square(z); |
| 333 | double xz = Math.sqrt(x2 + z2); |
| 334 | pitch = (float) Math.toDegrees(Math.atan(-vector.getY() / xz)); |
| 335 | |
| 336 | return this; |
| 337 | } |
| 338 | |
| 339 | /** |
| 340 | * Adds the location by another. |