Java-Gaming.org Hi !
 Featured games (84) games approved by the League of Dukes Games in Showcase (575) Games in Android Showcase (154) games submitted by our members Games in WIP (624) games currently in development
 News: Read the Java Gaming Resources, or peek at the official Java tutorials
Pages: [1]
 ignore  |  Print
 Car Physics Problem  (Read 1595 times) 0 Members and 1 Guest are viewing this topic.
Ikaron

Junior Newbie

 « Posted 2013-08-24 15:14:58 »

Hello. I have started developing a racing game, so I started by implementing the car physics by the guide of "Marco Monster", as well as a little "debug screen". It is partly german though, so here a little translation:
"Geschwindigkeit"="Speed"
"LW-Kraft"="Air resistance force"
"RW-Kraft"="Rolling resistance force"
"Beschleunigung"="Acceleration"

Anyways, I'm having a few problems here. First of all, I'm tracking the wheel speed separately, this works pretty well for high speeds, but at low speeds it just spazzes out. Also, if you're not accelerating, the wheels decelerate wayyy to fast. The third problem is that the maximum speed is about 180 km/h (or 112mph) in 4th gear, because in 5th and 6th gear, the car continues to decelerate (This is pretty logical, considering the engine torque goes down, because of that the driving force goes down, so the air resistance is greater than the driving force, which decelerates the car, but that shouldn't happen, correct?)

Here is the most important part of the source:
 1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16  17  18  19  20  21  22  23  24  25  26  27  28  29  30  31  32  33  34  35  36  37  38  39  40  41  42  43  44  45  46  47  48  49  50  51  52  53  54  55  56  57  58  59  60  61  62  63  64  65  66  67  68  69 public void update(float delta, boolean accelerating) {   float speed = v.length();   Main.renderStringFormatted(29, "%d", (int) rpm);   Main.renderStringFormatted(30, "%d", gear + 1);   Main.renderStringFormatted(0, "Geschwindigkeit: %.1fkm/h", speed * 3.6);   rpm = wheelSpeed * vehicle.getGearRatio(gear) * 30 / Constants.PI;   if (rpm < vehicle.getMinimumRPM())      rpm = vehicle.getMinimumRPM();   if (rpm > vehicle.getMaximumRPM())      if(shiftGearsUp())         rpm = wheelSpeed * vehicle.getGearRatio(gear) * 30 / Constants.PI;      else         rpm = vehicle.getMaximumRPM();   float Fdrive = vehicle.getEngineTorque(gear, rpm)         / vehicle.getTireDiameter();   Vector2f Ftraction = accelerating ? dir.mul(Fdrive) : new Vector2f();   Vector2f Fdrag = v.mul(-vehicle.getShapeDrag() * speed);   Vector2f Frr = v.mul(-vehicle.getShapeDrag() * 30);   Vector2f Flong = Ftraction.add(Fdrag).add(Frr);   Main.renderStringFormatted(1, "LW-Kraft: %.1fN",         Fdrag.lengthSigned(dir));   Main.renderStringFormatted(2, "RW-Kraft: %.1fN", Frr.lengthSigned(dir));   float Wr = vehicle.getBackTireLoad(Flong.lengthSigned(dir)         / vehicle.getMass())         / (vehicle.getMass() * 9.81f);   Main.renderStringFormatted(5, "Load V: %.2f%s", Wr, "%");   Flong = Ftraction.mul(Wr);   Vector2f Ffriction = new Vector2f();   if (speed > 0) {      float slip = (wheelSpeed * vehicle.getTireDiameter() - speed)            / speed;      Main.renderStringFormatted(4, "Slip: %.2f", slip);      Ffriction = Ftraction.mul(Math.min(16.6666667f * slip, 1));   }   Flong = Flong.add(Frr);   float Tfriction = Ffriction.lengthSigned(dir) * vehicle.getTireDiameter();   float Tdrive = Flong.lengthSigned(dir) * vehicle.getTireDiameter();   float Ttotal = Tdrive - Tfriction;   float aa = Ttotal / vehicle.getRearTireInertia();   wheelSpeed += aa * delta;   Main.renderStringFormatted(6, "Tfriction: %.2fNm", Tfriction);   Main.renderStringFormatted(7, "Tdrive: %.2fNm", Tdrive);   Main.renderStringFormatted(8, "Ttotal: %.2fNm", Ttotal);   Main.renderStringFormatted(9, "Angular acc.: %.2frad/s^2", aa);   Main.renderStringFormatted(10, "Wheel vel.: %.2fkm/h", wheelSpeed * vehicle.getTireDiameter() * 3.6);      Flong = Flong.add(Fdrag);   Vector2f a = Flong.div(vehicle.getMass());   Main.renderStringFormatted(3, "Beschl.: %.2fm/s^2", a.lengthSigned(dir));   v = v.add(a.mul(delta));   if (dir.dot(v) < 0)      v = new Vector2f();   pos = pos.add(v.mul(delta));}

