Search Unity

  1. Calling all beginners! Join the FPS Beginners Mods Challenge until December 13.
    Dismiss Notice
  2. It's Cyber Week at the Asset Store!
    Dismiss Notice

Imparting Velocity from Moving Static Colliders

Discussion in 'Physics' started by Mister Fister, Jan 10, 2015.

  1. Mister Fister

    Mister Fister

    Joined:
    Jun 3, 2013
    Posts:
    6
    I have a physics conundrum that's been stumping me for a while now. I am building a game called SineRider which involves physically colliding with a graph. The function the graph is drawn from can include time variables, which means it can move.

    The problem is that when the graph is moving, it doesn't impart velocity on the sled. If a graph moves rapidly upward and then stops, you would expect the sled to be launched into the air. Instead the sled just sort of gets pushed out of the way, then stops with the graph. Here is an example of this behavior (press play when the game loads).

    The reason for this is that the graph is not a moving rigidbody, but rather a static collider with a mesh that gets redrawn every frame. So even though the graph moves, it does not technically have velocity. Thus when the graph moves the sled, the velocity of the sled does not change.

    I have been racking my brain for a way to fix this but have come up with nothing. If anyone has even a hackish workaround, I would be very grateful.
     
  2. Nanako

    Nanako

    Joined:
    Sep 24, 2014
    Posts:
    1,047
    well one problem to consider. A graph as an abstract object, what mass would it have?

    You can directly set the velocity of rigidbodies as well as turning off their gravity. And consider kinematic rigidbodies too, do research into that.


    if all else fails, there's always manual physics. Apply forces and set velocities of things when you think they should (generally when stuff is colliding)
     
  3. skusku

    skusku

    Joined:
    Jan 4, 2015
    Posts:
    17
    I mean... this does not look so wrong does it. If I did something similar in reallife with a not bouncy material, I would expect what I see in your demo.

    Are you using a Physics material with bounciness?
     
  4. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,618
    just a note from your title - moving static colliders

    if you are in fact, moving you static colliders, you need to stop. That is definitely something you should not be doing
     
  5. Mister Fister

    Mister Fister

    Joined:
    Jun 3, 2013
    Posts:
    6
    The transform of the static collider does not move, but the mesh changes every frame.
     
  6. MortenSkaaning

    MortenSkaaning

    Joined:
    Jan 16, 2015
    Posts:
    120
    @Mister Fister, what happens if you add a RigidBody component and make it kinematic?
     
  7. Mister Fister

    Mister Fister

    Joined:
    Jun 3, 2013
    Posts:
    6
    Nothing changes. The problem is that even if there is a rigidbody on the graph, it has no velocity because it is the mesh that changes and not the position of the object. So when the sled rigidbody collides with the graph, the collision is processed as inelastic and no velocity is imparted.
     
  8. MortenSkaaning

    MortenSkaaning

    Joined:
    Jan 16, 2015
    Posts:
    120
    When happens if you instead create a box collider with a kinematic rigid body. Then set the x-position where your character is and the y-position to the function value on the graph. Maybe that will help, or we have to look at exposing a property for treating position changes as velocity. If it works you could create boxes for every object you need to collide with the terrain.
     
  9. MortenSkaaning

    MortenSkaaning

    Joined:
    Jan 16, 2015
    Posts:
    120
    great, the functions are exposed. They are RigidBody.MovePosition and MoveRotation.
     
  10. Mister Fister

    Mister Fister

    Joined:
    Jun 3, 2013
    Posts:
    6
    I tried this, but it's a pretty weak solution since it only works properly for vertical motion. When the sled is pushed sideways by the graph you get weird behavior because the x position of the box is dependent on the player instead of the function. It also has problems when there are multiple dynamic objects on the graph, since they collide with each other's ground boxes. I could set each object to a different collision layer, but you can see that this gets unwieldy pretty fast especially since collision layers are limited...
     
  11. MatthewW

    MatthewW

    Joined:
    Nov 30, 2006
    Posts:
    1,355
    Would need experimenting, but maybe track your own velocity and apply back in?

    Save position every FixedUpdate, compare current position this frame to last frame's position for a velocity. The difference between this manual velocity and rigidbody.velocity should just be the position change due to the static collider pushing the object around. Apply that difference with AddForce with a ForceMode that ignores mass...?

    I'm not 100% sure those two velocities would match up in normal circumstances, so maybe only apply if in contact with the graph, or recently in contact with the graph, etc...