Jump to content

Rendering ring of particles around entity that faces direction of entity travel


Recommended Posts

Posted (edited)

I currently spawn a ring of particles around my entity when it is spawned, however depending on how the player is facing to cast the entity the particle ring does not always appear in front of the player. I know this is something to do with the z position (vz), but I dont 100% understand how to set it in this case. For example in some directions it appears fine, but in some the ring is effectively flipped 90 degrees so all I can see is a line of particles instead of the ring.

@Override
  public void tick() {
  if (this.world.isRemote || (this.shootingEntity == null || !this.shootingEntity.removed) && this.world.isBlockLoaded(new BlockPos(this.getPosX(), this.getPosY(), this.getPosZ()))) {
    super.tick();
    ++this.ticksInAir;

    RayTraceResult raytraceresult = ProjectileHelper.func_234618_a_(this, entity -> entity.isAlive() && entity != this.shootingEntity);
    if (raytraceresult.getType() != RayTraceResult.Type.MISS && !net.minecraftforge.event.ForgeEventFactory.onProjectileImpact(this, raytraceresult)) {
      this.onImpact(raytraceresult);
    }

    Vector3d vec3d = this.getMotion();
    this.setPosition(getPosX() + vec3d.x, getPosY() + vec3d.y, getPosZ() + vec3d.z);
    //ProjectileHelper.rotateTowardsMovement(this, 0.2f);

    float f = this.getMotionFactor();

    double radius = 1.25D;
    double d0 = this.getPosX() - vec3d.x;
    double d1 = this.getPosY() - vec3d.y;
    double d2 = this.getPosZ() - vec3d.z;

    for(double angle = 0.0D; angle < 2 * Math.PI; angle += 4d / 180d * (2 * Math.PI)) {
      double vx = d0 + MathHelper.cos((float)angle) * radius;
      double vy = d1 + MathHelper.sin((float)angle) * radius;
      double vz = d2 + MathHelper.sin((float) angle) * radius;

      this.world.addParticle(ParticleTypes.SMOKE, vx, vy, vz, vec3d.x, vec3d.y, vec3d.z);
    }

    this.setMotion(vec3d.add(this.accelerationX, this.accelerationY, this.accelerationZ).scale(f));
    //this.setPosition(this.getPosX(), this.getPosY(), this.getPosZ());
  } else {
    this.remove();
  }
}

 

Edited by ryanshah
updated code
Posted

You are rotating around vector3.UP (that is, the axis that points vertically) and then doing something weird by adding the same value to Y and Z (this literally makes no sense) and what you want to do is rotate around a vector that points in the direction the player is facing.

 

https://stackoverflow.com/q/31225062

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Posted

I'm still not sure I totally understand. For reference I have updated the OP to contain the tick method so you can what posX etc. I am referencing in this question.

Posted (edited)

