Posted May 15, 201312 yr 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.
May 15, 201312 yr 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.
May 15, 201312 yr Author 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?
May 15, 201312 yr 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.
May 15, 201312 yr Author 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?
May 15, 201312 yr 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)
May 15, 201312 yr Author That seems pretty self-explanitory, but is the function tracePath to check the area for entities? If it ism is tx,etc the end of the ray? And I assume boarder size is how big I want it to be?
May 16, 201312 yr 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).
May 17, 201312 yr Author 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.
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.