2
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.
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;
}
{
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));
}
{
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));
}
2
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
If so, you should do the Bearings subject in maths (or sin/tan/cos)
If not, please elaborate
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.
I have just solved it, Minecraft has some weird stuff going on with base rotations.
