# Solved How to calculate armorstand arm tip location

Discussion in 'Plugin Development' started by TerZer, Jan 4, 2019.

Thread Status:
Not open for further replies.
1. Offline

### TerZer

I need to get armorstands arm tip location, I guess it is possible with some kind of calculation with
Code:
`#.getRightArmPose()`
I have this code:

Code:
```@EventHandler
public void onAttack(EntityDamageByEntityEvent event) {
if(event.getDamager() instanceof Player && event.getEntity() instanceof ArmorStand) {

event.setCancelled(true);
//Get Shoulder Location
ArmorStand as = (ArmorStand) event.getEntity();
Location asl = as.getLocation();
asl.setYaw(asl.getYaw() + 90f);
Vector dir = asl.getDirection();
asl.setX(asl.getX() + 5f/16f * dir.getX());
asl.setY(asl.getY() + 22f/16f);
asl.setZ(asl.getZ() + 5f/16f * dir.getZ());
asl.getWorld().spawnParticle(Particle.VILLAGER_HAPPY, asl, 1);
//Get Hand Location
EulerAngle ea = as.getRightArmPose();
Vector armDir = getDirection(ea.getY(), ea.getX());
asl.setX(asl.getX() + 10f/16f * armDir.getX());
asl.setY(asl.getY() + 10f/16f * armDir.getY());
asl.setZ(asl.getZ() + 10f/16f * armDir.getZ());

asl.getWorld().spawnParticle(Particle.VILLAGER_HAPPY, asl, 1);

}
}

public Vector getDirection(Double yaw, Double pitch) {

yaw = Math.toRadians(Math.toDegrees(yaw) + 90);
pitch = Math.toRadians(Math.toDegrees(pitch) + 180);

Double x = Math.sin(pitch) * Math.cos(yaw);
Double y = Math.sin(pitch) * Math.sin(yaw);
Double z = Math.cos(pitch);

return new Vector(x, z, y);
}```
But that code did not worked, it somehow messes up with Y. If I replace x with y, then it rotates normally, but rotation sometimes goes in opposite direction. Basically direction of starting rotation then messes up.

#1
2. Offline

### TerZer

Now I have this code, but it still not good working code

Code:
```    public static Location getArmTip(ArmorStand as) {
// Gets shoulder location
Location asl = as.getLocation();
asl.setYaw(asl.getYaw() + 90f);
Vector dir = asl.getDirection();
asl.setX(asl.getX() + 5f / 16f * dir.getX());
asl.setY(asl.getY() + 22f / 16f);
asl.setZ(asl.getZ() + 5f / 16f * dir.getZ());
//asl.getWorld().spawnParticle(Particle.VILLAGER_HAPPY, asl, 1);
// Get Hand Location

EulerAngle ea = as.getRightArmPose();
Vector armDir = getDirection(ea.getY(), ea.getX(), asl.getDirection());
asl.setX(asl.getX() + 10f / 16f * armDir.getX());
asl.setY(asl.getY() + 10f / 16f * armDir.getY());
asl.setZ(asl.getZ() + 10f / 16f * armDir.getZ());

asl.getWorld().spawnParticle(Particle.VILLAGER_HAPPY, asl, 1);
return asl;
}

public static Vector getDirection(Double yaw, Double pitch, Vector look) {

yaw = Math.toRadians(Math.toDegrees(yaw) + 90);
pitch = Math.toRadians(Math.toDegrees(pitch) + 180);

Double x = Math.sin(pitch) * Math.cos(yaw);
Double y = Math.sin(pitch) * Math.sin(yaw);
Double z = Math.cos(pitch);

if (look.getX() > 0)
z = -z;
if (look.getX() > 0)
y = -y;
return new Vector(z, x, y);
}```

#2
3. Offline

### TerZer

The right code by one of spigot community member:

Code:
```    public static Location getArmTip(ArmorStand as) {
// Gets shoulder location
Location asl = as.getLocation().clone();
asl.setYaw(asl.getYaw() + 90f);
Vector dir = asl.getDirection();
asl.setX(asl.getX() + 5f / 16f * dir.getX());
asl.setY(asl.getY() + 22f / 16f);
asl.setZ(asl.getZ() + 5f / 16f * dir.getZ());
asl.getWorld().spawnParticle(Particle.VILLAGER_HAPPY, asl, 1);
// Get Hand Location

EulerAngle ea = as.getRightArmPose();
Vector armDir = getDirection(ea.getY(), ea.getX(), -ea.getZ());
armDir = rotateAroundAxisY(armDir, Math.toRadians(asl.getYaw()-90f));
asl.setX(asl.getX() + 10f / 16f * armDir.getX());
asl.setY(asl.getY() + 10f / 16f * armDir.getY());
asl.setZ(asl.getZ() + 10f / 16f * armDir.getZ());

asl.getWorld().spawnParticle(Particle.VILLAGER_HAPPY, asl, 1);
return asl;
}

public static Vector getDirection(Double yaw, Double pitch, Double roll) {
Vector v = new Vector(0, -1, 0);
v = rotateAroundAxisX(v, pitch);
v = rotateAroundAxisY(v, yaw);
v = rotateAroundAxisZ(v, roll);
return v;
}

private static Vector rotateAroundAxisX(Vector v, double angle) {
double y, z, cos, sin;
cos = Math.cos(angle);
sin = Math.sin(angle);
y = v.getY() * cos - v.getZ() * sin;
z = v.getY() * sin + v.getZ() * cos;
return v.setY(y).setZ(z);
}

private static Vector rotateAroundAxisY(Vector v, double angle) {
angle = -angle;
double x, z, cos, sin;
cos = Math.cos(angle);
sin = Math.sin(angle);
x = v.getX() * cos + v.getZ() * sin;
z = v.getX() * -sin + v.getZ() * cos;
return v.setX(x).setZ(z);
}

private static Vector rotateAroundAxisZ(Vector v, double angle) {
double x, y, cos, sin;
cos = Math.cos(angle);
sin = Math.sin(angle);
x = v.getX() * cos - v.getY() * sin;
y = v.getX() * sin + v.getY() * cos;
return v.setX(x).setY(y);
}```

#3
Thread Status:
Not open for further replies.