public bool IterateAll(int stepCount, float timeDelta) { var didFinalUpdate = false; var keys = m_ProxyDef.Keys; // TODO: check that this doesn't cause any allocations for (var si = 0; si < stepCount; si++) { var didFrameUpdate = false; foreach (var pi in keys) { var def = m_ProxyDef[pi]; var proxyPose = m_ProxyMotions[pi]; var state = SolverFieldState.Create(proxyPose); if (CalculateForces(ref state, def, pi)) { state.didUpdate = true; didFrameUpdate = true; didFinalUpdate = true; } m_ProxyStates[pi] = state; } if (!didFrameUpdate) { return(didFinalUpdate); } foreach (var pi in keys) { var state = m_ProxyStates[pi]; if (state.didUpdate) { state.forces.AddMotionDragForces(); var forcedPose = state.forces.ApplyForceToMotion(timeDelta); var oldPose = state.worldMotion; var def = m_ProxyDef[pi]; var newPose = def.allowedMotion.TrimMotionUpdate(oldPose, forcedPose); m_ProxyMotions[pi] = newPose; } } } return(didFinalUpdate); }
public bool IterateSingle(ProxyForceFieldId id, int stepCount, float initialTimeDelta) { var didFinalUpdate = false; for (var si = 0; si < stepCount; si++) { var didFrameUpdate = false; { var def = m_ProxyDef[id]; var proxyPose = m_ProxyMotions[id]; var state = SolverFieldState.Create(proxyPose); if (CalculateForces(ref state, def, id)) { state.didUpdate = true; didFrameUpdate = true; didFinalUpdate = true; } m_ProxyStates[id] = state; } if (!didFrameUpdate) { return(didFinalUpdate); } { var state = m_ProxyStates[id]; if (state.didUpdate) { state.forces.AddMotionDragForces(); float timeDelta = initialTimeDelta; if (stepCount > 1) { // slowly decrease the time delta: timeDelta *= Mathf.Pow(1.0f - (((float)si) / ((float)(stepCount + 1))), 0.5f); } var forcedPose = state.forces.ApplyForceToMotion(timeDelta); var oldPose = state.worldMotion; bool isDisableForceApply = false; if (isDisableForceApply) { forcedPose = oldPose; } var def = m_ProxyDef[id]; var newPose = def.allowedMotion.TrimMotionUpdate(oldPose, forcedPose); if (ShowForcesAsLines) { Debug.DrawLine(oldPose.location.position, newPose.location.position, Color.green); } m_ProxyMotions[id] = newPose; } } } return(didFinalUpdate); }