rungo73 Posted January 29, 2014 Share Posted January 29, 2014 I'm building an old school top down racer but I can't get my car to drift when cornering.Can anyone share any code / tips / maths on how I could get a bit more 'fun' out of my car? Quote Link to comment Share on other sites More sharing options...
tipztv Posted January 30, 2014 Share Posted January 30, 2014 Do you use any game engine? Quote Link to comment Share on other sites More sharing options...
rungo73 Posted January 30, 2014 Author Share Posted January 30, 2014 Yes, using impact js for this one. Quote Link to comment Share on other sites More sharing options...
1-800-STAR-WARS Posted January 30, 2014 Share Posted January 30, 2014 Haven't got any code to share, but how about looking at some asteroids type code, where the velocity is increased in the direction the ship is facing, and then adding friction? That should add some 'swing' around corners. Quote Link to comment Share on other sites More sharing options...
rungo73 Posted January 30, 2014 Author Share Posted January 30, 2014 @Lewis - great minds.. that was my thought too. Unfortunately youget too much rotation with the forward momentum - no turning circle as such. Quote Link to comment Share on other sites More sharing options...
moe091 Posted February 2, 2014 Share Posted February 2, 2014 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.