agateau / pixelwheels

A top-down retro racing game for PC (Linux, macOS, Windows) and Android.

Home Page:https://agateau.com/projects/pixelwheels

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Catch-up mechanic by relaxing max speed limit a bit?

complyue opened this issue · comments

I'm figuring out a method to implement catch-up, off the top of my head, I would give a behinder slightly higher max-speed limit, proportional to how far the other cars ahead of it.

But I'm some confused in reading the speeding code:

final float currentSpeed = mBody.getLinearVelocity().len() * Box2DUtils.MS_TO_KMH;
final float limit =
1 - 0.2f * Interpolation.sineOut.apply(currentSpeed / GamePlay.instance.maxSpeed);
amount *= limit;
float force = mMaxDrivingForce * amount;
float angle = mBody.getAngle();
Vector2 pos = mBody.getWorldCenter();
mBody.applyForce(
force * MathUtils.cos(angle), force * MathUtils.sin(angle), pos.x, pos.y, true);

Seems there will always be some force applied to the wheel, then how it actually get max speed limited?

if (groundSpeed < 1f && !turboOn) {
Box2DUtils.applyDrag(mBody, (1 - groundSpeed) * GP.groundDragFactor);
}

Also confused here why groundSpeed < 1f? I'd perceive 1 as a very small speed, is that the case? So the drag force only applies when it nearly stopped, so as to get it to completely still?

Seems there will always be some force applied to the wheel, then how it actually get max speed limited?

That's a good question indeed. It's been a long time since I wrote this code, but playing with it a bit, if I remove the "0.2" factor the vehicles are very slow. I guess given a long enough straight road they would accelerate indefinitely. Or maybe there is a cap in Box2D. This game is the first time I use a physics engine, and I sometimes don't fully understand what's happening under the hood (or I dive in to understand, manage to understand, then a few weeks later I have forgotten about it...)

Also confused here why groundSpeed < 1f? I'd perceive 1 as a very small speed, is that the case?

No, groundSpeed is a "normalized" speed, where 1 means normal, no change, <1 means slow down (sand, grass, water tiles), >1 means speed up (turbo tiles). See Material.getSpeed() for the actual values. The material of a tile is defined in the .tsx files defining the tiles (in android/assets/maps). You can look at them using Tiled.

Forgot to add: there already is a catch up mode. AI players ahead of the player are slowed down if they are better ranked, see AIPilot.updateAcceleration().

I created #437 to experiment with the idea, shall we move the discussion to there and close this issue?

I created #437 to experiment with the idea, shall we move the discussion to there and close this issue?

Sure.