#Planet Orbits
1 messages · Page 1 of 1 (latest)
This is probably happening, it’s velocity moves it to a position outside of orbit
@hallow trout
I did attempt Simferoce’s idea, since it also works out mathematically but it has the opposite problem where the planet actually starts moving towards the sun.
I could’ve did something incorrectly since I was in a rush, but my guess was that it created a velocity that moved the object inside the orbit
The only ideas i have are to either
- Use a hinge joint to physically constrain the planet to the sun so it doesn’t move out of range. https://docs.unity3d.com/ScriptReference/HingeJoint.html
- Use what you have already but apply a clamp on the distance every frame
Vector2 dir = (sun.pos - planet.pos);
planet.position = dir.normalized * fixedDistance;```
Actually you might also be able to do some fancier math that creates a vector to move the planet to a point on the orbit by the next physics timestep.
Vector2 dirToMove = new Vector2(5,0);
rb2d.velocity = (dirToMove.x/Time.fixedDeltaTime, dirToMove.y/Time.fixedDeltaTime);```
This should cause the rb to move at 5 units per **frame**. If you can calculate the next point on the circle to move to, you can generate a direction to that and the rb should in theory be at the position on the next frame
Ah clamping the distance is a great idea, i’ll try that out when I get home!
by far the easiest solution, hopefully that’s enough 🙏
This sounds effective but Im curious as to how that would work in making the planet rotate in a circle rather than a straight line
The idea would be to calculate a point on the circle to move to, and then plug in a velocity that would move the planet to that point in 1 physics frame
instead of generating a velocity to a point off the circle
I really appreciate it, I’ve just been trying a ton of different ways orbiting planets in a solar system so these ideas are super helpful
good luck on your project! 🍀
Thank you!
Oh right, I think I can manage that. Though I might have to whip out my old notes from Pre-Calc 😅