   homeing to a certain point
  okay so I have the mouse cursors x position and y position. I also have the position of a sprite. I want to have it so that when I tell the program to it will make the sprite to straight to the mouse cursor. I figured this would go here since it looked like math would be involved. does anyone know how with that information I could set the sprites x velocity and y velocity so it goes toward the cursor?   
  This is a very common thing to come across in programming, from my experience; you have, say, a ship or a tank, and you want the thing to move to a spot on a map or whatever depending on where the user clicked. Now, before you go any further with this, you have to determine for yourself at what speed the object is going to be moving at. If you have a class with all your object's information, add a "int speed" variable in there somewhere if you can, and then set its speed once you initialize it depending on the object's type (i.e. if boat, move at speed 2, if car, move at speed 10, etc.). But you've probably done that already, so I'll just get on to the math...You're going to have a 90 degree triangle whichever way you look at this thing, and for anyone that hasn't taken any trig classes, there are a lot of nice identities and things that are very useful in this sort of situation. To start, you need to find the distance between the two points; distance formula is sqrt((difference of x's squared) + (difference of y's squared)). Now, you need to determine the angle at which the the "goto" point is at relative to wherever the object is currently at. To do this, you can use either arcsin, arccos, or arctan, whichever you find easier, using either the x or y distance between the two points in conjunction with the hypotnuse of the triangle we've formed (the triangle, if you didn't picture it in your head already, is just a right triangle, the distance between the two points being the hypotnuse and then your two legs the x and y distance from the two points respectively).Anyway, assuming you have that angle and the distance, you can now start moving your object. To do this, you have to look at a new triangle formed by the velocity vector of your object, which is another right-triangle with the hypotnuse now being the speed you set earlier, and the angle between the hypotnuse and the horizontal leg the angle you also found earlier. Now, we do some more simple tricks with sin and cos to find the x and y displacement of the object's movement. Your x displacement will be (Speed * cos(angle)) and your y displacement will be (Speed * sin(angle)). And... that should give you what you want.You do have to be careful, though, when the movement starts getting into different cartesian quadrants, because then you'll start getting negative answers and stuff, and depending upon the coordinate system you've set in your own program, you may have to insert negative signs here or there to re-configure the movement values. Anyway, hope that helps.   ;Quote:Original post by Anonymous PosterYou do have to be careful, though, when the movement starts getting into different cartesian quadrants, because then you'll start getting negative answers and stuff, and depending upon the coordinate system you've set in your own program, you may have to insert negative signs here or there to re-configure the movement values. Anyway, hope that helps.Just wanted to add that in c++ you can use atan2() and avoid all these problems, as well as the various numerical issues associated with acos(), asin() and atan().   ;  And yeah, I also forgot to add that you'd probably want to throw in some code that checks at each pass how close to the target point the object is, and once it gets within a certain distance, stop, or blow up, or whatever. A problem you'll run into though, if you just try to say "if(object.x == point.x && object.y == point.y)" is that rarely will those x and y values be precise, since sin/cos/tan return double values (which are rounded), and if you're object is moving so fast that it overshoots the target point continuing at its present speed, then the aforementioned "stop-check code" wouldn't work there, either. How you want to go about solving that problem is up to you, but something simple you could do is check if the object were to move again, would it overshoot the target point? If so, then set the object's position equal to the target point and stop the object. And yeah, another variable like "bool moving" in your class would be useful for that.   ;  Trig? TRIG? in this forum? Burn the witch! ;)start with: mouse.x, mouse.y, sprite.pos.x, sprite.pos.ywe want sprite.vel.x and sprite.vel.y, yes?so here we go.  There are only 4 steps here (7 if you count doing the same thing with x and y as 2 steps).  There is no explanation, but read the variable names and follow each one and it should be blindingly clear.distance.x = mouse.x - sprite.pos.xdistance.y = mouse.y - sprite.pos.ytotal_distance = sqrt(distance.x * distance.x + distance.y * distance.y)direction.x = distance.x / total_distancedirection.y = distance.y / total_distancesprite.vel.x = direction.x * sprite.speedsprite.vel.y = direction.y * sprite.speedNow look again... no angles, no quadrants, no weird negatives or atan2... one square root and then it's just add and multiply all the way.   ;Quote:Original post by SquirmNow look again... no angles, no quadrants, no weird negatives or atan2...That may be an adequate solution for the OP's problem, but keep in mind that it's often useful to have the angle itself rather than a simple direction vector (for example, if you want an object to turn toward a target gradually rather than just head straight for it).   ;Quote:Original post by jykQuote:Original post by SquirmNow look again... no angles, no quadrants, no weird negatives or atan2...That may be an adequate solution for the OP's problem, but keep in mind that it's often useful to have the angle itself rather than a simple direction vector (for example, if you want an object to turn toward a target gradually rather than just head straight for it).When I want an object to turn, I repeatedly add a tiny perpendicular fraction of the target direction vector onto the object's current forward vector and re-normalize. Can limit the length added to adjust speed of rotation.Still no angles.   ;Quote:When I want an object to turn, I repeatedly add a tiny perpendicular fraction of the target direction vector onto the object's current forward vector and re-normalize. Can limit the length added to adjust speed of rotation.Still no angles.Hehe, well, I'm not on an 'angle crusade' or anything :-) I guess I'm just not sure why the OP should be discouraged from using them.   ;Quote:Original post by jykQuote:When I want an object to turn, I repeatedly add a tiny perpendicular fraction of the target direction vector onto the object's current forward vector and re-normalize. Can limit the length added to adjust speed of rotation.Still no angles.Hehe, well, I'm not on an 'angle crusade' or anything :-) I guess I'm just not sure why the OP should be discouraged from using them.Sure, sure... we can accept that you're a closet anglophile [wink][wink];Quote:Original post by jjdSure, sure... we can accept that you're a closet anglophile [wink][wink]:-)   