Search Unity

Trains, tracks, physics, and mechanics

Discussion in 'Animation' started by Y-van, Sep 19, 2013.

  1. Y-van

    Y-van

    Joined:
    Sep 6, 2013
    Posts:
    4
    Hi!

    I'm writing an application that simulates a children's wooden toy train set.

    My goal is to move the train around the track only by applying a force in the vector of its direction.

    Before I go further; yes, I could do this using a pathfinding algorithm. That is Plan B, I was hoping however that the physics engine would be sophisticated enough that I could use it alone.

    Here is a screen shot of what I'm trying to do.

    $train.screen-shot.png

    In this screen shot,
    • the track has a mesh collider
    • the track is not a rigid body
    • the train is a rigid body with a mass of 1
    • the train (except for wheels, wheel hubs, and axles) has a mesh collider. This should not play a role while the train is upright.
    • each wheel has wheel collider(s)
    • the mass of each wheel collider is 100

    My goal, obviously is for the wheels to stay in the tracks.

    On straight segments I have been able to keep the wheels in the tracks, using the side forces of the track's mesh collider against the wheel collider.

    As soon as there is a slight curve, this no longer works, and the train goes off the track, and bloody gore ensues.

    I have played with the model for a few hours without much luck. I've tried
    • using one wheel collider per wheel
    • using two wheel colliders per wheel (inside and outside), each the size of the larger diameter part of the wheel
    • using three wheel colliders per wheel (inside large, middle large, outside small)
    • adjusting the mass of the wheels, between 1 and 100
    • adding mesh colliders to the wheels
    • adding convex mesh colliders to the wheels
    • adjusting the sideways stiffness factor between 0 and 1 (1, 20000, 2, 10000)
    • adjusting the suspension distance and springs
    • changing the train's rigidbody collision detection

    At my current scale (I am using 1u = 1mm), gravity is set to -9810, which results in the correct amount of time for a ball to drop from 10m.

    I've realized that there is no need for more than one wheel collider per wheel because the forces from the sides of the track have no effect on them, so I need another collider on each wheel for that. I've tried mesh, but maybe cylinder colliders would work better.

    Anyways, I'm still working on this without much luck. Any advise or even "you can't do that because" would help.
     
  2. Y-van

    Y-van

    Joined:
    Sep 6, 2013
    Posts:
    4
    Progress update.
    This is working better.
    1 wheel collider per wheel.
    1 capsule collider per wheel (vertical, diameter=width of wheel, height=diameter of wheel) to follow the groove of the tracks. PS: why no cylinder collider?
    Also, the geometry of the locomotive was colliding with the track, which is weird since this is based on a real-world design. Over compensated diameters of wheel colliders for now.
     
  3. Y-van

    Y-van

    Joined:
    Sep 6, 2013
    Posts:
    4
    It looks like the wheel colliders really don't like colliding with the mesh collider of the tracks. They will sink right through.
     
  4. halley

    halley

    Joined:
    Aug 26, 2013
    Posts:
    2,439
    Two mesh colliders will not collide with each other unless one of them is marked as "Convex," meaning it makes a new mesh that is shrink-wrapped around your original mesh to simplify the shape (math term: Convex Hull).

    I also noticed they had no cylinder collider. The wheel collider is technically the shape we want, but it has a lot of physics extras for driving games. Apparently, it's not very good at hitting curbs.

    I would experiment with a box collider that matches the bed and outside curbs of the track, instead of a full mesh collider for the track. That's not ideal, but it's enough to give new ideas.
     
  5. Y-van

    Y-van

    Joined:
    Sep 6, 2013
    Posts:
    4
    Your comment about the mesh colliders makes sense, but my current iteration has only one mesh collider - the track. Interestingly the wheel collider falls through the mesh eventually. Using a capsule collider like a vertical dowel seems to work for direction, but prevents movement since the wheels aren't colliding.

    I agree that I might need to use box colliders for the track segments; but this would be tricky/compilcated on curved segments.

    Thanks for the input.
     
  6. Gabo_campitelli

    Gabo_campitelli

    Joined:
    Oct 17, 2012
    Posts:
    426
    Don't use colliders for the train or wheels. First work out a suspension system that holds the train "in the air". This can be as simple as seting the position of the train to the suspesion distance by raycasting down to the floor.
    Next, find a way to tell how far is the train from the track center line. Then add a force from the side proportional to the distance to the cener of the track. This will keep the train on the track the same way a tire would keep a car on the disaired direction. This script could be placed in every wheel axle of a wagon. Then each wagon can be hooked together with a hinge joint. I've done this before and seems to be the best and simplest way. Just keep in mind that when you add the force at the weels position sideways it can still make your train flip. To avoid this you cold do 2 things, one put the center of gravity of the wagon/train at the floor level or apply the force at the center of mass.
    The problem when working with rigidbody and forces is that it requires a lot of tweeking and testing to avoid undisired behaviours like the wheel going out of the track at curves.
    Hope it helps.
     
    my_little_kafka likes this.