NokiMo
Megan Fox
Megan Fox

patreon


Faking Physics is good

Just a teensy bonus post here on... well... this.

Ok, so. This code went through three iterations. First, I was forcibly setting wheel rotations in my normal Update cycle, which then controlled steering. This was... dangerous.

That fed into the anistropic friction code, which did run in FixedUpdate, and was responsible for steering. Without this, when you try and turn the board, nothing would happen. It'd be like skating on ball bearings.

Now the problem here is, well, that's a FixedUpdate cycle relying on what an Update is doing. Super dangerous. Depending on if you get a double physics step in a frame, or a half step, or whatever else, it's gonna be unpredictable. So first step was just this:

So we move the orientation setting inside the FixedUpdate, so that it's all reliable and consistent. This does work (did work) fine, but it's also incredibly dumb. Those wheels aren't actual wheels - if they were, I wouldn't be faking anistropic friction this way. Those wheels are balls.

I am forcibly setting the orientation, every single frame, of... constructs which literally have no directionality. I'm painting little dots on 4 ball bearings under the skateboard, and saying NO YOU GOTTA MAKE THE BLUE DOTS POINT FORWARD LIKE THE WHEELS BECAUSE BECAUSE. It's dumb.

Mind you, it's also convenient, because it means you can side-step a lot of hard thinking about linear algebra. Even if all I'm using those wheels for is essentially holding a rotation for me, I mean, the math involved in calculating that raw is... a bit weird.

It looks like this:

You'll probably need to stare at it for a sec to get it. I did a lot of making spatial basis vectors with my fingers and waggling my hands around, it's fine, you can be dumb like me. Or maybe you're genius and got it instantly, yay! The gist is, I'm raw calculating that rotation from the wheel. All I was using the wheel's rotation for was a converter - I could feed a local rotation into it, and then get the world rotation back. Which was super dumb.

The above is better. It's also totally, 100% fake now.

That's fine. It feels identical to how it used to, and now there's no chance of physical absurdity. It ALSO guarantees that if the wheel twitches in some weird way (which an actual wheel would never be able to do, but a ball on a phys-joint/spring DEFINITELY can do), my steering is unaffected. So faked is better!

Yay!

Comments

eagle eyed math types will notice a line there has the order of multiplication wrong on the quaternions. Should be: Quaternion wheelRot = Quaternion.AngleAxis(angle, boardUp) * skateboard.RigidBody.rotation; (without that, I found that steering stopped working on inclines, WOOPS)

Megan Fox


Related Creators