简体   繁体   English

弹丸速度盒

[英]Projectile velocity box2d

I want to launch projectiles from the bottom-right corner of the screen towards the left side of the screen. 我想从屏幕的右下角向屏幕的左侧发射弹丸。 Now, I want the projectiles to fly with random velocities and angles according to the screen dimensions , just like that. 现在,我希望弹丸根据屏幕尺寸以随机的速度和角度飞行,就像这样。 Now, I know this is very simple but from some reason I can't manage to make this work. 现在,我知道这很简单,但是由于某种原因,我无法设法完成这项工作。

Here is what I have tried so far: 到目前为止,这是我尝试过的:

My first try - Launch function 我的第一次尝试-启动功能

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));
    }

Here is my second try - Launch function 这是我的第二次尝试-启动功能

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));
}

In both tries, the projectile flies way more than I need and it doens't take in consideration the screen size like it should. 在这两次尝试中,弹丸的飞行方式都比我需要的要多,并且它并未考虑应有的屏幕尺寸。

Can you guys please tell me what is the right way to do this? 你们能告诉我什么是正确的方法吗?

Thanks!! 谢谢!!

Start with this page: http://www.iforce2d.net/b2dtut/projected-trajectory 从此页面开始: http : //www.iforce2d.net/b2dtut/projected-trajectory

In the "How fast should it be launched to reach a desired height?" 在“应该以多快的速度发射到期望的高度?” section, you can see how much vertical velocity will be required to make the projectile reach the top of the screen. 部分,您将看到使弹丸到达屏幕顶部所需的垂直速度。 So you would pick a random number less than that, to make sure it doesn't go off the top of the screen. 因此,您应该选择一个比该数字小的随机数,以确保它不会超出屏幕顶部。

Next, in the "How high will it go?" 接下来,在“它将涨多高?”中 section, you can see the formula to find out how many time steps it will take for the projectile to reach maximum height. 部分,您可以查看公式以找出弹丸达到最大高度需要多少时间。 It will then take the same amount of time to come back down to the starting height. 然后,将花费相同的时间返回到初始高度。 For example, let's say it would take 60 time steps to reach maximum height. 例如,假设要花60个时间步才能达到最大高度。 That means it would take 120 time steps to fall down again to the same height as it started. 这意味着需要120个时间步才能再次下降到与开始时相同的高度。 Then you can set the horizontal part of the launch velocity so that it cannot go outside the screen in 120 time steps. 然后,您可以设置发射速度的水平部分,以使其在120个时间步内不会超出屏幕。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM