I'm having a little trouble translating a point in my game's world space to an object's local space.
I have a cannon in my game with a normalized heading vector that's always supposed to point in the direction of a target. The target is set by an incoming screen touch event.
My problem is that the coordinates I get from the incoming touch events are relative to the game world's axis centered at the world's origin, not relative to the cannon's position like I need. The angle I calculate to rotate the heading is relative to the world's X-axis, not the cannon's local X-axis, this means I can never rotate the heading by anything outside of 0 < x < 90.
I hope my crude drawing conveys what I mean. In the picture below, A is the cannon's position (with the blue heading vector), and B is the target's location (set by an event). The green line represents the angle the game is giving me, and the angle between red line and the heading is what I need to find (the little curved blue arrow).
Here's the code I use to calculate the heading:
float xDistance = target.x - position.x; float yDistance = target.y - position.y; float angleToTurn = (float) Math.atan2(yDistance, xDistance); vec2RotateAroundOrigin(heading, angleToTurn);
Thanks in advance. Any help is greatly appreciated.