【发布时间】:2014-10-12 20:18:21
【问题描述】:
我想从屏幕右下角向屏幕左侧发射弹丸。现在,我希望弹丸以随机的速度和角度飞行根据屏幕尺寸,就像这样。现在,我知道这很简单,但由于某种原因,我无法完成这项工作。
这是我迄今为止尝试过的:
我的第一次尝试——启动功能
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));
}
在这两次尝试中,弹丸飞得比我需要的多,而且它没有考虑到应有的屏幕尺寸。
你们能告诉我这样做的正确方法是什么吗?
谢谢!!
【问题讨论】:
-
SCALAR_HEIGHT和SCALAR_WIDTH是什么? -
只是我定义的一个变量,它将根据屏幕尺寸缩放速度
-
那么他们的价值观是什么?
-
屏幕宽度除以十和屏幕高度除以十
标签: java libgdx box2d game-physics projectile