Java - 对角线运动
Java - Diagonal Movement
我正在制作 SpaceInvaders 的一个版本,我希望我的 Shoots 具有对角线运动。我试过google,但我知道怎么做。
我有 class 实体:
public abstract class Entity extends Parent{
protected double x;
protected double y;
protected double dx;
protected double dy;
private Rectangle me = new Rectangle();
private Rectangle him = new Rectangle();
Image ref;
StackPane root;
ImageView content;
public Entity(Image ref,int x,int y, StackPane root) {
this.ref=ref;
this.root=root;
this.x = x;
this.y = y;
content = new ImageView();
content.setSmooth(true);
content.setImage(ref);
content.setScaleX(Game.scalar);
content.setScaleY(Game.scalar);
content.setTranslateX(this.x);
content.setTranslateY(this.y);
content.setRotate(0);
Game.root.getChildren().add(content);
}
public void move(long delta) {
this.x += (delta * this.dx) / 1000;
this.y += (delta * this.dy) / 1000;
content.setTranslateX(this.x);
content.setTranslateY(this.y);
}
现在,如何设置对角线移动?我用 Shoot Entity.
打印了游戏
谢谢。
您应该使用三角函数,尤其是 sin
和 cos
。标准 java class Math
有那些方法。
例如,我们可以认为一颗子弹从下往上飞行的角度是 90 度(0 是从左到右,180 是从右到左,270 是从上到下):
double angle = 90.0;
double angleInRadians = Math.toRadians(angle); // convert from degrees to radians
x += Math.cos(angleInRadians); // cos of 90 is 0, so no horizontal movement
y += Math.sin(angleInRadians); // sin of 90 is 1, full vertical movement
要对角线使用不同的角度,例如 60 度,子弹将水平和垂直移动:
double angle = 60.0;
double angleInRadians = Math.toRadians(angle); // convert from degrees to radians
x += Math.cos(angleInRadians); // cos of 60 is 0.58.., so a bit of horizontal movement
y += Math.sin(angleInRadians); // sin of 60 is 0.80.., a decent amount of vertical movement
为什么是正弦和余弦?视觉表示。
下图就是我们想要达到的效果,基本上是找到delta x
和delta y
加到我们的origin
位置,从而到达destination
位置,它们彼此不垂直。
现在我们来看看sine
和cosine
分别代表什么:
(来源:维基百科。)
我们可以注意到相似点:点O
和P
分别是我们的origin
和destination
,而sin θ
和cos θ
对应于delta y
和delta x
.
将这些概念应用于我们的情况,我们最终得到:
正弦和余弦需要一个角度,这将定义我们对象的运动方向。
它们会return一个介于-1和1之间的值,所以如果我们想改变物体的移动,我们需要将它乘以一个系数"speed"。
根据您提供的有限信息,我假设 x
和 y
是您飞船的坐标,而 dx
和 dy
是坐标弹丸的目标。您可以使用 Math.atan2()
来计算射弹图像的旋转度并将其提供给您那里的 content.setRotate()
。
// consider your spaceship an origin in coordinate system
double relativeX = this.dx - this.x;
double relativeY = this.y - this.dy;
double degree = Math.toDegrees(Math.atan2(relativeY, relativeX));
// rotate a projectile image counterclockwise from horizontal direction
content.setRotate(90 - degree);
我正在制作 SpaceInvaders 的一个版本,我希望我的 Shoots 具有对角线运动。我试过google,但我知道怎么做。
我有 class 实体:
public abstract class Entity extends Parent{
protected double x;
protected double y;
protected double dx;
protected double dy;
private Rectangle me = new Rectangle();
private Rectangle him = new Rectangle();
Image ref;
StackPane root;
ImageView content;
public Entity(Image ref,int x,int y, StackPane root) {
this.ref=ref;
this.root=root;
this.x = x;
this.y = y;
content = new ImageView();
content.setSmooth(true);
content.setImage(ref);
content.setScaleX(Game.scalar);
content.setScaleY(Game.scalar);
content.setTranslateX(this.x);
content.setTranslateY(this.y);
content.setRotate(0);
Game.root.getChildren().add(content);
}
public void move(long delta) {
this.x += (delta * this.dx) / 1000;
this.y += (delta * this.dy) / 1000;
content.setTranslateX(this.x);
content.setTranslateY(this.y);
}
现在,如何设置对角线移动?我用 Shoot Entity.
打印了游戏谢谢。
您应该使用三角函数,尤其是 sin
和 cos
。标准 java class Math
有那些方法。
例如,我们可以认为一颗子弹从下往上飞行的角度是 90 度(0 是从左到右,180 是从右到左,270 是从上到下):
double angle = 90.0;
double angleInRadians = Math.toRadians(angle); // convert from degrees to radians
x += Math.cos(angleInRadians); // cos of 90 is 0, so no horizontal movement
y += Math.sin(angleInRadians); // sin of 90 is 1, full vertical movement
要对角线使用不同的角度,例如 60 度,子弹将水平和垂直移动:
double angle = 60.0;
double angleInRadians = Math.toRadians(angle); // convert from degrees to radians
x += Math.cos(angleInRadians); // cos of 60 is 0.58.., so a bit of horizontal movement
y += Math.sin(angleInRadians); // sin of 60 is 0.80.., a decent amount of vertical movement
为什么是正弦和余弦?视觉表示。
下图就是我们想要达到的效果,基本上是找到delta x
和delta y
加到我们的origin
位置,从而到达destination
位置,它们彼此不垂直。
现在我们来看看sine
和cosine
分别代表什么:
(来源:维基百科。)
我们可以注意到相似点:点O
和P
分别是我们的origin
和destination
,而sin θ
和cos θ
对应于delta y
和delta x
.
将这些概念应用于我们的情况,我们最终得到:
正弦和余弦需要一个角度,这将定义我们对象的运动方向。
它们会return一个介于-1和1之间的值,所以如果我们想改变物体的移动,我们需要将它乘以一个系数"speed"。
根据您提供的有限信息,我假设 x
和 y
是您飞船的坐标,而 dx
和 dy
是坐标弹丸的目标。您可以使用 Math.atan2()
来计算射弹图像的旋转度并将其提供给您那里的 content.setRotate()
。
// consider your spaceship an origin in coordinate system
double relativeX = this.dx - this.x;
double relativeY = this.y - this.dy;
double degree = Math.toDegrees(Math.atan2(relativeY, relativeX));
// rotate a projectile image counterclockwise from horizontal direction
content.setRotate(90 - degree);