0

I am making a game with a space ship that rotates when the left and right keys are pressed and moves forward when the up key is pressed.

Currently the ship can rotate while its moving forward but it will continue in the same direction that it is going in.

How would i make it so that the ship can change the direction its is moving while the up key is being held down?

This is the update method for the SpaceShip class:

public void update(){
    radians += ri;
    System.out.println(radians);
    if(radians < 0){
        radians = 2 * Math.PI;
    }if(radians > (2 * Math.PI)){
        radians = 0;
    }

    x += xx;
    y += yy;
}

this is the right event:

    public void actionPerformed(ActionEvent e) {
    if(pressed){
        Board.getShip().setRI(0.05);
    }else{
        Board.getShip().setRI(0);
    }
}

and this is the up event:

    public void actionPerformed(ActionEvent e) {
    if(pressed){
        Board.getShip().setXX(Math.cos(Board.getShip().getRadians()) * Board.getShip().getSpeed());
        Board.getShip().setYY(Math.sin(Board.getShip().getRadians()) * Board.getShip().getSpeed());
    }else{
        Board.getShip().setXX(0);
        Board.getShip().setYY(0);
    }
}
Ben
  • 1
  • 1
  • Set a flag with determines which key or direction is active. In the main "game loop" check the flag and apply the appropriate delta – MadProgrammer Jan 02 '16 at 21:43
  • Something like [this](http://stackoverflow.com/questions/22748547/java-swing-timer-only-works-once-then-keyevents-fire-in-rapid-succession-holdi/22749251#22749251) or [this](http://stackoverflow.com/questions/16622630/gradually-speeding-a-sprite/16623202#16623202) or [this](http://stackoverflow.com/questions/34125578/gradually-accelerate-sprite-on-key-pressed-gradually-decelerate-on-key-released/34126260#34126260) – MadProgrammer Jan 02 '16 at 21:51
  • And something like [this](http://stackoverflow.com/questions/13041297/java-moving-an-object-at-an-angle-and-changing-angle-with-keypress/13041547#13041547) should allow you to calculate the x/y points based on the angle and the delta you want apply – MadProgrammer Jan 02 '16 at 21:57

1 Answers1

1

Rockets

A rocket defined as

// pseudo code 
rocket = {
    mass : 1000,
    position : {  // world coordinate position
         x : 0,
         y : 0,
    },
    deltaPos : {   // the change in position per frame
         x : 0,
         y : 0,
    },
    direction : 0, // where the front points in radians
    thrust: 100, // the force applied by the rockets
    velocity : ?,  // this is calculated 
}  

The formula for movement is

deltaVelocity = mass / thrust;

The direction of the thrust is along the direction the ship is pointing. As there are two components to the change in position per frame and that thrust changes the deltas the way to apply thrust is;

// deltaV could be a constant but I like to use mass so when I add stuff
// or upgrade rockets it has a better feel.
float deltaV = this.mass / this.thrust;
this.deltaPos.x += Math.sin(this.direction) * deltaV;
this.deltaPos.y += Math.cos(this.direction) * deltaV;

As the thrust delta is added to the position deltas the result is acceleration in the direction the ship is pointing.

Each frame you then update the position by the delta pos.

this.position.x += this.deltaPos.x;
this.position.y += this.deltaPos.y;

You may want to add some drag to slow the ship over time. You can add a simple drag coefficient

rocket.drag = 0.99;  // 1 no drag 0 100% drag as soon as you stop thrust the ship will stop.

To apply the drag

this.deltaPos.x *= this.drag;
this.deltaPos.y *= this.drag;

To get the current velocity, though not needed in the caculations.

this.velocity = Math.sqrt( this.deltaPos.x * this.deltaPos.x + this.deltaPos.y * this.deltaPos.y);

This will produce rocket behaviour that is the same as in the game Asteroids. If you want behaviour that is more like a boat on water, or car (ie the changing direction changes the deltas to match the direction) let me know as it is a simple modification of the above.

Blindman67
  • 51,134
  • 11
  • 73
  • 136