Search Unity

Realistic physics along spline like tracks

Discussion in 'Physics' started by Phunyun, Jan 18, 2020.

  1. Phunyun

    Phunyun

    Joined:
    Jun 23, 2018
    Posts:
    4
    Hi all, been trying to figure out how to best approach this for a while but without much luck so far.

    I'm looking to try to do realistic physics for objects being pull and pushed along a spline. I already have a spline solution in place where all I need to give it is a speed to move an object attached to it at. The problem I've been trying to figure out is how to realistically calculate those speeds on each tick along said splines as everything I've seen so far advises against trying to use Unity's built-in physics engine for this kind of thing, or at least I haven't seen a good way to use the engine for it.

    The idea is that there will be a vehicle you control, like a train, that pushes cars or items along the spline and I'm trying for the most realistic possible physics in their interactions, including weight and the slack between them.

    Any ideas? Thanks!
     
  2. Edy

    Edy

    Joined:
    Jun 3, 2010
    Posts:
    2,510
    Phunyun likes this.
  3. Phunyun

    Phunyun

    Joined:
    Jun 23, 2018
    Posts:
    4
    Thanks for the response, that actually looks very useful. The one other thing I would require though would be branching paths, like switches and controlling which route an object would take. Do you know whether it has support for that? The asset's website linked to from the store page appears to be down and I'm not finding any other documentation so far.

    Thanks again!
     
  4. Edy

    Edy

    Joined:
    Jun 3, 2010
    Posts:
    2,510
    I don't know if PathMagic supports it. If it doesn't, maybe you could learn from the physics joint and apply it to a different spline solution.

    Anyways, I think that implementing branching and routes may be very dependent of each specific project, so I'd consider designing that part on top of an existing spline asset.
     
  5. BoogieD

    BoogieD

    Joined:
    Jun 8, 2016
    Posts:
    236
    You need to code a spline walker by dividing the curve into many steps and storing the data in an array for reference because the 't' value in a spline is not proportional to the distance along the spline. Search Google for 'spline walker' for code samples.
     
  6. Edy

    Edy

    Joined:
    Jun 3, 2010
    Posts:
    2,510
    While this an off-topic reply, I'd like to point out that a spline can be walked at constant speed without need of sampling the spline nor storing any data. I've developed a method that does that by using the spline's tangent:

    https://twitter.com/VehiclePhysics/status/1214195538184290304
     
  7. Phunyun

    Phunyun

    Joined:
    Jun 23, 2018
    Posts:
    4
    This is a super late reply but do you happen to have that available anywhere? I'm very curious to see how that works.
     
  8. Edy

    Edy

    Joined:
    Jun 3, 2010
    Posts:
    2,510
  9. Phunyun

    Phunyun

    Joined:
    Jun 23, 2018
    Posts:
    4
  10. Edy

    Edy

    Joined:
    Jun 3, 2010
    Posts:
    2,510
    It's simply diving the movement rate by the tangent magnitude at the current position.

    Each spline segment is interpolated from 0 to 1. Let's say that you move at a rate of 0.1, so the position t is calculated as t = t + 0.1 * deltaTime. Simply using t = t + 0.1 / tangent(t).magnitude * deltaTime results in a motion of exactly 0.1 m/s along the entire length of this spline segment.

    I'm using Hermite splines, where the tangents are explicitly defined in each control point. An Hermite segment is Point0, Tangent0, Point1, Tangent1, while Bezier is defined with four control points. However, tangents in Bezier splines can also be calculated easily.

    The real problem with this approach is keeping the rate constant when crossing control points. Tangents at each side of a the control point are likely different. The movement step that crosses the control point must be handled considering distances and tangents at both sides, or the motion will experience hiccups. Also, high speeds may result in several points crossed in a single movement step, which also requires special considerations. Not to mention that the tangent length may be zero at the control points in some specific configurations. Figuring out and testing all this is what took most of the time when developing my Spline component.
     
    Last edited: Apr 30, 2020