Java 弹丸速度盒2D
我想从屏幕右下角向屏幕左侧发射射弹。现在,我希望投射物按照屏幕尺寸以随机速度和角度飞行,就像这样。现在,我知道这很简单,但出于某种原因,我无法使它工作 以下是我迄今为止所尝试的: 我的第一次尝试启动功能Java 弹丸速度盒2D,java,libgdx,box2d,game-physics,projectile,Java,Libgdx,Box2d,Game Physics,Projectile,我想从屏幕右下角向屏幕左侧发射射弹。现在,我希望投射物按照屏幕尺寸以随机速度和角度飞行,就像这样。现在,我知道这很简单,但出于某种原因,我无法使它工作 以下是我迄今为止所尝试的: 我的第一次尝试启动功能 private void launchProjectile() { projectiles.peek().getBody().applyForce(projectiles.peek().getBody().getWorldVector(new Vector2(MathUtils.
private void launchProjectile() {
projectiles.peek().getBody().applyForce(projectiles.peek().getBody().getWorldVector(new Vector2(MathUtils.random(-20,-1*SCALAR_HEIGHT),
MathUtils.random(2*SCALAR_HEIGHT,8*SCALAR_HEIGHT)).scl(MathUtils.random(3*SCALAR_HEIGHT,5*SCALAR_HEIGHT))),
projectiles.peek().getBody().getWorldCenter(), true);
Gdx.app.log("System", String.valueOf(SCALAR_HEIGHT));
}
private void launchProjectile() {
float xVelocity;
float yVelocity;
xVelocity = (float) MathUtils.random(0,0)*SCALAR_WIDTH/2;
yVelocity = (float) MathUtils.random(20,20)*SCALAR_HEIGHT;
velocityProjectile.set(xVelocity,yVelocity); // Sets the velocity vector to the above values
velocityProjectile.sub(projectiles.peek().getBody().getPosition());
velocityProjectile.nor(); // Normalize the vector - Now it's fine and ready!
// Sets the start velocity of the projectile Trajectory to the current velocity
projectiles.peek().getBody().setLinearVelocity(velocityProjectile.scl(18+SCALAR_HEIGHT));
}
这是我的第二个尝试启动功能
private void launchProjectile() {
projectiles.peek().getBody().applyForce(projectiles.peek().getBody().getWorldVector(new Vector2(MathUtils.random(-20,-1*SCALAR_HEIGHT),
MathUtils.random(2*SCALAR_HEIGHT,8*SCALAR_HEIGHT)).scl(MathUtils.random(3*SCALAR_HEIGHT,5*SCALAR_HEIGHT))),
projectiles.peek().getBody().getWorldCenter(), true);
Gdx.app.log("System", String.valueOf(SCALAR_HEIGHT));
}
private void launchProjectile() {
float xVelocity;
float yVelocity;
xVelocity = (float) MathUtils.random(0,0)*SCALAR_WIDTH/2;
yVelocity = (float) MathUtils.random(20,20)*SCALAR_HEIGHT;
velocityProjectile.set(xVelocity,yVelocity); // Sets the velocity vector to the above values
velocityProjectile.sub(projectiles.peek().getBody().getPosition());
velocityProjectile.nor(); // Normalize the vector - Now it's fine and ready!
// Sets the start velocity of the projectile Trajectory to the current velocity
projectiles.peek().getBody().setLinearVelocity(velocityProjectile.scl(18+SCALAR_HEIGHT));
}
在这两次尝试中,投射物飞得比我需要的多,而且它没有像应该的那样考虑屏幕大小
你们能告诉我做这件事的正确方法吗
谢谢 从本页开始:
在“应该以多快的速度发射以达到所需高度?”部分中,您可以看到需要多少垂直速度才能使射弹到达屏幕顶部。因此,您可以选择一个小于该值的随机数,以确保它不会从屏幕顶部消失
接下来,在“它会飞多高?”一节中,您可以看到公式,以确定射弹达到最大高度需要多少时间步。然后需要相同的时间才能回到起始高度。例如,假设需要60个时间步才能达到最大高度。这意味着它需要120个时间步才能再次下降到与开始时相同的高度。然后你可以设置发射速度的水平部分,这样它就不能在120个时间步内离开屏幕。什么是
标量高度和标量宽度?我定义的变量将根据屏幕大小缩放速度。它们的值是多少?屏幕宽度除以10和屏幕高度除以tenI我理解你答案的第一部分,但我不理解第二部分。我怎样才能准确地使用你的公式来计算纬度速度?120个时间步是什么意思?我如何计算身体在120个时间步中的位置?一个时间步是当你调用world->step时。大多数模拟以60帧/秒的速度运行,因此120个时间步长为2秒。假设你的屏幕是50米宽的物理单位。如果你想在2秒内跑完50米,那么速度应该是25米/秒。