lethinh Posted January 27, 2018 Share Posted January 27, 2018 (edited) 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 January 27, 2018 by lethinh Quote Link to comment Share on other sites More sharing options...
jabelar Posted January 27, 2018 Share Posted January 27, 2018 Well, obviously it should have to do with position (posX, posZ) so show what you coded so far. Quote Check out my tutorials here: http://jabelarminecraft.blogspot.com/ Link to comment Share on other sites More sharing options...
lethinh Posted January 28, 2018 Author Share Posted January 28, 2018 (edited) 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 January 28, 2018 by lethinh Quote Link to comment Share on other sites More sharing options...
Draco18s Posted January 28, 2018 Share Posted January 28, 2018 You need to use vector math. And the entity's LookVec Quote 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 More sharing options...
lethinh Posted January 28, 2018 Author Share Posted January 28, 2018 Vector? You mean Vec3d? And by the way how to check using vector and the entity's LookVec? (normalizing?) Quote Link to comment Share on other sites More sharing options...
Draco18s Posted January 28, 2018 Share Posted January 28, 2018 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. Quote 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 More sharing options...
lethinh Posted January 28, 2018 Author Share Posted January 28, 2018 43 minutes ago, Draco18s said: same Vec3d direction = entity.getPositionVector().add(entity.getLookVec()); And after that, I wonder what to do next? Quote Link to comment Share on other sites More sharing options...
Draco18s Posted January 28, 2018 Share Posted January 28, 2018 Eh, fuck it. I can't be bothered to work out the math myself. Have this instead Quote 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 More sharing options...
lethinh Posted January 28, 2018 Author Share Posted January 28, 2018 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 Quote Link to comment Share on other sites More sharing options...
Draco18s Posted January 28, 2018 Share Posted January 28, 2018 You probably want something more like dotProd < -0.8 Quote 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 More sharing options...
jabelar Posted January 28, 2018 Share Posted January 28, 2018 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. Quote Check out my tutorials here: http://jabelarminecraft.blogspot.com/ Link to comment Share on other sites More sharing options...
lethinh Posted January 28, 2018 Author Share Posted January 28, 2018 1 hour ago, Draco18s said: You probably want something more like dotProd < -0.8 Is it like so: boolean behind = entity.getLookVec().dotProduct(otherEntity.getLookVec()) < -0.8D; ? Quote Link to comment Share on other sites More sharing options...
jabelar Posted January 28, 2018 Share Posted January 28, 2018 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. Quote Check out my tutorials here: http://jabelarminecraft.blogspot.com/ Link to comment Share on other sites More sharing options...
Recommended Posts
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.