Jump to content

Recommended Posts

Posted

Hello everyone,

I created a bullet, which can be shot using a rifle. Everything is working as it should. 

The problem is, that the bullet is not moving smoothly through the air. Very often it glitches back.

As I'm not sure how to fix this issue, I would appreciate any help.

 

EntityBullet:

Spoiler

public class EntityBullet extends Entity implements IEntityAdditionalSpawnData
{
	private static final AxisAlignedBB FLARE_AXIS_ALINGNED_BB = new AxisAlignedBB(0.0D, 0.0D, 0.0D, 3.0D/16.0D, 3.0D/16.0D, 3.0D/16.0D);
	private int lifeTime = -1;
	private Entity shooter;
	private String shooterName = "";
	
	public EntityBullet(World worldIn)
	{
		super(worldIn);
		this.setSize((float)(FLARE_AXIS_ALINGNED_BB.maxX - FLARE_AXIS_ALINGNED_BB.minX), (float)(FLARE_AXIS_ALINGNED_BB.maxY - FLARE_AXIS_ALINGNED_BB.minY));
	}
	
	public EntityBullet(World worldIn, double x, double y, double z)
	{
		this(worldIn);
		this.setPosition(x, y, z);
	}
	
	public EntityBullet(World worldIn, double x, double y, double z, double xVel, double yVel, double zVel)
	{
		this(worldIn, x, y, z);
		
		if(!world.isRemote)
		{
			this.addVelocity(xVel, yVel, zVel);
		}
	}
	
	public EntityBullet(World worldIn, double x, double y, double z, double velocity, float yaw, float pitch)
	{
		this(worldIn, x, y, z, -MathHelper.sin((float)Math.toRadians(Utils.modulo(yaw, 360.0F))) * MathHelper.cos((float)Math.toRadians(Utils.modulo(pitch, 360.0F))) * velocity, -MathHelper.sin((float)Math.toRadians(Utils.modulo(pitch, 360.0F))) * velocity, MathHelper.cos((float)Math.toRadians(Utils.modulo(yaw, 360.0F))) * MathHelper.cos((float)Math.toRadians(Utils.modulo(pitch, 360.0F))) * velocity);
		this.setRotation((float)Utils.modulo(yaw, 360.0F), (float)Utils.modulo(pitch, 360.0F));
	}
	
	public EntityBullet(World worldIn, double x, double y, double z, double velocity, int lifeTime, Entity entity)
	{
		this(worldIn, x, y, z, velocity, entity.rotationYaw, entity.rotationPitch);
		this.lifeTime = lifeTime;
		this.shooter = entity;
		this.noClip = false;
		this.shooterName = entity.getName();
	}
	
	@Override
	public void onUpdate() 
	{		
		if(this.ticksExisted == lifeTime && !this.isDead)
		{
			this.setDead();
			return;
		}
		
		if(!this.world.isRemote)
		{
			if(!this.isDead)
			{
				this.motionY += -0.004905;
				this.velocityChanged = true;
			}
		}
		
		if(!this.world.isRemote)
		{
			this.checkForEntityCollision();
			this.checkForBlockCollision();
		}
		
		this.prevPosX = this.posX;
		this.prevPosY = this.posY;
		this.prevPosZ = this.posZ;
		
		this.move(MoverType.SELF, this.motionX, this.motionY, this.motionZ);
		this.setRotation((float)Math.toDegrees(Utils.getATanWithOffset(this.motionZ, -this.motionX)), (float)Math.toDegrees(Math.asin(-this.motionY / Utils.calcVel(this.motionX, this.motionY, this.motionZ))));
	}
	
	public void checkForEntityCollision() 
	{
		if(!this.isDead)
		{
			List<Entity> list = this.world.getEntitiesWithinAABBExcludingEntity(this, this.getEntityBoundingBox().expand(this.motionX, this.motionY, this.motionZ));
			
			if(list != null)
			{
				Entity nearest = null; 
				
				for(Entity e : list)
				{
					if(!e.equals(this.shooter) || this.ticksExisted > 3)
					{
						if(nearest == null)
						{
							nearest = e;
						}
						else if(Utils.getEntityDist(this, e) < Utils.getEntityDist(this, nearest))
						{
							nearest = e;
						}
						
						e.attackEntityFrom(DamageSource.causeIndirectDamage(this, (EntityLivingBase)this.shooter), 10);
						this.setDead();
						return;
					}
				}
			}
		}
	}
	
