Jump to content

Ray tracing from an entity's head angle.


BlueSpud

Recommended Posts

Hi,

I would like to get a general idea what an entity could see, kinda create a field of vision. To do this i was planning to use ray casting, however I can't seem to find a good way to ray cast with mine craft, other than selecting blocks. If I were to check for only blocks, some of my calculations, I'm sure would be messed up. Thanks in advance.

Link to comment
Share on other sites

 

MC has _built_in_ methods for doing ray-casting/raytracing.  (although it is kind of sloppy, you have to trace once for blocks, and again for entities).

 

For blocks, the method is located in world.  Entitiy collision detection is a bit more complex.

 

Perhaps a code example would work best

--taken from a utility class (hence the static method)

 

simply call it with with the position of the head of the entity, and the target end-vector.  The end-vector can be calculated with some 'simple' trig (take the yaw/pitch of the head, determine what distance you want to check out to, and apply some translation to get an endpoint/target point).

 

 

public static MovingObjectPosition tracePath(World world, float x, float y, float z, float tx, float ty, float tz, float borderSize, HashSet<Entity> excluded)
  {
  Vec3 startVec = Vec3.fakePool.getVecFromPool(x, y, z);
  Vec3 lookVec = Vec3.fakePool.getVecFromPool(tx-x, ty-y, tz-z);
  Vec3 endVec = Vec3.fakePool.getVecFromPool(tx, ty, tz);
  float minX = x < tx ? x : tx;
  float minY = y < ty ? y : ty;
  float minZ = z < tz ? z : tz;
  float maxX = x > tx ? x : tx;
  float maxY = y > ty ? y : ty; 
  float maxZ = z > tz ? z : tz;
  AxisAlignedBB bb = AxisAlignedBB.getAABBPool().getAABB(minX, minY, minZ, maxX, maxY, maxZ).expand(borderSize, borderSize, borderSize);
  List<Entity> allEntities = world.getEntitiesWithinAABBExcludingEntity(null, bb);  
  MovingObjectPosition blockHit = world.rayTraceBlocks(startVec, endVec);
  startVec = Vec3.fakePool.getVecFromPool(x, y, z);
  endVec = Vec3.fakePool.getVecFromPool(tx, ty, tz);
  float maxDistance = (float) endVec.distanceTo(startVec);
  if(blockHit!=null)
    {
    maxDistance = (float) blockHit.hitVec.distanceTo(startVec);
    }  
  Entity closestHitEntity = null;
  float closestHit = Float.POSITIVE_INFINITY;
  float currentHit = 0.f;
  AxisAlignedBB entityBb;// = ent.getBoundingBox();
  MovingObjectPosition intercept;
  for(Entity ent : allEntities)
    {    
    if(ent.canBeCollidedWith() && !excluded.contains(ent))
      {
      float entBorder =  ent.getCollisionBorderSize();
      entityBb = ent.boundingBox;
      if(entityBb!=null)
        {
        entityBb = entityBb.expand(entBorder, entBorder, entBorder);
        intercept = entityBb.calculateIntercept(startVec, endVec);
        if(intercept!=null)
          {
          currentHit = (float) intercept.hitVec.distanceTo(startVec);
          if(currentHit < closestHit || currentHit==0)
            {            
            closestHit = currentHit;
            closestHitEntity = ent;
            }
          } 
        }
      }
    }  
  if(closestHitEntity!=null)
    {
    blockHit = new MovingObjectPosition(closestHitEntity);
    }
  return blockHit;
  }

 

This code is _heavily_ based upon the code found in entityPlayer that determines the block/entity to hit/interact with.

 

It first ray-traces for a block hit (using the world  raytrace methods), and sets that as the real end-point of the ray-trace.  It then grabs all entities in the remaining bounding box, and tests each of those for collision.

 

 

 

Link to comment
Share on other sites

 

MC has _built_in_ methods for doing ray-casting/raytracing.  (although it is kind of sloppy, you have to trace once for blocks, and again for entities).

 

For blocks, the method is located in world.  Entitiy collision detection is a bit more complex.

 

Perhaps a code example would work best

--taken from a utility class (hence the static method)

 

simply call it with with the position of the head of the entity, and the target end-vector.  The end-vector can be calculated with some 'simple' trig (take the yaw/pitch of the head, determine what distance you want to check out to, and apply some translation to get an endpoint/target point).

 

 

