Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/main/java/org/spongepowered/api/entity/Entity.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@
import org.spongepowered.api.world.World;
import org.spongepowered.api.world.schematic.Schematic;
import org.spongepowered.api.world.server.ServerWorld;
import org.spongepowered.math.imaginary.Quaterniond;
import org.spongepowered.math.vector.Vector3d;
import org.spongepowered.math.vector.Vector3i;

import java.util.Collection;
import java.util.EnumSet;
Expand Down Expand Up @@ -157,6 +159,16 @@ default boolean setLocationSafely(ServerLocation location) {
*/
void setRotation(Vector3d rotation);

/**
* Gets the unit vector representing the direction of this entity.
*
* @return The direction
*/
default Vector3d getDirection() {
final Vector3d rotation = this.getRotation();
return Quaterniond.fromAxesAnglesDeg(rotation.getX(), -rotation.getY(), rotation.getZ()).getDirection();
}

/**
* Moves the entity to the specified location and sets the rotation.
*
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/org/spongepowered/api/entity/living/Living.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.spongepowered.api.entity.Entity;
import org.spongepowered.api.projectile.source.EntityProjectileSource;
import org.spongepowered.api.scoreboard.TeamMember;
import org.spongepowered.math.imaginary.Quaterniond;
import org.spongepowered.math.vector.Vector3d;

import java.util.Optional;
Expand Down Expand Up @@ -91,4 +92,14 @@ default Optional<Value.Immutable<Double>> lastDamageReceived() {
* @param targetPos Position to target
*/
void lookAt(Vector3d targetPos);

/**
* Converts the {@link Living}'s head rotation into a quaternion direction unit vector.
*
* @return The direction of the head
*/
default Vector3d getHeadDirection() {
final Vector3d headRotation = this.headRotation().get();
return Quaterniond.fromAxesAnglesDeg(headRotation.getX(), -headRotation.getY(), headRotation.getZ()).getDirection();
}
}