3 Replies - 769 Views - Last Post: 31 December 2012 - 12:08 PM Rate Topic: -----

#1 mystra  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 31-December 12

making a 2D bullet fire towards the X and Y position of the mouse?

Posted 31 December 2012 - 06:56 AM

Hi guys,
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[1].pos.x -= (((newPos.x))*0.02);
playerMissiles[1].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 have checked through numerous website and forums for people with similar problems to me but I have not found any good results. I have considered using angles as well but I thought that would simply be over complicating the problem.

I would be so grateful if anyone can provide any sort of assistance :)/>

Is This A Good Question/Topic? 0
  • +

Replies To: making a 2D bullet fire towards the X and Y position of the mouse?

#2 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: -4
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: making a 2D bullet fire towards the X and Y position of the mouse?

Posted 31 December 2012 - 07:48 AM

Hi

Thank you for the detailed post.

1. Constructors create objects, not operations on those objects. More of a convention thing than anything else. Get the mouse position in an init method instead.

2. You need to look into vectors, matrices and the dot product. This will allow you to manipulated missile orientation.
Was This Post Helpful? 0
  • +
  • -

#3 mystra  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 31-December 12

Re: making a 2D bullet fire towards the X and Y position of the mouse?

Posted 31 December 2012 - 09:49 AM

Ok, I'm still pretty stumped but here is the logic I am attempting to implement now:

If I have the X and Y for the mouse in the form of
newPos.x

and
newPos.y


and the X and Y of the missile in the form of
playerMissiles[1].pos.x

and
playerMissiles[1].pos.y


I was thinking I could make a new vector storing the starting position of the missile and then do something along the lines of

playerMissiles[1].pos.x += ((starting.x - pos.x)*0.01) 
and something similar for the y axis to make it move towards pos.x and pos.y and eventually equal the same thing essentially reaching the target. Does that sound feasible?
Was This Post Helpful? 0
  • +
  • -

#4 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: -4
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: making a 2D bullet fire towards the X and Y position of the mouse?

Posted 31 December 2012 - 12:08 PM

I don't know how much you've looked into vectors, but all vectors have a head and a tail with these two points joined by a straight line. You also need to get to grips with some calculus.

Your starting x and y positions are where your rocket is placed - this is the tail of your vector. The head of the vector is where you want it to end up; the x and y positions of you target.

As your rocket moves (or more accurately 'translates' along the length of the vector) you will need a function of time that will control the speed at which the rocket travels along said vector.

You really need to read up on this.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1