#### Archived

This topic is now archived and is closed to further replies.

# Basic 2D tank movement

This topic is 5240 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I'm having trouble getting the 'feel' of a simple tank game. The basic problem is that I'm not sure how to simulate rolling objects. The tank has a left tread and a right tread. The way the code works now is this: 1) If the user presses 'up', the tank accelerates in the direction it is facing 2) If the user presses 'down', it reverses 3) 'left' and the tank rotates left by a constant amount. Friction is applied perpendicular to the velocity vector in the direction of the left tread (pulling it to the left). 4) 'right' and " " " " " 5) Friction is applied opposite to the direction of the tank's velocity Instead of the tank rolling along the direction its treads are pointing, it continues to 'slide' in the direction it was moving, simply because I don't know how to implement the rolling behavior in terms of vectors. Can someone please shed some light on this? Note: I only need something simple that 'feels' right, nothing too fancy.

CVector zaxis(0.0f, 0.0f, 1.0f);

g_accel.Clear();
// friction1 = drag it backwards, 2 and 3 = left and right treads

friction1.Clear();
friction2.Clear();
friction3.Clear();

if (g_keys->keyDown [VK_UP] == TRUE) {
g_accel = g_facing * 50.0f;
}
if (g_keys->keyDown [VK_DOWN] == TRUE) {
g_accel = g_facing * -50.0f;
}

if (g_keys->keyDown [VK_LEFT] == TRUE) {
g_angle -= 60.0f * delta;
g_facing.RotateZ(-60.0f * delta);
g_facing.Normalize();

friction2 = g_vel;
friction2.Normalize();
// make it perp to the velocity

friction2 = CrossProduct(friction2, zaxis);
friction2 *= 20.0f;
}
if (g_keys->keyDown [VK_RIGHT] == TRUE) {
g_angle += 60.0f * delta;
g_facing.RotateZ(60.0f * delta);
g_facing.Normalize();

friction3 = g_vel;
friction3.Normalize();
// make it perp to the velocity

friction3 = CrossProduct(friction3, zaxis);
friction3 *= -20.0f;
}

if (g_angle > 360.0f) g_angle -= 360.0f;
if (g_angle < 0.0f) g_angle += 360.0f;

// make the tank gradually slow down

friction1 = g_vel;
friction1.Normalize();
friction1.Invert();
friction1 *= 20.0f;

// sum the forces

g_accel += friction1;
g_accel += friction2;
g_accel += friction3;

g_vel += g_accel * delta;
g_pos += g_vel * delta;

[edited by - paulrpaul on January 15, 2004 7:08:19 PM]

##### Share on other sites
for a simple tank game, you can fudge it more easily, by doing

tank.accel = (g_keys->keyDown[VK_UP])? 50.0f : (g_keys->keyDown[VK_down])? -50.0f : 0.0f;

tank.heading += (g_keys->keyDown[VK_LEFT])? -60.0f * delta : (g_keys->keyDown[VK_RIGHT])? + 60.0f * delta : 0.0f;

tank.speed += (tank.accel * delta) - (tank.speed * friction);

tank.Dir = Vector(cos(tank.heading * PI / 180.0f), sin(tank.heading * PI / 180.0f));

tank.Pos += tank.Dir * (tank.speed * delta);

if you really want to use friction and treads, then you have to use dynamics, where you apply a different amount of force on each tread, and use slip angles for friction.

##### Share on other sites
That works perfectly. I was making it way too complicated. Thanks!

no problemo

• 20
• 10
• 19
• 14
• 20