(C++) Creating Homing Missiles

Started by
9 comments, last by Side Winder 16 years, 12 months ago
I'm currently creating an Asteroids game and I want the UFO to be particularly hard on the player, like having extra "lives", firing homing missiles etc. I want the homing missiles to always go after the player and I have an idea on how this can be done... I'll have an update function for the missile which updates the Vector towards the ship every cycle. But how would I calculate the maths for this?
Advertisement
Also, in the game I have a function to check whether a unit goes off the edge of the screen so it'll come back to the other side... This works for the player ship and the bullets, but not for the UFO or any of the asteroids. I'm assuming it's something to do with the velocity being negative but can someone shed some light on this please?
How do these objects vary ?

My initial thought would be to have a class for all objects, since all your objects have the same properties pretty much.

All have velocity/speed, direction, all can enter/exit screen, all explode/destroy, rate of fire, range of fire, lives etc etc.

So I would create a generic 'object' class and use it for everything.

----------------------------------------Now just hit that link that says 'Rate This User' [wink]
gpwiki.org has a tutorial on missile design under the 'programming techniques' section.
Quote:Original post by darren_mfuk
How do these objects vary ?

My initial thought would be to have a class for all objects, since all your objects have the same properties pretty much.

All have velocity/speed, direction, all can enter/exit screen, all explode/destroy, rate of fire, range of fire, lives etc etc.

So I would create a generic 'object' class and use it for everything.


Yeah, inheritance would've been helpful but when I started this project, I didn't know much about it and haven't bothered to fix it on this particular game...

And thanks for the gpwiki article, it's helpful.

I still don't know how to fix this "edge-of-screen" problem, however.
I've looked at the gpwiki article and I've implemented the code. It mostly works. I am, however, having a problem with the speed of the missile. Because the speed is based on how far it is from the object, it'll move slower when it gets nearer which isn't what i want. I want it to move at a fixed speed. This is the code I've got right now for the movement:

MissileDirection.x = pMiddleX - MissilePosition.x;MissileDirection.y = pMiddleY - MissilePosition.y;


And then in the other function:

MissilePosition.x = MissilePosition.x + MissileDirection.x * MissileVelocity.x;MissilePosition.y = MissilePosition.y + MissileDirection.y * MissileVelocity.y;


I've had to set velocity to a ridiculously low amount like 0.05 for the missile not to be moving at light speeds...
You could normalize your vector, and then multiply it by any velocity you want.
To normalize, find d = sqrt(vel.x^2 + vel.y^2 + vel.z^2) and divide each component with d. Then multiply each component with your velocity.
if ( youreHappyAndYouKnowIt ) clapYourHands;
y = mx + b

8)
Sure is a big 'ol world.
Yeah but what's wrong with the actual code?? I don't really know what you mean by normalising the vectors, or using y = mx + b. I know of them, but why should I use them in this? What do they do??
The reason your missiles are becoming slower as they get nearer is this:
    MissileDirection.x = pMiddleX - MissilePosition.x;    MissileDirection.y = pMiddleY - MissilePosition.y;

Here you are calculating your MissileDirection vector, with an x and y component. Imagine your missile being very far from the player's ship; the x and y will be very large, but as the missile comes closer to the player's ship the MissileDirection x and y will become smaller (try to visualize this in your head).

Now, look at your next piece of code:
    MissilePosition.x = MissilePosition.x + MissileDirection.x * MissileVelocity.x;    MissilePosition.y = MissilePosition.y + MissileDirection.y * MissileVelocity.y;

Here you are applying your 'MissileDirection' vector to calculate the new position of the missile. As you can see, if your MissileDirectio x and y is large the missile will move a lot (moving quickly) toward the player's ship, but if the MissileDirection x and y is small it will only move a little bit (moving slowly) toward the player's ship.

So you can see that your MissileDirection vector is large when the missile is far from the player's ship, and small when the missile is close to the player's ship. Since the purpose of the MissileDirection vector is JUST to tell the missile which direction the player's ship is in (and it should not tell how far the player's ship is away), we should "normalize" the MissileDirection vector so that it ALWAYS has a length of 1, where length of the vector is calculated as length = sqrt(x*x + y*y), or if you have 3 dimentions, use length = sqrt(x*x + y*y + z*z).

To actually normalize your MissileDirection vector, you must divide each component by this magnitude. So in the end, you could create a "normalize" function that does all this for you. Define a function something like this (where Vector2d is the data type of your 'MissileDirection' variable):
Vector2d normalize2d(Vector2d direction) {    int magnitude = sqrt(direction.x ^ 2 + direction.y ^ 2);    direction.x   = direction.x / magnitude;    direction.y   = direction.y / magnitude;    return direction;}

...And when you're calculating your MissileDirection, you will want to use the normalize function:
    MissileDirection.x = pMiddleX - MissilePosition.x;    MissileDirection.y = pMiddleY - MissilePosition.y;    MissileDirection = normalize (MissileDirection);

Now that your MissileDirection vector is normalized, it will tell you the direction of the player's ship, and its magnitude will always be 1 no matter how far away it is from the player. This means that the speed will be constant the whole way to the player's ship.

Hope this helps
-Matt

This topic is closed to new replies.

Advertisement