Posted June 24, 20178 yr I have made an item which shoots my custom projectile all is good until i bring the velocity of the projectile to around 15 or higher. At this point it begins to prefer aiming at 45 degree angles. rather than where i am actually aiming. I assume it has to do with how im calculating the motion of the projectile. Does anyone know how i can fix this problem. Spoiler package com.drok.progressiveg.common.item; import com.drok.progressiveg.GunMod; import com.drok.progressiveg.entity.projectile.EntityLeadBullet; import net.minecraft.entity.Entity; import net.minecraft.entity.item.EntityEnderPearl; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.init.SoundEvents; import net.minecraft.item.Item; import net.minecraft.item.ItemStack; import net.minecraft.stats.StatList; import net.minecraft.util.ActionResult; import net.minecraft.util.EnumActionResult; import net.minecraft.util.EnumHand; import net.minecraft.util.SoundCategory; import net.minecraft.world.World; public class ItemLeadGun extends Item { public Item ammo; public int damage; public int velocity; public ItemLeadGun(String name, Item ammo, int damage, int velocity) { this(); this.ammo = ammo; this.damage = damage; this.velocity = velocity; this.setRegistryName(name); this.setUnlocalizedName(name); } public ItemLeadGun() { this.setCreativeTab(GunMod.TABGUNS); this.setMaxStackSize(1); } public ActionResult<ItemStack> onItemRightClick(World worldIn, EntityPlayer playerIn, EnumHand handIn) { ItemStack item = findAmmo(playerIn); if(!item.isEmpty()) { EntityLeadBullet bullet = new EntityLeadBullet(worldIn, playerIn, damage); bullet.setAim(playerIn, playerIn.rotationPitch, playerIn.rotationYaw, 0.0F, 20, 0); bullet.posX = playerIn.posX; bullet.posY = playerIn.posY + 1; bullet.posZ = playerIn.posZ; worldIn.spawnEntity(bullet); return new ActionResult<ItemStack>(EnumActionResult.SUCCESS, playerIn.getHeldItemMainhand()); } else { return new ActionResult<ItemStack>(EnumActionResult.FAIL, playerIn.getHeldItemMainhand()); } } private ItemStack findAmmo(EntityPlayer player) { for(int i = 0; i < player.inventory.getSizeInventory(); i++) { ItemStack item = player.inventory.getStackInSlot(i); if(item != null) { if(item.getItem().getUnlocalizedName().equalsIgnoreCase(ammo.getUnlocalizedName())) { return item; } } } return ItemStack.EMPTY; } } package com.drok.progressiveg.entity.projectile; import com.drok.progressiveg.GunMod; import net.minecraft.block.material.Material; import net.minecraft.block.state.IBlockState; import net.minecraft.entity.Entity; import net.minecraft.entity.EntityLivingBase; import net.minecraft.entity.IProjectile; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.entity.projectile.EntityArrow; import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.util.DamageSource; import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.MathHelper; import net.minecraft.util.math.RayTraceResult; import net.minecraft.util.math.Vec3d; import net.minecraft.world.World; import net.minecraftforge.fml.common.registry.IThrowableEntity; public class EntityLeadBullet extends Entity implements IProjectile { public Entity owner; public int damage; public float speed() { float a = (float) Math.abs(this.motionX); float b = (float) Math.abs(this.motionY);; float c = (float) Math.abs(this.motionZ);; return (a + b + c) / 3; }; public EntityLeadBullet(World worldIn) { this(worldIn, null, 0); } public EntityLeadBullet(World worldIn, Entity own, int dmg) { super(worldIn); owner = own; damage = dmg; this.setSize(0.5F, 0.5F); this.setNoGravity(false); this.setVelocity(0, 0, 50); } public void setAim(Entity entityThrower, float rotationPitchIn, float rotationYawIn, float pitchOffset, float velocity, float inaccuracy) { double f = -MathHelper.sin(rotationYawIn * 0.017453292F) * MathHelper.cos(rotationPitchIn * 0.017453292F); double f1 = -MathHelper.sin((rotationPitchIn + pitchOffset) * 0.017453292F); double f2 = MathHelper.cos(rotationYawIn * 0.017453292F) * MathHelper.cos(rotationPitchIn * 0.017453292F); this.setThrowableHeading((double)f, (double)f1, (double)f2, velocity, inaccuracy); this.motionX += entityThrower.motionX; this.motionZ += entityThrower.motionZ; if (!entityThrower.onGround) { this.motionY += entityThrower.motionY; } } @Override public void setVelocity(double x, double y, double z) { this.motionX = x; this.motionY = y; this.motionZ = z; } @Override public void onUpdate() { super.onUpdate(); this.posX += this.motionX; this.posY += this.motionY; this.posZ += this.motionZ; Vec3d from = new Vec3d(this.posX, this.posY, this.posZ); Vec3d to = new Vec3d(this.posX + this.motionX, this.posY + this.motionY, this.posZ + this.motionZ); RayTraceResult raytraceresult = this.world.rayTraceBlocks(from, to, false, true, false); if(raytraceresult != null) { this.onHit(raytraceresult); } this.motionX *= 0.99; this.motionY *= 0.99; this.motionZ *= 0.99; this.motionY -= 0.05000000074505806D; this.setPosition(this.posX, this.posY, this.posZ); this.doBlockCollisions(); } protected void onHit(RayTraceResult result) { GunMod.log(result.getBlockPos().toString()); Entity entity = result.entityHit; if(entity != null) { DamageSource damagesource = null; if (this.owner == null && this.owner instanceof EntityPlayer) { damagesource = DamageSource.causePlayerDamage((EntityPlayer) this.owner); } else if (this.owner == null && this.owner instanceof EntityLivingBase) { damagesource = DamageSource.causeMobDamage((EntityLivingBase) this.owner); } else { GunMod.log("No Valid Bullet Owner! Please contact the mod developer."); return; } entity.attackEntityFrom(damagesource, this.damage); } else { BlockPos blkpos = result.getBlockPos(); IBlockState blkst = this.world.getBlockState(blkpos); if(blkst.getMaterial() == Material.GLASS && speed() > 7) { world.setBlockToAir(blkpos); for(int x = -1; x < 2; x++) { for(int y = -1; y < 2; y++) { for(int z = -1; z < 2; z++) { BlockPos blkpos2 = blkpos; blkpos2 = blkpos2.add(x, y, z); IBlockState blkst2 = this.world.getBlockState(blkpos2); GunMod.log("1: " + blkpos + ", 2: " + blkpos2); if(blkst2.getMaterial() == Material.GLASS && GunMod.rand.nextBoolean()) { world.setBlockToAir(blkpos2); this.motionX *= 0.5f; this.motionY *= 0.5f; this.motionZ *= 0.5f; } } } } } else { this.setDead(); } } } @Override public void setThrowableHeading(double x, double y, double z, float velocity, float inaccuracy) { float f = MathHelper.sqrt(x * x + y * y + z * z); x = x / (double)f; y = y / (double)f; z = z / (double)f; x = x + this.rand.nextGaussian() * 0.007499999832361937D * (double)inaccuracy; y = y + this.rand.nextGaussian() * 0.007499999832361937D * (double)inaccuracy; z = z + this.rand.nextGaussian() * 0.007499999832361937D * (double)inaccuracy; x = x * (double)velocity; y = y * (double)velocity; z = z * (double)velocity; this.motionX = x; this.motionY = y; this.motionZ = z; float f1 = MathHelper.sqrt(x * x + z * z); this.rotationYaw = (float)(MathHelper.atan2(x, z) * (180D / Math.PI)); this.rotationPitch = (float)(MathHelper.atan2(y, (double)f1) * (180D / Math.PI)); this.prevRotationYaw = this.rotationYaw; this.prevRotationPitch = this.rotationPitch; //this.ticksInGround = 0; } @Override protected void entityInit() { // TODO Auto-generated method stub } @Override protected void readEntityFromNBT(NBTTagCompound compound) { // TODO Auto-generated method stub } @Override protected void writeEntityToNBT(NBTTagCompound compound) { // TODO Auto-generated method stub } }
June 26, 20178 yr Author Ok i have done some testing and it travels correctly when it comes to its position and collisions but visually it's incorrect. Why could this be happening and is there a way to fix it?
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.