Like many others diving into the world of programming, I decided to challenge myself with a spaceship game project. At this point, I have successfully incorporated parallax stars and other essential features expected in a space-themed game. The spacecraft remains centered on the screen, capable of rotating 360 degrees, accelerating, coasting, and moving around like an object in outer space should. However, one small issue persists – capping the ship's speed.
The ship operates with dx and dy values that translate its movement through my update() function. It applies thrust along either the x or y axis depending on the current angle it faces. I have set a maximum speed (let’s say 5) and an acceleration rate (0.1 per frame). When thrusting, the ship accelerates as anticipated. Since I am using digital input controls (arrow keys), the thrusters are always at full power without throttle control. To determine the ship's velocity, I check the hypotenuse of the triangle created by its current dx and dy, and I also track its travel angle for future AI implementation, weapons firing, and autopilot-like braking when pressing the down arrow.
Some of the key variables governing the ship's movement include:
x_speed
y_speed
speed
move_angle // Tracks motion direction
face_angle // Determines x_accel and y_accel during thrusting
x_accel
y_accel
accel = 0.1 // Multiplied by the dx and dy from getDxDy(face_angle)
max_x
max_y
max_speed = 5 // Multiplied by the dx and dy from getDxDy(face_angle)
I employ the following function to obtain the dx and dy based on the face_angle:
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;
}
Each frame, I update the speed with acceleration as follows:
x_speed += x_accel
y_speed += y_accel
I attempted various methods to cap the speed and experimented with checks such as:
if (x_speed > max_x)
{
x_speed = max_x
}
and:
if (x_speed > max_x)
{
x_speed -= x_accel
}
The initial approach succeeded in capping the speed but restricted further acceleration modifications or caused sudden reversals in speed when changing directions due to constant recalculation of max_speed based on the ship's face_angle. The second method resulted in unwanted negative thrust upon stopping, leading to unintended acceleration in the opposite direction.
My goal is to limit thrust to a specific value (e.g., 5) regardless of direction and ensure that the ship gradually adjusts its trajectory after a change in direction. Furthermore, I aim for realistic deceleration when reversing thrust.
If you’ve ever played Escape Velocity, you’ll recognize the movement and control system I aspire to recreate as my "Gold Standard."
You can view my code, which is somewhat functional, at
For the specific .js file associated with this issue, visit . Please excuse the clutter of commented-out code as I've dedicated a week to tackling this particular aspect.
Although I explored numerous online solutions, those aligning closely with my design primarily employ the first "if" statement provided above, proving ineffective for me. Additionally, I reviewed related inquiries on StackOverflow:
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
While these solutions offer valuable insights, they seem complex for my needs. Omitting any gravity system, unlike the first example, the remaining suggestions do not immediately address my specific dilemma.
I welcome basic pseudocode or any guidance direct towards overlooked resources addressing my query. While I seek resolutions compatible with my existing setup, I am open to slight adjustments in my ship's movement logic if it streamlines the max_speed restriction process.
Thank you sincerely for engaging with this comprehensive explanation. :)
UPDATE:
After refining my code with some adjustments, it now operates more effectively than before. Refinements, tweaking, and debugging remain to achieve perfection. :)