banner



Gamemaker How to Make a Top Down Car

FTLRalph

Level 1
*

What?

View Profile


Hey guys.

I've been spending my week trying to get the down the basics of a top-down racing game I'm working on.  I'm really, really trying to focus on getting the movement just right, I want the car to feel like it has weight to it, you know?

It's just a few basic things, accelerating, skidding, and friction - for the life of me I can't seem to find a decent balance between these three components of the car.  No matter how I go about it, the car either feels like its on ice, its turning radius is unacceptable, it doesn't skid realistically, or any combination of the three.

I hate to ask this without providing some examples / code, but there's not much to show at this point.  I guess I'm asking more for advice rather than physical code (thought that may be helpful too Grin)

Note, I'm not looking for anything terribly realistic (for example, nevermind collisions at this point, or bouncing off walls, etc, I'm just focusing on some satisfying basic movement).

Does anyone have any pointers / tips / top-secret physics formulas on how to go about accomplishing this?  I'm starting to run out of ideas.

Logged
Xienen

Level 3
* * *

Greater Good Games

View Profile WWW


I would also suggest posting some more details on your physics model:

Are you using "proper" physics with velocity, acceleration, centrifugal force, and friction coefficients? Or are you attempting to fudge it together? Or somewhere in between?

Are you running physics on the 4 tires separately or are you just treating the whole car as a single entity?

Logged

Triplefox

Cheating is my recommended way to do most game physics, including cars. Instead of using physics equations to determine turn speeds etc., make up "turning equations" that vary your turn abilities with time accelerating, time turning, time braking, etc. - with actual velocities as a secondary factor. If you want a really definite drifting behavior make two or more sets of turning equations and toggle as you hit a braking threshold to make a "ghost" velocity appear and push the car sideways. If that toggle is too sudden you can crossfade between the results of each equation.

For even more tuning power, you can define the curves for these behaviors by interpolating on a set of predefined points (rather than trying to build a custom polynomial or something). Then you have the possibility of visually editing every curve to be exactly how you want it.

Lastly, you need to have a variety of track types tested in tandem with the car so you aren't overlooking some possibilities. Needless to say, not every car tuning will support every track style well, and in many cases you may have a completely unplayable mismatch where the car is great on one or two tracks and useless on the rest.

While you can get a very playable feel from a textbook Newtonian approach, you have to work a lot harder at it, and it isn't appropriate if the intention is to have a really tightly controlled, easy-to-play arcade game.

Logged

FTLRalph

Level 1
*

What?

View Profile


I appreciate all of the replies, like Udderdude said, it'd probably be best if I can provide some examples.  Here's my simplified code for the car's update() function, it may look complex, but its relatively simple.  I guess the point of interest would be the "calculate friction for skidding" portion.

// Acceleration.
// -------------
_accelerationScalar += (_maxAcceleration * _acceleratingDirection - _accelerationScalar) * (Math.pow(_accelerationEasing, deltaTime * _maxFPS));
acceleration.set(Math.cos(rotation) * _accelerationScalar, Math.sin(rotation) * _accelerationScalar);
velocity.add(null, acceleration.x * deltaTime, acceleration.y * deltaTime);
_speed = velocity.x * Math.cos(rotation) + velocity.y * Math.sin(rotation);

// Rotate the tires.
// -----------------
_tireRotation += (MAX_TIRE_ROTATION * _steeringDirection - _tireRotation) * (Math.pow(TIRE_TURN_EASING, deltaTime * _maxFPS));
_tireLeft.rotation = _tireRotation;
_tireRight.rotation = _tireRotation;

// Rotate the car.
// ---------------
if (_speed > 0)
rotation += _tireRotation * deltaTime * Math.min(_speed / TURNING_THRESHOLD, 2) * 2;
else
rotation += _tireRotation * deltaTime * Math.max(_speed / TURNING_THRESHOLD, -2) * 2;

// Calculate friction for skidding.
// --------------------------------

// What's going on here is this, I'm taking the difference of the
// car's angle vs the velocity angle (snapping it somewhere between
// 0 radians and Math.PI * 0.5 radians).  Then, I'm converting this
// number to something between 1 and 0.92 (or 0.5, if the car is not
// accelerating). My logic is, the higher the difference, the greater
// the degree of friction (think skidding at a 90 degree angle, you
// should come to a stop nearly instantly because of the friction).