	public void checkForBlockCollision() 
	{
		if(!this.isDead)
		{
			List<AxisAlignedBB> list = this.world.getCollisionBoxes(this, this.getEntityBoundingBox().expand(this.motionX, this.motionY, this.motionZ));
			
			if(list != null)
			{
				for(AxisAlignedBB e : list)
				{
					this.setDead();
					return;
				}
			}
		}
	}
	
	@Override
	public boolean canBePushed() 
	{
		return false;
	}
	
	@Override
	public void onKillCommand() 
	{
		if (!world.isRemote && !this.isDead)
		{
			this.setDead();
		}
	}
	
	@Override
	public boolean isInRangeToRenderDist(double distance) 
	{
		if(distance <= 200.0D)
		{
			return true;
		}
		return false;
	}
	
	@Override
	public boolean isInRangeToRender3d(double x, double y, double z) 
	{
		double x1 = this.posX-x;
		double y1 = this.posY-y;
		double z1 = this.posZ-z;
		
		return isInRangeToRenderDist(Math.sqrt(x1*x1+y1*y1+z1*z1));
	}
	
	@Override
	protected void entityInit() 
	{
		
	}

	@Override
	protected void readEntityFromNBT(NBTTagCompound compound)
	{		
		if(compound.hasKey("OwnerName"))
		{
			this.shooterName = compound.getString("OwnerName");
			this.shooter = Utils.getShooter(shooterName, this);
		}
		
		if(compound.hasKey("LifeTime"))
		{
			this.lifeTime = compound.getInteger("LifeTime");
		}
	}
	
	@Override
	protected void writeEntityToNBT(NBTTagCompound compound)
	{
		if(shooter != null)
		{
			this.shooterName = shooter.getName();
		}
		
		compound.setTag("OwnerName", new NBTTagString(this.shooterName));
		compound.setTag("LifeTime", new NBTTagInt(this.lifeTime));
	}

	@Override
	public void writeSpawnData(ByteBuf buffer) 
	{
		if(shooter != null)
		{
			this.shooterName = shooter.getName();
		}
		
		NBTTagCompound nbt = new NBTTagCompound();
		nbt.setString("OwnerName", this.shooterName);
		nbt.setInteger("LifeTime", this.lifeTime);
		ByteBufUtils.writeTag(buffer, nbt);
	}

	@Override
	public void readSpawnData(ByteBuf additionalData) 
	{
		NBTTagCompound nbt = ByteBufUtils.readTag(additionalData);
		
		if(nbt.hasKey("OwnerName"))
		{
			this.shooterName = nbt.getString("OwnerName");
			this.shooter = Utils.getShooter(shooterName, this);
		}
		
		if(nbt.hasKey("LifeTime"))
		{
			this.lifeTime = nbt.getInteger("LifeTime");
		}
	}
}

 

 

RenderBullet:

Spoiler

@SideOnly(Side.CLIENT)
public class RenderBullet extends RenderEntity
{
	private ModelBase model;
	
	public RenderBullet(RenderManager renderManagerIn) 
	{
		super(renderManagerIn);
		this.model = new ModelBullet();
	}
	
	@SideOnly(Side.CLIENT)
	@SubscribeEvent
	public void doRender(Entity entity, double x, double y, double z, float entityYaw, float partialTicks) 
	{
		if(entity.world.isRemote)
		{
			if(entity.isEntityAlive() && entity instanceof EntityBullet)
			{
				EntityPlayer player = Minecraft.getMinecraft().player;
				
				GlStateManager.pushMatrix();
				GlStateManager.translate(Utils.getPlayerXOffset(entity, player, partialTicks), Utils.getPlayerYOffset(entity, player, partialTicks), Utils.getPlayerZOffset(entity, player, partialTicks));

				this.bindTexture(this.getEntityTexture(entity));
				
				this.applyRotations(entity, this.model, partialTicks);
				
				this.model.render(entity, 0.0F, 0.0F, entity.ticksExisted + partialTicks, entity.rotationYaw, entity.rotationPitch, 0.0625F);
				GlStateManager.popMatrix();
			}
		}
	}
	
