amateur programmer 133 Report post Posted June 10, 2006 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. 0 Share this post Link to post Share on other sites
meeshoo 512 Report post Posted June 10, 2006 What do you mean by variable speed? Do you want a specific trajectory also? 0 Share this post Link to post Share on other sites
NIm 210 Report post Posted June 10, 2006 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. 0 Share this post Link to post Share on other sites
amateur programmer 133 Report post Posted June 11, 2006 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. 0 Share this post Link to post Share on other sites
Bob Janova 769 Report post Posted June 11, 2006 Well then, use a vector velocity: v = speed×(r_{player}-r_{launcher}); r's are position vectors. Each frame you just adjust the missile's position based on its velocity. 0 Share this post Link to post Share on other sites
amateur programmer 133 Report post Posted June 11, 2006 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. 0 Share this post Link to post Share on other sites
markr 1692 Report post Posted June 11, 2006 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 0 Share this post Link to post Share on other sites
amateur programmer 133 Report post Posted June 14, 2006 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?????? 0 Share this post Link to post Share on other sites
JohnBolton 1372 Report post Posted June 14, 2006 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 ); 0 Share this post Link to post Share on other sites
reana1 277 Report post Posted June 14, 2006 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.click1click2click3(Also use the search tool above - there should be more.) 0 Share this post Link to post Share on other sites
Palidine 1315 Report post Posted June 14, 2006 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 0 Share this post Link to post Share on other sites
amateur programmer 133 Report post Posted June 14, 2006 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. 0 Share this post Link to post Share on other sites
alvaro 21270 Report post Posted June 14, 2006 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. 0 Share this post Link to post Share on other sites
amateur programmer 133 Report post Posted June 15, 2006 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 wantVectors 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). 0 Share this post Link to post Share on other sites
reana1 277 Report post Posted June 15, 2006 A quick search on Yahoo gave me these sites.Check them out to learn more about vector math.http://chortle.ccsu.edu/VectorLessons/vectorIndex.htmlhttp://mathforum.org/~klotz/Vectors/vectors.htmlhttp://mathworld.wolfram.com/Vector.htmlFor 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.htmlAnother 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. 0 Share this post Link to post Share on other sites
Foobar of Integers 253 Report post Posted June 20, 2006 I had to do something like this making an object chase the mouse. Here's what I didgiven your object is at X, Y and the target is at X2, Y2Each 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. 0 Share this post Link to post Share on other sites