简体   繁体   English

Java libgdx以恒定速度向目标射击

[英]Java libgdx Shooting bullet at direction with constant speed

I am trying to get my bullets to fire towards (input coords) at a constant speed. 我试图使子弹以恒定的速度向(输入坐标)发射。 So far I was able to get it to shoot at the direction but the farther I click (touch, android game) the faster the bullet goes. 到目前为止,我能够使它朝特定方向射击,但是我单击(触摸,Android游戏)的距离越远,子弹越快。 I have tried different methods by scaling but failed miserably, I have started coding just a month ago and using this as a project to increase my knowledge of how things work before I work on a full game but having too much trouble with this. 我尝试通过缩放来尝试不同的方法,但失败了,但不幸的是,我在一个月前才开始编写代码,并将其作为一个项目使用,以增加我在开发完整游戏之前对事物工作方式的了解,但是这样做有太多麻烦。

This is what I have been using to get the bullet to move towards the direction I want it to, the codes with // in front were other samples I got while browsing through the internet in hopes of getting what I wanted. 这就是我一直在使用的方式,使子弹朝我想要的方向移动,//前面的代码是我在互联网上浏览以获取我想要的东西时获得的其他示例。 I have thought of not using velocity to set the direction, but I have no clue of another method for this. 我曾考虑过不要使用速度来设置方向,但是我不知道另一种方法可以做到这一点。

EDIT: All in short, I cannot get all the bullets to move in the same speed, farther I click, higher velocity bullet has. 编辑:总之,我无法让所有子弹以相同的速度移动,我单击得越远,速度就越高。

Any help guys? 有帮助吗? Thanks a bunch 谢谢一大堆

Player Class : 玩家等级:

    public void update(float delta) {
    if (Gdx.input.isTouched()) {
        if (System.currentTimeMillis() - lastShot >= FIRE_RATE) {
            bullets.add(new Bullet(position.x + 6,position.y + 6,4,4,Gdx.input.getX() / 2,Gdx.input.getY() / 2));
            lastShot = System.currentTimeMillis();
        }
    }
    for (int i=0;i<bullets.size();i++) {
        bullets.get(i).update(delta);
    }
}

Bullet Class : 子弹班:

    public Bullet(float x, float y, int width, int height, float targetX, float targetY) {
    this.width = width;
    this.height = height;
    position = new Vector2( x , y );
    velocity = new Vector2( 0 , 0 );
    velocity.set(targetX - position.x,targetY - position.y);
    //velocity.set(targetX - position.x, targetY - position.y).nor().scl(Math.min(position.dst(targetX, targetY), speedMax));
}

public void update(float deltaTime) {
    //position.add(position.x + speedMax * deltaTime * ax,position.y + speedMax * deltaTime * ay);
    position.add(velocity.x * deltaTime, velocity.y * deltaTime);
    //velocity.scl(1 - (0.98f * deltaTime)); 
    // Linear dampening, otherwise the ball will keep going at the original velocity forever
    }

Well, normalizing vectors should be rather straightforward. 好吧,归一化向量应该非常简单。 Take your components, square them, and add them together (pythagorean theorem) and then divide each component by this result. 拿起组件,将它们平方,然后将它们加在一起(毕达哥拉斯定理),然后将每个组件除以该结果。 Ie vX = (targetX - position.x)/Math.sqrt(((targetX - position.x) * (targetX - position.x)) + ((targetY - position.y) *(targetY - position.y ))) vX = (targetX - position.x)/Math.sqrt(((targetX - position.x) * (targetX - position.x)) + ((targetY - position.y) *(targetY - position.y )))

Then you can multiply vX by some constant, and do the same for a vY and then set your velocity. 然后,您可以将vX乘以某个常数,并对vY进行相同操作,然后设置速度。

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

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