I am searching for a simple tutorial how to add arcade car physics for my game.

Exactly same user units behaviour i found in next games:

Tank Hero: Laser Wars enter image description here

Death Worm: enter image description here

Video: http://www.youtube.com/watch?v=tbJbvPwYT_I

Any good tutorials with simplified physics? Because if I understand this issue correctly I don't need any complex car physics to add unit behaviour like presented in those games.

  • 1
    \$\begingroup\$ Take a look at emanueleferonato.com/2009/04/06/two-ways-to-make-box2d-cars In my opinion, you will need to start trying a few things and then ask more specific questions about the implementation. \$\endgroup\$ – jgallant May 20 '13 at 11:56
  • 1
    \$\begingroup\$ The above code was also converted to farseer (XNA), you can get it here: farseerphysics.codeplex.com/discussions/248169 \$\endgroup\$ – jgallant May 20 '13 at 12:01
  • \$\begingroup\$ @Jon thanks for good links but looks like they are using physics framework and i need pure implementation. I am just started with game development so I even dont know that car phys will fit for this scenario. And also i am asking about good tutorials that people know for sure that they are correct.) \$\endgroup\$ – Evgeny May 20 '13 at 12:35
  • \$\begingroup\$ I will post you some code below. \$\endgroup\$ – jgallant May 20 '13 at 12:36
  • 1
    \$\begingroup\$ It looks that the Tank Hero: Laser Wars is actually a 3D game, using 2.5D camera, thus, probably using one of thousands physics libraries available, or even better - an engine that comes packed with it and more. \$\endgroup\$ – tomsseisums May 20 '13 at 13:16

Since I researched this in the past, I will post some code I had converted previously that may help you. This is basically the bare minimum of what you can do to achieve this effect. You will most likely want to add damping, and effects such as braking etc. It also works on a system where you only have two tires, one in front and one in back.

The code can be extended to add the two extra tires, but that will be up to you.

The variables that we will use:

Texture2D vehicle;
Texture2D tire;
Vector2 position;
Vector2 frontTire, backTire;
float direction;
float speed;
float angle;
float wheelBase = 32;

In our Update method:

KeyboardState kbstate = Keyboard.GetState();
if (kbstate.IsKeyDown(Keys.Up))
    speed += 10;
else if (kbstate.IsKeyDown(Keys.Down))
    speed -= 100;
if (kbstate.IsKeyDown(Keys.Left))
    angle -= 0.03f;
else if (kbstate.IsKeyDown(Keys.Right))
    angle += 0.03f;

if (speed < 0)
    speed = 0;
if (speed > 300)
    speed = 300;

frontTire = position + wheelBase / 2 * new Vector2((float)Math.Cos(direction), (float)Math.Sin(direction));
backTire = position - wheelBase / 2 * new Vector2((float)Math.Cos(direction), (float)Math.Sin(direction));

backTire += speed * elapsed * new Vector2((float)Math.Cos(direction), (float)Math.Sin(direction));
frontTire += speed * elapsed * new Vector2((float)Math.Cos(direction + angle), (float)Math.Sin(direction + angle));

position = (frontTire + backTire) / 2;
direction = (float)Math.Atan2(frontTire.Y - backTire.Y, frontTire.X - backTire.X);

And then draw:

spriteBatch.Draw(vehicle, position, null, Color.White, direction, new Vector2(vehicle.Width/2,vehicle.Height/2), 1, SpriteEffects.None, 0);
spriteBatch.Draw(tire, frontTire, null, Color.White, direction, new Vector2(tire.Width/2, tire.Height/2), 1, SpriteEffects.None, 0);
spriteBatch.Draw(tire, backTire, null, Color.White, direction, new Vector2(tire.Width/2, tire.Height/2), 1, SpriteEffects.None, 0);

This will provide you with the similar behavior as shown in your video. Again, you will need to tinker greatly with this code in order to get the exact behavior you are looking for.

| improve this answer | |

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