# [1.8] [SOLVED] Spawning Particles Behind the Player

## Recommended Posts

What I need to do is spawn particles behind the player. I know I have seen topics on this, but I can never seem to find them when I start looking for them. So my question is, what is the best way to spawn particles behind the player?

Here is the code I have right now for spawning particles, and it does it right on the player instead of behind them.

##### Share on other sites

hi

```		double posX = (double)player.posX + (double)(this.rand.nextFloat() * player.width * 2.0F) - (double)player.width;
double posY = player.posY + 0.8D + (double)(this.rand.nextFloat() * (player.height / 4));
double posZ = (double)player.posZ + (double)(this.rand.nextFloat() * player.width * 2.0F) - (double)player.width;
```

these [posX, posY, posZ] tell your particle where to spawn.

currently you are choosing values centred around the player pos.

eg posX = player's Xpos, plus a random value from 0 to 2* players width, minus the players width

i.e

player's Xpos plus a random value from minus width to plus width

You need to choose values centred behind the player, and also probably have a start velocity pointing away from the player.

player.rotationYaw will tell you which way the player is facing.

You can convert an angle (yaw) to a deltaX, deltaZ using these formulae

```        float deltaZ = MathHelper.cos((float)(-yaw * 180.0/Math.PI - Math.PI));
float deltaX = MathHelper.sin((float)(-yaw * 180.0/Math.PI - Math.PI));
```

-TGG

##### Share on other sites

hi

```		double posX = (double)player.posX + (double)(this.rand.nextFloat() * player.width * 2.0F) - (double)player.width;
double posY = player.posY + 0.8D + (double)(this.rand.nextFloat() * (player.height / 4));
double posZ = (double)player.posZ + (double)(this.rand.nextFloat() * player.width * 2.0F) - (double)player.width;
```

these [posX, posY, posZ] tell your particle where to spawn.

currently you are choosing values centred around the player pos.

eg posX = player's Xpos, plus a random value from 0 to 2* players width, minus the players width

i.e

player's Xpos plus a random value from minus width to plus width

As I said, I was spawning them on the player trying to figure out how to spawn behind them and that is where I left the code.

You need to choose values centred behind the player, and also probably have a start velocity pointing away from the player.

player.rotationYaw will tell you which way the player is facing.

You can convert an angle (yaw) to a deltaX, deltaZ using these formulae

```        float deltaZ = MathHelper.cos((float)(-yaw * 180.0/Math.PI - Math.PI));
float deltaX = MathHelper.sin((float)(-yaw * 180.0/Math.PI - Math.PI));
```

-TGG

Ok. My biggest issue is knowing how to convey the deltaX and deltaZ to player relational positions. Do I simply have to add them to the value of posX and posZ?

##### Share on other sites

You should just use the player's look vector and reverse it and multiply by distance you want behind. Add the resulting vector to the player position and spawn particles there.

Check out my tutorials here: http://jabelarminecraft.blogspot.com/

##### Share on other sites

You should just use the player's look vector and reverse it and multiply by distance you want behind. Add the resulting vector to the player position and spawn particles there.

So you're saying to do

```float deltaZ = MathHelper.cos((float)(-yaw * 180.0/Math.PI - Math.PI)*2.0F);
float deltaX = MathHelper.sin((float)(-yaw * 180.0/Math.PI - Math.PI)*2.0F);
```

to spawn the particles at make the spawn 2 blocks behind the player once put into the x and y? If so, that doesnt make any sence as to how that would work.

##### Share on other sites

You should just use the player's look vector and reverse it and multiply by distance you want behind. Add the resulting vector to the player position and spawn particles there.

So you're saying to do

```float deltaZ = MathHelper.cos((float)(-yaw * 180.0/Math.PI - Math.PI)*2.0F);
float deltaX = MathHelper.sin((float)(-yaw * 180.0/Math.PI - Math.PI)*2.0F);
```

to spawn the particles at make the spawn 2 blocks behind the player once put into the x and y? If so, that doesnt make any sence as to how that would work.

No, he's saying get the player's look vector and multiply each coordinate by -2, then add those values to the player's position / height values and spawn your particles there. They will appear exactly two blocks behind the player.

```Vec3 vec = player.getLookVec();
double dx = player.posX - (vec.xCoord * 2);
double dy = player.posY + player.getEyeHeight(); // you probably don't actually want to subtract the vec.yCoord, unless the position depends on the player's pitch
double dz = player.posZ - (vec.zCoord * 2);
spawnParticle(dx, dy, dz); // not actual method signature
```

##### Share on other sites

Oh, ok, looking at that it makes perfect sence. This is something Ive never done before, so seeing the code has really helpped me understand what you are saying. Thank You guys for the help. I think this will work when Im at my computer and can try it.

EDIT: He mentioned reversing the look vector, would that be adding/subtracting 180 or multiplying by -1?

##### Share on other sites

EDIT: He mentioned reversing the look vector, would that be adding/subtracting 180 or multiplying by -1?

Multiplying each vector coordinate by -1 is identical to subtracting the vector coordinate:

```Vec3 vec = player.getLookVec();
double dx = player.posX + (vec.xCoord * -1);
// is the same as:
double dx = player.posX - vec.xCoord;
```

##### Share on other sites

Oh, derp I should have figured that! I think I knkw what I need to do now.

##### Share on other sites

The look vector won't work because it will be behind the player's head, not behind the player's body.  They are often not pointing the same way.

