Jump to content

[1.7.10] Homing arrow Yaw & Pitch


artman41

Recommended Posts

I've been working on homing arrows for ProjectE and I have the homing part working. However, after finding the entity to attack, the arrow doesn't change it's yaw or pitch and I have no clue about how to work this out. Does anybody know how I'd go about this? The class of code is below:

 

package moze_intel.projecte.gameObjs.entity;

import moze_intel.projecte.utils.WorldHelper;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityLiving;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.projectile.EntityArrow;
import net.minecraft.util.AxisAlignedBB;
import net.minecraft.world.World;

import java.util.List;

public class EntityHomingArrow extends EntityArrow
{
EntityLiving target;
World world;

private void init(World world)
{
	this.world = world;
}

public EntityHomingArrow(World world)
{
	super(world);
	init(world);
}

public EntityHomingArrow(World world, EntityLivingBase par2, float par3) 
{
	super(world, par2, par3);
	init(world);
}

@Override
public void onUpdate()
{
	//TODO Create proper custom arrow. This one is duplicating because of the super call for onUpdate();
	super.onUpdate();

	AxisAlignedBB box = this.boundingBox;

	if (target == null && !WorldHelper.isArrowInGround(this))
	{
		System.out.println("TARGET == NULL");
		AxisAlignedBB bBox = box.expand(8, 8, ;
		List<EntityLiving> list = this.worldObj.getEntitiesWithinAABB(EntityLiving.class, bBox);

		double distance = 100000;

		double disToEnt1 = 1000;

		for (EntityLiving entity : list)
		{
			double toIt = distanceTo(entity);

			double disToEnt2 = distanceTo(entity);

			if(disToEnt2 < disToEnt1){
				disToEnt1 = disToEnt2;
				target = entity;
			}

//				if (distance > toIt)
//				{
//					distance = toIt;
//					target = entity;
//				}
		}

		if (target == null)
		{
			return;
		}

		double xyz[] = checkDistance(target, this.posX, this.posY, this.posZ);

		double x = xyz[0];
		double y = xyz[1];
		double z = xyz[2];

		this.setThrowableHeading(x, y, z, 2.0F, 0.0F);
	}
	else if (!WorldHelper.isArrowInGround(this))
	{
		System.out.println("TARGET != NULL");
		if (target.getHealth() == 0) 
		{
			target = null;
			return;
		}

		world.spawnParticle("flame", box.maxX, box.maxY, box.maxZ, 0.0D, 0.0D, 0.0D);

		this.setPositionAndRotation(target.posX, target.posY, target.posZ, target.rotationYaw, target.rotationPitch);

		double xyz[] = checkDistance(target, this.posX, this.posY, this.posZ);

		double x = xyz[0];
		double y = xyz[1];
		double z = xyz[2];

		this.setThrowableHeading(x, y, z, 2.0F, 0.0F);
	}
}

private double distanceTo(EntityLiving entity)
{
	double [] ds = new double []
	{
		this.posX - entity.posX,
		this.posY - entity.posY,
		this.posZ - entity.posZ
	};

	double d = 0;

	for (int i = 0; i < 3; i++)
		d += ds[i] * ds[i];

	return Math.sqrt(d);
}

private double[] checkDistance(EntityLiving entity, double x, double y, double z){
	double rX, rY, rZ;

	System.out.println("LOCATION OF ENTITY: " + entity.posX + ", " + entity.posY + ", " + entity.posZ);

	rX = x;
	rY = y;
	rZ = z;

	if(entity.posX != x){
		if(entity.posX > x){
			rX =+ entity.posX;
		} else{
			rX--;
		}
	}

	if(entity.posY != y){
		if(entity.posY > y){
			rY =+ entity.posY;
		}
	}

	if(entity.posZ != z){
		if(entity.posZ > z){
			rZ =+ entity.posZ;
		} else{
			rZ--;
		}
	}

	double rValues[] = {rX, rY, rZ};

	for(int i = 0; i < rValues.length; i++){
		if(i == 0){System.out.println("x " + rValues[i]);}
		if(i == 1){System.out.println("y " + rValues[i]);}
		if(i == 2){System.out.println("z " + rValues[i]);}
	}

	return rValues;
}
}

Link to comment
Share on other sites

Does the arrow still go toward the target it is homing?

 

If so then via basic trigonometry you can figure the pitch and yaw out. Sorry, I cant exactly post and example code as I am going to class but that is the basic thing to do.

If my post helped you, please press that "Thank You"-button to show your appreciation.

 

Also if you don't know Java, I would suggest you read the official tutorials by Oracle to get an idea of how to do this. Thanks, and good modding!

 

Also if you haven't, set up a Git repo for your mod not only for convinience but also to make it easier to help you.

Link to comment
Share on other sites

Even if you had the correct code for targeting, your arrow would not work because you extend EntityArrow - the EntityArrow's onUpdate method and yours are conflicting.

 

A workaround is to override onUpdate and make sure to NOT call super.onUpdate; instead, call super.onEntityUpdate - this will bypass the EntityArrow update code (meaning you will need to duplicate it in your class, changing it where you need) and go straight to Entity#onUpdate, which you still need to be called.

 

For homing, the basic trig looks about like this:

double dx = target.posX - this.posX;
double dy = target.boundingBox.minY + (double)(target.height) - this.posY;
double dz = target.posZ - this.posZ;
setThrowableHeading(dx, dy, dz, 3.0F, 1.0F);

Of course, it can get more complicated if you want the arrow to have a pretty arc instead of aiming directly at the target every tick.

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



×
×
  • Create New...

Important Information

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