0

i have made a game with a spaceship but i have a problem with moving of it. this is the script:

public  class spaceship {  

private final local_client local_client;

private final int startX;
private final int startY;
private final int startR;

private double x,y,r;
public static double rotation;
public static double velo;

public static AffineTransform at;

public spaceship(local_client local_client,int startX,int startY,int startR) {
    this.local_client = local_client;
    this.startX = startX;
    this.startY = startY;
    this.startR = startR;
    x=200;
    y=200;
    r=90;
}  

public void drawImage(Graphics2D g2d) {  
    g2d.drawImage(local_client.spaceship_img,200,200, local_client.game);
     at = g2d.getTransform();

              at.translate(x , y);
                       at.rotate(Math.toRadians(r));
            at.translate(-(local_client.spaceship_img.getWidth()/2),-(local_client.spaceship_img.getHeight()/2));
     System.out.println(at.getTranslateX() + " - " + at.getTranslateY());
              g2d.setTransform(at);
     g2d.drawImage(local_client.spaceship_img,0,0, local_client.game); 

}

So with this script i can rotate image (see the screen): http://gyazo.com/c982b17afbba8cdc65e7786bd1cbea47

My problem is that if i increment X or Y move on the normal axis (screen) : http://gyazo.com/1fb2efb5aff9e95c56e7dddb6a30df4a and not in diagonal

terzi_matte
  • 175
  • 1
  • 10

1 Answers1

1

at.translate(x , y);

This line of code moves your ship around, correct? Incrementing x and y will only move them up and down according to the axis.

You will have to use trigonometry like you have with your rotations to calculate the actual x and y to move if you want to move along the ship's path.

The following is the approximate pseudocode that should get you in the right direction. May or may not be accurate depending on your r.

//drive along the ship's nose line
void moveAlongShipAxis(int magnitude) {
    x += magnitude * Math.cos(Math.toRadians(r));
    y += magnitude * Math.sin(Math.toRadians(r));       
}
//strafe left and right, if you have that, otherwise you can skip this one
void moveYAlongTransverseAxis(int magnitude) {
    y += magnitude * Math.cos(Math.toRadians(r));
    x += magnitude * Math.sin(Math.toRadians(r));       
}
Compass
  • 5,867
  • 4
  • 30
  • 42
  • Hi, what is it magnitude, and when i have to call this method? – terzi_matte Sep 28 '14 at 18:45
  • @terzi_matte Let's say the vehicle is a car. When you press on the gas pedal, does it go at 100kmph instantly, or gradually go faster up to 100kmph? By adding a magnitude, you can determine how fast the ship is going. So a magnitude of 100 in your game would mean the ship is flying at 100px per second. A magnitude of 10 would mean 10px per second. – Compass Sep 28 '14 at 20:44
  • Yea thank your very much, i understood it after 1 hourse but thanks, i think you have reversed cos and sin :) – terzi_matte Sep 30 '14 at 18:45
  • @terzi_matte yeah I have to do most of the code-writing in my head so I can't really test it out but the concept is there :3 – Compass Sep 30 '14 at 19:00