Jump to content

How to check whether there are any entities behind entity or not?


Recommended Posts

Posted

What have you tried so far?

 

I find it is good to think about similar things the game already does and then check the code to see how they do it. For example there are many entities that turn to look at a player, so check out how they know which way to turn. There are also entities that will react if you're in front so it shouldn't be too hard to convert that into detecting something behind. Basically, find examples of where the direction an entity is facing is important and see how that is done.

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

Posted
1 hour ago, jabelar said:

What have you tried so far?

 

I find it is good to think about similar things the game already does and then check the code to see how they do it. For example there are many entities that turn to look at a player, so check out how they know which way to turn. There are also entities that will react if you're in front so it shouldn't be too hard to convert that into detecting something behind. Basically, find examples of where the direction an entity is facing is important and see how that is done.

I have no ideas in this 

Posted
4 hours ago, uncleofbob said:

Hi!

A Quick (2 sec) look at the vanilla code:

 

EntityAIFIndEntityNearest.class

EntityAIFindEntityNearestPlayer.class

 

 

Thanks for your help but it doesn't seem to work. I saw the method canEntityBeSeen in EntityLivingBase but it is only for blocks.I wonder if there are anyways to make it check Entity?

Posted
2 hours ago, lethinh said:

I wonder if there are anyways to make it check Entity?

 

Yes. But that is your job. Modding is about coding new ways of doing things. 

 

So if you look at canEntityBeSeen() it calls a method called rayTraceBlocks(). If you look at the code for that method it is admittedly kinda complicated if you're new to modding, but there should be a few things that are interesting. For example, you'll see that you need to use vectors (Vec3d class) and loops to check positions in a direction.

 

Also, if you're lucky you might notice that right after the canEntityBeSeen() method is one called getLook() which returns a vector for the direction that the entity is looking. So the vector for behind could be opposite of that. 

 

So one idea that comes to mind would be to check if the vector for behind is close the the vector for the position between the entities. In other words, get the getLook() vector and invert it, calculate the vector for positions between the entities, normalize both and then compare to see how close they are to each other.

 

Now, if you're not familiar with vectors in math or minecraft you might need some explanation. A vector is basically described by a point in 3d space, but it is relative to a 0, 0, 0 point. So imagine a vector as an "arrow" that points from 0, 0, 0 to x, y, z. That arrow will have a length and a direction. You can figure length and direction by using trigonometry (yay Pythagoras). If you want to compare directions it is easiest if you scale the length of the arrows to both be 1.0 which is called "normalizing".

 

Since the vector is contained in the Vec3d class, you can check to see what methods in that class might help you. Guess what, you'll find that there is a normalize() method, as well as getting the length, pitch and yaw. So one idea might be to compare the pitch and yaw of the your vectors and if they are both close consider it a match.

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

Posted
20 hours ago, jabelar said:

 

Yes. But that is your job. Modding is about coding new ways of doing things. 

 

So if you look at canEntityBeSeen() it calls a method called rayTraceBlocks(). If you look at the code for that method it is admittedly kinda complicated if you're new to modding, but there should be a few things that are interesting. For example, you'll see that you need to use vectors (Vec3d class) and loops to check positions in a direction.

 

Also, if you're lucky you might notice that right after the canEntityBeSeen() method is one called getLook() which returns a vector for the direction that the entity is looking. So the vector for behind could be opposite of that. 

 

So one idea that comes to mind would be to check if the vector for behind is close the the vector for the position between the entities. In other words, get the getLook() vector and invert it, calculate the vector for positions between the entities, normalize both and then compare to see how close they are to each other.

 

Now, if you're not familiar with vectors in math or minecraft you might need some explanation. A vector is basically described by a point in 3d space, but it is relative to a 0, 0, 0 point. So imagine a vector as an "arrow" that points from 0, 0, 0 to x, y, z. That arrow will have a length and a direction. You can figure length and direction by using trigonometry (yay Pythagoras). If you want to compare directions it is easiest if you scale the length of the arrows to both be 1.0 which is called "normalizing".

 

Since the vector is contained in the Vec3d class, you can check to see what methods in that class might help you. Guess what, you'll find that there is a normalize() method, as well as getting the length, pitch and yaw. So one idea might be to compare the pitch and yaw of the your vectors and if they are both close consider it a match.

 

Thanks for your reply. I have got the player and the entity look vec. So I wonder what to do next?

Posted
2 hours ago, lethinh said:

 

Thanks for your reply. I have got the player and the entity look vec. So I wonder what to do next?

You should start posting your code so people can give specific suggestions.

 

But basically the steps you need to figure out are:

1) Get a list of all the entities nearby within a range you care about. Check out the methods mentioned by someone else above in this thread for that.

2) Iterate through that list and:

     a) Create another vector from the entity to the player.

     b) Compare the pitch and yaw of the vector in (a) with the pitch and yaw of the player look vector. You won't want them to be exact matches, but depending on how accurate you want "behind you" to mean check for how close they are. For example, maybe you want to allow +/- 30 degrees. When these vectors are close enough, that means it is behind you.

 

Try coding each of the steps above, post your code, and people here will help you get it right!

 

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

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



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • When I first heard about Bitcoin back in 2018, I was skeptical. The idea of a decentralized, digital currency seemed too good to be true. But I was intrigued as I learned more about the technology behind it and its potential. I started small, investing just a few hundred dollars, dipping my toes into the cryptocurrency waters. At first, it was exhilarating to watch the value of my investment grow exponentially. I felt like I was part of the future, an early adopter of this revolutionary new asset. But that euphoria was short-lived. One day, I logged into my digital wallet only to find it empty - my Bitcoin had vanished without a trace. It turned out that the online exchange I had trusted had been hacked, and my funds were stolen. I was devastated, both financially and emotionally. All the potential I had seen in Bitcoin was tainted by the harsh reality that with decentralization came a lack of regulation and oversight. My hard-earned money was gone, lost to the ether of the digital world. This experience taught me a painful lesson about the price of trust in the uncharted territory of cryptocurrency. While the technology holds incredible promise, the risks can be catastrophic if you don't approach it with extreme caution. My Bitcoin investment gamble had failed, and I was left to pick up the pieces, wiser but poorer for having placed my faith in the wrong hands. My sincere appreciation goes to MUYERN TRUST HACKER. You are my hero in recovering my lost funds. Send a direct m a i l ( muyerntrusted ( @ ) mail-me ( . )c o m ) or message on whats app : + 1 ( 4-4-0 ) ( 3 -3 -5 ) ( 0-2-0-5 )
    • You could try posting a log (if there is no log at all, it may be the launcher you are using, the FAQ may have info on how to enable the log) as described in the FAQ, however this will probably need to be reported to/remedied by the mod author.
    • So me and a couple of friends are playing with a shitpost mod pack and one of the mods in the pack is corail tombstone and for some reason there is a problem with it, where on death to fire the player will get kicked out of the server and the tombstone will not spawn basically deleting an entire inventory, it doesn't matter what type of fire it is, whether it's from vanilla fire/lava, or from modded fire like ice&fire/lycanites and it's common enough to where everyone on the server has experienced at least once or twice and it doesn't give any crash log. a solution to this would be much appreciated thank you!
    • It is 1.12.2 - I have no idea if there is a 1.12 pack
  • Topics

×
×
  • Create New...

Important Information

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