Fix Entity#lookAt(Pos) not correctly looking at the position (#1058)

This commit is contained in:
MrGazdag 2022-05-12 23:35:46 +02:00 committed by GitHub
parent f774cc3b0f
commit 5f9d44433d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 15 deletions

View File

@ -336,10 +336,10 @@ public class Entity implements Viewable, Tickable, Schedulable, Snapshotable, Ev
* Changes the view of the entity so that it looks in a direction to the given position if
* it is different from the entity's current position.
*
* @param position the position to look at.
* @param point the point to look at.
*/
public void lookAt(@NotNull Pos position) {
final Pos newPosition = this.position.withLookAt(position);
public void lookAt(@NotNull Point point) {
final Pos newPosition = this.position.add(0, getEyeHeight(), 0).withLookAt(point);
setView(newPosition.yaw(), newPosition.pitch());
}
@ -350,8 +350,7 @@ public class Entity implements Viewable, Tickable, Schedulable, Snapshotable, Ev
*/
public void lookAt(@NotNull Entity entity) {
Check.argCondition(entity.instance != instance, "Entity can look at another entity that is within it's own instance");
double eyeHeightDifference = entity.getEyeHeight() - getEyeHeight();
lookAt(entity.position.withY(entity.position.y() + eyeHeightDifference));
lookAt(entity.position.withY(entity.position.y() + entity.getEyeHeight()));
}
/**

View File

@ -51,31 +51,31 @@ public class EntityViewDirectionIntegrationTest {
public void lookAtPos(Env env) {
var instance = env.createFlatInstance();
var entity = new Entity(EntityType.ZOMBIE);
double eyeHeight = entity.getEyeHeight(); // adding this to some position Y coordinates, to look horizontally
entity.setInstance(instance, new Pos(0, 40, 0)).join();
// look at itself, direction should not change
float prevYaw = entity.getPosition().yaw();
float prevPitch = entity.getPosition().pitch();
// make it look at its feet's position, it should look down
entity.lookAt(entity.getPosition());
assertEquals(prevYaw, entity.getPosition().yaw());
assertEquals(prevPitch, entity.getPosition().pitch());
// looking vertically, not checking yaw
assertEquals(90f, entity.getPosition().pitch());
entity.lookAt(new Pos(16, 40, 16));
entity.lookAt(new Pos(16, 40 + eyeHeight, 16));
assertEquals(-45f, entity.getPosition().yaw());
assertEquals(0f, entity.getPosition().pitch(), EPSILON);
entity.lookAt(new Pos(-16, 40, 56));
entity.lookAt(new Pos(-16, 40 + eyeHeight, 56));
assertEquals(15.94f, entity.getPosition().yaw(), EPSILON);
assertEquals(0f, entity.getPosition().pitch(), EPSILON);
entity.lookAt(new Pos(48, 36, 48));
assertEquals(-45f, entity.getPosition().yaw(), EPSILON);
assertEquals(4.76f, entity.getPosition().pitch(), EPSILON);
assertEquals(6.72f, entity.getPosition().pitch(), EPSILON);
entity.lookAt(new Pos(48, 36, -17));
assertEquals(-109.50f, entity.getPosition().yaw(), EPSILON);
// should have the same pitch as the previous position
assertEquals(4.76f, entity.getPosition().pitch(), EPSILON);
assertEquals(6.72f, entity.getPosition().pitch(), EPSILON);
entity.lookAt(new Pos(0, 87, 0));
// looking from below, not checking the yaw
@ -83,7 +83,7 @@ public class EntityViewDirectionIntegrationTest {
entity.lookAt(new Pos(-25, 42, 4));
assertEquals(80.90f, entity.getPosition().yaw(), EPSILON);
assertEquals(-4.57f, entity.getPosition().pitch(), EPSILON);
assertEquals(-0.78f, entity.getPosition().pitch(), EPSILON);
}
@Test