	public void applyRotations(Entity e, ModelBase m, float partialTicks)
	{
		if(e instanceof EntityBullet && m instanceof ModelBullet)
    	{
			EntityBullet entity = (EntityBullet)e;
    		ModelBullet model = (ModelBullet)m;
    		
    		model.setRotateAngle(model.bullet, (float)Math.toRadians(Utils.interpolateModulo((float)Utils.modulo(entity.rotationPitch, 360.0F), (float)Utils.modulo(entity.prevRotationPitch, 360.0F), 360.0F, partialTicks)), (float)Math.toRadians(Utils.interpolateModulo((float)Utils.modulo(entity.rotationYaw, 360.0F), (float)Utils.modulo(entity.prevRotationYaw, 360.0F), 360.0F, partialTicks)), 0.0F);
    	}
	}
	
	@Override
	protected ResourceLocation getEntityTexture(Entity entity) 
	{
		return new ResourceLocation(Reference.MODID, "textures/entity/bullet.png");
	}
}

 

 

ModelBullet:

 

Spoiler

public class ModelBullet extends ModelBase
{
	public ModelRenderer root;
	public ModelRenderer bullet;

    public ModelBullet() 
    {
        this.textureWidth = 16;
        this.textureHeight = 16;
        
        root = new ModelRenderer(this);
		root.setRotationPoint(0.0F, -1.5F, 0.0F);

		bullet = new ModelRenderer(this);
		bullet.setRotationPoint(0.0F, 0.0F, 0.0F);
		root.addChild(bullet);
		bullet.cubeList.add(new ModelBox(bullet, 0, 0, -0.5F, -0.5F, -1.0F, 1, 1, 2, 0.0F, true));
    }

    @Override
    public void render(Entity entityIn, float limbSwing, float limbSwingAmount, float ageInTicks, float netHeadYaw, float headPitch, float scale) 
    { 		
    	GlStateManager.rotate(180.0F, 180.0F, 0.0F, 1.0F);
    	
        this.root.render(scale);
    }

    public void setRotateAngle(ModelRenderer modelRenderer, float x, float y, float z) 
    {
        modelRenderer.rotateAngleX = x;
        modelRenderer.rotateAngleY = y;
        modelRenderer.rotateAngleZ = z;
    }
}

 

 

Utils:

Spoiler

public class Utils 
{
	public static final double getPlayerXOffset(Entity eR, Entity eC, float partialTick)
	{
		return Utils.interpolate((float)eR.posX, (float)eR.prevPosX, (float)partialTick) - Utils.interpolate((float)eC.posX, (float)eC.prevPosX, (float)partialTick);
	}
	
	public static final double getPlayerYOffset(Entity eR, Entity eC, float partialTick)
	{
		return Utils.interpolate((float)eR.posY, (float)eR.prevPosY, (float)partialTick) - Utils.interpolate((float)eC.posY, (float)eC.prevPosY, (float)partialTick);
	}
	
	public static final double getPlayerZOffset(Entity eR, Entity eC, float partialTick)
	{
		return Utils.interpolate((float)eR.posZ, (float)eR.prevPosZ, (float)partialTick) - Utils.interpolate((float)eC.posZ, (float)eC.prevPosZ, (float)partialTick);
	}
}

 

 

Posted (edited)

Extending ArrowEntity fixes some of this.

More info here and here.

Edited by Cadiboo
  • Like 1

About Me

Spoiler

My Discord - Cadiboo#8887

My WebsiteCadiboo.github.io

My ModsCadiboo.github.io/projects

My TutorialsCadiboo.github.io/tutorials

Versions below 1.14.4 are no longer supported on this forum. Use the latest version to receive support.

When asking support remember to include all relevant log files (logs are found in .minecraft/logs/), code if applicable and screenshots if possible.

Only download mods from trusted sites like CurseForge (minecraft.curseforge.com). A list of bad sites can be found here, with more information available at stopmodreposts.org

Edit your own signature at www.minecraftforge.net/forum/settings/signature/ (Make sure to check its compatibility with the Dark Theme)

Posted

Add velocity also need to be done on client side to provide smooth movement.

Some tips:

Spoiler

Modder Support:

Spoiler

1. Do not follow tutorials on YouTube, especially TechnoVision (previously called Loremaster) and HarryTalks, due to their promotion of bad practice and usage of outdated code.

2. Always post your code.

3. Never copy and paste code. You won't learn anything from doing that.

4. 

Quote

Programming via Eclipse's hotfixes will get you nowhere

5. Learn to use your IDE, especially the debugger.

6.

Quote

The "picture that's worth 1000 words" only works if there's an obvious problem or a freehand red circle around it.

Support & Bug Reports:

