I need some help with a particular part of a C++ DirectX game. My course has tasked me with creating a game similar in look and function to the old missile command arcade game (something like this), however I have hit a bit of a wall in terms of getting the missile to fire in the direction I want it to. I'll preface this by saying I am more of a designer than a mathmatician/programmer... but I would like to improve my design skills by getting an understanding of what happens from a programming standpoint in order to make my game designs more feasable, so please forgive me in the likely event of me making a fundamentally rookie mistake >_<. With that out of the way here is what I have done so far.
I have created a missile class that has an instance in the game state. When the game state calls it in, depending on the key press it creates a missile at the X and Y position of the silo the missile is being fired from. That part I have got to work exactly how I want it to.
What I struggle with is the syntax regarding how to define the missile's trajectory. I have created a vector made up of the cursor's X and Y position
D3DXVECTOR2 newPos; //The above bit is declared within the missile class newPos = InputManager::Get().GetMousePos(); // that code is called within the constructor.
I have also done something similar for the missile's starting position and as far as I know, the declarations do their job as intended. Now for the part that stumps me... I'm not quite sure how to utilise this data to do what I want it to do.
In the update function of the missile class I have inserted a piece of code just to see if the vector actually works.
playerMissiles.pos.x -= (((newPos.x))*0.02); playerMissiles.pos.y += (((newPos.y))*0.02);
I know that pice of code is incorrectt however at least I know that it does actually move across the screen with it's direction affected by the position of the cursor, so I suppose the question is:
Using the x and y position of the mouse, how can I create a translation that updates pos.x and pos.y to always follow the mouse position?
I know the answer lies in something along the lines of pos.x+= (/something to calculate the x tracectory/) and pos.y+= (/something to calculate the y position/) but I just don't know what it is.
I have created a short animation below to demonstrate the effect I am after. Click here to see it just to get the jist of what I'm talking about.
I would be so grateful if anyone can provide any sort of assistance :)