That's why you need to use player.rotationYaw.

The math in the player.getLook() is the same as what you want to do, except you use rotationYaw instead of head's yaw, and can ignore pitch.

-TGG

##### Share on other sites

Excellent point by TGG there

##### Share on other sites

Im not using the look vectors y anyway. ive got everything working right now. Thanks for the help.

##### Share on other sites

Im not using the look vectors y anyway. ive got everything working right now. Thanks for the help.

His point is that there are too yaw values - one for the player's body, and one for the player's head; the look vector is based on the head yaw, so your particles will be spawning relative to the back of the player's head, rather than the back of their body.

Thus, if the player is looking a different direction than their body is facing, your particles will likely be in the wrong spot.

##### Share on other sites

oh, ok, I did need to take into account what he said, I was quickly reading it earlier, but after I read it again I made the adjustments.

##### Share on other sites

This topic is now closed to further replies.

• ### Recently Browsing

• No registered users viewing this page.
• ### Posts

• Sorry, but I would like to ask if anyone else is experiencing the problem: java.lang.ClassCastException: class net.minecraft.client.player.LocalPlayer cannot be cast to class net.minecraft.server.level.ServerPlayer (net.minecraft.client .player.LocalPlayer and net.minecraft.server.level.ServerPlayer are in module [email protected] of loader 'TRANSFORMER' @19a64eae) This problem appeared yesterday, while I was testing parts of my mod, which I had previously tested. I thought I might have messed up somewhere, so today I went back to a previous version, already tested and functional, and it turns out that I ran into the same problem. Someone knows what is due? I can't share the entire code, but I can give you the parts that cause the error, I hope you can help me. Thanks in advance.   DataUtil Class public static void setBooleanOnServerAndSync(Player p, String key, boolean value, boolean temporary) { p.getCapability(PlayerDataProvider.PLAYER_DATA, null).ifPresent(playerData -> { playerData.setBoolean(key, value, temporary); Messages.sendToPlayer(SyncPlayerDataPacket.toClient(key).setBoolean(value), (ServerPlayer) p); //It fails here if (value) { ResearchUtil.markResearchAsUpdated(p, key); } }); } Gui that calls the previous method public void tick() { this.timePassed++; if (this.timePassed > 1000) { this.minecraft.setScreen(null); assert minecraft.level != null; assert minecraft.player != null; DataUtil.setBooleanOnServerAndSync(Objects.requireNonNull(minecraft.level.getPlayerByUUID(minecraft.player.getUUID())),"eldritchDream", true,false); } else if (this.timePassed == 20) { Minecraft.getInstance().player.playSound(Registration.DREAM_ALIENIS.get(), 1, 1); } super.tick(); } Error Log (Website paste.ee)
• This is the code for my enchantment tags provider for data generation: public final class ModEnchantmentTagsProvider extends EnchantmentTagsProvider { public ModEnchantmentTagsProvider(PackOutput packOutput, CompletableFuture<Provider> lookupProvider) { super(packOutput, lookupProvider); } @Override protected void addTags(Provider provider) { tag(EnchantmentTags.ARMOR_EXCLUSIVE) .add(ArmorEnchantments.FREEZING_PROTECTION) .add(ArmorEnchantments.LIGHTNING_PROTECTION) .add(ArmorEnchantments.MAGIC_PROTECTION) .add(ArmorEnchantments.SONIC_PROTECTION); tag(ModEnchantmentTags.XP_GAIN_EXCLUSIVE) .add(Enchantments.MENDING) .add(ArmorEnchantments.EXPLORATION); } @Override public String getName() { return "Unique Magic Enchantment Tags"; } }   So what am I doing here is: 1) Adding my custom protection enchantments to be ARMOR_EXCLUSIVE so they don't clash with existing protection enchantments 2) Adding tag that makes my custom EXPLORATION enchantment to be exclusive with MENDING enchantment This is then registered in ModDataGenerators: dataGen.addProvider(event.includeServer(), new ModEnchantmentTagsProvider(packOutput, lookupProvider)); When datagen is run, I end up with this error: Caused by: java.util.concurrent.CompletionException: java.lang.IllegalArgumentException: Couldn't define tag minecraft:exclusive_set/armor as it is missing following references: uniquemagic:freezing_protection,uniquemagic:lightning_protection,uniquemagic:magic_protection,uniquemagic:sonic_protection Enchantments have been migrated to 1.21 and they work in game as they did in 1.20.6. I also get enchantment JSON files in src/generated/resources so that is working well too. However, I cannot make these tags to work no matter what I try. Is there something that I am missing?
• i dont know what is cousing this i am creating my own private modpack to play with  a friend but for some reason less then a minute after loading in a world the games freezes becouse my memory is 99% used (the memory i allocated for minecraft to use)   i keep searching in the log but i cant find the couse cen some pls help my find it   i play in minecraft version 1.20.1 and forge version 47.2.32 log: https://drive.google.com/file/d/1HlCl6R_20LwEsgk4Z9Y84IPBUUorxcA9/view?usp=sharing
• If you want to access Resource from Minecraft, use: ResourceLocation.withDefaultNamespace("potato"); or If you want to access Resource from your mod, use: ResourceLocation.fromNamespaceAndPath("your_mod_id", "your_key");

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

×

• #### Activity

×
• Create New...

## Important Information

By using this site, you agree to our Terms of Use.