And here is a SSCE: download (Source code/debug scripts/LWJGL/runnable jar included, start the debug.bat for your system)

mentalfloss

Junior Newbie

Medals: 1

 « Reply #1 - Posted 2013-09-01 03:41:29 »

I want you to take a week off from programming this and come back to it. When you come back, make note of how little sense this code makes to you. From this lesson, realize what you can change about your code so that it is more readable after taking a break. Now imagine never having seen the code and this is what you're presented with. I see why this has almost 100 views and no input.

So, I took the red pill and decided to see what this was all about. Some things impressed me - some things scared me.

Anyway, whenever there is weird behavior in a program, I immediately try to find out where all of the pertinent divisions are. Why? Because divisions are difficult and error prone. I do not know much about your domain here. I do however believe that when you getBackTireLoad, it should never be passed a value like this: -6.943892E-4. Let's just look at how that code is done since you didn't include it:

 1  2  3  4 public float getBackTireLoad(float a) {    return 4.905f * mass + (shape.getCenterOfMassHeight() / shape.getAxleDistance()) * mass * a;}

What in the world is 4.905? Where did you get it? What does it mean? When we explore the Car Physics page you presented, it *can* be located as: a = 7350 N / 1500 kg  = 4.9 m/s2  (=0.5 G)  but why not just put it in a comment so I know what's going on?

So, to get the Wr we do this:

4.905f * mass + (shape.getCenterOfMassHeight() / shape.getAxleDistance()) * mass * (Flong.lengthSigned(dir) / vehicle.getMass() / (vehicle.getMass() * 9.81f))

(I just combined the formulas)

You need to do error checking on your division results. I'm going to keep looking at this and toying around with it to see how it ticks but I suggest that if you want to move forward for now, you start there.
Pages: [1]
 ignore  |  Print

You cannot reply to this message, because it is very, very old.

 ClaasJG (30 views) 2015-04-27 13:36:51 BurntPizza (34 views) 2015-04-23 03:42:11 theagentd (37 views) 2015-04-22 16:23:07 Riven (51 views) 2015-04-16 10:48:47 Duke0200 (60 views) 2015-04-16 01:59:01 Fairy Tailz (44 views) 2015-04-14 20:13:12 Riven (47 views) 2015-04-12 21:36:37 bus hotdog (66 views) 2015-04-10 02:39:32 CopyableCougar4 (67 views) 2015-04-10 00:51:04 BurntPizza (72 views) 2015-04-06 22:06:58
 theagentd 23x BurntPizza 17x Spasi 17x wessles 15x alwex 11x kingroka123 11x Riven 7x kevglass 7x Olo 7x Ecumene 7x ra4king 7x Rayvolution 7x Hanksha 7x chrislo27 7x KevinWorkman 6x trollwarrior1 5x
 How to: JGO Wikiby Mac702015-02-17 20:56:162D Dynamic Lighting2015-01-01 20:25:42How do I start Java Game Development?by gouessej2014-12-27 19:41:21Resources for WIP gamesby kpars2014-12-18 10:26:14Understanding relations between setOrigin, setScale and setPosition in libGdx2014-10-09 22:35:00Definite guide to supporting multiple device resolutions on Android (2014)2014-10-02 22:36:02List of Learning Resources2014-08-16 10:40:00List of Learning Resources2014-08-05 19:33:27
 java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org