var skiddingFrictionCoefficient:Number = 1.0;

var velocityAngle:Number = velocity.angle() * Vector2D.TO_RADIANS;
if (velocityAngle < 0)
velocityAngle += 2 * Math.PI;

var rotationAngle:Number = rotation;
while (rotationAngle < 0)
rotationAngle += 2 * Math.PI;

var difference:Number = Math.abs(velocityAngle - rotationAngle);
if (difference > Math.PI)
difference = 2 * Math.PI - difference;

if (difference > Math.PI * 0.5)
difference = Math.PI - difference;

var oldRange:Number = Math.PI - 0;
var newRange:Number = 0.0;
if (_speed == 0)
newRange = 0.50 - 1;
else
newRange = 0.92 - 1;

skiddingFrictionCoefficient = (((difference - 0) * newRange) / oldRange) + 1;

// Apply friction.
// ---------------
_friction = FRICTION_NORMAL * skiddingFrictionCoefficient;
velocity.multiply(Math.pow(_friction, deltaTime * _maxFPS));

// Limit acceleration and velocity, snap to 0 and apply upper limit.
// -----------------------------------------------------------------
/// etc...

// Move the car.
// -------------
x += velocity.x * deltaTime;
y += velocity.y * deltaTime;

Check out the swf here (use WASD):
http://fortheloss.org/misc/cars/Main.html

I have to admit, things don't appear to be absolutely terrible at this point, but there is still work to be done.  Try just tapping the acceleration button plus rotating the car and let go, the car shouldn't just kind of glide like that, it really shouldn't skid at all to be honest.

Anyway, hopefully this will help you guys help me.  I appreciate the replies so far.

Logged
ஒழுக்கின்மை

Level 10
* * * * *

Also known as रिंकू.

View Profile WWW


i'd suggest that your friction is too linear. it feels like you are reducing speed linearly (e.g. friction as subtracting a set amount from the speed each step). but that's not how friction works, friction works by reducing the speed by a *percent* each step, not by a fixed amount (game maker also does it by the fixed amount per step and it's annoying, although it's easy enough to code the percent thing)

also to avoid the ice skid thing, what you want to do is *stop the car* when the speed goes below a certain value. e.g. don't make really really small levels of speed possible, just make the speed stop when it's below a certain value and the gas isn't being pressed. think of it as the minimum force required to move the car: if you push a car with some force (try it) it will *not* move until the force is strong enough

Logged

Oddball

Level 10
* * * * *

David Williamson

View Profile WWW


but that's not how friction works, friction works by reducing the speed by a *percent* each step, not by a fixed amount (game maker also does it by the fixed amount per step and it's annoying, although it's easy enough to code the percent thing)

I think you are thinking of wind resistance. Friction has very little to do with the speed you are travelling, and more to do with the friction coefficient and the perpendicular force. By that I mean how rough the two surfaces are and how much force is pushing them together.

Logged

randomnine

You need to model longitudinal and lateral friction separately. This is the main reason your car feels like it's on ice. Tyres on tarmac resist sideways movement, hard, but will happily keep rolling forwards or back unless they lock up under braking. This is the effect that, for example, allows cars to carry speed around corners.

Also, skidding friction shouldn't be proportionate to your speed. It should absolutely be linear. On the other hand: cars also experience rolling resistance forces which are proportionate to speed, and air resistance which is proportionate to speed cubed.

Here, replace all your friction code with this and tweak as needed:

// assumes deltaTime is in seconds
// assumes velocity/speed etc is in metres per second

// split velocity into components
forwardsSpeed = velocity.x * Math.cos(rotation) + velocity.y * Math.sin(rotation);
lateralSpeed = velocity.x * Math.sin(rotation) - velocity.y * Math.cos(rotation);

// lateral friction: scrub off 7 m/s^2
// you may want to reduce this effect when accelerating at low speeds to get some basic powersliding and low speed manoeuverability
if ( lateralSpeed > 0 )
  lateralSpeed = Math.max( 0.0, lateralSpeed - 7*deltaTime );
