Jump to content

[1.8] [SOLVED] Offset a projectile in a certain direction


Recommended Posts

Posted

I have a a flamethrower I've created for my mod, and it works fine, the only problem is that when I use it, it's coming directly out of the player's face. I want to offset so that it appears to be emanating from the player's hand. I only want the the flame particles to be offset. Here's my code for generating flames particles:

@Override
public void onUpdate(EntityPower spell) {

	if (!spell.isInWater()) {

		World world = spell.worldObj;
		float distTraveled = getSpellVelocity() * spell.getTicksInAir();

		if (distTraveled >= 5) {

			spell.setDead();

		}

		boolean positionInvalid = false;

		awaylabel:

		for (float j = 0.0F; j < 1.0F; j += 0.05F) {

			for (int i = 0; i < 10; ++i) {

				BlockPos pos = new BlockPos(
						spell.prevPosX + (spell.motionX * j),
						spell.prevPosY + (spell.motionY * j),
						spell.prevPosZ + (spell.motionZ * j));

				Vec3 motionVec = new Vec3(spell.motionX, spell.motionY, spell.motionZ);
				motionVec = motionVec.normalize();

				if (UsefulMethods.getBlockAtPos(pos, world).getMaterial().isSolid()) {
					positionInvalid = true;
					break awaylabel;
				}

				world.spawnParticle(spell.castState < 4 ? EnumParticleTypes.FLAME : EnumParticleTypes.LAVA,
					spell.prevPosX + (spell.motionX * j) - world.rand.nextFloat() * 0.5F,
					spell.prevPosY + (spell.motionY * j) - world.rand.nextFloat() * 0.5F,
					spell.prevPosZ + (spell.motionZ * j) - world.rand.nextFloat() * 0.5F,
					0, 0, 0);

			}
		}


		if (spell.getTicksInGround() > 0 || positionInvalid) spell.setDead();

	} else spell.setDead();

}

 

This is called every tick on my flamethrowing projectile. How can I offset this to the right (relatively), so that it appears to be coming from their hand? Any assistance is appreciated.

 

For reference, the rest of my code can be found here:

https://github.com/Himself12794/Heroes-Mod/blob/6edb401796e93d54cbbf6f0a1e90b0a1e4a8324e/src/main/java/com/himself12794/heroesmod/power/Flames.java

With all due respect, sir: I do, what I do, the way I do it. ~ MacGyver

Posted

Just change the x,y and z coordinates into the required position.

 

So, for example, if you like to move the particles to the right, just increase/decrease the x/z coordinates in the spawnParticle(..) method:

world.spawnParticle(spell.castState < 4 ? EnumParticleTypes.FLAME : EnumParticleTypes.LAVA,
					3+spell.prevPosX + (spell.motionX * j) - world.rand.nextFloat() * 0.5F,
					spell.prevPosY + (spell.motionY * j) - world.rand.nextFloat() * 0.5F,
					spell.prevPosZ + (spell.motionZ * j) - world.rand.nextFloat() * 0.5F,
					0, 0, 0);

Posted

Just change the x,y and z coordinates into the required position.

 

So, for example, if you like to move the particles to the right, just increase/decrease the x/z coordinates in the spawnParticle(..) method:

world.spawnParticle(spell.castState < 4 ? EnumParticleTypes.FLAME : EnumParticleTypes.LAVA,
					3+spell.prevPosX + (spell.motionX * j) - world.rand.nextFloat() * 0.5F,
					spell.prevPosY + (spell.motionY * j) - world.rand.nextFloat() * 0.5F,
					spell.prevPosZ + (spell.motionZ * j) - world.rand.nextFloat() * 0.5F,
					0, 0, 0);

I tried that, but the offset is static, it always moves them in a specified direction, regardless of the orientation of the player. Perhaps I need to take into account the pitch and yaw?

With all due respect, sir: I do, what I do, the way I do it. ~ MacGyver

Posted

If you want to move it left/right with respect to its current heading, then yes, you need to account for yaw, and that involves lots of math (just look at EntityArrow or EntityThrowable constructors).

 

Alternatively, and I'm not sure how well this will work, you could try using this.getLookVec() - this should be defined for all entities, and hopefully will give you the current directing the spell projectile is heading.

 

However, given that you seem to be spawning a projectile for your flame thrower, surely the projectile was sent in the direction the player was initially facing, right? And you keep spawning new entities each tick, right?

 

