I'm making a space shooter game that involves other ships attacking your ship. I want to make it so the other ship moves toward your ship in a straight line without exceeding its max speed. Here is an example:
Here is the screen, (0,0) is the top-left (800,600) is the bottom right.
_________________________
| | Object 1: enemy ship, moves
| (1) | to object 2. Has a max
| (2) | speed along the x axis of
| | 10 pixels per second, and
| | 3 p/s along the y axis.
| | Current Position: (10, 50)
| | Object 2: Your ship, Current
|_________________________| Position: (450, 200)
I need to know how to set the speed of the ship along the x and y axis without having the ship move more than 10 p/s along the x axis, and 3 p/s along the y axis, but ideally be moving its max speed along one of the axis’s, and the a speed along the other axis that can't exceed the max speed along that axis, but moves in a straight line. If it is not possible for the speed to be equal to the max speed along an axis, the max speed possible to be moving in a straight line.
</html>
Edited by - cybrgeek on September 28, 2001 1:40:20 PM
A vector from P1 to P2 is P2 - P1. It has a magnitude of sqrt((P2.x - P1.x)^2 + (P2.y - P1.y)^2). If you multiply that vector by 1/magnitude you get a unit vector, magnitude 1, pointing from P1 to P2. If instead you multiply by speed/magnitude you get a velocity vector with a magnitude of the speed. If the speed is in pixels per frame and you add the vector to the P1 you get a position in a straight line from P1 towards P2 at a distance you can move in a frame. If the speed is per second instead you have to multiply by the number of seconds which would be either 1/fps for a fixed frame rate or the elapsed times since the last frame for a variable frame rate.
You describe capping the movement to a rectangle around the ship or at least that is what I get from your description. Normally you are limited by a circle instead, i.e. a circle is the set of all points within a single plane equal distance from a point. If you actually want to cap it to a rectangle then you have to use sin and cos to calculate the velocity vector. atan2 will convert the x and y displacement from P1 to P2 to an angle. The x component will then be Speed*cos(angle) and the y component will be Speed*sin(angle). That is still going to give you a circle around the ship. You have to compare both of those values to the max in that direction. If it is greater than the max then you have to recalculate both using the max it exceeded. The other component may still be greater than the max in the other direction and if so then you recalculate with the other max. The recalculation is basically because if you extend the sides of the rectangle you can cross both lines. If you draw a circle around the ship representing where the max speed can take you and also draw a rectangle with the ship in the center with sides equal to twice the max along an axis then there are three possiblities. The first is that the circle just touchs the corners of the rectangle and the second is that it doesn''t reach the corner. In both of those cases you can only exceed one axis max. The third case is that the circle doesn''t touch the rectangle at all in which case you can exceed both. You can see that easily if you extend the sides of the rectangle. That divides the circle into eight arcs. Four of those exceed just one max while the other four exceed both.
Keys to success: Ability, ambition and opportunity.