The Re-Inventing the Wheel Thread

You don’t necessarily need a custom plugin for that, just remove the gravity from the main rigidbody and add the gravity force yourself on every physics step. Of course the rules how to apply it depends on many things on your gameplay.

If you just base the rules for gravity direction by ground normal, you’d simply add force to your rigidbody with value that is -980 * mass_of_the_rigidbody * ground_normal where 980 is the gravity value on unreal.

Turning simulate physics on to do that actually broke my makeshift kart’s movement but I think my test showed enough to try changing the mechanics to use physics.

Btw, I recently found out why I initially didn’t get addforces to act right on custom physics (on substepping). You can use AddForce and AddForceAtPoint just fine on substeps, but you need to change the default value of bAllowSubstepping to false. If you don’t set it separately, it’ll be set to true and (I think at least) it’ll spread the addforce to all substeps between full physics cycle and not just for next substep. If you set it to false, BodyInstance will send the AddForce call to physx directly. For example, simple AddForce when using substepping would look like:


BodyInstance->AddForce(Force, false, false);

or

BodyInstance->AddForceAtPosition(Force, Position, false);

you can omit the last false from the AddForce() if you want as it’ll default to false anyway.

If you however call just either:


BodyInstance->AddForce(Force);

or

BodyInstance->AddForceAtPosition(Force, Position);

it’ll default the bAllowSubstepping bool to true and you end up spreading the forces on the physx substepper which doesn’t work for our purposes (it does work if you just calculate your forces on regular tick). Hope this helped to clear out things out as I sure missed this before :slight_smile:

I changed my substepping example to show this as well.

[QUOTE=
Turning simulate physics on to do that actually broke my makeshift kart’s movement but I think my test showed enough to try changing the mechanics to use physics.
[/QUOTE]
You’d keep simulating physics of course, but you can untick the gravity setting for each physics object individually.

I think I already turned that off in the process of playing /experimenting around when I got the cheap trick of doing hover wheels (like mario kart 8) and having a basic motorcycle working.

Adding a second car in the plugin project has an issue, second player is basically glued in place. I’ll be looking into it more when I get the chance.

it shouldnt be too hard to get it working with that gravity plugin. take a look at how it handles its custom physics actor. this car is basically just a rigid body actor, no special movement component or anything to worry about.

right so i think i have a Pacejka tire model working. the class is becoming too large to handle easily so i want to move appropriate parts into components, eg wheelcomponent ect
im having trouble making components run on substep.
there is only 1 physics body in the main pawn class and im trying to have the components addforce ect but everything i try crashes or does nothing.
setting up customphysics deligate on the components crashes, guessing its because the 1 physics body is being ‘substepped’ multiple times.
calling a function on the component from the pawn substep does nothing.
how are you supposed to handle this?

thanks

I’ve just used regular c++ functions on components for that use (with no UFUNCTION macro). If it doesn’t do anything, you probably don’t reference things right. For example if you call a function on some custom scenecomponent, it has to know what BodyInstance it needs to add forces to.

If you have trouble figuring if the code executes somewhere or not, just use breakpoints to debug it.

yep you are right calling a function does work. it was something else wrong in my code that made it appear not to work.
thanks again :slight_smile:

Does anyone have any suggestions of where to start for hitchable trailers? I tried making a 2 wheeled trailer with 2 “False wheels” but cant come up with a good way to be able to “hitch” it to another vehicle to pull it. Its for a pet project. The closest amount of succcess I had was with a physics constraint component but I’ve heard of people using spring arms too? Maybeuse a spline mesh somehow as the “connector”. Or would it be agood idea to dig into physx’s NWheel API?

Once I finish learning enough Java in school I’ll move ontos UE4’s API and maybe one day complete my pet project. Vehicles just arnt fun in ue4,

I did it using constraint actor. The setup was a bit weird by that was the only way to get it working. The truck had invisible physics box component attached to its root static mesh on a back using constraint component, similar box was attached in front of the carriage, connect to carriage root body with constraint component too. Inside of the truck blueprint, on event begin play, I spawn constraint actor and provide it references not of the actors but box components inside of truck and carriage actors. To get references to components I’ve used simple interface.
This way two actors will be connected together using those box components and you just need to turn constraint actor into a ball constraint. If connections is not too stable then just play with masses of components.

I think I still have all of this code inside of my Tracked Vehicles repository on GIT. The truck with connected carriage is a huge 8 wheeler car. Feel free to take a look at it.

ill take a look at that, thanks. ive tried all sorts to get a trailer working without it going crazy.

Don’t know how readable is that blueprint, this is schematics:

Thanks I downlaoded your project will check it out for sure. No c++ it seems according to git?

How did you get the trailer wheels to turn, I assume the trailers were not WheeledVehicle classes and rather had some sort of skeletal mesh with animations that played at a certain speed to spin the wheels based on velocity?

I took your advice and made this setup and it works perfectly. I just have to play with the angular limits and other settings to get it to “turn and follow” like a regular trailer


For the other guy trying to make a trailer… I took the gun_carriage BP and reparented its parent class carriage to be pawn instead of actor so that it could be possed and added a camera above the back coupler then just did the above BP, posseed the first gun_Carriage exactly like in the stock level bp and pulled on them with the manipulators. Its touchy but I think once I start playing around with trailers built off your design and using the physics constraint spawned when I want to “hitch” a ttrailer and using a regular wheeledVehicle class for the pulling vehicle I’ll be in business.

Much thanks! Your project has spawned hope in a pet project that goes back since 2013! Particularilly the gearbox and engine setup. I dunno why I never thought to use curves for an engine torque curve for example. Having to rely on the stock wheeledvehicle class which we all know sucks ATM for doing any sort of “pulling” or wheel turning has always caused my ideas to fail. Your vehicles are so rock solid I should be able to use them for inspiration. I’m not making tracked vehicles (well maybe a few later) so that makes it much easier. Thank you :slight_smile:

If I recall correctly they are simply rigid bodies connected by physics constraint to a static mesh of the carriage.

In case if someone wants to use PhysX classes for this than NoDrive class is a way to go. I’m not sure if pulling one PhysX vehicle by another can work in UE4, it should work as long as they behave as physics simulated entities and not kinematic like pawn for example.of carriage.

Hi Tegleg,

I found a major math flaw in my friction code. We had this expression for the force necessary to completely stop vehicle from moving: -velocity*mass
But technically it’s a moment and should be used as impulse not as force! The proper math should be:

  • velocity * mass / delta time
    which makes it a much larger vector.
    Furthermore it should be divided by the amount of contact points where friction is calculated. So first I’m counting how many wheels or other surfaces are in contact with ground. Then for each of them “stopping force” becomes:
  • local relative velocity * vehicle mass / delta time / number of contact points.

The other good result from tests is ability to use regular OnCimponentHit event result to run custom friction code. Normal impulse / delta time, can be used as normal force. Benefit of this approach is that it allows to calculate friction with parts of the car not connected to any kind of suspension.

I briefly tested ComponentSweepMulti for wheel tracing. You can use it to sweep any custom collider against geometry. I did get it working but of course the performance wasn’t anywhere near as good as with traditional tracing. It could however work pretty well on games where you only have few vehicles (with 4 or less wheels).

Edit-> Just realized that I actually tested it only against primitive collisions, I don’t have test environment for this anymore so can’t test it against landscapes but I don’t expect it to work there.

I’ve tried SweepTrace from blueprints and it did worked for tanks but had serious issues with scaled geometry. Periodically it would push wheels underground to collide with an edge of the scaled box collision.