Jump to content

(SOLVED) [1.17.1] Getting Entity That Player Is Pointing At


Warven22

Recommended Posts

I need to know how to get the mob a player is pointing at from some distance (not just regular reach distance). I looked around quite a lot in both Google and source code, but I find myself stumped, confused, and unsure.

I attempted this:

public static EntityHitResult getTargetedEntity(Player player) {
  Vec3 start = player.getEyePosition();
  Vec3 addition = player.getLookAngle().multiply(new Vec3(10000D, 10000D, 10000D));
  return ProjectileUtil.getEntityHitResult(
  	player.level, 
  	player,
  	start,
  	start.add(addition),
  	player.getBoundingBox().expandTowards(player.getDeltaMovement()).inflate(1.0D),
	(val) -> true);
}

It seems to work, but the range is very short regardless of the number I multiply with.

Am I misunderstanding this function? Is there a better way to get the entity the player is pointing at?

Edited by Warven22
Solved
Link to comment
Share on other sites

3 hours ago, poopoodice said:

The problem is it's only gathering the entities that are inside/intersects with the bb:

player.getBoundingBox().expandTowards(player.getDeltaMovement()).inflate(1.0D)

So make the bounding box bigger.

I've tried expandingTowards using

player.getLookAngle().multiply(new Vec3(10000D, 10000D, 10000D));

rather than the player's movement (since movement can be 0), along with an inflate of a similarly large number. My range seems to have increased, but not as expected, as if I'm not visualizing how these changes to the bounding box actually affects anything. Can I set up this bounding box to be drawn in the debug elsewhere?

I feel like I do not understand how the ProjectileUtil works. I don't like the idea that I'm "shooting" the player towards a point, but it seems to be the only way.

Is there no other way for this to be done? No raycast function? No better way all the other modders use?

Link to comment
Share on other sites

8 hours ago, Luis_ST said:

did you read poopoodice post? since expanding the LookAngle != BoundingBox

I should have made it more clear that I did try that solution and what I actually did:

Vec3 start = player.getEyePosition();
Vec3 addition = player.getLookAngle().multiply(new Vec3(1000000D, 1000000D, 1000000D));
return ProjectileUtil.getEntityHitResult(
  player.level, player,
  start, start.add(addition),
  player.getBoundingBox().expandTowards(addition).inflate(1000000D), (val) -> true);

It didn't seem to work, as the range doesn't seem as big as it should be, needing me to be a few blocks (around 10) away rather than anything in view.

Edited by Warven22
clarification, less snarky wording
Link to comment
Share on other sites

  • Warven22 changed the title to (SOLVED) [1.17.1] Getting Entity That Player Is Pointing At

Please forgive this additional bump, but I want to assist future google visitors by sharing my solution.

I have found a solution by using a combination of two different functions: ProjectileUtil.getEntityHitResult & Item.getPlayerPOVHitResult.

getEntityHitResult
doesn't take into account player's rotation. getPlayerPOVHitResult is for blocks, but takes into account player's rotation. So by using the positions from getPlayerPOVHitResult, creating a bounding box from it, and using the loop for entities in getEntityHitResult, it creates a raycast function that fires from the player's eyes to where they are pointing.

I'm not sure if any of that is right, but one thing I'm sure of is it works. If you increase the range to a high number, you can detect an entity quite far away.

public static EntityHitResult getPlayerPOVHitResult(Player player) {
  float playerRotX = player.getXRot();
  float playerRotY = player.getYRot();
  Vec3 startPos = player.getEyePosition();
  float f2 = Mth.cos(-playerRotY * ((float)Math.PI / 180F) - (float)Math.PI);
  float f3 = Mth.sin(-playerRotY * ((float)Math.PI / 180F) - (float)Math.PI);
  float f4 = -Mth.cos(-playerRotX * ((float)Math.PI / 180F));
  float additionY = Mth.sin(-playerRotX * ((float)Math.PI / 180F));
  float additionX = f3 * f4;
  float additionZ = f2 * f4;
  double d0 = ZapEffect.RANGE;
  Vec3 endVec = startPos.add((double)additionX * d0, (double)additionY * d0, (double)additionZ * d0);
  AABB startEndBox = new AABB(startPos, endVec);
  Entity entity = null;
  for(Entity entity1 : player.level.getEntities(player, startEndBox, (val) -> true)) {
	AABB aabb = entity1.getBoundingBox().inflate(entity1.getPickRadius());
	Optional<Vec3> optional = aabb.clip(startPos, endVec);
    if (aabb.contains(startPos)) {
      if (d0 >= 0.0D) {
        entity = entity1;
        startPos = optional.orElse(startPos);
        d0 = 0.0D;
      }
    } else if (optional.isPresent()) {
      Vec3 vec31 = optional.get();
      double d1 = startPos.distanceToSqr(vec31);
      if (d1 < d0 || d0 == 0.0D) {
        if (entity1.getRootVehicle() == player.getRootVehicle() && !entity1.canRiderInteract()) {
          if (d0 == 0.0D) {
            entity = entity1;
            startPos = vec31;
          }
        } else {
          entity = entity1;
          startPos = vec31;
          d0 = d1;
        }
      }
    }
  }

  return (entity == null) ? null:new EntityHitResult(entity);
}
Link to comment
Share on other sites

Guest
This topic is now closed to further replies.