Spoiler

1. Read the EAQ before asking for help. Remember to provide the appropriate log(s).

2. Versions below 1.11 are no longer supported due to their age. Update to a modern version of Minecraft to receive support.

 

 

Posted (edited)

Thank you very much for your replies. 

I tried your recommendations and found, that adding the velocity only on the server and using the update frequency, that vanilla uses for EntityArrow (20) works the best.

 

EntityBullet:

Spoiler

public class EntityBullet extends Entity implements IEntityAdditionalSpawnData
{
	private static final AxisAlignedBB FLARE_AXIS_ALINGNED_BB = new AxisAlignedBB(0.0D, 0.0D, 0.0D, 3.0D/16.0D, 3.0D/16.0D, 3.0D/16.0D);
	private int lifeTime = -1;
	private Entity shooter;
	private String shooterName = "";
	
	public EntityBullet(World worldIn)
	{
		super(worldIn);
		this.setSize((float)(FLARE_AXIS_ALINGNED_BB.maxX - FLARE_AXIS_ALINGNED_BB.minX), (float)(FLARE_AXIS_ALINGNED_BB.maxY - FLARE_AXIS_ALINGNED_BB.minY));
	}
	
	public EntityBullet(World worldIn, double x, double y, double z)
	{
		this(worldIn);
		this.setPosition(x, y, z);
	}
	
	public EntityBullet(World worldIn, double x, double y, double z, double xVel, double yVel, double zVel)
	{
		this(worldIn, x, y, z);
		
		if(!world.isRemote)
		{
			this.addVelocity(xVel, yVel, zVel);
		}
	}
	
	public EntityBullet(World worldIn, double x, double y, double z, double velocity, float yaw, float pitch)
	{
		this(worldIn, x, y, z, -MathHelper.sin((float)Math.toRadians(Utils.modulo(yaw, 360.0F))) * MathHelper.cos((float)Math.toRadians(Utils.modulo(pitch, 360.0F))) * velocity, -MathHelper.sin((float)Math.toRadians(Utils.modulo(pitch, 360.0F))) * velocity, MathHelper.cos((float)Math.toRadians(Utils.modulo(yaw, 360.0F))) * MathHelper.cos((float)Math.toRadians(Utils.modulo(pitch, 360.0F))) * velocity);
		this.setRotation((float)Utils.modulo(yaw, 360.0F), (float)Utils.modulo(pitch, 360.0F));
	}
	
	public EntityBullet(World worldIn, double x, double y, double z, double velocity, int lifeTime, Entity entity)
	{
		this(worldIn, x, y, z, velocity, entity.rotationYaw, entity.rotationPitch);
		this.lifeTime = lifeTime;
		this.shooter = entity;
		this.noClip = false;
		this.shooterName = entity.getName();
	}
	
	@Override
	public void onUpdate() 
	{		
		if(this.ticksExisted == lifeTime && !this.isDead)
		{
			this.setDead();
			return;
		}
		
		if(!this.world.isRemote)
		{
			if(!this.isDead)
			{
				this.motionY += -0.004905;
				this.velocityChanged = true;
			}
		}
		
		if(!this.world.isRemote)
		{
			this.checkForEntityCollision();
			this.checkForBlockCollision();
		}
		
		this.prevPosX = this.posX;
		this.prevPosY = this.posY;
		this.prevPosZ = this.posZ;
		
		this.move(MoverType.SELF, this.motionX, this.motionY, this.motionZ);
		this.setRotation((float)Math.toDegrees(Utils.getATanWithOffset(this.motionZ, -this.motionX)), (float)Math.toDegrees(Math.asin(-this.motionY / Utils.calcVel(this.motionX, this.motionY, this.motionZ))));
	}
	
	public void checkForEntityCollision() 
	{
		if(!this.isDead)
		{
			List<Entity> list = this.world.getEntitiesWithinAABBExcludingEntity(this, this.getEntityBoundingBox().expand(this.motionX, this.motionY, this.motionZ));
			
			if(list != null)
			{
				Entity nearest = null; 
				
				for(Entity e : list)
				{
					if(!e.equals(this.shooter) || this.ticksExisted > 3)
					{
						if(nearest == null)
						{
							nearest = e;
						}
						else if(Utils.getEntityDist(this, e) < Utils.getEntityDist(this, nearest))
						{
							nearest = e;
						}
						
						e.attackEntityFrom(DamageSource.causeIndirectDamage(this, (EntityLivingBase)this.shooter), 10);
						this.setDead();
						return;
					}
				}
			}
		}
	}
	
