【问题标题】:2D game - Missile shooting problem2D游戏 - 导弹射击问题
【发布时间】:2011-01-18 17:44:20
【问题描述】:

我必须制造一辆静止不动但移动炮塔并发射导弹的坦克。

由于这是我的第一个 Android 应用程序,而且我也没有做过任何游戏开发,所以我遇到了一些问题......

现在,当我阅读了 LunarLander 示例代码的 Android 教程后,我做了坦克和移动炮塔。所以这段代码是基于 LunarLander 代码的。

但是我在发射导弹时遇到问题,然后按下 SPACE 按钮。

private void doDraw(Canvas canvas) {

canvas.drawBitmap(backgroundImage, 0, 0, null);
// draws the tank
canvas.drawBitmap(tank, x_tank, y_tank, new Paint());
// draws and rotates the tank turret
canvas.rotate((float) mHeading, (float) x_turret + mTurretWidth, y_turret);
canvas.drawBitmap(turret, x_turret, y_turret, new Paint());

// draws the grenade that is a regular circle  from ShapeDrawable class
bullet.setBounds(x_bullet, y_bullet, x_bullet + width, y_bullet + height);
bullet.draw(canvas);

}

更新游戏方法

private void updateGame() throws InterruptedException {

long now = System.currentTimeMillis();

if (mLastTime > now) 
return;
double elapsed = (now - mLastTime) / 1000.0;
mLastTime = now;


// dUP and dDown, rotates the turret from 0 to 75 degrees.
if (dUp)
mHeading += 1 * (PHYS_SLEW_SEC * elapsed);

if (mHeading >= 75) mHeading = 75;

if (dDown)
mHeading += (-1) * (PHYS_SLEW_SEC * elapsed);
if (mHeading < 0) mHeading = 0;


if (dSpace){
//  missile Logic, a straight trajectorie for now
x_bullet -= 1;
y_bullet -= 1;

}

}

运行游戏的方法 run...

public void run() {
            while (mRun) {
                Canvas c = null;
                try {
                    c = mSurfaceHolder.lockCanvas(null);
                    synchronized (mSurfaceHolder) {
                        if (mMode == STATE_RUNNING) 
                         updateGame();
                        doDraw(c);
                    }
                } catch (InterruptedException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    } finally {
                    // do this in a finally so that if an exception is thrown
                    // during the above, we don't leave the Surface in an
                    // inconsistent state
                    if (c != null) {
                        mSurfaceHolder.unlockCanvasAndPost(c);
                    }
                }
            }
        }

所以问题是,如何让子弹在从炮塔到屏幕末端的单个 SPACE 键下发射?

你能帮帮我吗,我似乎在黑暗中......

谢谢,

日照

【问题讨论】:

  • 你知道gamedev.stackexchange.com 吗?
  • 有什么问题?你的(dSpace) 触发器不工作吗?还是子弹不可见?还是轨迹错了?
  • 问题是 dSpace 正在工作,但我无法使用 update 方法让子弹从炮塔移动到屏幕边缘。
  • 继续,x_bullety_bullet 会发生什么?以及这些变量最初有什么值,当子弹在屏幕边缘时它们应该有什么值?
  • 最初 x_bullet 和 y_bullet 与 x_turret & y_turret 相同...并且当击中屏幕边缘时,它们必须是 x_bullet = 0,y_bullet 取决于它垂直击中的角度炮塔。我知道距离 = 时间 * 速度,所以我做了 x_bullet -= BULLET_SPEED * elapsed ...这有点用,但动画效果不佳...

标签: 2d physics bullet


【解决方案1】:

从 cmets 看来,您的问题是您希望子弹以抛物线运动以看起来更逼真。 distance = time * velocity 解决方案的一半,但您还需要 velocity = time * acceleration。您希望在每次更新中都这样做:

x_bullet += elapsed * vx_bullet;
y_bullet += elapsed * vy_bullet;
vx_bullet += elapsed * ax_bullet;  // ax_bullet = 0 unless you want simple wind resistance
vy_bullet += elapsed * ay_bullet;  // ay_bullet = gravity

您的重力常数将取决于图形比例。在现实生活中是 9.8 m/s^2,但您必须根据每米的像素进行调整。速度矢量将根据坦克的初速进行初始化。如果炮口初速存储在muzzle_vel中,炮塔与x轴正方向的夹角为theta,那么你就有

vx_bullet = muzzle_vel * Math.cos(theta);
vy_bullet = muzzle_vel * Math.sin(theta);

有很多调整可以使轨迹更加逼真,但这应该可以帮助您入门。

【讨论】:

    猜你喜欢
    • 2018-02-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-05
    • 2011-04-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多