Topic category: Help with modding (Java Edition)
I've been working on a mod that adds various overlays for damage, potions, etc. One thing I'm working on is a system so that the direction of the overlay indicates where the damage is coming from. (To the left, back, right, above the player, etc.) The trick is I need to detect which overlay to display relative to the direction the player is looking. The simplest way is to just get the direction the player is looking at, (north, south, etc.), and then check the position of the attacker relative to the player's position, but this only works well if the player is looking one of the four cardinal directions. I've worked with look-vectors before, and I'm pretty confident I can solve it, (eventually), but if anyone's interested in doing a bit of geometry, the problem is such:
- I need to know whether an attacker is in front of, behind, to the right, or to the left of the player, relative to the direction they're facing.
- I have the positions of both entities, as well as the look angle x value and look angle z value of the player. Both of these values range from 1 to -1. For example:
- North; x = 0, z = -1
- East; x = 1, z = 0
- South; x = 0, z = 1
- West; x = -1, z = 0
I threw together a quick diagram to try to help answer this question.
It is probably worth mentioning that if you wanted to localize these directions relative to the player all you would need to do is subtract the angle of the player (atan2 using 1 block in front of the player instead of attacker pos) from the attacker angle, then make sure that the value is within the -180 to +180 range (you can do this by adding 360, running the new value through MOD with a value of 360, then subtracting 180).
Thanks, this is a big help; it was the relative angles part that was causing me trouble. (Also wound up finding out Minecraft does angles with negative values through trial and error- it makes sense in hindsight, but caused several headaches.) The good news is the thing works now, but the bad news is I'm now doing geometry in my free time for some reason...
Also, (incidentally), if anyone does need a system like this but doesn't want to mess with angles, you can also just use the coordinates of the target and attacker positions to roughly determine which of four/eight cardinal directions the attack is coming from depending on the difference between the two. It'll work for most purposes, but will run into problems determining which direction to choose from on diagonals and whatnot. (This is the essentially the earlier system I was using.)