- Home /
Adaptive Force smoothing? (Updated)
Hey, my first question here. updated: i didnt get any answers to my first explanation so im simplifying it in hopes to get some feedback.
roughly what im trying to do:
my visual testing code(re-written since last time, and runs in the unity editor), now im using an animationcurve instead of a smoothing function:
public class SimForce : MonoBehaviour {
public float forcePool = 100f;
public float maxDeltaForce = 10f;
float forceRef = 0f;
float timeRef = 0f;
float peakForce;
public float normalTime = 4f;
float startTime;
float currentTime;
public AnimationCurve aCurve;
public Transform tPeak;
public Transform tTotal;
void Start ()
{
TrailRenderer tr;
tr = tTotal.gameObject.AddComponent<TrailRenderer>();
tr.startWidth = 0.1f;
tr.endWidth = 0.1f;
tr.time = 100f;
tr.material = tTotal.renderer.material;
//tr.material.color = new Color32(100,100,100);
tr = tPeak.gameObject.AddComponent<TrailRenderer>();
tr.startWidth = 0.1f;
tr.endWidth = 0.1f;
tr.time = 100f;
tr.material = tPeak.renderer.material;
}
// Update is called once per frame
void FixedUpdate ()
{
if (Sim()) Destroy();
//transform.position = mathf.lerp();
}
void Destroy()
{
DestroyObject(this);
}
bool Sim()
{
float force;
if (timeRef == 0f) timeRef = Time.fixedTime;
currentTime = Time.fixedTime - timeRef;
//this is working!!, but peak curve isnt smoothed
force = (forcePool) * aCurve.Evaluate(currentTime / normalTime) - forceRef;
//use two stacked smoothing functions?, not effective
//force = (forcePool) * aCurve.Evaluate(Mathf.SmoothStep(0,1, currentTime / normalTime)) - forceRef;
//save highest peak
if(force > peakForce) peakForce = force;
var total = forceRef / forcePool * 5;
var peak = force / (forcePool / normalTime) * 50;
var x = 5 * (currentTime / normalTime);
tPeak.position = new Vector3(x,peak,0);
tTotal.position = new Vector3(x,total,0);
forceRef += force;
Debug.Log("force this frame = " + force + "forceoutput = " + (int)forceRef + " of " + forcePool + " currenttime = " + currentTime);
if(forceRef >= forcePool)
{
Debug.Log("Result = " + (int)forceRef + "/" + forcePool + " highest force experienced= " + peakForce +" time to complete = " + currentTime);
return true;
}
return false;
}
}
the peak(blue)curve amplitude is exaggerated so its easier to see. it is almost working now, i could probably get away with this smoothing function but if it could be able to do 2 more things i will have achieved my goals for it, 1: smooth the start and ending of the peak curve by a small bit, 2: clamp and smooth the highest point of the peak to the variable maxDeltaForce.
to make it clear im not asking for someone to write the code for me, im just asking for some pointers as to how to do it. this is for a free project so i wont be making any money on it. thanks:-)