2D Car Physics
IntroductionSomeone asked me in the #gamedev IRC channel about how to make a 2d vehicle simulator. Instead of spending all day trying to explain the concepts to him, I decided just to write this tutorial. Please bear with me, this is my first tutorial. So as I mentioned we’re going to be learning how to make a basic 2d vehicle simulator. We’re going to do it in C# and try do use as few hacks as possible. I’ve broken the process down into three steps. First, we will learn how to setup a basic game application in C#.NET and how to draw some basic graphics (emphasis on basic.) Next, we will learn how to create a rigid body simulator using a simple Euler integrator with a variable time step. And last but not least, we will calculate vehicle forces simulating the tire patch contacting the road. And that’s all there is to it! Let’s get started. Math RequirementsThere are two ways to get through this tutorial: you can rush to the end and download the project, or you can read through it and hopefully I’ll be able to explain things clearly. If you choose the second route, you’re going to need to have a bit of math background. In a 2D simulation this is mostly in the form of a vector object. You’ll need to be able to add, subtract, dot, and project 2 vectors. Also you’ll need to be able to use a cross product. In 2D this is kind of a fake situation since we know the result will point in the screen’s direction, so the result is returned as a scalar. If you’re not familiar with any of these terms please look them up now. I tried to write this tutorial without using a matrix object but eventually I cracked and used the Drawing2D.Matrix object to transform and inversely transform a vector between spaces. If you don’t know what I’m talking about let me give you an example. Let’s say your personal body is your “local space” and the room you’re sitting in is the “world space.” Let’s also say that your monitor is the front of the world, and your eyes look in the forward direction of your local space. If you turn sideways, and transform the monitor’s direction into your space, it is now the side direction. Vise versa, if you transform your facing direction into world space, it is the opposite side direction. This is a critical concept so please, if that didn’t make sense, do some searching on Google for transforming between spaces. The reason this is so important is because we will be doing all of our vehicle force calculations in local vehicle space. Yet the vehicle itself, and its integrator, persist in world space. |
|