Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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.

share|improve this question
1  
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. –  Jon May 20 '13 at 11:56
1  
The above code was also converted to farseer (XNA), you can get it here: farseerphysics.codeplex.com/discussions/248169 –  Jon May 20 '13 at 12:01
    
@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.) –  Evgeny May 20 '13 at 12:35
    
I will post you some code below. –  Jon May 20 '13 at 12:36
1  
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. –  joltmode May 20 '13 at 13:16
show 1 more comment

closed as not a real question by msell, Anko, bummzack, Byte56, Josh Petrie May 23 '13 at 14:55

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.If this question can be reworded to fit the rules in the help center, please edit the question.

1 Answer

up vote 2 down vote accepted

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.Begin();
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);
spriteBatch.End();

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.

share|improve this answer
    
Thanks. Will try it. –  Evgeny May 20 '13 at 13:04
add comment

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