Trigonometry Problem
Posted: Mon Feb 29, 2016 4:21 pm
Well, first of all, I'm not English fluent so sorry for any misspell or something like that. I'm actually Brazilian.
I'm developing a game (for android) in which a space ship can turn and shot in any direction. My first solution for doing this was a movement pad at the left and turning buttons at the right near to the fire button, like this:
The print was took from it running on my pc, in the phone the controls look smaller
Well, some of my testers (read close friends with nothing better to do) complained about the difficult to turn and shot at the same time (I did not found it that hard but everyone that tested complained) so I choose to change it. Then I decided that the ship should aim the same direction that the pad is making it move (the force applied to the ship is proportional to the distance of the touch from the center of the circle). So I have the x variation of the touch and the y variation, but I needed an angle that I could set to the ship. My solution was to use acos(x) function that give me an angle for the relation (XVariation / LineLength), but it gives only values between 0 and pi and, this way, my ship would never aim up.
My solution for that was: aways that the YVariation (TouchY - CenterOfTheCircleY) where negative I just multiplied acos(x) by -1. That worked but the ship movement got really ugly because I was instantly setting the angle of the movement to the ship, what means: if you move to the right and then touch the left it instantly aims to the left (like an angular teleport). I want the ship to choose the shorter direction and turn fast, but not instantly and I'm failing miserable to find a solution for that.
Is there any other way to get the angle from YVariation and XVariation that would not get stucked between 0 and pi (or -pi / 2 and pi / 2 for asin(x))? Because the problem is that the movement angle is only between 0 and pi but the angle of the ship should be able to be any value (if it only turns into the clockway it would always increase) so I can't compare then for choosing the shorter angular distance.
Please help me.
Thanks for reading.
I already löve you.
I'm developing a game (for android) in which a space ship can turn and shot in any direction. My first solution for doing this was a movement pad at the left and turning buttons at the right near to the fire button, like this:
The print was took from it running on my pc, in the phone the controls look smaller
Well, some of my testers (read close friends with nothing better to do) complained about the difficult to turn and shot at the same time (I did not found it that hard but everyone that tested complained) so I choose to change it. Then I decided that the ship should aim the same direction that the pad is making it move (the force applied to the ship is proportional to the distance of the touch from the center of the circle). So I have the x variation of the touch and the y variation, but I needed an angle that I could set to the ship. My solution was to use acos(x) function that give me an angle for the relation (XVariation / LineLength), but it gives only values between 0 and pi and, this way, my ship would never aim up.
My solution for that was: aways that the YVariation (TouchY - CenterOfTheCircleY) where negative I just multiplied acos(x) by -1. That worked but the ship movement got really ugly because I was instantly setting the angle of the movement to the ship, what means: if you move to the right and then touch the left it instantly aims to the left (like an angular teleport). I want the ship to choose the shorter direction and turn fast, but not instantly and I'm failing miserable to find a solution for that.
Is there any other way to get the angle from YVariation and XVariation that would not get stucked between 0 and pi (or -pi / 2 and pi / 2 for asin(x))? Because the problem is that the movement angle is only between 0 and pi but the angle of the ship should be able to be any value (if it only turns into the clockway it would always increase) so I can't compare then for choosing the shorter angular distance.
Please help me.
Thanks for reading.
I already löve you.