Jump to content

Forge1.18.1 how to raycast?


volkov

Recommended Posts

5 hours ago, Luis_ST said:

re you exactly talking about

ammm a ray casting like when you send a ray from one dot in direction of another

in my case cast a ray from player's head in direction of players view ,sorry for confusion

Link to comment
Share on other sites

thanks b.t.w. dose anybody know how to detect entity that collides with ray

public static BlockHitResult getTargetofgun(Level pLevel, Player pPlayer, ClipContext.Fluid pFluidMode,double reach/*reach*/) {
        float f = pPlayer.getXRot();
        float f1 = pPlayer.getYRot();
        Vec3 vec3 = pPlayer.getEyePosition();
        float f2 = Mth.cos(-f1 * ((float)Math.PI / 180F) - (float)Math.PI);
        float f3 = Mth.sin(-f1 * ((float)Math.PI / 180F) - (float)Math.PI);
        float f4 = -Mth.cos(-f * ((float)Math.PI / 180F));
        float f5 = Mth.sin(-f * ((float)Math.PI / 180F));
        float f6 = f3 * f4;
        float f7 = f2 * f4;
        Vec3 vec31 = vec3.add((double)f6 * reach, (double)f5 * reach, (double)f7 * reach);
        return pLevel.clip(new ClipContext(vec3, vec31, ClipContext.Block.OUTLINE, pFluidMode, pPlayer));
    }

is there a function like clip() that detects LivingEntity instead of block

Link to comment
Share on other sites

33 minutes ago, MFMods said:

that sounds a lot like looking at enderman to piss if off. look at vanilla code.

im looked if im not mistaken you are talking about function isLookingAtMe()?

boolean isLookingAtMe(Player pPlayer) {
      ItemStack itemstack = pPlayer.getInventory().armor.get(3);
      if (itemstack.isEnderMask(pPlayer, this)) {
         return false;
      } else {
         Vec3 vec3 = pPlayer.getViewVector(1.0F).normalize();
         Vec3 vec31 = new Vec3(this.getX() - pPlayer.getX(), this.getEyeY() - pPlayer.getEyeY(), this.getZ() - pPlayer.getZ());
         double d0 = vec31.length();
         vec31 = vec31.normalize();
         double d1 = vec3.dot(vec31);
         return d1 > 1.0D - 0.025D / d0 ? pPlayer.hasLineOfSight(this) : false;
      }
   }

 

Link to comment
Share on other sites

yes.

scalar product of two vectors is "length" (magnitude) of first one, multiplied by "length" (magnitude) of second one, multiplied by cosine of angle between them. in this case first two are equal to 1 and d1 is cosine of angle between players exact line of sight and the line between player and enderman. then you basically say "if angle is small enough (and has line of sight), return true".

Link to comment
Share on other sites

  • 3 weeks later...

sorry for stupid question that im about to ask but dose vectors need tow dots but vec3 constructor only requires 1 dot(set of coordinates) and is called vector ?

also it works thanks!

boolean islookinatit(Player player,LivingEntity entity) {
        Vec3 vec3 = player.getViewVector(1.0F).normalize();
        Vec3 vec31 = new Vec3(entity.getX() - player.getX(), entity.getY() - player.getEyeY(), entity.getZ() - player.getZ());
        double d0 = vec31.length();
        vec31 = vec31.normalize();
        double d1 = vec3.dot(vec31);
        return d1 > 1.0D - 0.025D / d0 && AtoElineOfSight(player, entity);
    }
    public boolean AtoElineOfSight(Entity Ea,Entity Eb) {
        if (Eb.level != Ea.level) {
            return false;
        } else {
            Vec3 Avec = new Vec3(Ea.getX(), Ea.getEyeY(), Ea.getZ());
            Vec3 Bvec = new Vec3(Eb.getX(), Eb.getY(), Eb.getZ());
            if (Bvec.distanceTo(Avec) > 128.0D) {
                return false;
            } else {
                return Ea.level.clip(new ClipContext(Avec, Bvec, ClipContext.Block.COLLIDER, ClipContext.Fluid.NONE, Ea)).getType() == HitResult.Type.MISS;
            }
        }
    }

but seems like im understood that im need it to collide not just with position of entity but with its boundingbox or hitbox of entity sorry for putting question so badly and also sorry for such a long delay with answer

Link to comment
Share on other sites

2 hours ago, volkov said:

sorry for stupid question that im about to ask but dose vectors need tow dots but vec3 constructor only requires 1 dot(set of coordinates) and is called vector ?

it is just a name, there is no physical connection

Link to comment
Share on other sites

  • 1 year later...

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.