Jump to content
  • Advertisement
Sign in to follow this  
Shakedown

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.

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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
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 this post


Link to post
Share on other sites
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
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
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
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
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
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!