1

Geometry Warsスタイルのゲームを作ろうとしていますが、障害があります。グラフィックオブジェクトをスムーズに動かす方法を模索してきました。しかし、それは一方向にしか動きません。たとえば、上ボタンを押すと上に上がり、その上にある右ボタンを押すと、右上に向かって斜めに移動します。しかし、オブジェクトをまっすぐ上から右上に向かってスムーズに回転させたいと思います。これはこれまでの私のコードです:

public class MoveTest extends JPanel implements ActionListener, KeyListener {

Timer t = new Timer(5, this);
double x = 0, y = 0, velX = 0, velY = 0;
 private Set<Integer> pressed = new HashSet<Integer>();
private static int WIDTH = 800;
private static int HEIGHT = 600;
private double d = 0;
private double topSpeed = 0;
private boolean stop = false;
private double a = 0;

PVector location;
PVector velocity;
PVector acceleration;

public MoveTest() {
  location = new PVector(WIDTH/2,HEIGHT/2);
  velocity = new PVector(0,0);
  acceleration = new PVector(0,0);
  topSpeed = 5.0;
  t.start();
  addKeyListener(this);
  setFocusable(true);
  setFocusTraversalKeysEnabled(false);
}

public void paintComponent(Graphics g) {
  super.paintComponent(g);
  Graphics2D g2 = (Graphics2D) g;
  g2.setColor(Color.GREEN);
  g2.drawOval((int)location.x, (int)location.y, 40, 40);
}

void update() {
  velocity.add(acceleration);
  if(stop == false) {
    velocity.limit(topSpeed);
  }
  else {
    velocity.decellerate(0);
  }
  location.add(velocity);    
}

public void north() {
  acceleration = new PVector(0,-5);
}

public void south() {
  acceleration = new PVector(0,5);
}

public void west() {
  acceleration = new PVector(-5,0);
}

public void east() {
  acceleration = new PVector(5,0);
}

public void northEast() {
  acceleration = new PVector(5,-5);
}

public void northWest() {
  acceleration = new PVector(-5,-5);
}

public void southEast() {
  acceleration= new PVector(5,5);
}

public void southWest() {
  acceleration = new PVector(-5,5);
}

public void stop(){
  stop = true;
}


public void actionPerformed(ActionEvent arg0) {
  //System.out.println("" + velocity.x + " " + velocity.y);
  repaint();
  update();
  checkEdges();
}

public void keyPressed(KeyEvent e) {

int code = e.getKeyCode();

stop = false;

pressed.add(code);

if(pressed.size() > 1) {
  if(pressed.contains(KeyEvent.VK_W) && pressed.contains(KeyEvent.VK_D)) {
    northEast();
  }
  if(pressed.contains(KeyEvent.VK_W) && pressed.contains(KeyEvent.VK_A)) {
    northWest();
  }
  if(pressed.contains(KeyEvent.VK_S) && pressed.contains(KeyEvent.VK_D)) {
    southEast();
   }
  if(pressed.contains(KeyEvent.VK_S) && pressed.contains(KeyEvent.VK_A)) {
    southWest();
  } 
}

else if(pressed.size() == 1){
  if(code == KeyEvent.VK_W) {
    north();
  }
  if(code == KeyEvent.VK_S) {
    south();
  }
  if(code == KeyEvent.VK_D) {
    east();
  }
  if(code == KeyEvent.VK_A) {
    west();
  }
}
}

public void keyReleased(KeyEvent e) {
  pressed.remove(e.getKeyCode());
  if(pressed.size() == 0) {
    stop();
  }
}

public void keyTyped(KeyEvent e) {

}

void checkEdges() {
  if(location.x > WIDTH) {
    location.x = 0;
  }
  else if(location.x < 0) {
    location.x  = WIDTH;
  }
  if(location.y > HEIGHT) {
    location.y = 0;
  }
  else if(location.y < 0) {
    location.y = HEIGHT;
  }
}

}

どんな助けでもいただければ幸いです。

4

1 に答える 1

1
//MoveTest
topSpeed = 5.0;

//update
velocity.add(acceleration);
if(stop == false) {
  velocity.limit(topSpeed);

//south
acceleration = new PVector(0,5);

したがって、キーを押すと、加速度は常に 5 または 7 (おそらくエラー) 単位になります。次に、これを速度に追加し、速度を 5 に制限します。加速が非常に強いため、慣性はほとんどありません。加速を小さくするか、topSpeed を大きくしてみると、よりスムーズになるはずです。

余談ですが、フレームレートを補正していますか? PVectorコードには表示されませんが、実装はわかりません。

于 2012-07-12T07:06:32.723 に答える