2

[SOLVED] Need help with some maths stuff

Toma1O6 •2/26/19 2:52 pm history
•
310
•
2/27/2019 9:18 am
Hello, I'm working on entity which will travel between 2 locations. I'm having problems calculating the angle properly, so the entity never reaches the position it should.
I have 4 coordinates (x and z of start and end destination), I just don't know, how to get angle between them. Someone knows how?

EDIT: I managed to solve this, I'll post solution here in case someone is having the same issue.
Entity Rotation Math:
private void updateHeading(int x, int z, BlockPos endPos)
{
  int fx = x - endPos.getX();
  int fz = z - endPos.getZ();
  float rot = (float) (MathHelper.atan2(fz, fx) * (180D / Math.PI)) - 90f;
  PUBGMCUtil.updateEntityRotation(this, endPos);
  rotationYaw += 180f;
}


Helper Functions:
public static float getAngleBetween2Points(Entity entityToRotate, BlockPos targetPos)
{
  float angle = (float) (MathHelper.atan2(entityToRotate.posZ - targetPos.getZ(), entityToRotate.posX -   targetPos.getX()) * (180D / Math.PI)) - 90f;
  return angle;
}

public static float updateRotation(float prevRotation, float additionalRotation)
{
  float f = MathHelper.wrapDegrees(additionalRotation - prevRotation);
  return prevRotation + f;
}

public static void updateEntityRotation(Entity entity, BlockPos targetPos)
{
  entity.rotationYaw = updateRotation(entity.rotationYaw, getAngleBetween2Points(entity, targetPos));
}
Posted by
Toma1O6
Level 43 : Master Modder
20

  Have something to say?

Join • Sign in

2

HoboMaggot
• 02/27/2019 4:04 am
• Level 56 : Grandmaster Blob
Wait do you mean you are continually tping the entity before you reach the spot?
If so, you should do the Bearings subject in maths (or sin/tan/cos)

If not, please elaborate
1
Toma1O6
• 02/27/2019 9:18 am
• Level 43 : Master Modder
No, I set the entity rotation when it's spawned and then it's moving based on it's look vector until it reaches destination.
I have just solved it, Minecraft has some weird stuff going on with base rotations.
2

Welcome