# Math needed to create a very simple 2D side view car game

I'm trying to create a 2D side view car game (something like Hill Climb Racing, but much more simpler), but I'm confused on the math and physics.

Most of the tutorials I found were about making a top-down view game. The rest referred to the Unity3D engine, which does all the math behind the scenes and uses complex objects, whereas I'd like to achieve my goal using simple objects, such as 2D vectors and abstract them on my own (that is, without any game engine).

The project I'm working on assumes that a car is a rectangle with 2 wheels, which are ellipses. It doesn't need to brake, turn back and speed up, it should just go straight ahead (and eventually stop in case it looses all of its velocity).

An example terrain the car would be driving on:

I can't figure out how to move the car on an irregular terrain, as well as how to rotate and move it when it falls from a hillock, etc.

Have you any idea how to move and rotate the car with respect to its current position and velocity?

• What have you built so far? How is your terrain's surface represented in your internal physics math? Commented Apr 23, 2019 at 14:04
• @DMGregory The terrain is rather sharp - at the moment it's just a polygon made by combining lines, but it can be improved to be smooth if needed. Commented Apr 23, 2019 at 14:21

I'm assuming you can understand pseudocode, and know the various terms I use. I also assume screen is defined as origin the top left and max width and max height being the right and bottom respectively where width is x and height is y. I also assume your wheels are always on the ground. I also assume your terrain is a segmented line curve.

Create your car, and assign it's center-on-screen location to a 3D vector, and do the same for your wheels if they're seperate objects. The third demension is the rotation of the object relative to the screen width, i.e. 0 rotation is completely horizontal and max is vertical. Max rotation should be ±90 degrees based on your question. Store the total height and width of the vehicle. Create your terrain, storing an array of 3D vectors, each being a location and a rotation of a line segment on screen. Also store height and width information for each of these objects. Create a gravity constant. Create a velocity variable.

Each frame: Conceptualize a line straight down from the center of the wheels by iterating and cacheing all the points where the screen point X equals the wheel center X and the screen point Y equals wheel center Y plus some small constant. Then, using those points, iterate through your terrain and find the one that intersects that line i.e. terrain.y - (terrain.width/2) < screen point y < terrain point y + (terrain width /2) and store it. This also gives you a triangle to determine the correct wheel and car angle based on the hypotenuse and the Pythagorean theorem. If the wheel intersects the terrain (same as above, just different vector now), move the wheels away from it by the difference so you stop clipping. Once you have that you can apply your gravity and velocity physics. Each frame your velocity updates to velocity - gravity if the angle of the terrain is positive or velocity + gravity if your angle is negative. Then subtract any terrain friction you might like. If your velocity is less than or equal to 0 then game over.

Not exactly the best algorithm, the iterations will impact performance at especially high object counts. The math will need tweaking if any of my assumptions are incorrect but something tells me your a smart human and will be able to adapt in that case.

• Thanks for your reply. I don't quite understand what do you mean by "Unity has built in 2d vectors for this." - I said I don't want to use any game engine, including Unity. The problem is all the source codes available are either written using a game engine (actually most of them are) or they're not really readable - it's hard to find out which part of the code is responsible for what I need (e.g. this one written in plain OpenGL and using a lot of hard-coded values. I don't really fancy deciphering that). Commented Apr 23, 2019 at 15:40
• I don't also need some "features" I mentioned in my question, so I thought someone could explain it (or just paste the required math) easier than the unreadable source codes available online. Commented Apr 23, 2019 at 15:40
• Ah I'm sorry I missed the lack of an engine. I'll update. Commented Apr 23, 2019 at 15:48
• The engines are specialized for these sorts of problems -- as an exercise in skill not using an engine is s good idea. But as an exercise in production, I'd argue that it is better to use the tool. The velocity math should handle being in the air, but you'll need to handle the rotation of the object in air differently. I hope that helped! Commented Apr 23, 2019 at 16:41
• Thank you very much! I've been looking for such a nice explanation, instead of reading a whole book about game programming for just a single exercise. Commented Apr 23, 2019 at 17:23