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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Helical Spring Movement 2D

Discussion in 'Physics' started by mondeon, Sep 21, 2016.

  1. mondeon

    mondeon

    Joined:
    May 29, 2015
    Posts:
    46
    Hello Unity Community,

    in my 2d game I am trying to manipulate the velocity of a rb2d, so that the object moves like on a helical spring. There are also upper and lower boundaries (so that object doesn't go off the screen) To visualize my idea I sketched out a basic figure (kinda ugly, sorry):

    path.png
    I was thinking of using the Mathf.Sin and Time.deltaTime, but have no idea how to add the boundaries in the equation? Can someone help me, please?

    [EDIT]
    Just to clarify what I mean - with helical spring I mean a typical coil spring (like the one in a ball pen). Following atomic steps would happen:
    1 - object appers on the right side of the screen
    2 - object moves upwards and on reaching upper bound (say screen height - 100px) it starts to move downwards
    3 - on reaching the middle of the screen it starts to move backwards
    4 - on reaching bottom border (bottom of screen - 100px) it starts to move upwards
    5 - on reaching lets say 1/4 of the screen it starts moving forward
    6 - go to step 1 and repeat

    Note: it's important that I add somehow a kind of Lerping (+ damping) so the movement is smooth. Also, I could write the described function with 6 steps, but maybe someone knows a better solution, where velocity is a function of Time.time or/and Time.deltaTime - cause that will be a one-rower and a lot more elegant solution.. Anyone?
     
    Last edited: Sep 22, 2016
  2. esuotfartete

    esuotfartete

    Joined:
    Jan 15, 2014
    Posts:
    6
    I am no expert, but I think your question might use some clarity to be answered. E.g. I don't understand the tangle you have drawn. If it is just up and down movement, a sine, maybe with some modifications should do, and it has its natural max and min limits. If you want some kind of jack-in-the box movement, as the drawing would suggest, the physics will be complicated, and so will be its limits, if any can be reasonably found. I understand that by helical spring you mean a typical coil spring?
    If you want to additionally (and unnaturally) limit the values, there's always the Mathf.Clamp function which will do it for you.
    But basically this is is just to say it is not quite clear what movement you want and where you want to limit the movement.
     
  3. mondeon

    mondeon

    Joined:
    May 29, 2015
    Posts:
    46
    I edited the ticket, hope I could clarify my problem. If not - let me know! Thank you!
     
  4. esuotfartete

    esuotfartete

    Joined:
    Jan 15, 2014
    Posts:
    6
    Well it looks to me like, to produce this strange movement, you'd have to combine the simple up-down sine (with 1 occurring at the upper limit -100px, and -1 at the lower +100px) on the vertical axis with a left-right sine added to a linear function to keep it moving left, on the x axis. Something like: enterscreentime=Time.time; and then loop: Vector2 (screenwidth - (Mathf.Sin ((Time.time - enterscreentime)/somenormalizingfactor) * sweepwidth + Time.deltaTime * sidespeed), horizcenterline + (Mathf.Sin ((Time.time - enterscreentime)/somenormalizingfactor) * sweepheight).
     
    Last edited: Sep 23, 2016
  5. mondeon

    mondeon

    Joined:
    May 29, 2015
    Posts:
    46
    Thanks! Gonna check it out and try on weekend
     
  6. esuotfartete

    esuotfartete

    Joined:
    Jan 15, 2014
    Posts:
    6
    Okay, just remember that the line is just to give you an idea of what I mean, not proven, working code, so you'll have to figure out the details yourself :) Instead of the time functions, you could also use your own counter which you will convert to radians for the Sin function, but then FixedUpdate would probably work better.