News › Forums › RAIN › General Discussion and Troubleshooting › Custom motors and the Kinematic class
Tagged: custom motor, physics, vehicle
This topic contains 3 replies, has 2 voices, and was last updated by angrypenguin 6 months, 2 weeks ago.
-
AuthorPosts
-
January 20, 2023 at 7:31 pm #39921
I’m looking at writing a number of custom motors for vehicles. The vehicles in my game are physics based, so the motor doesn’t apply motion directly - it will apply controls the the vehicle, which will result in forces, which will result in motion.
Before I get too far, I’ve a question about how the Kinematic class is used. My intent at this stage is to use the Velocity and Rotation fields to store the desired ideal values. In ApplyMotionTransforms() I will then set appropriate controls to move towards those ideals. In UpdateMotionTransforms() I will then read in the actual values from the Rigidbody for use in the next tick.
My question: is that usage of the Kinematic class is likely to cause issues elsewhere in RAIN, given that it will store desired values rather than actual ones?
- This topic was modified 6 months, 3 weeks ago by angrypenguin.
January 21, 2023 at 12:31 am #39925You should be OK. UpdateMotionTransforms occurs first in the update order, which means all of our code should have the actual values to work with (given what you are saying in your post). As far as ApplyMotionTransforms is concerned, even our own code is applying the ideal or desired velocities at that point, so it shouldn’t be much different.
One thing to watch out for: Normally in UpdateMotionTransforms we reset our velocity to zero before do our update. You’ll have to figure out how you want to handle this, as our movement code won’t take the prior velocity into account when figuring out where it wants to go. You should be able to handle this outside of our code though, depending on what you want to do.
Here’s our code for those two functions in case it helps (this is close to what the MecanimMotor does):
/// <summary> /// UpdateMotionTransforms updates the AI Kinematic structure with the current /// values associated with the AI Body. /// </summary> public override void UpdateMotionTransforms() { if (AI.Body.transform.parent == null) AI.Kinematic.ParentTransform = Matrix4x4.identity; else { AI.Kinematic.ParentTransform = Matrix4x4.TRS(AI.Body.transform.parent.position, AI.Body.transform.parent.rotation, Vector3.one); } AI.Kinematic.Position = AI.Body.transform.position; AI.Kinematic.Orientation = AI.Body.transform.rotation.eulerAngles; AI.Kinematic.ResetVelocities(); } /// <summary> /// ApplyMotionTransforms applies physical forces back to the AI Body /// </summary> public override void ApplyMotionTransforms() { // Note that if root motion is applied, then UpdateTransformData may produce incorrect results // because position and rotation are not directly controlled AI.Kinematic.UpdateTransformData(AI.DeltaTime); // Update mecanim parameters if we can if (UnityAnimator != null) UpdateMecanimParameters(); // Need to make sure we run this if we don't have an animator as well if (UnityAnimator == null || !UseRootMotion) { if (UnityCharacterController == null) AI.Body.transform.position = AI.Kinematic.Position; else UnityCharacterController.SimpleMove(AI.Kinematic.Velocity); AI.Body.transform.rotation = Quaternion.Euler(AI.Kinematic.Orientation); } else if (OverrideRootMotionRotation) { AI.Body.transform.rotation = Quaternion.Euler(AI.Kinematic.Orientation); } }
January 22, 2023 at 7:01 pm #39942Cheers! I’ll let you know how it goes once I’ve got a vehicle working.
January 22, 2023 at 9:35 pm #39945Well, it’s already on it’s way to working. Nothing really more to report than that, as the code is exactly what I described and otherwise still many iterations and a lot of tweaking away from being “done”, but my first vehicle is already moving between waypoints, driven by RAIN via a control adapter for my vehicle.
-
AuthorPosts
You must be logged in to reply to this topic.