Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5961165
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T18:55:37+00:00 2026-05-22T18:55:37+00:00

So like just about everyone else learning some programming language, I’ve given myself a

  • 0

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. 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T18:55:37+00:00Added an answer on May 22, 2026 at 6:55 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

...just like packages do. I use Emacs (maybe, it can offer some kind of
I have a custom c# type like (just an example): public class MyVector {
Just like it reads.
Just like Carl's question over here I would like to ask you (because I
I'd like to just put in a comment in the block of my if-statement,
I'd like to just create a thread, then call CloseHandle immediately and let the
I need a collection that works just like a Dictionary/Hashtable in a sense that
I'd like to populate an arraylist by specifying a list of values just like
I want to write something that acts just like confirm() in javascript, but I
I have a collection of objects to which I'd like to just add a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.