Public Group

# How do I get missiles to follow a curve?

This topic is 3774 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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 on other sites
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 on other sites
If the missile is moving on a curve then you'll need to rotate it depending on the direction its facing to give a more realistic missile.

##### Share on other sites
Might want to look into bezier curves.

##### Share on other sites
Quote:
 Original post by Mr_ThreepwoodCouldn'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 on other sites
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 ShakedownDo you mean something likemissile.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 on other sites
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 on other sites
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 on other sites
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

Then render it

##### Share on other sites
Quote:
 Original post by ZahlmanWhy 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_ccSuppose 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 = x2angle = 90 degreeelseslope = (y2 - y1) / (x2 - x1)angle = atan(slope) //Tan InverseendifVx = V * Cos(angle)Vy = V * Sin(angle)x2 += Vxy2 += VyRotate 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?

• 17
• 11
• 11
• 9
• 49
• ### Forum Statistics

• Total Topics
631394
• Total Posts
2999753
×