Jump to content

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


ryanshah

Recommended Posts

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
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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..

Link to comment
Share on other sites

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



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • They were already updated, and just to double check I even did a cleanup and fresh update from that same page. I'm quite sure drivers are not the problem here. 
    • i tried downloading the drivers but it says no AMD graphics hardware has been detected    
    • Update your AMD/ATI drivers - get the drivers from their website - do not update via system  
    • As the title says i keep on crashing on forge 1.20.1 even without any mods downloaded, i have the latest drivers (nvidia) and vanilla minecraft works perfectly fine for me logs: https://pastebin.com/5UR01yG9
    • Hello everyone, I'm making this post to seek help for my modded block, It's a special block called FrozenBlock supposed to take the place of an old block, then after a set amount of ticks, it's supposed to revert its Block State, Entity, data... to the old block like this :  The problem I have is that the system breaks when handling multi blocks (I tried some fix but none of them worked) :  The bug I have identified is that the function "setOldBlockFields" in the item's "setFrozenBlock" function gets called once for the 1st block of multiblock getting frozen (as it should), but gets called a second time BEFORE creating the first FrozenBlock with the data of the 1st block, hence giving the same data to the two FrozenBlock :   Old Block Fields set BlockState : Block{minecraft:black_bed}[facing=east,occupied=false,part=head] BlockEntity : net.minecraft.world.level.block.entity.BedBlockEntity@73681674 BlockEntityData : id:"minecraft:bed",x:3,y:-60,z:-6} Old Block Fields set BlockState : Block{minecraft:black_bed}[facing=east,occupied=false,part=foot] BlockEntity : net.minecraft.world.level.block.entity.BedBlockEntity@6d1aa3da BlockEntityData : {id:"minecraft:bed",x:2,y:-60,z:-6} Frozen Block Entity set BlockState : Block{minecraft:black_bed}[facing=east,occupied=false,part=foot] BlockPos{x=3, y=-60, z=-6} BlockEntity : net.minecraft.world.level.block.entity.BedBlockEntity@6d1aa3da BlockEntityData : {id:"minecraft:bed",x:2,y:-60,z:-6} Frozen Block Entity set BlockState : Block{minecraft:black_bed}[facing=east,occupied=false,part=foot] BlockPos{x=2, y=-60, z=-6} BlockEntity : net.minecraft.world.level.block.entity.BedBlockEntity@6d1aa3da BlockEntityData : {id:"minecraft:bed",x:2,y:-60,z:-6} here is the code inside my custom "freeze" item :    @Override     public @NotNull InteractionResult useOn(@NotNull UseOnContext pContext) {         if (!pContext.getLevel().isClientSide() && pContext.getHand() == InteractionHand.MAIN_HAND) {             BlockPos blockPos = pContext.getClickedPos();             BlockPos secondBlockPos = getMultiblockPos(blockPos, pContext.getLevel().getBlockState(blockPos));             if (secondBlockPos != null) {                 createFrozenBlock(pContext, secondBlockPos);             }             createFrozenBlock(pContext, blockPos);             return InteractionResult.SUCCESS;         }         return super.useOn(pContext);     }     public static void createFrozenBlock(UseOnContext pContext, BlockPos blockPos) {         BlockState oldState = pContext.getLevel().getBlockState(blockPos);         BlockEntity oldBlockEntity = oldState.hasBlockEntity() ? pContext.getLevel().getBlockEntity(blockPos) : null;         CompoundTag oldBlockEntityData = oldState.hasBlockEntity() ? oldBlockEntity.serializeNBT() : null;         if (oldBlockEntity != null) {             pContext.getLevel().removeBlockEntity(blockPos);         }         BlockState FrozenBlock = setFrozenBlock(oldState, oldBlockEntity, oldBlockEntityData);         pContext.getLevel().setBlockAndUpdate(blockPos, FrozenBlock);     }     public static BlockState setFrozenBlock(BlockState blockState, @Nullable BlockEntity blockEntity, @Nullable CompoundTag blockEntityData) {         BlockState FrozenBlock = BlockRegister.FROZEN_BLOCK.get().defaultBlockState();         ((FrozenBlock) FrozenBlock.getBlock()).setOldBlockFields(blockState, blockEntity, blockEntityData);         return FrozenBlock;     }  
  • Topics

×
×
  • Create New...

Important Information

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