Can anyone share top down car code?

Recommended Posts

I'd recommend trying to modify the asteroids code on your own to make it work like a car. This may be a bit harder but you'll understand your code better in the end and it'll be a really good exercise. You may not feel comfortable modifying the code and trying to get it to work but if you keep trying at it and every time you get stumped if you just ask for help or find a solution until you get it working, you'll be much more comfortable writing such code in the future.


for a simple car-style simulation where it just drives wherever the car is pointing you'd just change the way you calculate velocity. In asteroids, you take the velocity of the ship from the last frame and then add another small velocity to it, based on your direction and your amount of thrust. newVelocity = lastXVelocity + (xDirection * thrust). and same for the Y. xDirection is just a value, say -1 to 1, where -1 is pointing left, 1 is pointing right, 0 is pointing up or down, .5 is pointing right at a 45 degree angle, etc. For a car you can do speed and direction seperately. you need a variable for speed, and every frame you do speed = speed + acceleration. make hitting the gas increase acceleration and hitting the break decrease it. after you calculate the speed, you can find rotation. use an angle in radians to hold rotation. each frame you do rotation = rotation + turn. turn is just which way your turning. if you're pressing the left butting i think it'd be a positive number, right would be negative, and if your not turning the car it'd be 0. now that you have speed and rotation, use them to find your velocity. the yVelocity is sin(rotation) * speed, and xVelocity is cos(rotation) * speed. just do that to find your cars velocity every turn and it'll behave like a car. It won't account for traction and sliding though. Just because I feel like typing I'll try to make up a simple solution to simulate traction.



you could start with asteroids code because you need to get an 'asteroids style' velocity. find your asteroids style velocity(you may want to then divide it by 10 or something because you need to to be a smaller amount, you can mess around with that till you get a value that feels right. then calculate a 'car-style' velocity, which doesn't have momentum, when you turn your velocity changes to the direction your turning. you don't keep sliding like a ship but you turn like a car. Find it like I explained above.


Now to make it realistic, by having some form of traction simulated allowing you to slide if you turn to fast, you'll need to utilize both the car and the asteroids-style velocity. Have a traction variable, which represents how 'sticky' the car is, the higher the number the less likely it is to lose traction and slide. Now if the difference between your 'car velocity' and 'asteroids velocity' is more than the traction variable, set the velocity to be the average of the 2 velocities.


to calculate the difference between velocities, just represent each velocity as an x velocity and a y velocity. so for the car and asteroids velocity you'll have a unique x and y value, I showed you how to find the xvelocity and yVelocity for the car. then do sqrt((carX - asteroidX)^2 + (carY - asteroidY)^2), which is the distance formula, and if that is more than your traction variables value, set the realVelocity to the average of the 2. that'd be realX = (carX + asteroidX) / 2 and realY = (carY + asteroidY) / 2. sorry if I'm pointing out simple things for you, I'm not sure of your math background.


so if the difference between car and asteroid velocity is more than traction, calculate the realX and realY values as I did above and set that as the actual car velocity. If the difference is less than the traction, just use the car style velocity.


This is a simple solution and not physically accurate. It's basically the most basic way to simulate turning with traction and whatnot at all, and I didn't even test this out or do it ever so I'm not sure how good it'll turn out, but if you get the numbers right I imagine it could do just fine for any simple game.

Share this post

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    No registered users viewing this page.