我正在尝试制作一款几何 war 风格的游戏,但遇到了障碍。我一直在试图找出一种方法来平滑地移动我的图形对象。但它只向一个方向移动。例如,如果我按向上按钮,那么它就会上升,然后当我按其顶部的右侧按钮时,我会沿对角线向右上角移动。但我希望我的对象能够顺利地从直线向上转向右上角。这是我到目前为止的代码:
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
实现。
关于java - 如何在 Java 中平滑地移动图形对象 360 度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11446097/