2014-09-26 5 views
0

나는 우주선과 게임을 만들었지 만 움직이는 데 문제가있다. 스크립트입니다.자바 게임 회전 이미지와 축

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); 

} 

그래서 내가 이미지를 회전 할 수있는이 스크립트 (화면 참조) http://gyazo.com/c982b17afbba8cdc65e7786bd1cbea47

내 문제를 내가 정상 축 X 또는 Y 이동 (화면) 증가하는 경우 : http://gyazo.com/1fb2efb5aff9e95c56e7dddb6a30df4a 및 하지

답변

0

at.translate(x , y);

대각선에

코드 행은 당신의 배는 올바른 주위 이동? x 및 y를 증가 시키면 축에 따라 위/아래로 만 이동합니다.

우주선의 경로를 따라 이동하려면 실제 x와 y를 계산하려면 회전과 마찬가지로 삼각법을 사용해야합니다.

다음은 올바른 방향으로 사용자를 유도하는 대략적인 의사 코드입니다. 귀하의 연구에 따라 정확하지 않을 수도 있습니다.

//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));  
} 
+0

안녕하세요, 크기는 얼마입니까?이 방법을 호출해야합니까? –

+0

@terzi_matte 차량이 자동차라고 가정 해 봅시다. 가스 페달을 밟을 때 즉시 100kmph로 가나, 점차 빨라 100kmph까지 올라갑니다. 크기를 추가하면 배가 얼마나 빨리 진행되는지 확인할 수 있습니다. 따라서 게임에서 100의 크기는 우주선이 초당 100 픽셀로 비행하고 있음을 의미합니다. 10의 크기는 초당 10px를 의미합니다. – Compass

+0

네, 대단히 감사합니다, 1 시간 후 이해했지만 고마워요. 나는 당신이 코사인과 죄를 뒤집어 썼다고 생각합니다. :) –