Search code examples
javamath2dgame-physicstrigonometry

Physics based 2D movement: Changing direction of an object over time


I am currently creating a 2D space game in Java, in which you control a ship in, well, space. The game does not use any external libraries.

The ship is supposed to move towards the cursor. However, when moving the cursor, the old force does not magically disappear; the ship changes its course, over time, to eventually move in the desired direction.

However, I have run into an issue regarding the movement of the ship. Basically, what I want to achieve is crudely illustrated by this image:

enter image description here

The image shows how the ship is supposed to move during one game tick. To explain further:

  • The Ship's max speed is illustrated by the circle.

  • The Target Angle is where the cursor currently is.

  • The Current Angle is the direction that the ship is currently traveling.

  • The Current Angle should move closer and closer to the Target Angle until it reaches the point where these two angles are the same.

  • The ship should change direction toward the target angle taking the shortest route possible; it can turn both left and right, not just left or right.

Now I've explained what I want to achieve, now I will instead describe what I so far have achieved and how it works.

Basically, the "ship" is an image sitting in the center of the screen. When you "move" the ship, the ship stays put; what moves is the rest of the play area.

The current "position" of the ship relative to the coordinate system that represents the play area are the integers xPos and yPos.

Now for some sample code that shows how the system works:

 int xPos;
 int yPos;

 public void updateMovement() {
     xPos += xSpeed;
     yPos += ySpeed; 
 }

 public void moveForward() {
     double yTempSpeed = ySpeed;
     double xTempSpeed = xSpeed;
     yTempSpeed += 0.01 * Math.sin(Math.toRadians(targetAngle));
     xTempSpeed += 0.01 * Math.cos(Math.toRadians(targetAngle));
     double resultVector = Math.sqrt(xTempSpeed * xTempSpeed + yTempSpeed * yTempSpeed);
     if (resultVector < 2) {
         ySpeed += 0.01 * Math.sin(Math.toRadians(targetAngle));
         xSpeed += 0.01 * Math.cos(Math.toRadians(targetAngle));
 }

This code successfully sets the Ship's max speed to the desired value, however, this does not work (the ships' course does not change) in the event where the resulting "vector" is larger than 2, i.e. when the speed is already at it's maximum and the targetAngle is too close to the angle which the ship is currently traveling (+- Pi / 2).

How would I go about changing the current angle based on this implementation?


Solution

  •   public void moveForward() {
         ySpeed += 0.01 * Math.sin(Math.toRadians(targetAngle));
         xSpeed += 0.01 * Math.cos(Math.toRadians(targetAngle));
         double currentSpeed = Math.sqrt(xTempSpeed * xTempSpeed + yTempSpeed * yTempSpeed);
         if (currentSpeed > maxSpeed) {
             //the resulting speed is allways <= maxspeed (normed to that)
             ySpeed *= maxSpeed/currentSpeed;
             xSpeed *= maxSpeed/currentSpeed;
     }
    

    hope this is what you needed... although it is quite unrealistic, that a spacecraft has a maximum Speed, but in terms of "playability" i would do the same.