Rotate physics object towards camera direction over time

I was toying with this not so long ago and wrote a full [PID controller][1], 2 in fact. And it worked but become a small nightmare to maintain.

I then played with vSpring in the engine which is a simplified PID or so I assumed, but the results were underwhelming. Or I never figured out how to use it correctly.

And then I wrote this dumbed down version which worked surprisingly well for what I needed - minimum complexity and just enough fidelity and control. It works well in 6DOF and does not interrupt any other physics forces:

Image from Gyazo

At more snappy settings, 0.0025 and 5000 respectively:

Image from Gyazo

This does not account for time controls, you’d need to work that in. Promised myself not to touch PID-related stuff for a while.

Hope it helps at least a little.

4 Likes

Nice cross-post. I am going to look into this as soon as I recover mentally :wink:

1 Like

I have a jet vehicle pawn that uses physics to move and it has a camera with a spring arm. I got all the forward movement stuff working but I want to know how can I rotate the jet mesh with the Add Torque node or something to make it face the camera direction over a certain time. So for example if i look 90 degrees to the left then the jet mesh will turn to that direction over the period of 1 second.

I’ve been using the following setup to get my pawn roll to adjust to the floor beneath it (pitch and yaw are handled by the player)… it uses the MMT plugin PID controller (very simple. basically it’s a spring)… feeds in a generic error then multiplies by a factor that you manually set (needs adjustment).

If you need all 3 axes, you’d use 3 PIDS (yaw, pitch, roll)… feed in the delta between the camera’s rotation (inverted I think, because you’d like it to face the camera and not the same direction as the camera) and the player rotation (the error amount)

Then you’d convert from local space to AddTorque in worldspace (or AddForce, or AddRotation, anything)

alt text

thank heaven I didn’t even attempt to figure out actually writing the PID… mindboggling mindpower :slight_smile:

At the core it is a glorified spring, as you said; it works but I needed it to accumulate torque over time (account for it, too) and it was right there where my mindboggling mindpower failed me. :expressionless:

Anyway, glad I could vent.

1 Like

It’s pretty late for me right now but I’ll give this a try in the morning and then see if it works out.

That’s just an example, not a ready-to-go solution; I should have mentioned that. You will need to adjust it to your needs. Was going for a full 3d one. Not sure what you’re working on.

edit: do you need physics in the first place? Working with Torque is a challenge at times. Maybe you just need to vInterp / Lerp something.

1 Like

Ok I tried it but it the mesh just spins out of control, I’m probably doing it wrong though.

Also just for clarification, I want the jet to rotate in the same direction as the camera, so it copies its rotation basically rather than pointing at the camera.

This would work but I need the roll axis to stay the same cause other wise it wont really look good for a jet.

I’ve tried setting the rotation to the camera rotation with the set actor rotation node but that really doesn’t work well, it allows the jet to clip through walls when it’s in a small area and also causes a tonne of jerkyness.

Ok I’ve removed the multiplication of the delta seconds and adjusted the proportional coefficient.
Also the camera is just rotated with the mouse. I made the roll stabilization very low but it still keeps building up angular velocity and then spazzes out.

Yeah I’ve set the linear and angular damping and the only thing simulating physics is the jet mesh. Also yeah im using the mouse pitch and yaw to rotate the camera, and then I want the jet to follow it.

I don’t think we’re on the same page, the actual pawn isn’t being rotated by the controllers pitch, yaw and roll, the camera is being rotated though. Without the Add Torque node the pawn won’t rotate. I need the pawn to rotate into the same direction as the camera with torque.

One more thing… there are two approaches to stabilizing your physics speed across different machines… it’s a complication of working with UE4 Tick… a quite fast machine might Tick 300 times per second, a quite slow machine just 30 times per second. So you can imagine the difference in how the results would play :slight_smile:

Use the

  • Use the MMT physics plugin which will give you a regular tick, with substepping calculations. This takes a bit of understanding and setup. It’s what I use and it works very well but I’m thinking of trying the second option…
  • Multiply the AddTorque by WorldDeltaSeconds… this means that your Torque amount will vary in ine with each machine’s varying tick rate… I haven’t really tested it but I’ve read good things… Everynone may weigh in here with a more informed opinion than mine

One more thing… there are two approaches to stabilizing your physics speed across different machines… it’s a complication of working with UE4 Tick…

Use the

  • Use the MMT physics plugin which will give you a regular tick, with substepping calculations. This takes a bit of understanding and setup. It’s what I use and it works very well but I’m thinking of trying the second option…
  • Multiply the AddTorque by WorldDeltaSeconds… this means that your Torque amount will vary in ine with each machine’s varying tick rate… I haven’t really tested it but I’ve read good things… Everynone may weigh in here with a more informed opinion than mine

remember to change the Linear Damping and Angular Damping settings… this will help…

also, ensure that only one component in your Pawn is Simulating Physics

First, thing I’d suggest is to fix your control approach… are you using something like mouse Pitch and Yaw to move your pawn on these axes? If so… I can suggest something that is a bit different to your current setup, but simple and reliable

Ok! On your Player Pawn, the base class… turn off Use Pitch, Yaw and Roll… you’ll be using AddTorque to rotate your Pawn instead… Then use your mouse inputs to drive a Pitch and Yaw variable… multiplied by a suitable amount for your speed at which your Pawn will turn (I use about 100)…

Finally, add the Pitch and Yaw in your Pawn’s localspace, converting the vector to worldspace (don’t break this vector up… it’s been rotated by your Pawn’s transform). Get back here, I’m interested to ;earn how you get on

300535-uncheck-these.jpg

][3]

oh… this bit might be useful…

ah… alright then… for your setup, you’d need 3 MMT PIDs, one each for pitch, yaw, and roll…