L-Tec Tracked Tank suspension (C++)

L-Tec Tracked Tank suspension (C++)

Hi,

i am L-tec, an old farting dude looking for some new challenges.
Recently, about 5 weeks ago, i found UE4 on the net, played a bit around with the starter content and got addicted somehow.

Since i do not know anything about UE4 and how it works but have some minor knowledge in C++, i started experimenting a bit with code snippets trying to understand the linking between UE4 and the code itself.

This is my hopefully somedays epic journey of wins and fails. I would really like to share this with you.

Some stuff to mention:

  • this is just my single person hobby project, no team for gfx, sounds whatever. It will be a slow but continouus (well i hope) journey of multiply fails and some small archievments.
  • please do not spam, flame or whatever, i will not comment on any bs-talk.
  • since i failed totally in attempting to code myself, i joined an Unreal Coding tutorial on the net, which really helped me a lot to get started. Thanks to Ben Tristem here. Some of the content you see is based on that tutorial, it is free to use and royality free.
  • english is NOT my native language, so please have some patience with me and my writing style.

Let’s start with a review of the last 4 weeks ( please keep in mind, i am no Pro Coder with tons of time and godlike abilities…)

This is the state of development 4 weeks ago:

Its basically what you get from Ben Tristems tutorial plus my first skript kiddy attempt:

Using constraints and there settings…so i got this:

A lot better then the one form the tutorial, which is just a moving block with massive physics issues, but imho a total fail.

Physics and Blueprint Constraints drive me mad, do not do what i want them to do…just a FAIL…attempt dumped.

So then i started coding this in C++

Here is state of work about 3 weeks ago:

Features:

  • 6 sockets measuring their height and the reflecting impact-normal to the terrain with a collision routine (HitResult with WorldStatic setting so far)

Each wheel(socket) calculates a progressive spring rate depending on the hit result, suspension inbound travel and suspension max inbound value.

An additional damping is applied when the suspension has high travel velocity to avoid too much bouncing of the tank after jumping

The calculated force is scaled with a cosine function and a small magic threshold by the impact normal from the HitResult and the rotational offset of a normailzed up-vector, rotated by the tnaks current orientation and then applied to the related socket.

Since i did not get angular movement into the tank (roll & pitch) just by adding force to different socket positions as i wanted to, I calculate an angular force impulse for each wheel(socket) by position of the soket and suspension inbound ratio and apply it to the tank.

  • spring rate and suspension heigt blueprint settable
  • damping for inbound and outbound blueprint settable
  • Any wheel with positive HitResult triggers the DRIVE function on its track side, so no speed can be applied when doing aerial stunts.
  • complete track-section with the hitevent is removed from code
  • the bug with the tracks getting stuck in the terrain and flipping the tank is fixed with this. There are no colliding components at all hitting the ground. If the tank jumps high and lands hard, then the suspension will inbound so much, that the tanks body collides with the terrain, just like in real life.

I am quite happy with the result so far, it needs a bit more tuning etc though.

Next step will be adding friction to the ground and calculating it into the tanks movement to get rid of the ApplySidewaysForce function, which is right now in use again.

This is the state of development 3 weeks ago:

Features added:

  • reworked the tank mesh, removed the wheels and sprocket, centered the mesh, fixed some issues and added a texture
  • 10 wheels, visible, moving and rotating…(what really gave me a hard time), wheel mesh reworkedt & textured
  • 10 x lower damper rod added, moving with the suspension
  • reduced suspension travel maximum and adapted the setup values for a better look and feel
  • all wheels full suspended and propelled
  • suspension damping reworked, feels and looks way better now

Here is the current state of development:

More to come…

About 2 weeks ago:

Todday ( after work) i started refactoring my mess and succeded…code is readable again, no function filling more then a screen, unnecessary stuff removed and documentation and variable names make sense now.

