我正在尝试制作一款《几何战争》风格的游戏,但遇到了障碍。我一直在试图找到一种方法来平滑地移动我的图形对象。但它只朝着一个方向发展。例如,如果我按下向上按钮,它就会向上移动,然后当我按下上面的右按钮时,我会朝着右上角对角移动。但我希望我的物体能顺利地从直上转向右上角。这是我迄今为止的代码:
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;
}
}
}
任何帮助都将不胜感激。
//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
的实现。