如何使物体向任何方向移动?

时间:2015-08-01 13:03:02

标签: java rotation libgdx sprite

我有一个旋转360度的枪精灵,我不想向任何方向射击子弹,我该怎么做?我不能只增加子弹的x,y坐标,因为它会上下左右移动。

编辑: 子弹运动似乎只是旋转但不会去任何地方或屏幕外

 bulletPosition.x=MathUtils.cosDeg(cannonObj.cannonAngle())*2;
        bulletPosition.y=MathUtils.sinDeg(cannonObj.cannonAngle())*2;
        bulletVelocity.x=MathUtils.cosDeg(cannonObj.cannonAngle())*10;
        bulletVelocity.y=MathUtils.sinDeg(cannonObj.cannonAngle())*10;

        bulletPosition.x+=bulletVelocity.x*deltaTime;
        bulletPosition.y+=bulletVelocity.y*deltaTime;

//spawning bullets
 public void draw(SpriteBatch batch){
        bulletIterator=bullets.iterator();
        while(bulletIterator.hasNext()){
            Sprite sprite=bulletIterator.next();
            sprite.draw(batch);
            sprite.setPosition(bulletPosition.x,bulletPosition.y);
        }

1 个答案:

答案 0 :(得分:0)

Vector gunPos; // the gun position,dah
Vector bulletPos; //the bullet position
Vector bulletVelocity; //need explanation?
float gunAngle; //the rotation of the gun in degrees
float distanceFromCenter=gunSize.x/2; //the distance from the gun center you want the bullet to appear

bulletPos.x=MathUtils.cosDeg((gunAngle)) * distanceFromCenter;
bulletPos.y=MathUtils.cosDeg((gunAngle)) * distanceFromCenter;
bulletPos.plus(gunPos);

bulletVelocity.x=MathUtils.cosDeg((gunAngle)) * bulletSpeed;
bulletVelocity.y=MathUtils.cosDeg((gunAngle)) * bulletSpeed;
相关问题