Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

How to control velocity through math function ?

Discussion in 'Physics' started by Monique_Dumont, Oct 26, 2020.

  1. Monique_Dumont

    Monique_Dumont

    Joined:
    Feb 18, 2020
    Posts:
    41
    Hello,

    I'm wondering how I could make the speed of an object follow a function's curve. For example, what if I want a car to accelerate with its speed matching ln(x) function ?

    I'm having trouble because in physics (if my highschool memory serves me right) we change the speed with time as a variable. In Unity is this behaviour implemented natively in the rigidbody physics ? If that's the case do I have to write my own physics to get something like that ?

    Thank you
     
  2. AlTheSlacker

    AlTheSlacker

    Joined:
    Jun 12, 2017
    Posts:
    326
    Nearly all of these type of problems can be answered by some combination of F = m a (force = mass * acceleration, i.e. a = F/m) and https://en.wikipedia.org/wiki/Equations_of_motion (look past the rather daunting start to the section "Constant translational acceleration in a straight line").

    Unfortunately, the car is a little more complex (assuming you are accelerating it by driving the wheel colliders or equivalent) as the wheels can slip before delivering the required torque (torque is just Force x distance from centre of rotation). If you have speed curves that require accelerations that are well within the grip of the car then it still follows the same rules - work out the force required from the above, divide it by 2 (for 2 wheel drive) x by radius of tyre to get torque. Apply that as the motor torque to each wheel.

    You just need to calculate your required change in speed each physics step, which with physics timestep (v_final = v_init + a * t, see reference above) gets you acceleration, which gives you force (F = ma), which gives you required torque.

    If your car is accelerating near the limit of its grip, then you will have to write a small control system to try and match the target curve as best you can, this is likely to be a bit more complex (you will have to add a traction control system and allow feedback from the actual vehicle velocity versus your speed curve).

    Sorry: To answer your final question. Yes you have to do the physics. Although for simpler problems you should check out Rigidbody.AddForce and ForceMode
     
    Last edited: Oct 28, 2020
    Monique_Dumont likes this.
  3. Monique_Dumont

    Monique_Dumont

    Joined:
    Feb 18, 2020
    Posts:
    41
    Thank you for your detailed answer!

    I talked about a car but I should have talked about a general unity object as it was just a theoretical question (for now at least, I will need it later). I remember about the equations you linked, I just couldn't understand how to implement them ingame with the time variable but with your explanation it makes sense. I will play a bit with it to see if I can wrap my head around it.

    Thanks again, have a good day!
     
  4. AlTheSlacker

    AlTheSlacker

    Joined:
    Jun 12, 2017
    Posts:
    326
    If the problem is reasonably straightforward e.g. setting rb velocity, definitely look at the last bit I mentioned, ForceMode will let you set a velocity change and Unity will work out the force required for you.
     
    Monique_Dumont likes this.
  5. Maeslezo

    Maeslezo

    Joined:
    Jun 16, 2015
    Posts:
    295
    Although is not recommended, rigidbody allows to set its velocity direcly
    https://docs.unity3d.com/ScriptReference/Rigidbody-velocity.html

    In your case, sorry if I didn't get you correctly, you could do:
    //pseudocode
    //Speed(time) =Ln(time);
    float logSpeed = Mathf.Log(Time.time);
    vector3 logVelocity = vector3.forward*speed;
    rigidbody.velocity = logVelocity;
     
    Monique_Dumont likes this.
  6. Monique_Dumont

    Monique_Dumont

    Joined:
    Feb 18, 2020
    Posts:
    41
    Thank you guys I think I get it now :)