 # Solved Rotating Particles?

Discussion in 'Plugin Development' started by Leviticalpixel10, Nov 15, 2016.

Not open for further replies.
1. Offline

### Leviticalpixel10

Hi! I've been playing around with particles, and made one particle effect and was like how can I get it to turn with the player? I've been toying around for hours, adding the yaw and to the y and such but I can't figure it out. Can someone explain to be how to rotate these particles? Here's my original code that works:
Code:
```public static void doublecircle(Player player, EnumParticle particle) {
double t = 0;
double r = 1;

public void run() {
Location loc = player.getLocation();
double x = r * Math.sin(t);
double y = -r * Math.sin(t) + 1;
double z = r * Math.cos(t);
Location xyz = new Location(loc.getWorld(), loc.getX() + x, loc.getY() + y, loc.getZ() + z);
double x2 = r * Math.sin(t);
double y2 = r * Math.sin(t) + 1;
double z2 = r * Math.cos(t);
Location xyz2 = new Location(loc.getWorld(), loc.getX() + x2, loc.getY() + y2, loc.getZ() + z2);
CraftPlayer p = (CraftPlayer) player;
PacketPlayOutWorldParticles packet = new PacketPlayOutWorldParticles(particle, false,
(float) (xyz.getX()), (float) (xyz.getY()), (float) (xyz.getZ()), (float) 0, (float) 0,
(float) 0, (float) 0, 1, 0);
p.getHandle().playerConnection.sendPacket(packet);
PacketPlayOutWorldParticles packet2 = new PacketPlayOutWorldParticles(particle, false,
(float) (xyz2.getX()), (float) (xyz2.getY()), (float) (xyz2.getZ()), (float) 0, (float) 0,
(float) 0, (float) 0, 1, 0);
p.getHandle().playerConnection.sendPacket(packet2);
t = t + Math.PI / 16;
}

}, 0, (long) 1);
}```

2. Offline

#2
3. Offline

### Leviticalpixel10

Yeah, that's what I meant @Zombie_Striker lemme change that real quick
I also didn't mean a helix. I've mastered those though. Here's what I'm dealing with:  as you can see the particles don't rotate with the player

4. Offline

### Zombie_Striker

@Leviticalpixel10
To make the particles rotate with the player's head, do the following:
1. get the player's "direction". Convert the XZ direction to radians. Use THIS to do that (Read #3, but do it in reverse. What you want is to get the "Yaw")
2. After getting the player's direction in radians, add it to "t". (e.g. Math.sin(t+ Yaw ) ).
3. If you want to include pitch, you will need to include a new axis to offset all the particles by. This is very complex, so I will only say how to do it if you actually intended on including the pitch.

#4
5. Offline

### Leviticalpixel10

@Zombie_Striker sorry to bother you but how do you get the yaw from the xz? or do you just add the xz to it

6. Offline

### Zombie_Striker

Code:
```double pitch = ((player_location.getPitch() + 90) * Math.PI) / 180;
double yaw = ((player_location.getYaw() + 90) * Math.PI) / 180;
//Ignore this ^

x = Math.sin(pitch) * Math.cos(yaw);
Z = Math.sin(pitch) * Math.sin(yaw);
Y = Math.cos(pitch);```
This is the code from the link.(I decided to change the Z and Y, because the thread chose to use "real" XYZ values instead of MC values.) What we want to do is, by having the X and Z, get the Yaw (we are going to ignore the pitch for simplicity.)

Because we are going to ignore the pitch, we can assume the pitch is at eye level (90*). Since sin(90) is equal to 1, we can remove these values so the lines become:
Code:
```x =Math.cos(yaw);
Z =Math.sin(yaw);```
Now, we can use inverse Sin or Cos to get the "yaw". Since the inverse of both X and Z give the same yaw, I am just going to use inverse sin.
Code:
`double yaw = Math.acos(x);`
Now that we have the yaw, all you need to do is add the yaw to "t" to rotate the particles. Again, it will look like:

#6
7. Offline

### Leviticalpixel10

I'm still kinda confused :/ @Zombie_Striker does this look right? It's still not working if it is.
Code:
```public static void doublecircle(Player player, EnumParticle particle) {
double t = 0;
double r = 1;

public void run() {
Location loc = player.getLocation();
Vector vec = loc.getDirection();
double yaw = Math.acos(vec.getX());
double x = r * Math.cos(t + yaw);
double y = -r * Math.cos(t + yaw) + 1;
double z = r * Math.sin(t + yaw);
Location xyz = new Location(loc.getWorld(), loc.getX() + x, loc.getY() + y, loc.getZ() + z);
double x2 = r * Math.sin(t + yaw);
double y2 = r * Math.sin(t + yaw) + 1;
double z2 = r * Math.cos(t + yaw);
Location xyz2 = new Location(loc.getWorld(), loc.getX() + x2, loc.getY() + y2, loc.getZ() + z2);
CraftPlayer p = (CraftPlayer) player;
PacketPlayOutWorldParticles packet = new PacketPlayOutWorldParticles(particle, false,
(float) (xyz.getX()), (float) (xyz.getY()), (float) (xyz.getZ()), (float) 0, (float) 0,
(float) 0, (float) 0, 1, 0);
p.getHandle().playerConnection.sendPacket(packet);
PacketPlayOutWorldParticles packet2 = new PacketPlayOutWorldParticles(particle, false,
(float) (xyz2.getX()), (float) (xyz2.getY()), (float) (xyz2.getZ()), (float) 0, (float) 0,
(float) 0, (float) 0, 1, 0);
p.getHandle().playerConnection.sendPacket(packet2);
t = t + Math.PI / 16;
}

}, 0, (long) ```

8. Offline

### mythbusterma

@Leviticalpixel10

You're missing code there, and what do you mean "not working?" Also, that's because r is just set to 1. How do you expect it to work?

#8
9. Offline

### Leviticalpixel10

Nevermind I got it. Thanks for the help! @Zombie_Striker

Last edited: Nov 16, 2016