Personally, I find an entity-based implementation to be somewhat unwieldy for something like a flamethrower - I prefer to simply spawn particles in the direction the player is facing each tick (while using the item), and check for entities within that area to damage / set on fire. Then you can use the player's look vector and current position to figure out where to spawn the particles and what values to give them for motion (e.g. in the same direction as the player is looking).

Posted

If you want to move it left/right with respect to its current heading, then yes, you need to account for yaw, and that involves lots of math (just look at EntityArrow or EntityThrowable constructors).

 

Alternatively, and I'm not sure how well this will work, you could try using this.getLookVec() - this should be defined for all entities, and hopefully will give you the current directing the spell projectile is heading.

 

However, given that you seem to be spawning a projectile for your flame thrower, surely the projectile was sent in the direction the player was initially facing, right? And you keep spawning new entities each tick, right?

 

Personally, I find an entity-based implementation to be somewhat unwieldy for something like a flamethrower - I prefer to simply spawn particles in the direction the player is facing each tick (while using the item), and check for entities within that area to damage / set on fire. Then you can use the player's look vector and current position to figure out where to spawn the particles and what values to give them for motion (e.g. in the same direction as the player is looking).

I have no problem getting its heading, I just use its normalized velocity vector. I don't want to change that vector, I just want to offset the location.

 

Also, my projectile only fires every 2 ticks. I had it as a projectile because I really wanted it to behave like one, i.e. have a modifiable velocity. I actually do have an instant variant, it just doesn't have particles.

 

I'll start looking through the constructors and see if I can determine the underlying logic.

With all due respect, sir: I do, what I do, the way I do it. ~ MacGyver

Posted

If you just want to move it down:

this.posY -= 1.0F; // change to whatever looks good

If you want to move it to one side or the other:

float f = 1.0F; // factor of how far you want to move it one way or the other, 1.0F being roughly equivalent to one block, maybe
Vec3 vec = this.getLookVec(); // better if you have the player's look vector
this.posX += vec.zCoord * f; // yes, I mean 'z' - you're moving it to the right; change the sign (i.e. negative) to move to the left
this.posZ += vec.xCoord * f;

Posted

If you just want to move it down:

this.posY -= 1.0F; // change to whatever looks good

If you want to move it to one side or the other:

float f = 1.0F; // factor of how far you want to move it one way or the other, 1.0F being roughly equivalent to one block, maybe
Vec3 vec = this.getLookVec(); // better if you have the player's look vector
this.posX += vec.zCoord * f; // yes, I mean 'z' - you're moving it to the right; change the sign (i.e. negative) to move to the left
this.posZ += vec.xCoord * f;

That still seems to offset it in a specific direction, regardless of the player's orientation.

I did some digging through EntityThrowable , and came up with something similar to this:

double offset = 0.65 * PowersEntity.get( throwerIn ).getSecondaryPower() == spell ? -1 : 1;
posX -= (double) (MathHelper.cos( this.rotationYaw / 180.0F * (float) Math.PI ) * offset);
posY -= 0.25;
posZ -= (double) (MathHelper.sin( this.rotationYaw / 180.0F * (float) Math.PI ) * offset);

I've already modified the above to test if it works for me. So far, it's the closest thing to what I'm trying to do. I'm going to look through the EntityArrow class and see if I can find a less generalized version, but I believe I'm on the right track.

 

Edit:

EntityArrow uses the exact same thing, so I guess I'm satisfied with this result. Mostly, I just don't want the flame particles to obstruct the entity's view. How it looks to others is purely aesthetics.

With all due respect, sir: I do, what I do, the way I do it. ~ MacGyver

Posted

If you have the look vector from the player, the code I wrote above works perfectly for offsetting something to the right or left of the direction the player is looking. As I said, I don't know how well it will work if you use the projectile entity for the look vector - apparently, not perfectly.

 

But, if you just want the particles to not obstruct the player's vision, all you have to do is move them down so they don't spawn right in front of your face. Keep in mind, however, the view from 3rd person (which is what other players will see, at least if you spawned particles on their clients as well) - if you move it down too far, or in some other direction, it may end up looking great from 1st person, but very bizarre from 3rd.

Posted

Your solution is the same as what I had originally come up with (except I didn't know x and y should be inverted). I don't understand why it doesn't seen to work, because it seems like it should work, but for some reason it is off. I am using the caster's look vector too, since the power entity is modeled after a throwable entity. I really need to redo the power entity. When I first made it, I knew nothing about coding and pretty much copy pasted it. Now I've learned so much, and probably should start scratch since I actually know how it works.

With all due respect, sir: I do, what I do, the way I do it. ~ MacGyver

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

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