Capping Speed on 2D Spaceship
So like just about everyone else learning some programming language, I've given myself a spaceship game project. So far, I have parallax stars and all the nice things I expect from a spaceship game. The ship stays in the center of the game screen, can rotate 360 degrees, can thrust and coast, and generally behave properly like an object in outer space. I am having a small issue, however, with capping the ship's speed.
The ship has a dx and dy value (which it translates to the stars through my update() function, so they do the actual moving), and it applies thrust on either the x or y axis, based on the angle it is facing. I give the ship a max speed (let's say 5) and an acceleration (0.1 per frame). When thrusting, the ship accelerates as I expect it to. The thrusters are always on 100%, no throttle control, since I'm using a digital input device (arrow key). I check the hypotenuse of the triangle made by the ship's current dx and dy to determine its current velocity, and I track its angle of travel, as well (for use in later AI; weapons fire; and when I press the down arrow, the ship uses this value to know what angle it needs to rotate to in order to face opposite its current trajectory -- a sort of help-you-brake-properly autopilot).
The ship has these variables which are updated whenever necessary (this is an abbreviated list, since the rest of the stuff isn't directly relevant):
x_speed
y_speed
speed
move_angle // Used to track direction of motion
face_angle // Used to determine x_accel and y_accel when thrusting
x_accel
y_accel
accel = 0.1 // Used to determine x_accel and y_accel by multiplying by the dx and dy returned by getDxDy(face_angle)
max_x
max_y
max_speed = 5 // Used to determine max_x and max_y by multiplying by the dx and dy returned by getDxDy(face_angle)
I check my dx and dy for acceleration based on the face_angle using this function:
function getDxDy(degree)
{
var dx;
var dy;
var degree = degree;
dx = -Math.sin(degToRad(degree));
dy = Math.cos(degToRad(degree));
var dxdy = {
dx: dx,
dy: dy
};
return dxdy;
}
I update the speed every frame of acceleration like so:
x_speed += x_accel
y_speed += y_accel
I have tried capping the speed with multiple different types of checks, and I have tried this:
if (x_speed > max_x)
{
x_speed = max_x
}
and I have tried this:
if (x_speed > max_x)
{
x_speed -= x_accel
}
The first version caps the speed as desired, but then I am either completely unable to modify my acceleration further, or when I try to reverse course by facing the opposite direction and thrusting, my speed instantly jumps to the opposite direction's max_speed (since the max_speed is constantly calculated based on the ship's current face_angle). If I change that calculation to the move_angle, then I run into problems when the ship is starting out, and not moving.
The second version has an issue if I开发者_高级运维 happen to stop thrusting before it gets the speed back below the threshold; I about-face and thrust, and instead, my thrust is negative, which causes me to accelerate farther in the direction I'm travelling, exactly opposite to the direction I'm facing.
What I would ideally like is to be able to cap the thrust at some value (5 for example) in any direction, and when I change directions by a few degrees, for the ship to wind up going in that direction after a few seconds of thrust, and when I about-face and thrust, I would like the ship to decelerate as one would expect.
If anyone's ever played the game Escape Velocity, that's the kind of movement and control system I'm working towards -- sort of my "Gold Standard," if you will.
Check out my current somewhat-working code at http://adrian.thomas-prestemon.com/game/v3/
The specific .js file dealing with this can be found at http://adrian.thomas-prestemon.com/game/v3/js/ships.js. Please excuse the mess of commented-out code and stuff. I've been working on this particular bit of the overall problem for a week now.
I have already examined multiple other solutions available online, and the ones most similar to my design use the first "if" example I provided above, which doesn't work for me. I have looked at the following StackOverflow Q&As as well:
2D Spaceship movement math
2d trajectory planning of a spaceship with physics
Programming a smooth change of thrust from current velocity vector to a target vector
All of these solutions seem quite fantastic, but they all seem a little complicated for my purposes. I do not plan on implementing any kind of gravity system, as in the first example, and the other two, while related, don't seem to provide anything of immediate use to my specific problem.
Any basic pseudocode and/or other kind of help anyone can provide -- such as pointing me directly to something else that answers my question, which I may have missed -- would be greatly appreciated. I'm looking for solutions that work with my current setup, but I am absolutely willing to change a little bit of how my ship's movement code functions if it will make this max_speed checking easier.
Thank you for taking the time to read all of this. :)
UPDATE:
I have made some modifications to my code and I got it working much better than it once did. It's not perfect, but some more experimentation, tweaking, and debugging is all it should take. :)
If I change that calculation to the move_angle, then I run into problems when the ship is starting out, and not moving.
Initialize your dx
, dy
, x_speed
, and y_speed
values to 0, and have the x_speed
and y_speed
values determine the move angle rather than keeping move_angle
separate.
Or, if I've misunderstood your setup, just start off with speed
set to 0, and do calculations that don't involve trigonometry when the speed value is below a certain cutoff.
Or you could have most of the velocity and acceleration calculations operate on polar values and just convert to Cartesian coordinates when it's time to display to the screen.
Sorry if I'm semi-incoherent, it's the slump-time of the day for me and I'd rather be taking a nap right now than sitting at a desk.
精彩评论