The Re-Inventing the Wheel Thread

Looking good :slight_smile: If you want to keep the car from rolling over, you could add an anti-roll bar. For super simple implementation, you could check the script provided here: http://forum.unity3d.com/threads/how-to-make-a-physically-real-stable-car-with-wheelcolliders.50643/. This works nicely for arcade use and you can use multiplier (0.0-1.0) for it to make it less aggressive.

thanks
ive edited the first post with a download.
i think its wise to give away the basic version before i start messing around with it, so people can make of it what they want.

good job tegleg!!!

I tried to run the project but I got the message above and when I tried to recompile, I got a message saying that the project couldn’t be recompiled. How can I fix this problem?

You could right click the .uproject file and select generate project files, then open the generated .sln on visual studio and build it there.

did that get it working jeffmorris?
did i miss something out?

I tried to build the project but I got a lot of messages, some of them about missing files. Any suggestions?

I briefly tested tegleg’s project and got it running. What kind of errors you are getting?

I think project was built for UE 4.10 and VS2015, do you have them both installed?

UE 4.10 and VS2015 is correct,
ill add it to the first post, sorry

VS2015 requires “Common Tools for Visual C++ 2015” under “Programming Languages” in the setup program.

From https://www.unrealengine.com/blog/unreal-engine-4-10-released:

The truck acts like it has bad shocks and doesn’t have anti-roll bars.

yes thats true. glad you got it working
its not supposed to be a fully functional car simulation. its an incredibly basic implementation of suspension, steering and lateral grip as a c++ plugin.
nothing else has been added yet as i know nothing of simulating vehicles, the Physx sdk and its integration into ue4 or Unreal’s special brand of c++.
also its kind of a forced side project im learning/doing as and when i get time.

anyone is very welcome to add their bit to help make it better :slight_smile:

just added anti roll front and back, wow what a difference.
the code has been ripped apart somewhat to allow for the anti roll formula to work, so ill glue it all back together and delete the rubbish before i post an update.

2 things im struggling with atm.

low speed grip, obviously the ‘sticky friction’ idea is a no go. what other options are there?

longitudinal grip, or more how to work out from a given power input how fast the wheels should be spinning. an extremely simple engine + transmission setup if you will.

any ideas?
thanks

Glad to see you working so hard on this tegleg.

Steven

Take a look at my code if you want. I have a rather simple setup but it seams to be working well.
I don’t have anything what would be called Traction Force, everything is handled using friction. Friction force is split into X and Y components, so I can use two different friction coefficient for axis. Friction Force is applied to the chassis and part of if is applied to the wheel itself in the form of torque. This will make wheel rolling by itself and will transfer torque to transmission, either demanding more torque from engine to drive uphill or accelerate engine driving downhill.
On each simulation step I store torque of drive axle (because all of my wheels are drive wheels, technically only sprocket is connected to axle, but drive wheels push track to the ground creating friction). Torque of axle is effected by engine, rolling friction and friction from wheels (tracks). From Torque I get angular acceleration and from angular acceleration I integrate angular speed, which is used to calculate relative speed of the wheel and ground.

