# Is there a way to get the ForgeDirection a Vec3 is pointing?

## Recommended Posts

Hi everybody.  I am wondering if there is a easy way or method to get the ForgeDirection that a Vec3 (or any other type of 3D vector) is pointing?  I am trying to make a block that blends in to it's souroundings, if it makes any difference.  Thank you in advance.

##### Share on other sites

Hi

I don't know of a  vanilla function for this but it is easy enough to do yourself.

Find which of the three components [x, y, z] have the largest absolute magnitude.  The vector is pointing mostly in that direction.

eg [0.5, -0.6, 0.9] is pointing mostly in the positive z direction

[0, -0.7, 0.3] is pointing mostly in the negative y direction

-TGG

##### Share on other sites

Thanks, but I think I can make a exact way.  If you can get the ForgeDirection an Entity is looking, that would also work.

##### Share on other sites

This is what I'm going to try to use.  Fell free to use it yourself

Revised:

```public static ForgeDirection getForgeDirection(Vec3 vec) {

vec.normalize();

double pitchR = Math.atan(vec.yCoord / Math.sqrt(Math.pow(vec.xCoord, 2) + Math.pow(vec.zCoord, 2)));
double yawR = Math.atan2(-vec.zCoord, vec.xCoord);
double pi = Math.PI;

ForgeDirection dir = ForgeDirection.UNKNOWN;

System.out.println("Pitch: " + pitchR + ", Yaw: " + yawR);

if(yawR < -pi || yawR > pi)
System.err.println("Yaw is too big!!");

if(pitchR > -pi/4 && pitchR < pi/4){ // if the vector is mostly horizontal

if(yawR >= -pi/4 && yawR <= pi/4)
dir = ForgeDirection.EAST;
else if(yawR < -pi/4 && yawR >= -3*pi/4)
dir = ForgeDirection.SOUTH;
else if(yawR > pi/4 && yawR <= 3*pi/4)
dir = ForgeDirection.NORTH;
else if(yawR < -3*pi/4 || yawR > 3*pi/4)
dir = ForgeDirection.WEST;

} else if(pitchR >= pi/4)
dir = ForgeDirection.UP;
else
dir = ForgeDirection.DOWN;

return dir;
}

```

Edit:  I have a gitch where it sometimes thinks South is West, and I have no idea why.

Edit 2: I fixed it.

##### Share on other sites

Hi

Keen, that will work too.  Both of our methods give the same answer.  Yours is better if you want to know the exact yaw & pitch, although it has a small bug in it - what happens if vec.xCoord and vec.zCoord are both zero?

-TGG

## Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.

×   Pasted as rich text.   Restore formatting

Only 75 emoji are allowed.

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

### Announcements

×

• #### Activity

×
• Create New...