Jump to content

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


lethinh

Recommended Posts

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/

Link to comment
Share on other sites

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 

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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/

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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/

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.