else
  lateralSpeed = Math.min( 0.0, lateralSpeed + 7*deltaTime );

// combine components back into velocity
velocity.x = forwardsSpeed*Math.cos(rotation) + lateralSpeed*Math.sin(rotation)
velocity.y = forwardsSpeed*Math.sin(rotation) - lateralSpeed*Math.cos(rotation)

// air resistance; screw it, let's just stick with what we had
velocity.multiply(Math.pow(FRICTION_NORMAL, deltaTime * _maxFPS));

That's just the start, but it should make a big difference to non-skidding handling. I'm sure you'll want to experiment with changes to lateral friction to see how different approaches feel.

If you really want to get some life into your skids I'd recommend simulating longitudinal and lateral friction independently at each tyre, simulating rotational momentum of the vehicle, controlling the rotation of the car in response to forces at the wheels and simulating wheelspin/skidding by reducing lateral friction on tyres that wheelspin or slide at an angle greater than their maximum slip angle. This'll give you understeer, oversteer, spins, all that cool stuff - but will be a fair bit harder to tune and drive than something like this where you point the car in a direction and press forwards.

Logged

ஒழுக்கின்மை

Level 10
* * * * *

Also known as रिंकू.

View Profile WWW


but that's not how friction works, friction works by reducing the speed by a *percent* each step, not by a fixed amount (game maker also does it by the fixed amount per step and it's annoying, although it's easy enough to code the percent thing)

I think you are thinking of wind resistance. Friction has very little to do with the speed you are travelling, and more to do with the friction coefficient and the perpendicular force. By that I mean how rough the two surfaces are and how much force is pushing them together.

yes, i was inexact -- i mean all "resistant forces" -- friction, wind resistance, drag, everything put together

Logged

FTLRalph

Level 1
*

What?

View Profile


Wow randomnine, that post was incredibly helpful.  I did exactly as you suggested and it already makes a world of difference.  I'm still playing around with the numbers but if anyone is curious to see how much has changed:

Old:
http://fortheloss.org/misc/cars/Main.html

New:
http://fortheloss.org/misc/cars_fixed/Main.html

My problem now are my actual numbers, I didn't actually strictly stick to meters / kilometers per hour or anything when coming up with acceleration, max speed, etc (for example, in your randomnine's post, I have his 7 m/s^2 swapped out for 400).  I need to fix that now and get everything proportional to real-world numbers.

Next up, some more tweaking, and I also want to look into what you were mentioning about applying the longitudinal / latitudinal friction to each tire, that sounds exciting.  Then of course, screeching and tire marks, like Udderdude mentioned Wink

Thanks again to everyone!

« Last Edit: August 02, 2012, 04:44:45 PM by FTLRalph » Logged
Sean A.

I think your wheels are too close to the center of the car because your entire car isnt long enough, it looks too much like a square and should be elongated more. This would pull the wheels away from the center.

Logged
FTLRalph

Level 1
*

What?

View Profile


Thanks for the advice.

I was actually going for a square look, to be honest.

While designing the actual art, I was taking inspiration from go-kart types of vehicles more-so than actual cars.  I posted this screenshot of where the art is currently at just a few days ago.

Logged
bartholomew

Guest


hey,
don't know how indepth you want to go but this helped me a bit(eg i just dumbported the code to flash) when i was trying to do some car stuff: tut
the results are  something like this: test
if it feels weird it's probably because at some point i just put in fantasy values and at really slow speeds there are some weird issues i never got around to fix.

edit 2:
the link for the source in the tutorial doesn't work but i've found it lying around, might (still) be useful for... someone. get the zip

« Last Edit: August 11, 2012, 04:03:27 PM by bartholomew » Logged
Sean A.

Thanks for the advice.

I was actually going for a square look, to be honest.

While designing the actual art, I was taking inspiration from go-kart types of vehicles more-so than actual cars.  I posted this screenshot of where the art is currently at just a few days ago.

Oh ok that makes more sense then, nevermind

Logged

Gamemaker How to Make a Top Down Car

Source: https://forums.tigsource.com/index.php?topic=27692.0

0 Response to "Gamemaker How to Make a Top Down Car"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel