Jump to content

[1.8] find entity a player is looking at


Failender

Recommended Posts

Use rayTracing.

Before 1.8 client look vector was on players eyes, server one was not.

 

Since 1.8 both are from eyes, but vanilla method is for client only. Copy vanilla rayTracing method and use it on server.

Note: Find here: Entity#rayTrace

1.7.10 is no longer supported by forge, you are on your own.

Link to comment
Share on other sites

I guess I got it. Only problem is, I am not sure what the two float values the method is taking do.

public MovingObjectPosition rayTrace(double p_174822_1_, float p_174822_3_)

It should be something with the direction the player is looking at, if im right. So it might be player.camerapitch/camerayaw?

Link to comment
Share on other sites

I think they are the distance and the partialTickTime.

Don't PM me with questions. They will be ignored! Make a thread on the appropriate board for support.

 

1.12 -> 1.13 primer by williewillus.

 

1.7.10 and older versions of Minecraft are no longer supported due to it's age! Update to the latest version for support.

 

http://www.howoldisminecraft1710.today/

Link to comment
Share on other sites

Explanation from diesieben07 for partialTickTime: http://www.minecraftforge.net/forum/index.php/topic,12681.msg65852.html#msg65852.

In your case, because you don't have a partialTickTime available, you can just use 1.0F as the partialTickTime. And yes, you are right about the distance.

Don't PM me with questions. They will be ignored! Make a thread on the appropriate board for support.

 

1.12 -> 1.13 primer by williewillus.

 

1.7.10 and older versions of Minecraft are no longer supported due to it's age! Update to the latest version for support.

 

http://www.howoldisminecraft1710.today/

Link to comment
Share on other sites

Well. The RayTrace seems to NEVER hit an entity, even if I am spawning a whole zoo around me.

 

MovingObjectPosition pos = Methods.rayTrace(ext.getEntity(), 10f,1f);
	System.out.println(pos);
	if(pos!=null && pos.entityHit!=null)
	{
		System.out.println("call");
		Entity entityHit= pos.entityHit;
		entityHit.attackEntityFrom(DamageSource.causePlayerDamage(ext.getEntity()), 2);

	}

 

public static MovingObjectPosition rayTrace(Entity entity, double distance, float p_174822_3_)
    {
        Vec3 vec3 = entity.getPositionEyes(p_174822_3_);
        Vec3 vec31 = entity.getLook(p_174822_3_);
        Vec3 vec32 = vec3.addVector(vec31.xCoord * distance, vec31.yCoord * distance, vec31.zCoord * distance);
        return entity.worldObj.rayTraceBlocks(vec3, vec32, false, false, true);
    }

 private static Vec3 getPositionEyes(Entity entity, float p_174824_1_)
 {
	 if (p_174824_1_ == 1.0F)
	 {
		 return new Vec3(entity.posX, entity.posY + (double)entity.getEyeHeight(), entity.posZ);
     }
	 else
     {
		 double d0 = entity.prevPosX + (entity.posX - entity.prevPosX) * (double)p_174824_1_;
		 double d1 = entity.prevPosY + (entity.posY - entity.prevPosY) * (double)p_174824_1_ + (double)entity.getEyeHeight();
		 double d2 = entity.prevPosZ + (entity.posZ - entity.prevPosZ) * (double)p_174824_1_;
		 return new Vec3(d0, d1, d2);
     }
 }

 

 

Link to comment
Share on other sites

I never knew what is the best way to get entities, BUT I know this. Let's say your vector is arrow, you entity is some box. To know that arrow hit box you need to check if vector is "touching" box. You need to actually do that.

 

Have a look at EntityThrowable#onUpdate()

Vec3 vec3 = new Vec3(this.posX, this.posY, this.posZ);
        Vec3 vec31 = new Vec3(this.posX + this.motionX, this.posY + this.motionY, this.posZ + this.motionZ);
        MovingObjectPosition movingobjectposition = this.worldObj.rayTraceBlocks(vec3, vec31);
        vec3 = new Vec3(this.posX, this.posY, this.posZ);
        vec31 = new Vec3(this.posX + this.motionX, this.posY + this.motionY, this.posZ + this.motionZ);

        if (movingobjectposition != null)
        {
            vec31 = new Vec3(movingobjectposition.hitVec.xCoord, movingobjectposition.hitVec.yCoord, movingobjectposition.hitVec.zCoord);
        }

        if (!this.worldObj.isRemote)
        {
            Entity entity = null;
            List list = this.worldObj.getEntitiesWithinAABBExcludingEntity(this, this.getEntityBoundingBox().addCoord(this.motionX, this.motionY, this.motionZ).expand(1.0D, 1.0D, 1.0D));
            double d0 = 0.0D;
            EntityLivingBase entitylivingbase = this.getThrower();

            for (int j = 0; j < list.size(); ++j)
            {
                Entity entity1 = (Entity)list.get(j);

                if (entity1.canBeCollidedWith() && (entity1 != entitylivingbase || this.ticksInAir >= 5))
                {
                    float f = 0.3F;
                    AxisAlignedBB axisalignedbb = entity1.getEntityBoundingBox().expand((double)f, (double)f, (double)f);
                    MovingObjectPosition movingobjectposition1 = axisalignedbb.calculateIntercept(vec3, vec31);

                    if (movingobjectposition1 != null)
                    {
                        double d1 = vec3.distanceTo(movingobjectposition1.hitVec);

                        if (d1 < d0 || d0 == 0.0D)
                        {
                            entity = entity1;
                            d0 = d1;
                        }
                    }
                }
            }

            if (entity != null)
            {
                movingobjectposition = new MovingObjectPosition(entity);
            }
        }

        if (movingobjectposition != null)
        {
            if (movingobjectposition.typeOfHit == MovingObjectPosition.MovingObjectType.BLOCK && this.worldObj.getBlockState(movingobjectposition.getBlockPos()).getBlock() == Blocks.portal)
            {
                this.setInPortal();
            }
            else
            {
                this.onImpact(movingobjectposition);
            }
        }

 

Every tick there is check if EntityThrowable's bounding box touches any other box in 1.0D "lenght" (box size).

That is the way to get the entity hit. Notice how it checks which entity was hit 1st and stuff. Pretty neat.

 

And yes - unless you want to go with VERY complicated 3D math on your own, this is best way provided by vanilla (for me at least).

1.7.10 is no longer supported by forge, you are on your own.

Link to comment
Share on other sites

Guys, it is a really common misconception but the rayTrace() methods only find blocks, not entities. The confusion is that these methods return a MovingObjectPosition which contains fields for entities hit but those will always be null.

 

There is a method called getMouseOver() which ray traces both blocks and entities, but only within the reach distance of the player -- it is used to figure out whether you hit an entity.

 

So I've made my own custom mouse over method which uses the same technique as the built-in mouse over but extends the distance as far as you want. I was able to use the method to create a weapon with "infinite" reach -- basically picking off anything I could see.

 

I'm writing a tutorial on ray tracing, but for now check out my tutorial on the extended reach weapon. It will give you what you need: http://jabelarminecraft.blogspot.com/p/minecraft-modding-extending-reach-of.html

Check out my tutorials here: http://jabelarminecraft.blogspot.com/

Link to comment
Share on other sites

got it to work now, thank you jabelar!

Just one thing for your tutorial you've got a thing you should really fix.

In your messagehandler for the server side u should queue up the execution of the task to prevent server crashes.

http://greyminecraftcoder.blogspot.com.au/2015/01/thread-safety-with-network-messages.html

 

Interesting. Yeah the separate thread for networking in 1.8 has created a few gotchas. For example, the timing of packets can sometimes be different that expected which affected the way I was constructing some custom entities with sync packets. Thanks for pointing this out.

Check out my tutorials here: http://jabelarminecraft.blogspot.com/

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.

×
×
  • Create New...

Important Information

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