I am making a mod and messing with something, and well, I think someone with a math degree would get it done quicker and better than I am. Maybe this is your thing so, if it is, I'm putting it out here...
So the player is standing there. He can rotate on his Z axis in 360 degrees (looking around from left to right), or he can pivot up and down to an extent, which is his X axis. When he is not pivotting up or down, just looking straight ahead, his X axis is zero. When he looks up, the X axis decreases. When he looks down, the X axis increases. The maximum that he can pivot on that X axis is around -90 and +90.
So, there is a long-ass light beam whose base is attached to his face by my pre-exsting script function. As he runs around, it stays on his face. The code that makes the light beam's base stay in front of the player's face is already done. Not a big deal.
This light beam is a particular pain in the ass because it does not appear in the world on exactly the player's same axis. It appears slanted at, I guess, 45 degrees both up and down, and sideways, from the player. When you query the light beam's X and Y axes, it will tell you 0 and 0, but when in that position it does not match up to the player when he's in a 0 and 0 set of angles.
My goal is to make the light beam stick out of the player's face, straight ahead of him, no matter which way he spins or looks. Like: a spotlight.
I found a way to do exactly HALF of the job. I have made the light beam pivot perfectly from side to side, like a big fat headlight on a car, when the player pivots on his Z axis, but it's got a catch. I have to lock the light beam's X angle at 90. and I am only changing the Y one. So, my intended spotlight is more of a ... headlight... like that, because the player can look up and down but the light will always be locked straight ahead of him.
The code which created the perfect pivot behavior of this light, while keeping it sticking straight out from his face and not up or down, is this:
if PlayerAngleZ == 180 set LightY to 0 elseif PlayerAngleZ == 0 set LightY to 180 elseif PlayerAngleZ < 180 set LightY to 180 - PlayerAngleZ else set LightY to 360 - (PlayerAngleZ - 180) endif PhalanxSpotlightBeamREF.setangle y LightY PhalanxSpotlightBeamREF.setangle x 90
Trying to use the above code in a way which also adjusts the X axis of the light beam to change with the player's X is kinda rough.
I have found that its necessary to change both the X and Y coordinates in a smooth sorta way to get the perfect thing I want.
I divided the player's Z axis into 4 quadrants, and then, I observed just what the light beam had to do to be correct throughout those quadrants. I took notes with the player's Z at 0, 90. 180, and 270. Those notes went like this:
With the player's Z angle at zero:with his X angle set to 0, he is facing straight. The correct X angle for the beam is 90, and its Y angle is 180.with his X angle set to -45, he is pointing up. The correct X angle for the beam is 45 and the Y angle is still 180with his X angle set to 45 he is pointing down. The correct X angle for the beam is -45, and the Y angle is 0.With the player's Z at 90:with his X set to 0, he is facing forward, the beam's X is 0, Y is 90With his X set to -45 (pointing up), the beam's X is 0, Y is 135.With his X set to 45 (pointing down), the beam's X is 0, Y is 45.With the player's Z at 180With his X set to 0, he is facing forward, the beam's X is 90, Y is 0.With his X set to -45 (pointing up), the X is 135, the Y is 0.With his X set to 45 (pointing down), the X is 45, the y is 0with the player's Z at 270:With the player's X at 0, he is facing forward, the beam's x is 0, y is 270.With the player's X at -45 (facing up), X is 0, Y is 225With the player's X at 45 (facing down), X is 0, Y is 315
That pretty much lays out a roadmap for what I need to do in my code. BUT, anything I do with this is going to be explicitly coded for each of the 4 quadrants of the player's Z, and within each one of those, I'd be using arithmatic for the beam's two possible X pivot directions. Because... I svck at arithmatic less than I svck at trigonometry and this looks like trigonometry to me.
So! If anyone with better math/trigonometry background than me would be interested in coming up with some class-act code that does this... it would be stupendous! I have spent some time on doing it the arithmatic-sort-of-way and, well, its DOABLE but I mean... its kinda goofy. I bet there are trigonometry functions that can do this, and the right person can put the code out there for it.
The final thing that this will be is a high-powered spotlight. It is going to be a very good spotlight. It uses the mesmetron weapon as its handheld object and will look natural and correct when its done. I'll probably release it as stand-alone but also Phalanx needs it. If someone came up with the part of the code that deals with this lovely math issue, I'd be happy to put them in the mod credits with me.