public static MovingObjectPosition tracePath(World world, float x, float y, float z, float tx, float ty, float tz, float borderSize, HashSet<Entity> excluded)
  {
  Vec3 startVec = Vec3.fakePool.getVecFromPool(x, y, z);
  Vec3 lookVec = Vec3.fakePool.getVecFromPool(tx-x, ty-y, tz-z);
  Vec3 endVec = Vec3.fakePool.getVecFromPool(tx, ty, tz);
  float minX = x < tx ? x : tx;
  float minY = y < ty ? y : ty;
  float minZ = z < tz ? z : tz;
  float maxX = x > tx ? x : tx;
  float maxY = y > ty ? y : ty; 
  float maxZ = z > tz ? z : tz;
  AxisAlignedBB bb = AxisAlignedBB.getAABBPool().getAABB(minX, minY, minZ, maxX, maxY, maxZ).expand(borderSize, borderSize, borderSize);
  List<Entity> allEntities = world.getEntitiesWithinAABBExcludingEntity(null, bb);  
  MovingObjectPosition blockHit = world.rayTraceBlocks(startVec, endVec);
  startVec = Vec3.fakePool.getVecFromPool(x, y, z);
  endVec = Vec3.fakePool.getVecFromPool(tx, ty, tz);
  float maxDistance = (float) endVec.distanceTo(startVec);
  if(blockHit!=null)
    {
    maxDistance = (float) blockHit.hitVec.distanceTo(startVec);
    }  
  Entity closestHitEntity = null;
  float closestHit = Float.POSITIVE_INFINITY;
  float currentHit = 0.f;
  AxisAlignedBB entityBb;// = ent.getBoundingBox();
  MovingObjectPosition intercept;
  for(Entity ent : allEntities)
    {    
    if(ent.canBeCollidedWith() && !excluded.contains(ent))
      {
      float entBorder =  ent.getCollisionBorderSize();
      entityBb = ent.boundingBox;
      if(entityBb!=null)
        {
        entityBb = entityBb.expand(entBorder, entBorder, entBorder);
        intercept = entityBb.calculateIntercept(startVec, endVec);
        if(intercept!=null)
          {
          currentHit = (float) intercept.hitVec.distanceTo(startVec);
          if(currentHit < closestHit || currentHit==0)
            {            
            closestHit = currentHit;
            closestHitEntity = ent;
            }
          } 
        }
      }
    }  
  if(closestHitEntity!=null)
    {
    blockHit = new MovingObjectPosition(closestHitEntity);
    }
  return blockHit;
  }

 

This code is _heavily_ based upon the code found in entityPlayer that determines the block/entity to hit/interact with.

 

It first ray-traces for a block hit (using the world  raytrace methods), and sets that as the real end-point of the ray-trace.  It then grabs all entities in the remaining bounding box, and tests each of those for collision.

Looks good, but can you give me an idea on how to test if the entity is a player?

Link to comment
Share on other sites

You heard of the 'instanceof' keyword?

Don't ask for support per PM! They'll get ignored! | If a post helped you, click the "Thank You" button at the top right corner of said post! |

mah twitter

This thread makes me sad because people just post copy-paste-ready code when it's obvious that the OP has little to no programming experience. This is not how learning works.

Link to comment
Share on other sites

I am relatively new to java actually. I do alot of c and c++ usually, so I haven't, but if I am not correct this is what I think the code would be after you mentioning that:

if (ray cast() == instanceof EntityPlayer)
{
//whatever I want
}

Would that be right, or maybe a bit different syntax?

Link to comment
Share on other sites

if you are referring to testing if the target hit is a player:

 

the method returns a movingObjectPosition object, which has an internal field for an entity hit if any-- will be null if no entity was hit. (it may also return null entirely, meaning no object was hit during the trace)

 

MovingObjectPosition hit = tracePath(world, x,y,z, x1, y1,z1);
if(hit!=null && hit.entityHit instanceof EntityPlayer)
{
//do logic
}

 

the instanceof operator essentially entails a null check and run-time class check (usually the only way to tell entity classes/subclasses apart)

 

Link to comment
Share on other sites

Yes, the tracePath function takes essentially two vectors x,y,z for start vector, and tx,ty,tz for target vector (end of the ray).  Border size merely expands the search radius slightly beyond what it would normally get (not sure if it actually needed, it was intended to grab entities that were technically outside of the bounding box but could still be collided -- I use it with a value of 0.2f).

 

 

Link to comment
Share on other sites

Yes, the tracePath function takes essentially two vectors x,y,z for start vector, and tx,ty,tz for target vector (end of the ray).  Border size merely expands the search radius slightly beyond what it would normally get (not sure if it actually needed, it was intended to grab entities that were technically outside of the bounding box but could still be collided -- I use it with a value of 0.2f).

Thanks for the help, but to simplify things, i just used some trig and some separating axis theorem to make a bounding triangle and if the player is in it, the mob responds.

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.