# How to make spawned fake player look always on the player

Discussion in 'Plugin Development' started by mouz_, Apr 22, 2016.

Not open for further replies.
1. Offline

### Forge_User_93961935

Hi, I am spawning a fake player near the player. How to make this fake player looking always in the direction of player's location?

2. Offline

### mine-care

Set the yaw and pitch of the fake player to match the one of the actual player. Unless you mean you want the fake player to look at the actual player where you need to do some math :S

#2
3. Offline

### mcdorli

Some is a correct term for it.
Code:
```dx = playerX - fakePlayerX
dz = playerZ - fakePlayerZ
yaw = atan2(dz, dx) //Use Math.atan2, and you need to enter the arguments in ZX order.
```
The pitch isnt much more complicated.
Code:
```dx = playerX - fakePlayerX
dy = playerY - fakePlayerY
pitch = atan2(dy, dx)
```

#3
4. Offline

### ChipDev

So that is what atan2 is used for! Awesome!

#4
5. Offline

### Gonmarte

#5
ChipDev likes this.
6. Offline

### mcdorli

atan2 is the same as atan, but it handles the side problems for you, e.g. atan returns the same for (60;50) and (60;-50).

I thought about posting a resource on how to make shapes with particles, but I'm not sure if it's ontopic or not.

#6
7. Offline

### ChipDev

Please do. I got everything but a sphere xD (Im good with sine, cos, etc.)

#7
8. Offline

#8
9. Offline

#9
10. Offline

### Forge_User_93961935

@mcdorli: Fake player is looking in complete different direction. Code:
Code:
```        double dx = p.getLocation().getX() - loc.getX();
double dz = p.getLocation().getZ() - loc.getZ();
double yaw = Math.atan2(dz, dx);

double dx2 = p.getLocation().getX() - loc.getX();
double dy2 = p.getLocation().getY() - loc.getY();
double pitch = Math.atan2(dy2, dx2);

wrapper.setYaw((float) yaw);
wrapper.setPitch((float) pitch);```

11. Offline

### mcdorli

Hm, what does setPitch and setYaw want? Degrees or radians?

#11
12. Offline

### Forge_User_93961935

I don't know, maybe degrees.

13. Offline

### mcdorli

What class is wrapper from?

#13
14. Offline

15. Offline

#15
16. Offline

### LucasEmanuel

All you have to do is a vector subtraction between the two locations. No need with atan or similar.

EDIT:
Ah, I see. Things have changed since i worked with Bukkit last. My bad.

#16
17. Offline

### Forge_User_93961935

@mcdorli Where should I use Math.toDegrees?

18. Offline

### mcdorli

Guess where you get the angle...

#18
19. Offline

### Forge_User_93961935

@mcdorli I don't know where and how, if you explain it to me how you did it in the first post it would be much easier.

20. Offline

### I Al Istannen

@mcdorli @mouz_

I haven't tested the following nor am I sure it works. It is merly a suggestion.
You need the yaw and pitch, right?
You could convert both Locations to vectors with Location#toVector(). Then you subtract the Vector of the fake player's location from the player location. Now you have a vector that points from the fake player to the player. You create a new Location Object somewhere (maybe player location, doesn't matter) and use the Location#setDirection(Vector) and pass the vector from above. You now have a Location Object pointing from the fake player to the player. Finally you can call Location#getYaw() and Location.getPitch() to get what you want.

Correct this please if it is wrong.

Last edited: Apr 25, 2016
#20
21. Offline

### mythbusterma

@I Al Istannen

Now why would you go jump through hoops like that rather than just do it directly, like he is doing?

#21
22. Offline

### I Al Istannen

@mythbusterma
There is no special reason apart from that you don't need to know what you are doing. Which is a bad thing in the long run surprisingly. It can be a quick temporary solution though until you fully understand or somebody explained to you how the direct way works. It shouldn't really be the final way to do it, but it is quickly implemented and you can do some other stuff in the meantime. It looks like mcdorli was doing the explanation part, but he is offline.

I will edit my post to reflect what I have said, thanks for pointing that out. I can also delete it fully, if you believe that that would be the better alternative.

#22
mythbusterma likes this.
23. Offline

### mythbusterma

@I Al Istannen

That is fair enough, but just a little bit of Math.toDegrees is not something to get your knickers in a bunch over.

#23
I Al Istannen likes this.
24. Offline

### mcdorli

The angle you get from math.atan is in radians, you need to convert it to degrees with math.todegrees.

#24
25. Offline

### Forge_User_93961935

Fake player is looking somewhere in the sky now.

This is some of my wrapper code:
Code:
```        double dx = p.getLocation().getX() - loc.getX();
double dz = p.getLocation().getZ() - loc.getZ();
double yaw = Math.toDegrees(Math.atan2(dz, dx));

double dx2 = p.getLocation().getX() - loc.getX();
double dy2 = p.getLocation().getY() - loc.getY();
double pitch = Math.toDegrees(Math.atan2(dy2, dx2));

wrapper.setPosition(loc.toVector());

wrapper.setYaw((float) yaw);
wrapper.setPitch((float) pitch);```
I tried with Math.toRadians, but it doesn't change anything then.

EDIT: There is something weird about it, fake player's body is in a different direction than his head.

Last edited: Apr 26, 2016
26. Offline

Refresh.

27. Offline

Refresh.

28. Offline

### mcdorli

You need to subtract the players position from the location

#28
29. Offline

### Forge_User_93961935

What is player position? When I will substract it, what to do next and where to put it?

30. Offline

### mcdorli

When you calculate the dx, dz, dx2 and dy2, you subtract the locations x/y/z from the player's x/y/z, reverse it.

#30