In Addition i added some…
Features:

  • tank wheels rotate now propper doing pivot turns from point pivot to one track at full stop, everything is fine
  • added a position forset to the wheels, at high speed they moved visually back and forth depending on driving forth and back. Could not find a bug in the code, but it looks like the main tank and the wheels as spawned components get rendered at different times in the visual frame. FIxed that by a calculating an offset depending on forward speed vector, i just ignored sideways velocity, cause there is never a visual effect, speed is never high enough sideways.
  • reworked the tanks physics and fiddled around with the suspension settings. Perfomred a hunt with 10 AI tanks and after 20 mins only 2 of them were upside down in a really rough terrain. Thats fine for me.
  • moved the DriveTrack function to the tankmovement component. it checks if one wheel of a track side is touching the ground, if so maximum power can be applied to that side.
  • fixed some minor bugs and todoos in the code, removed almost all magic numbers.

A short current state of progress video:

Stil to come: track visualisation, just looking into splines for realization

Aug 18th:

Features:

  • Added 13 tracktiles, no circular moving yet while driving forth and back, but got some maths figured out to place and rotate them following the landscapes normal provided by the Hitresult of the suspension code.

Actually i am calculating the tiles direct under the wheels and do some interpolation maths for the connecting ones to place and rotate them correct.
This will be replaced by using a spline, filling it with hitresult for each tracktile and then place the track meshes along the spline. This will finally allow me to do the circular movement of the track tiles too.
Yet a long way to go though…

I keep you updated.

Aug 20th:

Took me hours to understand splines, Bookwom style in Unrealdocumentation, Answerhub and Main Engines source code.
But got it finally working, makes life way easier with the track tiles :slight_smile:

New Features:

  • using a spline instead of calculating each tracktiles position and rotation in the code
  • added some more tiles, still have to make the mesh instancing procedural.

A video of the current state of progress:

I keep you updated…

AUgust 20th, later that day:

  • it rolls:

Aug 21th:

Looks abit similar to what you have seen before, but instead of ugly replicating Meshcode, i have managed to make my first C++ procedural Pointer-Array based Meshgeneration, Animation and Positioning method.
In addtion, the spline is now a real loop. Sounds easy, but gave me a hard time to implement correctly with hours of viually debugging the whole method. The Bug-Solution was in one of the last lines of code, a very very nasty typo error interferring with the second spline. But at least, i solved it, one more bug is wiped out, learned a lot about splines and had some fun too.

So now, with a second spline, i spawn 96 tracktiles like nothing, in addition its now about 350 lines less code, all refactured and pretty readable.

For me, this is a major achievment in coding with UE4.

Just for fun: A 100 tracktile whirling centipede whatever…lol

Aug 25th:

Complete spline track working now.
Everything you see is spawned procedural, Number of Wheels, Number and width of tracktiles to use per side, Position of Front and Rear Wheel/Sprocket is settable, suspension is settable with almost all values via Blueprint.

Visual Debugging the ranged Collision method of the track:

Aug 27th:

I need to change the actual method, because its clipping with edged objects:

A bit less tracing…not to get into problems with performance:

Aug 28th:

Meshed new castors for the track:

Swapped the collision method of the right track…which was a big mess, took me hours and 3 failed attemtps til i rolled back to codestate Aug, 27th and tried it again this morning.

Then i reworked the dropoff of the track in spacings, not perfect but the right way to go imho.
The new method makes a huge difference in tracktiles responding to objects, Right side (shot 1+3) is the new one…doooh:

Thats it, this is the current state of WIP

The story is of course very condensed, in future updates you will have more detailed information about my minor and major FAILS and some small WINS.
I will keep you updated.

UPDATE

Lower track castors added again.
Finding the right position for the wheel in relation to the spline gave me quite a hard time, took me 2 attempts to find the right and cpu-friendly maths to do so.
Result is quite ok imho:

I would like to call this minor viusal advance a small WIN

Watching the video…castor movment needs a minor smoothing though…

It’s a wondful project! Mabey you can make it a game like The World of Tanks!

Great job! I like your approach with simulating layout of the track instead of faking it with the wheels. Was thinking about doing something like this but I’m really worried about performance. How much does it take to run all raycasts for a single track?

Oh man, it’s nice! But, please, could give some advicesabout splines? It’s also necessarry for me in educational purposes. I also have bought ben’s course :wink:

Looks great. :cool:

Not much. it’s just line tracing and few math expressions. On my machine (4 GB RAM) it runs stable 60 FPS.