are you sure you understand the geometry in that code? adding sine to Z and cosine to X gives you a circle. but why are you adding anything to Y? that gives you a spiral (not a vertical one, also, but you didn't want any kind).

 

and are you sure you get the part about incrementing the angle? because that code would be much simpler if you did - you'd make a constant holding the number of particles and just divide 2pi by N.

Edited by MFMods
Posted
2 hours ago, ryanshah said:

double vx = d0 + MathHelper.cos((float)angle) * radius;

does not equal

Quote

double vx = d0*(d0*x + d1*y + d2*z)*(1d - MathHelper.cos(angle)) + x*MathHelper.cos(angle) + (-d2*y + d1*z)*MathHelper.sin(angle);

same goes for the other three values.

You need to rotate around the direction of travel to get the effect you want and right now you're rotating around a fixed vector.

2 hours ago, ryanshah said:

4d / 180d * (2 * Math.PI)

A constant, divided by a constant, multiplied by a constant times a constant.
Sounds like a constant to me. Specifically Math.PI/22.5d. Or you could just do an integer loop from 1 to 45 and multiply the loop index by that constant instead of doing a loop over a double.

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Posted
12 minutes ago, MFMods said:

are you sure you understand the geometry in that code? adding sine to Z and cosine to X gives you a circle. but why are you adding anything to Y? that gives you a spiral (not a vertical one, also, but you didn't want any kind).

 

and are you sure you get the part about incrementing the angle? because that code would be much simpler if you did - you'd make a constant holding the number of particles and just divide 2pi by N.

I am adding to Y instead of Z for the angle to have my ring appear upwards infront of the player, otherwise it just appears flat.

Posted (edited)
36 minutes ago, Draco18s said:

does not equal

same goes for the other three values.

You need to rotate around the direction of travel to get the effect you want and right now you're rotating around a fixed vector.

A constant, divided by a constant, multiplied by a constant times a constant.
Sounds like a constant to me. Specifically Math.PI/22.5d. Or you could just do an integer loop from 1 to 45 and multiply the loop index by that constant instead of doing a loop over a double.

I'll do some constant refactoring later. For now, I have the current code:

 

double radius = 1.25D;
double u = this.getPosX() - vec3d.x;
double v = this.getPosY() - vec3d.y;
double w = this.getPosZ() - vec3d.z;

for(double angle = 0.0D; angle < 2 * Math.PI; angle += 4d / 180d * (2 * Math.PI)) {
  double mod = (u * getPosX()) + (v * getPosY()) + (w * getPosZ());
  double vx = u * mod * (1d - MathHelper.cos((float)angle)) + getPosX() * MathHelper.cos((float)angle) + (-w * getPosY() + v*getPosZ()) * MathHelper.sin((float)angle);
  double vy = v * mod * (1d - MathHelper.cos((float)angle)) + getPosY() * MathHelper.cos((float)angle) + (w * getPosX() - u*getPosZ()) * MathHelper.sin((float)angle);
  double vz = w * mod * (1d - MathHelper.cos((float)angle)) + getPosZ() * MathHelper.cos((float)angle) + (-v * getPosX() + u*getPosY()) * MathHelper.sin((float)angle);

  this.world.addParticle(ParticleTypes.SMOKE, vx, vy, vz, vec3d.x, vec3d.y, vec3d.z);
}

 

But where would I include the radius of the ring?

Edited by ryanshah
updated code
Posted (edited)

vx, vy, and vz represent a unit vector. Multiply by the desired magnitude.

Edited by Draco18s

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Posted
3 hours ago, Draco18s said:

vx, vy, and vz represent a unit vector. Multiply by the desired magnitude.

Thanks for the help so far, I think I'm nearly there..

float radius = 2f;
// Get origins
double u = this.getPosX() - vec3d.x;
double v = this.getPosY() - vec3d.y;
double w = this.getPosZ() - vec3d.z;

Vector3d facing = new Vector3d(getPosX(), getPosY(), getPosZ());

for(double angle = 0.0D; angle < 2 * Math.PI; angle += 4d / 180d * (2 * Math.PI)) {
  double c = (u * facing.x) + (v * facing.y) + (w * facing.z); // constant
  double vx = u * c * (1d - MathHelper.cos((float)angle)) + facing.x * MathHelper.cos((float)angle) + (-w * facing.y + v*facing.z) * MathHelper.sin((float)angle);
  double vy = v * c * (1d - MathHelper.cos((float)angle)) + facing.y * MathHelper.cos((float)angle) + (w * facing.x - u*facing.z) * MathHelper.sin((float)angle);
  double vz = w * c * (1d - MathHelper.cos((float)angle)) + facing.z * MathHelper.cos((float)angle) + (-v * facing.x + u*facing.y) * MathHelper.sin((float)angle);
  this.world.addParticle(ParticleTypes.SMOKE, getPosX() + vx * radius, getPosY() + vy * radius, getPosZ() + vz * radius, vec3d.x, vec3d.y, vec3d.z);
}

So to my understanding, I first get the origin (0, 0, 0) and i want to do the transforms over the unit vector and then apply the vx,vy,vz offsets multiplied by the radius of the circle to the original position. However, nothing here is showing. I think I might have the wrong unit vector to use..

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.