import javax.media.opengl.*; /** * The Tanks */ class Tank { // position, orientation and velocity double currentAngle = 0; float x = 0; float y = 0; float z = 0; double velocity = 0; public Tank() { } public double getVelocity() { return velocity; } public void setVelocity(double velocity) { this.velocity = velocity; } // move the tank making sure it stays on the floor public void translate(float x, float y, float z) { this.x += x; if (this.x < -1000) this.x = -1000; if (this.x > 1000) this.x = 1000; this.y += y; this.z += z; if (this.z < -1000) this.z = -1000; if (this.z > 1000) this.z = 1000; } // rotates the tank of 'angle' degrees public void rotate(double angle) { currentAngle = (currentAngle + angle) % 360.0; } // updates the tank depending on its velocity public void update(double time) { if (velocity != 0) { float dx = (float) (velocity * time * Math.cos(currentAngle / 180.0f * Math.PI)); float dz = (float) (velocity * time * Math.sin(currentAngle / 180.0f * Math.PI)); translate(dx, 0, - dz); } } // Draws the tank public void draw(GL gl) { // transformation matrix gl.glPushMatrix(); gl.glTranslatef(x, y, z); // this is to take care of the tank modelisation gl.glRotatef(-90.0f, 1.0f, 0.0f, 0.0f); gl.glRotatef((float)currentAngle, 0.0f, 0.0f, 1.0f); // draw the turret gl.glColor3f(0.9f,0.9f,0.2f); gl.glShadeModel(gl.GL_FLAT); gl.glBegin(gl.GL_TRIANGLE_STRIP); gl.glNormal3f(0.991228f, 0.000000f, -0.132164f); gl.glTexCoord2f(0.107f, -0.009f); gl.glVertex3f(1.480f, -0.516f, 1.040f); gl.glTexCoord2f(0.617f, -0.559f); gl.glVertex3f(1.480f, 0.516f, 1.040f); gl.glTexCoord2f(0.224f, -0.178f); gl.glVertex3f(1.580f, -0.434f, 1.790f); gl.glTexCoord2f(0.457f, -0.429f); gl.glVertex3f(1.580f, 0.435f, 1.790f); gl.glNormal3f(0.087795f, 0.000000f, 0.996139f); gl.glTexCoord2f(0.866f, 0.402f); gl.glVertex3f(-1.370f, -0.765f, 2.050f); gl.glTexCoord2f(1.080f, 0.169f); gl.glVertex3f(-1.370f, 0.764f, 2.050f); gl.glNormal3f(-0.741466f, 0.000000f, -0.670990f); gl.glTexCoord2f(0.559f, 0.339f); gl.glVertex3f(-0.456f, -1.060f, 1.040f); gl.glTexCoord2f(0.996f, -0.132f); gl.glVertex3f(-0.456f, 1.080f, 1.040f); gl.glEnd(); gl.glShadeModel(gl.GL_SMOOTH); gl.glBegin(gl.GL_TRIANGLE_FAN); gl.glNormal3f(0.183504f, -0.940289f, 0.286676f); gl.glTexCoord2f(0.559f, 0.339f); gl.glVertex3f(-0.456f, -1.060f, 1.040f); gl.glNormal3f(0.269870f, -0.960420f, 0.069023f); gl.glTexCoord2f(0.107f, -0.009f); gl.glVertex3f(1.480f, -0.516f, 1.040f); gl.glNormal3f(0.136533f, -0.910814f, 0.389585f); gl.glTexCoord2f(0.224f, -0.178f); gl.glVertex3f(1.580f, -0.434f, 1.790f); gl.glTexCoord2f(0.866f, 0.402f); gl.glVertex3f(-1.370f, -0.765f, 2.050f); gl.glEnd(); gl.glBegin(gl.GL_TRIANGLE_FAN); gl.glNormal3f(0.235235f, 0.954665f, 0.182426f); gl.glTexCoord2f(0.996f, -0.132f); gl.glVertex3f(-0.456f, 1.080f, 1.040f); gl.glNormal3f(0.136569f, 0.903492f, 0.406265f); gl.glTexCoord2f(1.080f, 0.169f); gl.glVertex3f(-1.370f, 0.764f, 2.050f); gl.glNormal3f(0.279081f, 0.957980f, 0.066251f); gl.glTexCoord2f(0.457f, -0.429f); gl.glVertex3f(1.580f, 0.435f, 1.790f); gl.glTexCoord2f(0.617f, -0.559f); gl.glVertex3f(1.480f, 0.516f, 1.040f); gl.glEnd(); // draw the body gl.glColor3f(0.8f,0.8f,0.2f); gl.glBegin(gl.GL_TRIANGLE_STRIP); gl.glNormal3f(0.023598f, 0.000000f, 0.999722f); gl.glTexCoord2f(1.360f, 1.970f); gl.glVertex3f(2.575f, -0.877f, 1.111f); gl.glTexCoord2f(0.822f, 1.710f); gl.glVertex3f(2.575f, 0.877f, 1.111f); gl.glTexCoord2f(2.030f, 0.541f); gl.glVertex3f(-2.835f, -0.877f, 1.238f); gl.glTexCoord2f(1.490f, 0.289f); gl.glVertex3f(-2.835f, 0.877f, 1.238f); gl.glNormal3f(-0.898134f, 0.000000f, -0.439723f); gl.glTexCoord2f(1.840f, 0.932f); gl.glVertex3f(-2.229f, -0.877f, 0.000f); gl.glTexCoord2f(1.310f, 0.680f); gl.glVertex3f(-2.229f, 0.877f, 0.000f); gl.glNormal3f(0.000000f, 0.000000f, -1.000000f); gl.glTexCoord2f(1.350f, 1.980f); gl.glVertex3f(2.430f, -0.877f, 0.000f); gl.glTexCoord2f(0.815f, 1.730f); gl.glVertex3f(2.430f, 0.877f, 0.000f); gl.glNormal3f(0.991585f, 0.000000f, -0.129459f); gl.glTexCoord2f(1.360f, 1.970f); gl.glVertex3f(2.575f, -0.877f, 1.111f); gl.glTexCoord2f(0.822f, 1.710f); gl.glVertex3f(2.575f, 0.877f, 1.111f); gl.glEnd(); gl.glColor3f(0.3f,0.3f,0.3f); // left tread gl.glShadeModel(gl.GL_FLAT); gl.glBegin(gl.GL_TRIANGLE_STRIP); gl.glNormal3f(0.020362f, 0.000000f, 0.999793f); gl.glTexCoord2f(0.033f, 0.684f); gl.glVertex3f(2.730f, 0.877f, 1.294f); gl.glTexCoord2f(0.095f, 0.570f); gl.glVertex3f(2.730f, 1.400f, 1.294f); gl.glTexCoord2f(0.759f, 1.070f); gl.glVertex3f(-2.970f, 0.877f, 1.410f); gl.glTexCoord2f(0.813f, 0.970f); gl.glVertex3f(-2.970f, 1.400f, 1.410f); gl.glNormal3f(-0.885132f, 0.000000f, -0.465341f); gl.glTexCoord2f(0.375f, 1.300f); gl.glVertex3f(-2.229f, 0.877f, 0.000f); gl.glTexCoord2f(0.800f, 0.523f); gl.glVertex3f(-2.229f, 1.400f, 0.000f); gl.glNormal3f(0.000000f, 0.000000f, -1.000000f); gl.glTexCoord2f(-0.156f, 1.010f); gl.glVertex3f(2.597f, 0.877f, 0.000f); gl.glTexCoord2f(0.268f, 0.233f); gl.glVertex3f(2.597f, 1.400f, 0.000f); gl.glNormal3f(0.994712f, 0.000000f, -0.102699f); gl.glTexCoord2f(0.033f, 0.684f); gl.glVertex3f(2.730f, 0.877f, 1.294f); gl.glTexCoord2f(0.095f, 0.570f); gl.glVertex3f(2.730f, 1.400f, 1.294f); gl.glEnd(); gl.glShadeModel(gl.GL_FLAT); gl.glBegin(gl.GL_TRIANGLE_FAN); gl.glNormal3f(0.000000f, -1.000000f, 0.000000f); gl.glTexCoord2f(0.375f, 1.300f); gl.glVertex3f(-2.229f, 0.877f, 0.000f); gl.glTexCoord2f(-0.156f, 1.010f); gl.glVertex3f(2.597f, 0.877f, 0.000f); gl.glTexCoord2f(0.033f, 0.684f); gl.glVertex3f(2.730f, 0.877f, 1.294f); gl.glTexCoord2f(0.759f, 1.070f); gl.glVertex3f(-2.970f, 0.877f, 1.410f); gl.glEnd(); gl.glBegin(gl.GL_TRIANGLE_FAN); gl.glNormal3f(0.000000f, 1.000000f, 0.000000f); gl.glTexCoord2f(0.800f, 0.523f); gl.glVertex3f(-2.229f, 1.400f, 0.000f); gl.glTexCoord2f(0.813f, 0.970f); gl.glVertex3f(-2.970f, 1.400f, 1.410f); gl.glTexCoord2f(0.095f, 0.570f); gl.glVertex3f(2.730f, 1.400f, 1.294f); gl.glTexCoord2f(0.268f, 0.233f); gl.glVertex3f(2.597f, 1.400f, 0.000f); gl.glEnd(); // right tread gl.glShadeModel(gl.GL_FLAT); gl.glBegin(gl.GL_TRIANGLE_STRIP); gl.glNormal3f(0.020362f, 0.000000f, 0.999793f); gl.glTexCoord2f(-0.072f, 0.099f); gl.glVertex3f(2.730f, -1.400f, 1.294f); gl.glTexCoord2f(0.032f, 0.022f); gl.glVertex3f(2.730f, -0.875f, 1.294f); gl.glTexCoord2f(0.419f, 0.757f); gl.glVertex3f(-2.970f, -1.400f, 1.410f); gl.glTexCoord2f(0.511f, 0.690f); gl.glVertex3f(-2.970f, -0.875f, 1.410f); gl.glNormal3f(-0.885132f, 0.000000f, -0.465341f); gl.glTexCoord2f(-0.026f, 0.803f); gl.glVertex3f(-2.229f, -1.400f, 0.000f); gl.glTexCoord2f(0.690f, 0.279f); gl.glVertex3f(-2.229f, -0.875f, 0.000f); gl.glNormal3f(0.000000f, 0.000000f, -1.000000f); gl.glTexCoord2f(-0.383f, 0.314f); gl.glVertex3f(2.597f, -1.400f, 0.000f); gl.glTexCoord2f(0.332f, -0.209f); gl.glVertex3f(2.597f, -0.875f, 0.000f); gl.glNormal3f(0.994712f, 0.000000f, -0.102699f); gl.glTexCoord2f(-0.072f, 0.099f); gl.glVertex3f(2.730f, -1.400f, 1.294f); gl.glTexCoord2f(0.032f, 0.022f); gl.glVertex3f(2.730f, -0.875f, 1.294f); gl.glEnd(); gl.glBegin(gl.GL_TRIANGLE_FAN); gl.glNormal3f(0.000000f, -1.000000f, 0.000000f); gl.glTexCoord2f(-0.026f, 0.803f); gl.glVertex3f(-2.229f, -1.400f, 0.000f); gl.glTexCoord2f(-0.383f, 0.314f); gl.glVertex3f(2.597f, -1.400f, 0.000f); gl.glTexCoord2f(-0.072f, 0.099f); gl.glVertex3f(2.730f, -1.400f, 1.294f); gl.glTexCoord2f(0.419f, 0.757f); gl.glVertex3f(-2.970f, -1.400f, 1.410f); gl.glEnd(); gl.glBegin(gl.GL_TRIANGLE_FAN); gl.glNormal3f(0.000000f, 1.000000f, 0.000000f); gl.glTexCoord2f(0.690f, 0.279f); gl.glVertex3f(-2.229f, -0.875f, 0.000f); gl.glTexCoord2f(0.511f, 0.690f); gl.glVertex3f(-2.970f, -0.875f, 1.410f); gl.glTexCoord2f(0.032f, 0.022f); gl.glVertex3f(2.730f, -0.875f, 1.294f); gl.glTexCoord2f(0.332f, -0.209f); gl.glVertex3f(2.597f, -0.875f, 0.000f); gl.glEnd(); // barrel gl.glColor3f(0.1f,0.1f,0.1f); gl.glShadeModel(gl.GL_SMOOTH); gl.glBegin(gl.GL_TRIANGLE_STRIP); gl.glNormal3f(0.0f, -1.0f, 0.0f); gl.glVertex3f(1.570f, -0.18f, 1.530f); gl.glVertex3f(4.940f, -0.126f, 1.530f); gl.glNormal3f(0.0f, 0.0f, 1.0f); gl.glVertex3f(1.570f, 0.0f, 1.710f); gl.glVertex3f(4.940f, 0.0f, 1.660f); gl.glNormal3f(0.0f, 1.0f, 0.0f); gl.glVertex3f(1.570f, 0.18f, 1.530f); gl.glVertex3f(4.940f, 0.126f, 1.530f); gl.glNormal3f(0.0f, 0.0f, -1.0f); gl.glVertex3f(1.570f, 0.0f, 1.350f); gl.glVertex3f(4.940f, 0.0f, 1.410f); gl.glNormal3f(0.0f, -1.0f, 0.0f); gl.glVertex3f(1.570f, -0.18f, 1.530f); gl.glVertex3f(4.940f, -0.126f, 1.530f); gl.glEnd(); gl.glShadeModel(gl.GL_FLAT); gl.glBegin(gl.GL_TRIANGLE_FAN); gl.glNormal3f(1.000000f, 0.000000f, 0.000000f); gl.glVertex3f(4.940f, 0.0f, 1.410f); gl.glVertex3f(4.940f, 0.126f, 1.530f); gl.glVertex3f(4.940f, 0.0f, 1.660f); gl.glVertex3f(4.940f, -0.126f, 1.530f); gl.glEnd(); gl.glPopMatrix(); } }