	public void checkForBlockCollision() 
	{
		if(!this.isDead)
		{
			List<AxisAlignedBB> list = this.world.getCollisionBoxes(this, this.getEntityBoundingBox().expand(this.motionX, this.motionY, this.motionZ));
			
			if(list != null)
			{
				for(AxisAlignedBB e : list)
				{
					this.setDead();
					return;
				}
			}
		}
	}
	
	@Override
	public boolean canBePushed() 
	{
		return false;
	}
	
	@Override
	public void onKillCommand() 
	{
		if (!world.isRemote && !this.isDead)
		{
			this.setDead();
		}
	}
	
	@Override
	public boolean isInRangeToRenderDist(double distance) 
	{
		if(distance <= 200.0D)
		{
			return true;
		}
		return false;
	}
	
	@Override
	public boolean isInRangeToRender3d(double x, double y, double z) 
	{
		double x1 = this.posX-x;
		double y1 = this.posY-y;
		double z1 = this.posZ-z;
		
		return isInRangeToRenderDist(Math.sqrt(x1*x1+y1*y1+z1*z1));
	}
	
	@Override
	protected void entityInit() 
	{
		
	}

	@Override
	protected void readEntityFromNBT(NBTTagCompound compound)
	{		
		if(compound.hasKey("OwnerName"))
		{
			this.shooterName = compound.getString("OwnerName");
			this.shooter = Utils.getShooter(shooterName, this);
		}
		
		if(compound.hasKey("LifeTime"))
		{
			this.lifeTime = compound.getInteger("LifeTime");
		}
	}
	
	@Override
	protected void writeEntityToNBT(NBTTagCompound compound)
	{
		if(shooter != null)
		{
			this.shooterName = shooter.getName();
		}
		
		compound.setTag("OwnerName", new NBTTagString(this.shooterName));
		compound.setTag("LifeTime", new NBTTagInt(this.lifeTime));
	}

	@Override
	public void writeSpawnData(ByteBuf buffer) 
	{
		if(shooter != null)
		{
			this.shooterName = shooter.getName();
		}
		
		NBTTagCompound nbt = new NBTTagCompound();
		nbt.setString("OwnerName", this.shooterName);
		nbt.setInteger("LifeTime", this.lifeTime);
		ByteBufUtils.writeTag(buffer, nbt);
	}

	@Override
	public void readSpawnData(ByteBuf additionalData) 
	{
		NBTTagCompound nbt = ByteBufUtils.readTag(additionalData);
		
		if(nbt.hasKey("OwnerName"))
		{
			this.shooterName = nbt.getString("OwnerName");
			this.shooter = Utils.getShooter(shooterName, this);
		}
		
		if(nbt.hasKey("LifeTime"))
		{
			this.lifeTime = nbt.getInteger("LifeTime");
		}
	}
}

 

 

EntityRegistry:

Spoiler

public class EntityInit 
{
	public static void registerEntities()
	{
		registerEntityFrequency("bullet", EntityBullet.class, Reference.BULLET, 20, 200);
	}

	private static void registerEntityFrequency(String name, Class<? extends Entity> entity, int id, int frequency, int range)
	{
		EntityRegistry.registerModEntity(new ResourceLocation(Reference.MODID + ":" + name), entity, name, id, Main.instance, range, frequency, true);	
	}
}

 

 

Edited by Lyon
Posted
8 hours ago, Lyon said:

EntityRegistry.registerModEntity(new ResourceLocation(Reference.MODID + ":" + name), entity, name, id, Main.instance, range, frequency, true);

Don't use this, use the EntityEntry.Builder.

  • Like 1

About Me

Spoiler

My Discord - Cadiboo#8887

My WebsiteCadiboo.github.io

My ModsCadiboo.github.io/projects

My TutorialsCadiboo.github.io/tutorials

Versions below 1.14.4 are no longer supported on this forum. Use the latest version to receive support.

When asking support remember to include all relevant log files (logs are found in .minecraft/logs/), code if applicable and screenshots if possible.

Only download mods from trusted sites like CurseForge (minecraft.curseforge.com). A list of bad sites can be found here, with more information available at stopmodreposts.org

Edit your own signature at www.minecraftforge.net/forum/settings/signature/ (Make sure to check its compatibility with the Dark Theme)

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.