英文:
object move towards direction it is facing
问题
我正在制作一个2D赛车游戏,目前正在尝试让赛车移动。我不需要赛车的物理效果是真实的,我只需要赛车(在这种情况下是矩形)朝着它面对的方向移动。因此,如果我将车向右旋转大约20度,我希望它沿着那个方向前进。但是我不确定如何做到这一点(我已经对向量进行了一些研究,但是没有理解太多)。目前,该矩形在所有方向上移动,但不会旋转。
import java.awt.*;
public class Box {
public boolean[] KeyHold;
public double posx, posy, angle;
public Box() {
posx = 1280/2;
posy = 720/2;
}
public void update() {
KeyHold = Inputs.getKeyHold();
if(KeyHold[39]) {
posx++;
} else if (KeyHold[37]) {
posx--;
}
if(KeyHold[40]) {
posy++;
} else if (KeyHold[38]) {
posy--;
}
}
public void draw(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
g2d.clearRect(0, 0, 1280, 720);
g2d.setColor(Color.black);
g2d.fillRect((int) posx, (int) posy, 30, 60);
}
}
英文:
Im working on a 2d car racing game and I'm currently trying to get the car moving. I don't need the physics of the car to be realistic, I just need the car (rectangle in this case) to move towards the direction it is facing. So if i was to rotate the car right by about 20 degrees, I want it to then travel in that direction. But I'm not sure as to how I would do this (i've done a bit of research into vectors but didn't understand much). Currently the box moves in all directions but doesn't rotate.
import java.awt.*;
public class Box {
public boolean[] KeyHold;
public double posx, posy, angle;
public Box() {
posx = 1280/2;
posy = 720/2;
}
public void update() {
KeyHold = Inputs.getKeyHold();
if(KeyHold[39]) {
posx++;
} else if (KeyHold[37]) {
posx--;
}
if(KeyHold[40]) {
posy++;
} else if (KeyHold[38]) {
posy--;
}
}
public void draw(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
g2d.clearRect(0, 0, 1280, 720);
g2d.setColor(Color.black);
g2d.fillRect((int) posx, (int) posy, 30, 60);
}
}
专注分享java语言的经验与见解,让所有开发者获益!
评论