Jump to content

[1.8] [SOLVED] Spawning Particles Behind the Player


saxon564

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.

 

https://github.com/saxon564/Flatulence/blob/master/src/main/java/me/saxon564/flatulence/events/FlatulenceEventHandler.java

Link to comment
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

 

 

Link to comment
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?

Link to comment
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.

Link to comment
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

Link to comment
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?

Link to comment
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;

Link to comment
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

Link to comment
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.

Link to comment
Share on other sites

Guest
This topic is now closed to further replies.

Announcements



×
×
  • Create New...

Important Information

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