Tuesday, August 27, 2019

physics - 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:


Terrain


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?



Answer



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.



No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...