# Guided missle

## Recommended Posts

I want to be able to fire a missle from x1 y1 so that it hits x2 y2. But I want it to have it's speed as a variable. I am using SDL to paint my missle onto the screen. I'm not sure how I would do that. Something to do with good old trigometry which i cant remeber any of. I want to be able to dodge this thing.

##### Share on other sites
What do you mean by variable speed? Do you want a specific trajectory also?

##### Share on other sites
Let me see if I've got a good idea of what you are doing.

You want a missile. which will be launched at some finite speed. once launched, it will have inertia. It will also have a direction that it is facing, which because of the inertia, will be independant of the direction it is moving in. It will accelerate constantly in the direction it is facing, except when it is facing away form the enemy. It will always try to turn to face towards the enemy. IT will explode on impact.

Have I got it right? I wnat to make sure I've got everything your thinking before I start spouting algorithms.

##### Share on other sites
Sorry guys didn't explain it well. Basically by variable speed i mean i can specify it within a variable. Reason is it cant be traveling too fast otherwise the player wont even have a chance to move before it hits him. It doesn't have an inertia sorry my fault. IT will attempt to calculate the angle which the missle should be fired at in order to hit the player. When it's fired it then travels in a straight line at calculated angle. It's speed is a constant value.

##### Share on other sites
Well then, use a vector velocity: v = speed×(rplayer-rlauncher); r's are position vectors. Each frame you just adjust the missile's position based on its velocity.

##### Share on other sites
Ok maybe am jumping the gun again lol. Only recently started game programming so I never heard of position vectors. How do I use/declare them Am using SDL as my API. Perhaps a tutorial on it because it's better than u just giving me the code. I dont learn anything that way.

##### Share on other sites
You need to read up on vectors in general. Then you'll know what a vector is, and what a position vector is.

Then you can figure out how to give the missile a velocity vector as well as a position vector, and use (in practice) euler integration to handle its motion. This will enable you to have a consistent model of motion between different objects in your game, which is a Good Thing.

Mark

##### Share on other sites
so say the result of the vector subtraction was [5,4,3] do I just multiply each number by speed.

Position vectors: The position vector of a point A is simply the vector from the origin O = (0,0,0) to point A = (Ax,Ay,Az). The position vector of point A is A = (Ax-0,Ay-0,Az-0) = (Ax,Ay,Az).

MY position vector will just contain x and y codinates of both enemy and the player right? But what about the direction? But basically it's a point on the screen am refering to. I thought a vector is a directed line with magnitude and direction. Am confused??????

##### Share on other sites
In mathematical terms, a vector can represent many things, including a position and a direction/magnitude.

There are a few ways of getting the result you are looking for. Here is one.

The angle of the direction that the missile travels can be found using the arctangent. Specifically,
    angle = atan2( y1-y0, x1-x0 );
The location of the missile at any time can be now be determined.
    mx = x0 + speed * time * cos( angle );    my = y0 + speed * time * sin( angle );
You can also do the following if you use the time since the last update instead of the overall time:
    mx += speed * time * cos( angle );    my += speed * time * sin( angle );

##### Share on other sites
There have been some topics on this in the past. Although, it sounds like you are going to work on the basics more first. Anyhow, when you are ready, you might read these for some reference material or ideas on guided missiles others have provided.
click1
click2
click3
(Also use the search tool above - there should be more.)

##### Share on other sites
Quote:
 Original post by amateur programmerI want to be able to dodge this thing.

Definitely follow those resource links. Math, specifically Trigonometry and Linear Algebra, is one of the most important things you need to know for game programming. _Everything_ you do with moving objects, collision detection, AI, physics involves at least trig and linear algebra (occasionally calculus). Learn them. Learn as much math as you can, it's the heart of programming. The more math you know, the easier programming of all kinds becomes. =)

-me

##### Share on other sites
thanks for the advice . You see the problem is am not very good at maths. I haven't touched it for over a yr so I've forgotten even the most basic maths. Despite this am hoping I will be able to make a decent space invaders game. Then in future I want to make a basic 1st person shooter. Like the old dos based ones we used to play. I'd love to make my own textures and sprites but am useless at drawing.

##### Share on other sites
Quote:
 Original post by JohnBoltonIn mathematical terms, a vector can represent many things, including a position and a direction/magnitude.There are a few ways of getting the result you are looking for. Here is one.The angle of the direction that the missile travels can be found using the arctangent. Specifically,  angle = atan2( y1-y0, x1-x0 ); The location of the missile at any time can be now be determined.  mx = x0 + speed * time * cos( angle ); my = y0 + speed * time * sin( angle ); You can also do the following if you use the time since the last update instead of the overall time:  mx += speed * time * cos( angle ); my += speed * time * sin( angle );

Although this is a natural way to do things, you can avoid using trigonometric functions in most cases. You just need to take the vector from (x0,y0) to (x1,y1) and multiply it by an appropriate number, so its length becomes the speed. This number is the desired speed divided by the distance between (x0,y0) and (x1,y1). You need to compute a square root (or 1 divided by the square root, actually), and all the other operations are really simple and fast.

##### Share on other sites
how do I take the vector between the two points? I would use pythagoras theorem to find out the distance between two points right?

is this what I want
Vectors between points: Two points in space, A=(Ax,Ay,Az), and B=(Bx,By,Bz) may be considered to define a vector. The vector, V from A to B is V = (Bx-Ax,By-Ay,Bz-Az).

##### Share on other sites
A quick search on Yahoo gave me these sites.
http://chortle.ccsu.edu/VectorLessons/vectorIndex.html
http://mathforum.org/~klotz/Vectors/vectors.html
http://mathworld.wolfram.com/Vector.html

For your current question, the following page illustrates it. What you had written is correct. The vector between two points is found by subtracting the two vectors. The one thing to be careful of is where the vector starts. To find the vector from A to B, it would be B-A = (Bx-Ax,By-Ay,Bz-Az). From B to A would be A-B = (Ax-Bx,Ay-By,Az-Bz).
http://mathforum.org/~klotz/Vectors/subtraction.html

Another thing that I sort of use as a memory aid is the fact that adding 2 vectors produces a third vector and these 3 vectors will form a triangle. So in that previous link, W + (V-W) = V. So you can follow the W vector and then the (V-W) vector and it meets the V vector. If you think of how this relates to an object's motion, it makes sense. If you were able to go in straight lines from your house to school and then from the school to the bank. You'd be at the same point if you went straight from home to the bank. You can see another example on the addition page at that site: http://mathforum.org/~klotz/Vectors/add.alg.html In this case, if you were to slide b's starting point up to the end of a, you'd see that the end of b now meets at the end of c forming a triangle.

##### Share on other sites
I had to do something like this making an object chase the mouse. Here's what I did

given your object is at X, Y and the target is at X2, Y2

Each frame I would find the slope of the line between (X, Y) and (X2, Y2) That was the object's new velocity. As it approaches the target point, the object automatically levels out and goes towards it, and it works fine if the target keeps changing. You can also tweak the math to make it so you can cap your object's velocity but still have it travel in the direction of the line.