# Calculating pitch and yaw of a projectile.

## Recommended Posts

I want to make a projectile, extended from EntityThrowable, although it wont be thrown but it will have the same mechanics. This projectile will only appear when the player is looking at a block.

I want this projectile to reach the center of the block in a straight line, so I don't want to have the player's pitch and yaw as the projectile's but rather I want the pitch and yaw that will make the projectile reach the center of the block, but I can't seem to figure out how I would calculate the pitch and yaw of the projectile, as I don't know a lot about rotations.

Can someone help?

##### Share on other sites

Look into Math.atan2(y, x) -- it will return an angle given a vector from origin.

Calculate the vector by the difference between player position and the position of the target looked at (use a ray-trace for getting that position).

A good article explaining many of the basic geometric concepts related to angles/positions:

##### Share on other sites

I've got it sorted out by doing so:

```double degree = 180 / Math.PI;
double dx = player.posX - (playermop.blockX + 0.5);
double dy = (player.posY + player.eyeHeight) - (playermop.blockY + 0.5);
double dz = player.posZ - (playermop.blockZ + 0.5);
double yaw = -((Math.atan2(dx, dz) * degree) + 180);
double pitch = (Math.atan2(dy, Math.sqrt(dx * dx + dz * dz)) * degree);```

I wanted it in degrees to compare it with player's rotationYaw and rotationPitch and it worked perfectly, it gives the exact yaw and pitch if the player is looking at the center of the block.

I just want to know if it is theoretically correct to do it like so...

## 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.

• ### Posts

• The version of what? I just imported the MDK into Eclipse. I haven't made any edits to the Java code in it or even any other files. I haven't even gotten to changing the modid from "examplemod" yet.
• Captain WebGenesis is a Cyber skilled hacker with special abilities and skills that enact penetration to help victims of Crypto Fraudulent activities who are faced with cyber challenges get back their lost funds. Simply file your complaint through the website (https://captainwebgenesis. com) and have all your crypto recovered back.
• okay i've managed to fix it. looks like i made the stupidest error known to man. i hadn't installed forge to the client, which i assumed only affected the launcher which wouldn't affect me because i used a third party one. evidently not. the more you know
• tried a fresh install of above and beyond with java 8 installed. got a popup that says "Error: Unable to access jarfile forge-1.16.5-36.2.8.jar". every good programmer knows getting a different error means you've made progress!

• ### Who's Online (See full list)

×

• #### Activity

×
• Create New...