1
\$\begingroup\$

I have made a tank object that knows its position, speed, rotation, and a double value ranging from 1.0 to -1.0 for each tread. I want it to move base on those values. For example, if the value for the left tread is 1.0 and the value for the right tread is 0.5 the tank should follow a clockwise circular path. However, the closest I have come is when the left tread turns forward (positive) and/or the right tread turns backward (negative), the tank spins CCW and moves down the screen, while the reverse is true if the left tread turns backward and/or the right tread turns forward.

This is my code for moving the tank, where lts is the left tread speed and rts is the right tread speed:

rotation += speed * (rts / 12);
rotation -= speed * (lts / 12);
location.x += speed * (Math.cos(rts / 12) * 14.5);
location.x -= speed * (Math.cos(lts / 12) * 14.5);
location.y -= speed * (Math.sin(rts / 12) * 14.5);
location.y += speed * (Math.sin(lts / 12) * 14.5);
\$\endgroup\$
  • \$\begingroup\$ Curious how you've hooked up the input to this. How does a player affect one tread vs. another? \$\endgroup\$ – Jon Apr 3 '15 at 13:13
  • \$\begingroup\$ In the prototype, Q and A control the left tread (forward and reverse, respectively) and W and S control the right tread. However, I'm working on having a simple AI control the treads. \$\endgroup\$ – Giaphage47 Apr 5 '15 at 18:49
2
\$\begingroup\$

The equations of motion for a differential drive machine are as follows, where x, y, θ, and l are the 2D position, orientation, and drive shaft length (distance between wheels) respectively:

x(t)

y(t)

θ(t)

While a tank is not quite an ideal differential drive machine, this should be a sufficient approximation.

Translating this into code using a simple Euler integration:

// "speed" must be expressed in units per second
float velocityR = rts * speed;
float velocityL = lts * speed;

// "width" must be expressed in units
rotation += (velocityR - velocityL) / width * dt;
location.x += 0.5 * (velocityR + velocityL) * Math.cos(rotation) * dt;
location.y -= 0.5 * (velocityR + velocityL) * Math.sin(rotation) * dt;'

To make this work without a variable time step, you can keep your current speed value and remove the multiplication by dt (delta time). However, I strongly suggest that you move to a time step that is refresh-rate-agnostic.

\$\endgroup\$
  • 1
    \$\begingroup\$ +1 but, since it is arbitrary, 'width' should probably be rotationFactor, turnFactor, etc. Modifying the 'width' of the tank to vary the turn speed sounds funny; i.e. at open-throttle tanks cannot turn as fast, but are just as wide. (Considering rts/lts as a constant "desired" velocity) \$\endgroup\$ – Jon Apr 3 '15 at 13:10
  • \$\begingroup\$ Good call, I wasn't thinking about that. \$\endgroup\$ – jmegaffin Apr 3 '15 at 17:31
  • \$\begingroup\$ I had to change it so that location.y was being incremented rather than decremented, but then it was perfect! \$\endgroup\$ – Giaphage47 Apr 3 '15 at 21:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.