Announcements



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • I have no idea how a UI mod crashed a whole world but HUGE props to you man, just saved me +2 months of progress!  
    • So i know for a fact this has been asked before but Render stuff troubles me a little and i didnt find any answer for recent version. I have a custom nausea effect. Currently i add both my nausea effect and the vanilla one for the effect. But the problem is that when I open the inventory, both are listed, while I'd only want mine to show up (both in the inv and on the GUI)   I've arrived to the GameRender (on joined/net/minecraft/client) and also found shaders on client-extra/assets/minecraft/shaders/post and client-extra/assets/minecraft/shaders/program but I'm lost. I understand that its like a regular screen, where I'd render stuff "over" the game depending on data on the server, but If someone could point to the right client and server classes that i can read to see how i can manage this or any tip would be apreciated
    • Let me try and help you with love spells, traditional healing, native healing, fortune telling, witchcraft, psychic readings, black magic, voodoo, herbalist healing, or any other service your may desire within the realm of african native healing, the spirits and the ancestors. I am a sangoma and healer. I could help you to connect with the ancestors , interpret dreams, diagnose illness through divination with bones, and help you heal both physical and spiritual illness. We facilitate the deepening of your relationship to the spirit world and the ancestors. Working in partnership with one\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’s ancestors is a gift representing a close link with the spirit realm as a mediator between the worlds.*   Witchdoctors, or sorcerers, are often purveyors of mutis and charms that cause harm to people. we believe that we are here for only one purpose, to heal through love and compassion.*   African people share a common understanding of the importance of ancestors in daily life. When they have lost touch with their ancestors, illness may result or bad luck. Then a traditional healer, or sangoma, is sought out who may prescribe herbs, changes in lifestyle, a career change, or changes in relationships. The client may also be told to perform a ceremony or purification ritual to appease the ancestors.*   Let us solve your problems using powerful African traditional methods. We believe that our ancestors and spirits give us enlightenment, wisdom, divine guidance, enabling us to overcome obstacles holding your life back. Our knowledge has been passed down through centuries, being refined along the way from generation to generation. We believe in the occult, the paranormal, the spirit world, the mystic world.*   The services here are based on the African Tradition Value system/religion,where we believe the ancestors and spirits play a very important role in society. The ancestors and spirits give guidance and counsel in society. They could enable us to see into the future and give solutions to the problems affecting us. We use rituals, divination, spells, chants and prayers to enable us tackle the task before us.*   I have experience in helping and guiding many people from all over the world. My psychic abilities may help you answer and resolve many unanswered questions
    • Let me try and help you with love spells, traditional healing, native healing, fortune telling, witchcraft, psychic readings, black magic, voodoo, herbalist healing, or any other service your may desire within the realm of african native healing, the spirits and the ancestors. I am a sangoma and healer. I could help you to connect with the ancestors , interpret dreams, diagnose illness through divination with bones, and help you heal both physical and spiritual illness. We facilitate the deepening of your relationship to the spirit world and the ancestors. Working in partnership with one\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’s ancestors is a gift representing a close link with the spirit realm as a mediator between the worlds.*   Witchdoctors, or sorcerers, are often purveyors of mutis and charms that cause harm to people. we believe that we are here for only one purpose, to heal through love and compassion.*   African people share a common understanding of the importance of ancestors in daily life. When they have lost touch with their ancestors, illness may result or bad luck. Then a traditional healer, or sangoma, is sought out who may prescribe herbs, changes in lifestyle, a career change, or changes in relationships. The client may also be told to perform a ceremony or purification ritual to appease the ancestors.*   Let us solve your problems using powerful African traditional methods. We believe that our ancestors and spirits give us enlightenment, wisdom, divine guidance, enabling us to overcome obstacles holding your life back. Our knowledge has been passed down through centuries, being refined along the way from generation to generation. We believe in the occult, the paranormal, the spirit world, the mystic world.*   The services here are based on the African Tradition Value system/religion,where we believe the ancestors and spirits play a very important role in society. The ancestors and spirits give guidance and counsel in society. They could enable us to see into the future and give solutions to the problems affecting us. We use rituals, divination, spells, chants and prayers to enable us tackle the task before us.*   I have experience in helping and guiding many people from all over the world. My psychic abilities may help you answer and resolve many unanswered questions
    • Let me try and help you with love spells, traditional healing, native healing, fortune telling, witchcraft, psychic readings, black magic, voodoo, herbalist healing, or any other service your may desire within the realm of african native healing, the spirits and the ancestors. I am a sangoma and healer. I could help you to connect with the ancestors , interpret dreams, diagnose illness through divination with bones, and help you heal both physical and spiritual illness. We facilitate the deepening of your relationship to the spirit world and the ancestors. Working in partnership with one\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’s ancestors is a gift representing a close link with the spirit realm as a mediator between the worlds.*   Witchdoctors, or sorcerers, are often purveyors of mutis and charms that cause harm to people. we believe that we are here for only one purpose, to heal through love and compassion.*   African people share a common understanding of the importance of ancestors in daily life. When they have lost touch with their ancestors, illness may result or bad luck. Then a traditional healer, or sangoma, is sought out who may prescribe herbs, changes in lifestyle, a career change, or changes in relationships. The client may also be told to perform a ceremony or purification ritual to appease the ancestors.*   Let us solve your problems using powerful African traditional methods. We believe that our ancestors and spirits give us enlightenment, wisdom, divine guidance, enabling us to overcome obstacles holding your life back. Our knowledge has been passed down through centuries, being refined along the way from generation to generation. We believe in the occult, the paranormal, the spirit world, the mystic world.*   The services here are based on the African Tradition Value system/religion,where we believe the ancestors and spirits play a very important role in society. The ancestors and spirits give guidance and counsel in society. They could enable us to see into the future and give solutions to the problems affecting us. We use rituals, divination, spells, chants and prayers to enable us tackle the task before us.*   I have experience in helping and guiding many people from all over the world. My psychic abilities may help you answer and resolve many unanswered questions
  • Topics

×
×
  • Create New...

Important Information

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