Many thanks for coming back to me.
Context:
-----------
I am learning the computer programming language C++ and using a system called SFML to do the graphics. I am trying to write an improved version of the old Atari game
Asteroids. I use x,y coordinates to create the ship and the rocks. I use Sine and Cosine to calculate vector points from an origin. so,
for example: The ship has origin coordinates ox,oy. The radius of the ship is 20 pixels. The front point of the ship is calculated with the following.
nx = ox + sin(Angle) * 20;
ny = oy + cos(Angle) * 20;
Where nx and ny are the x,y coordinates of that point, ox and oy are the x,y coordinates of the centre of the ship, and Angle is the amount of rotation of the ship. With this system, I can then spin the ship around the central point. Sin and cos are the C++ functions for Sine and Cosine respectively. The asin, acos, tan and atan functions are also available. Using this technique, I can draw wire frame representations of the rocks and the ship. The gray frame shown here is simply for reference and is not drawn in the game.
Calculation:
---------------
With any computer game, there is a cost to calculation. Sine, Cosine and Tangent functions are particularly costly due to the number of calculations required to generate answers. I have largely mitigated this cost by calculating 360 values of Sine and Cosine (for degrees in a circle) and storing them in a table which is then referenced in the main game. Other computer functions will also take up time and so I am keen to reduce the number of lines of code to a minimum to keep the game speed.
Rocks:
--------
Each rock has a slightly different shape and this is calculated using random modifiers to a set structure. The rock is a seven sided shape. Each corner of the rock is calculated based on a radius defined for that corner which I have labelled rs[0] through rs[6] respectively. The structure may seem a little complex but the advantage is that the rocks can be spun. The effect is a great chunk of rock slowly spinning through space.
Question:
-------------
Missiles fired from the ship can impact a rock from any side. I need to calculate the angle for the point of impact so that the rock may be split at that point. I have some very clumsy code that sort of does this but thought there must be a better way. To simplify my calculations, I have set the rock origin to 0,0. Xa and Ya (from the first diagram) are the coordinates for the point of impact of the missile. My question is, how do I calculate how far round the rock the missile is, ie: the angle of impact.
Please let me know if you have any more questions. I am happy to share with you my code if that would help. Much appreciated.
Moon.