Sign in to follow this  
Shakedown

How do I get missiles to follow a curve?

Recommended Posts

Shakedown    230
Hey guys, I tried google'ing for something but I didn't really know what I was looking for. I'm making a space invaders clone (2D) and I'd like to add a secondary weapon to the players ship: heat-seeking missiles. The player uses the mouse to select an enemy, and then fires off some missiles. Using the Observer pattern, I'm having the missiles observe the user-clicked enemy ship. The enemy ships will continue to move about the screen as the missiles approach them. My problem is that I don't want the missiles to just shoot straight out and travel in some odd looking path towards these enemy ships. To be precise, when the user clicks the appropriate mouse button, I want to launch 2 missiles at the same target. I'd like them to shoot out the side of the player ship sideways a little bit, then blast forward (up) towards the enemy. So, I'm imagining that I need some sort of curve/path to do this. Unfortunately, I'm not familiar enough with graphical programming to know where to look. Can anybody provide some knowledge? Thank you.

Share this post


Link to post
Share on other sites
Mr_Threepwood    150
Couldn't you just tweak the missiles x velocity to make one shoot to the left, and one to the right of the ship, then as they travel increase/decrease the x velocity every few cycles to that it tracks the ship it targeted?

If you want to make the missiles always move at the exact same velocity at all times you will have to use trigonometry to get it exactly right, but it may not matter as far as the functionality goes.

Share this post


Link to post
Share on other sites
Shakedown    230
Quote:
Original post by Mr_Threepwood
Couldn't you just tweak the missiles x velocity to make one shoot to the left, and one to the right of the ship, then as they travel increase/decrease the x velocity every few cycles to that it tracks the ship it targeted?

If you want to make the missiles always move at the exact same velocity at all times you will have to use trigonometry to get it exactly right, but it may not matter as far as the functionality goes.


Do you mean something like

missile.setX(enemyShip.getX());

every cycle through the game loop?

I don't see why not actually...I'll try this out. Still, if anybody has any information on what I'm talking about, it would be great to hear it. Thanks

Share this post


Link to post
Share on other sites
Zahlman    1682
To do homing missiles, you can apply an acceleration every frame in the direction of the target. (Thrown objects "home in" on the Earth by virtue of gravity.) Alternatively, you can find the current direction, the direction towards the target, and rotate towards the target each frame. Or some combination; play around with it and find what looks best to you.

Quote:
Original post by Shakedown
Do you mean something like

missile.setX(enemyShip.getX());

every cycle through the game loop?


Why are you getting and setting positions like that? I really hope you're not making the tragic blunder of things like:


missile.setX(missile.getX() + dx);
missile.setY(missile.getY() + dy);


This is exactly missing the point of this whole OO business. You want the missile to do something which has a name:


missile.move(dx, dy);

void Missile::move(int dx, int dy) {
x += dx;
y += dy;
}


This way, tasks are given to the objects that are most able to do them (because they're the ones with the best access to the required information). That's how we organize code.

Share this post


Link to post
Share on other sites
stonemetal    288
What you are looking for is called flocking. That should at least land you in the right direction. It doesn't all apply but it should help you find something interesting.

Share this post


Link to post
Share on other sites
__ODIN__    479
You're looking for a simple steering routine, I suspect.

The basic idea is that you have a Speed (vec2) and a current rotation (float). Each frame you apply a force (Vec2 Acceleration) along the axis of the rocket (so it can only accelerate straight forward), and rotate the rocket a little to align it with the target (either by using a dot-product, or by calculating the actual angle with atan2). That'll curve it the way you want.

Allan

Share this post


Link to post
Share on other sites
yehdev_cc    124
Suppose you have your ship at (x, y)
And your target at (x1, y1)
and you have a misssile of velocity V, which has two components(Vx, Vy) and has initial position say (x2, y2)
and suppose you have your missile sprite(2d game) to be horizontal (-->)
Then for 'every' frame update, do the following..

if x1 = x2
angle = 90 degree
else
slope = (y2 - y1) / (x2 - x1)
angle = atan(slope) //Tan Inverse
endif

Vx = V * Cos(angle)
Vy = V * Sin(angle)

x2 += Vx
y2 += Vy

Rotate your sprite to 'angle'
Then render it


Share this post


Link to post
Share on other sites
Shakedown    230
Quote:
Original post by Zahlman

Why are you getting and setting positions like that? I really hope you're not making the tragic blunder of things like:

missile.setX(missile.getX() + dx);
missile.setY(missile.getY() + dy);


No, I'm not actually performing missile.setX(enemyShip.getX());, I just used code to clarify the idea of setting the missiles x-value to the enemy ship's x-value.

Quote:
Original post by yehdev_cc

Suppose you have your ship at (x, y)
And your target at (x1, y1)
and you have a misssile of velocity V, which has two components(Vx, Vy) and has initial position say (x2, y2)
and suppose you have your missile sprite(2d game) to be horizontal (-->)
Then for 'every' frame update, do the following..

if x1 = x2
angle = 90 degree
else
slope = (y2 - y1) / (x2 - x1)
angle = atan(slope) //Tan Inverse
endif

Vx = V * Cos(angle)
Vy = V * Sin(angle)

x2 += Vx
y2 += Vy

Rotate your sprite to 'angle'
Then render it


If I implemented this, I wouldn't have to calculate any paths,curves or splines for the missile to directly follow would I?

Share this post


Link to post
Share on other sites
yehdev_cc    124
Quote:
If I implemented this, I wouldn't have to calculate any paths,curves or splines for the missile to directly follow would I?


No, you wouldn't, and this shouldn't be a problem...after all why do yo need a curve or a path ? your missile is a 'Heat seeking' one so, it doesn't expect the final position of the target when it reaches it so that it can go on a straight line, specially if your target doesn't follow a straight or predefined path..
Instead, your missile should head to the target and correct its direction every while to make sure it's going to get it...
I think this implementation should work good for your case...

Share this post


Link to post
Share on other sites
werekarg    193
yehdev_cc, your code has a little flaw. it might be possible for the missile to rotate around the target (== never reach the destination) if its velocity is high and the steering speed is low. this would require manual tweaking of the velocity/steering speed and even so you're not entirely free of the error ever occuring.

rant: of course, you may hook up variable missile velocity and variable missile steering speed in those equations if you want realism, but i guess it will take you quickly to rocket science [wink] in reality, SAM and other explosive devices are equipped with proximity fuzes, so trajectory errors are somehow compensated but i dont know how this will fit into the op game design.

there would be another solution which involves curves. i'm thinking of a quadratic bezier curve, for example here. at each step, you compute the curve parameters, choosing P0 as the missile coordinate, P1 in the current direction (orientation) of the missile (at a distance of current missile velocity * several time delta) and P2 as the target point; then advance the missile to the next position and make sure to also compute its direction as tangent.

and maybe opensteer might help you.

Share this post


Link to post
Share on other sites
yehdev_cc    124
werekarg, thank you for the explosive informatoin [grin]

Well, my code doesn't even have steering velocity..the missle will just go for the target correcting it's direction instantly as needed...

To implement steering velocity, this shouldn't also be a problem and I think you wouldn't need Bezier curves or others...you'll just need to correct your velocity(the direction) a bit every frame(instead of instatnly) according to your steering velocity, and just keep the missle looking for its target until it hits it or just until its fuel runs out...

The case you're talking about is likely to happen also if the speed of the missle is much higher than that of the target and the initial direction of the missle is kinda away from the missle...

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this