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

Inertia, boats, and math!

Discussion in 'Scripting' started by Ishkur, Jul 4, 2013.

  1. Ishkur

    Ishkur

    Joined:
    Nov 18, 2011
    Posts:
    26
    Hello! I'm bad at math, which means I'm even worse when I actually have to apply it. Need some help (guidance) on how to implement this here:

    I have a boat that I want to list from side to side. So I use rigidbody, right? After the player stops commanding it, the boat must return to the upright position (because the boat has its ballast, right?) in a floaty, boat-like manner. Any suggestions on how to do that?

    But first, my guess:

    In Update(), keep checking the listing angle of the ship relative to upright angle and apply torque the "correcting-torque" to the ship in the opposite direction of the way it's leaning in relation to how much its leaning. So the closer the ship gets to upright, the smaller the correction torque will be until its completely upright.

    Which means when the player is turning the ship, say left, we must also apply torque in the same direction of the turn in a greater magnitude than the "correcting-torque" in order to get the effect of the ship listing while turning.

    Sounds about right? Any corrections, improvements, or a better way to do this? Thanks, all, have a nice night, woohoo fireworks, July4!@#!4
     
  2. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    8,988
  3. SpaceMammoth

    SpaceMammoth

    Joined:
    Jan 2, 2013
    Posts:
    220
    I think your guess is a good one, that is certainly what I would try. In general if you want something to osscilate then you have a force (torque here) proportional to how far we are from the middle position (upright angle). You are right when you turn the boat you will want to apply a turning torgue (constant depending on how much the boat is turning) - get this right and it should find a balance with the correcting torgue. You will probably find it lists back and forth too much - you may also want to include some dampening - the torque will give the boat an angular velocity so it will overshoot the upright position and list the other side. Great - you want a bit of this, and if you got your correcting torque right, it should slow down and comw back, but you want it to feel boaty with a weighty drag to it and not a free rotation. Dampening will give this and should be a torgue against the direction of rotation, aproximately proportional to the speed of rotation.

    Disclaimer - I've never done that in Unity, others may want to jump in with more informed advice.
     
  4. Ishkur

    Ishkur

    Joined:
    Nov 18, 2011
    Posts:
    26
    @mgear Thanks, I've looked over the script but its a little more complicated than what I want to implement and not much of the code is conserved so I can't really use it

    SpaceMammoth - Hey thanks for the input! I guess I'll go ahead with doing it according to the guess until someone advises otherwise.

    When you say dampening, is that a math term or a built-in Unity function? Or just a concept that is implemented using several math functions? And I notice there's some sort of built in feature for rigidbodies so you can set its weight... does that mean Unity will simulate inertia so that the boat will automatically "sway"?

    If so, is it bad practice and bad for the engine if I set the boat to its "true" weight in kilos, ending up at several metric tons...? :D
     
  5. SpaceMammoth

    SpaceMammoth

    Joined:
    Jan 2, 2013
    Posts:
    220
    dampening is the maths term - in unity I woudl think - again I've not tried this in Unity, you will just want to apply a torgue for this. But I'd suggest you get the rest of it working first.
     
  6. s_guy

    s_guy

    Joined:
    Feb 27, 2013
    Posts:
    102
    I think using one of the joint components would get you what you're after the fastest, and without having to dig into the math. The spring and dampen properties could upright your ships in a highly tunable way. I'd probably try a hinge joint first, if the listing is mostly on one axis. A custom joint could probably pull off semi-realistic bobbing as well (if you want that). Also, If you didn't want the player to fight the hinge when steering, you could just dynamically enable / disable the spring.