Jump to content

How to check if entity is behind, in front of, leftward or rightward other entity?


lethinh

Recommended Posts

I am wondering how to check if entity is behind, in front of, leftward or rightward other entity. Currently, I tried posX, posZ of two entities to check but it doesn't seem to be correct. Please help me. Thanks a lot!

Edited by lethinh
Link to comment
Share on other sites

8 hours ago, jabelar said:

Well, obviously it should have to do with position (posX, posZ) so show what you coded so far.

I tried to compare two entities' posX and posZ whether it is equal, greater or less, but I don't know which of them will check if entity is behind, in front of, leftward or rightward other entity

Edited by lethinh
Link to comment
Share on other sites

You need to use vector math. And the entity's LookVec

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

It's called vector math. "Vec3d" stands for "Vector (size 3, type double)" and contains 3 doubles.

Vectors can represent a position in space (like how a BlockPos does) or it can represent a direction (which is what LookVec does). Adding the two together can let you figure out where an entity is looking.

 

Which means you can use that same information to figure out if one entity is in front of another or not.

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

Eh, fuck it. I can't be bothered to work out the math myself.

Have this instead

 

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

17 minutes ago, Draco18s said:

Eh, fuck it. I can't be bothered to work out the math myself.

Have this instead

 

Thank you. Is it like this: entity.getPositionVector().dotProduct(otherEntity.getLookVec()) == -1 I want to check if the original entity is behind other entity

Link to comment
Share on other sites

You probably want something more like dotProd < -0.8

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

Okay, so dot product basically represents the "amount" of a vector is in same direction as another vector. In this particular case of trying to find out if one entity is behind another, you need to be very careful about which two vectors exactly you're using. As Draco18s mentioned, a "vector" can be used to indicate a position or a direction (really a relative position).

 

So one vector of course should be the look vector (make sure it is look vector from the correct entity). But the other vector must be constructed in the right way. It needs to be the vector going from the position of the entity that is "sneaking up behind" to the entity that is looking. 

 

Furthermore, you mentioned normalization. This is important two. You need both vectors to be normalized, meaning that the length of the vector is 1.0. I believe the look vector should already be normalized, but for the second vector you'll want to normalize it. I believe Vec3d has method for normalizing.

 

So basically I'm saying that the getPostionVector() part of your code is probably wrong. You don't want the position of either entity, you want the vector the represents the difference in position BETWEEN them, and you need to consider the direction otherwise the dot product will be negative what you expect/

 

I didn't test this, but (otherEntity.getPositionVector().subtract(entity.getPositionVector()).normalize().dotProduct(otherEntity.getLookVec()) < -0.8.

 

The reason why Draco18s and I are recommending a value of 0.8 is that the concept of "behind" isn't really exact. So this would mean that the direction is mostly (80%) overlapping the negative direction. 

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

Link to comment
Share on other sites

16 minutes ago, lethinh said:

Is it like so: boolean behind = entity.getLookVec().dotProduct(otherEntity.getLookVec()) < -0.8D; ?

No. That would only tell you that both entities are looking in opposite directions, no matter where they are positioned.

 

I gave you an example of the code in my previous comment. You need to subtract the positions and normalize the result and then dot product that.

 

Actually though you also need to clarify something. You asked about figuring out if an entity is "behind". That can actually mean a few different things. For example, the look vector is actually the look vector based on the head position. So if for example you really wanted to make sure it was behind the back (i.e. based on body rotation instead of head) you might want to do it a bit differently.

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.