Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Changing the direction of the velocity of a rigidbody to simulate an orbit around a point.

Discussion in 'Physics' started by ThomasBlank, Jun 9, 2021.

  1. ThomasBlank

    ThomasBlank

    Joined:
    Jun 9, 2021
    Posts:
    4
    Sorry if the title doesn't make sense, Ill try to elaborate. Most methods I've seen that orbit an object around a point involve simply transforming its position with a set amount 'degrees per second' (I.E transform.RotateAround). I'm wanting to know if there is a way to do that but with it effecting the direction of the velocity instead.

    for reference, this is in a 2D space, and thusly is using vector2 to describe velocity

    Basically, how do you change the direction of the velocity to simulate going around the circumference of a circle with the radius being equal to the distance between you and the centre at the time of entry (I.E when you press the button). I already think I know of ways to calculate the speed (using the difference between the hypotenuse and tangent at point of entry and what-not) so all I need is a way to directly change the direction.

    Thank you in advance.
     
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,333
    I presume you mean like this.

    Get the vector along the line defined by the orbit center to the position of the object orbiting and then get a perpendicular vector to that (+/- 90-deg) i.e. the tangental vector. With that (normalized) vector, scale it by the speed of orbit and you have the instantaneous velocity in the orbit.

    Using this velocity on a body and simulating though won't necessarily give you the perfect result because physics runs in discrete steps so by default fixed-update is 50hz or if you run it per-frame it could be anything. Physics doesn't run continuously so you'll only get an approximation of a curve.

    It really depends on what you want. You can move a body using cos/sin in an orbit of a certain radius and simply calculate the velocity for other things which is easy or you can set the body velocity to attain an orbit but then know that non-linear motion is only ever approximate so the body won't stay perfectly on that orbital radius so you have to be careful when calculating the next velocity so that you use the radius you want and not the radius it just moved to which won't be exact i.e. it'll drift above/below the orbit.

    There's lots of 2D orbital motion sites online though. Some good, some bad. Here's a useful one.
     
    ThomasBlank likes this.
  3. ThomasBlank

    ThomasBlank

    Joined:
    Jun 9, 2021
    Posts:
    4
    That's pretty spot on to what im trying to reproduce with velocity.

    In terms of using the radius I want and not the new radius to avoid drift surely thats as simple as just taking and storing the initial radius as a constant.

    It's a shame there isn't just a prebuild function which does this for me (I am very, very lazy) but nothing worth doing is ever easy. Thanks for the link to the 2D orbital site, will check it out.