The algorithm is following:

  1. Calculate relative speed of wheel. Which means take into account both linear and angular speed of the wheel. So if wheel is rotating proportional to the speed with which vehicle is moving, then X component of relative wheel speed is 0. Y component is non zero if car is sliding sideways or is on a slope perpendicular to direction of slope.

  2. Relative speed of wheel is projected to plane of collision, from it you get X and Y components of the speed in collision plane (Z would be collision plane normal).

  3. Before we calculate friction force we need to calculate absolute maximum of friction force. Using Coulomb model we can establish following limit: FrictionForce <= FrictionCoefficient * NormalForce. NormalForce is a force of your suspension pushing on wheel, pushing on ground (or what ever else your wheel is standing on). Which means that in whatever way you calculate friction force, it’s magnitude should be capped by values of FrictionCoefficient*NormalForce. This means that if you car is moving sideways for some reason, so friction in Y axis is rising, then friction in X axis is going down and your wheels start to skid.
    If I remember correctly, I calculate maximum Friction Force separately for X and Y. Which is not completely correct but I assuming it’s close enough as we are working with projected speed.
    fMaxFrictionX = length(fFirctionCoeffX * vNormalForce)
    fMaxFrictionY = length(fFirctionCoeffY * vNormalForce)

  4. For X and Y we calculate Friction Force as:
    vFrictionForceX = vVelocityX * fFrictionCoeffX * vNormalForce
    vFrictionForceY = vVelocityY * fFrictionCoeffY * vNormalForce
    then we scale them by fMaxFirctionX and fMaxFrictionY, there is function in blueprints which clamps vector length to a specific size:
    vFrictionToApplyX = ClampVector(vFrictionForceX, fMaxFirctionX)

  5. Now we can simply sum up vFrictionToApplyX, vFrictionToApplyY and apply them to chassis using AddForceInLocation(). Where location is a collision point between wheel and surface.

  6. Last piece is to apply torque to wheel which is simply:
    Torque = Force * Wheel Radius

  7. Now our wheel has some torque but we need linear velocity of the wheel for step #1.
    7.1) First we calculate angular acceleration using this: AngAcc = Torque / MomentOfInertia
    7.2) Moment Of Inertia of wheel is a pre-calculated constant, For cylinder this formula can be used: I = 1/2 * mass * radius^2
    7.3) Knowing AngularAcceleration we can integrate angular velocity as: AngularVelocity += AngularAcceleration * DeltaTime
    7.4) now we can calculate linear velocity of the surface of the wheel due rotation of the wheel: WheelLinearVelocity= AngularVelocity * WheelRadius
    7.5) For step one we just need to add linear velocity due to the movement of the vehicle itself. I have a function in my code or you can use blueprint function (should be in C++ too) GetLinearVeloctyAtPoint(), it will take into account linear and angular velocity of the chassis. Calculate it for collision point. The final linear velocity that we project in step #2 is: RelativeWheelVelocity = WheelLinearVelocity + ChasisLinearVelocityAtPoint

I need to look at code to clarify some details. I don’t remember if all Friction force is applied or a remove part of it which applies torque to wheel. Another thing I don’t remember is how I handle maximum friction force with two separate friction coefficients.

@UnWheelModeller, its slowly getting there :slight_smile:

thanks BoredEngineer, ill try digest all that at some point.

im going to change it to use a poseable mesh instead of having separate static mesh wheels, so ill add an update to the first post.
its not that far off being usable for some kind of fast pace arcade game.

update v0.3 in first post
changed to poseable mesh, static mesh for collision and physics body. fixed wheel mesh ‘lag’ that appeared in v0.2.

i think this version is pretty much good to go for a fun arcade style game :slight_smile:

Good progress tegleg.

I noticed that you still have that Hit.ImpactNormal as suspension force direction from my earlier example. Ideally, you’d want the suspension to apply forces only in direction that suspension geometry moves, in simple arcade game you could just assume it’s the up vector of the rigidbody. If you have that impact normal as is, it’ll mean suspension can push the spring force in any angle, even on 89 degree slope. This can be an issue especially if you have inclined surface laterally, as then your current code would push the car sideways.

For the wheel positioning, wheels only start to lag if you move them by reading their world space coordinates as base location. That would make them lag one tick behind for obvious reasons (you read the location from the previous frame). If you just move wheels using their relative coordinates, you can handle suspension for example just by adjusting the wheels relative position and it’s Z axis. You can already store the relative suspension position from the linetrace and then use that on tick. Not sure how you’d do that with bones though, I’ve just used separate meshes for wheels. Separate meshes are a lot faster to setup and it solves the issue we have on unreals vehicles where wheels are “welded” to the car body. If you keep them as separate objects, you can swap the wheels, tires etc on the fly.