Jump to content

AddForce and OnUpdate vs OnFixedUpdate


Recommended Posts

Trying to understand how to properly apply forces.

1) If I want to apply a constant force, do I need to call AddForce in every update?

2) Conceptually forces are applied over time. So if I call AddForce, how long is the force applied, until the next update? ( ~1/30th of a second)

3) I see lots of Unity discussions saying physics should be done in FixedUpdate, but I am not seeing an option to overload such a function, only OnUpdate. Am I just seeing old documentation, or has KSP exposed FixedUpdate as OnUpdate?

For example, the below code attracts vessels to each other.


++updateNth;

if ( updateNth % updateResolution == 0)
{

var centerTransform = this.part.FindModelTransform("MagneticDockingRing");

foreach (Vessel vessel in FlightGlobals.Vessels.Where(v => v.loaded))
{
Vector3d vesselPosition = vessel.GetWorldPos3D();

if (Vector3.Distance(centerTransform.position, vesselPosition) < 800f)
{
Vector3d forceVector = (centerTransform.position - vesselPosition) * (float)updateResolution ;

vessel.rigidbody.AddForce(forceVector);
}

}
}

Link to comment
Share on other sites

1) Yes

2) Timewarp.(fixed)DeltaTime is the timestep for (fixed) update which will be the time any force application will happen over

3) all partModules can override OnFixedUpdate (and anything physics related should definitely be done in the fixed update cycle)

Edited by Crzyrndm
Link to comment
Share on other sites

Thanks! I must have had a syntax error somewhere cause now OnFixedUpdate is showing up.

- - - Updated - - -

Is there a reference to help with scaling measurements ingame versus code?

I.e. a force vector magnitude of 100f equates to what? 100N?

In-code distance of 50f == 50 meters ingame?

Link to comment
Share on other sites

Always, always, always do physics in FixedUpdate. Update() runs once per frame, which means your physics will be dependent on your framerate if you do stuff there (along iwth other bad things happening).

If you need to do physics when a part is not staged (as well as only after it's staged) use public void FixedUpdate() rather than OnFixedUpdate. FixedUpdate() is called by Unity every physics tick. OnFixedUpdate is only called by KSP when the part is activated.

Forces are timeless. You're not passing an impulse (in kN-seconds), you're passing a force. AddForce means "at the next intergration, act as if the object has this force applied." (integration runs after all FixedUpdates are run). Let PhysX handle time, as it should. :)

Link to comment
Share on other sites

This thread is quite old. Please consider starting a new thread rather than reviving this one.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...