# Solved Get location with offset based on head rotation

Discussion in 'Spigot Plugin Development' started by cheesyfreezy, Feb 18, 2020.

1. ### cheesyfreezy

Hey there,

What I am trying to do is spawn a particle effect in front of the player. To do this I will have to add an offset to the location of the player. I know I have to use the getDirection() method, but how do I actually add this offset to the location of the player based on their head rotation? I do not really understand vectors too well, so if anyone could explain that, I would appreciate it a lot!

Thank you!

#1
Last edited: Feb 18, 2020
2. ### Schottky

Code (Java):
final Location eyeLoc = player.getEyeLocation(); // the location of the player's eyes
final Vector direction = player.getLocation().getDirection(); // the direction the player is facing at
final Location offsetedDirection = eyeLoc.add(direction);// direction with offset

3. ### cheesyfreezy

I know that this works, but how do I add the offset to the location relative to the head direction?

Hey you can do it like this,
Code (Java):
Location playerPos = p.getLocation();
Vector playerDir = p.getLocation().getDirection();
float spawnDistance = x; // offset

5. ### Warren1001

his example literally tells you. the #add method literally adds the offset to the location. he gave you the code straight up lol

• Like x 1
6. ### cheesyfreezy

This adds the direction the player is looking at, but I want to add offset to the x and z, like this:

8. ### Warren1001

if you want to go about it a different way if the math is too much. add the player's direction, then swap the x and the z of the directional vector and figure out if you need to make one of them negative or not depending on the way they're facing. then multiple the vector by 2 and add it to the already added location

9. ### cheesyfreezy

Why do I have to swap the x and z? I do not understand why.
And why would I consider making one of them negative?

10. ### Warren1001

because of how vectors work. i dont remember the proper directions so dont take my word for this, but a vector consisting of 3 numbers, x, y, z,
positive x is south, negative x is north, positive z is east, negative z is west -- remember, i dont remember if these are actually the proper directions, u should check and make sure.
you swap the x and the z because thats how you make a 90 degree turn. then you'd possibly invert one of the x or z to change the direction if you needed to. i dont want to put in enough thought to think if you need to or not. just a 'just incase' kinda suggestion

11. ### Esophose

Somebody asked pretty much this same question in my plugin support discord last week. I wrote up this pastebin which contains an example of spawning a particle effect in front of a player. I should note that I used this method to apply the rotation to offset the vectors.
I'll give a little synapsis here:
Code (Java):
Location eyeLocation = player.getEyeLocation();

// how many blocks to spawn the particles in front of the player
double distanceFromEyes = 1.5;

// how far to offset left/right from the center eye location
double distanceFromEyeCenter = 0.25;

Vector leftEye = VectorUtils.rotateVector(new Vector(distanceFromEyes, 0, distanceFromEyeCenter), eyeLocation.getYaw(), eyeLocation.getPitch());
Vector rightEye = VectorUtils.rotateVector(new Vector(distanceFromEyes, 0, -distanceFromEyeCenter), eyeLocation.getYaw(), eyeLocation.getPitch());

Location right location = eyeLocation.clone().add(right eye);
// Spawn particles at leftLocation and rightLocation and they will
// appear in front of the player and offset to the left and right
This is the way that I thought of doing it when I was asked the question. I'm not saying it's the best way to do it, but it definitely works.

Here's a gif of the full effect from the pastebin. I didn't move the mouse around at all, but it does always stay in front of the player no matter their rotation.

Hope this helps

• Useful x 1
12. ### cheesyfreezy

The VectorUtils class helped and solved my problem. Thank you so much!
Don't think I got completely spoon-fed here, because I looked into the VectorUtils and your example code until I understood every part of it.