1

I have to make a tank that sits still but moves his turret and shoots missiles.

As this is my first Android application ever and I haven't done any game development either, I've come across a few problems...

Now, I did the tank and the moving turret once I read the Android tutorial for the LunarLander sample code. So this code is based on the LunarLander code.

But I'm having trouble doing the missile firing then SPACE button is being pressed.

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

}

UPDATE GAME method

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;

}

}

a method run that runs the game...

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

So the question would be, how do I make that the bullet is fired on a single SPACE key press from the turret to the end of the screen?

Could you help me out here, I seem to be in the dark here...

Thanks,

Niksa

Nick
  • 11
  • 2
  • do you know http://gamedev.stackexchange.com/ ? – nkint Jan 18 '11 at 17:52
  • What's the problem? Is your `(dSpace)` trigger not working? Or is the bullet not visible? Or is the trajectory wrong? – Beta Jan 19 '11 at 03:47
  • The problem is that the dSpace is working but I can't get the bullet to move from the turret to the edge of the screen with the update method. – Nick Jan 19 '11 at 11:41
  • Go on, what happens to `x_bullet` and `y_bullet`? And what values do these variable have initially, and what values should they have when the bullet is at the edge of the screen? – Beta Jan 19 '11 at 19:47
  • Initially x_bullet and y_bullet are the same as x_turret & y_turret... and when the hit the edge of the screen they have to be x_bullet = 0, a y_bullet whereever it hits vertically depending on the angle of the turret. I know that distance = time * velocity so I did x_bullet -= BULLET_SPEED * elapsed ... that kinda works but it doesn't animate nicely... – Nick Jan 19 '11 at 20:33
  • It's too fast, I want to animate it slower so that the screen actually shows the trajectorie of the missile bullet... – Nick Jan 19 '11 at 20:43
  • So the bullet *does* move to the edge of the screen. You should put some more effort into working these things out yourself before you post. – Beta Jan 20 '11 at 00:26

1 Answers1

0

From the comments it sounds like your problem is that you want the bullet to move in a parabola to look more realistic. You have half of the solution with distance = time * velocity, but you also need velocity = time * acceleration. You want this in each update:

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

Your gravity constant will depend on the graphics scale. It's 9.8 m/s^2 in real life, but you'll have to adjust that according to your pixels per meter. The velocity vector will be initialized according to the muzzle velocity of your tank. If the muzzle velocity is stored in muzzle_vel, and the angle of the turret from the positive x-axis is theta, then you'll have

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

There are a lot of tweaks to make the trajectory more realistic, but this should get you started.

Alex
  • 2,152
  • 2
  • 17
  • 20