How to create a circular orbit and an angry bird space like orbit ?

Started by
27 comments, last by deltaweb 9 years ago

Here's a quick attempt to help you define the Earth's initial position and initial velocity vector if it were to have a circular orbit path, where the Sun is fixed in position and has no velocity. Try using these parameters first so you'll know if it works as it should, before you start monkeying around with the value of G.

G = 6.67384e11 // Newton's gravitational constant
M = 1.9891e30 // Mass of Sun in kilograms
r = 149597887500 // Earth's semi-major axis -- its average orbit distance in metres
v = sqrt(GM/r) = 29789 // Earth's average orbit speed in metres per second -- NOTE: THIS DIFFERS FROM THE EQUATION THAT YOU'RE USING IN YOUR INITIAL POST.

Sun's fixed position = {0,0,0}
Sun's fixed velocity vector = {0,0,0}
Earth's initial position = {0, 149597887500, 0}
Earth's initial velocity vector = {29789, 0, 0} // orthogonal to the position vector

It's that simple.

If you want to, you can rotate the Earth's initial position and velocity (make sure you rotate both using the same angle) along the z axis before the simulation begins: http://en.wikipedia.org/wiki/Rotation_matrix If you need help with this, let us know. Hint: you can just use the 2D rotation since the orbit path will lie along the xy plane http://en.wikipedia.org/wiki/Rotation_matrix#In_two_dimensions

If you're still stuck, then you're probably doing something wrong when it comes to how you're using a time step (assuming you're calculating acceleration correctly). Check out gaffer's Fix Your Timestep if you haven't already: http://gafferongames.com/game-physics/fix-your-timestep/ and also see the integration basics tutorital http://gafferongames.com/game-physics/integration-basics/ If you're still stuck after reading those two gaffer articles then upload your code into a zip file and post it here.

- Shawn

P.S. For more information, the following link discusses elliptical orbits:

http://www.gamedev.net/topic/447624-calculating-an-initial-velocity-for-desired-planetary-orbit/#entry3961958

So we're clear, the eccentricity parameter is set to 0 for circular orbits.

Advertisement

Oh, thank's for everyone who answered, i've found a way to do that by playing with rotation and gameobjects, For anyone who would like to know how i did it :

  1. Create an empty gameobject and set it postions to the planet pos
  2. make your orbited gameobject a child of this empty object .
  3. Rotate the empty game object with a Z angle ( 2D way ) .

This can only be done using unity.

Once again thank's for everyone, i've learned a lot .

Oh, thank's for everyone who answered, i've found a way to do that by playing with rotation and gameobjects, For anyone who would like to know how i did it :

  1. Create an empty gameobject and set it postions to the planet pos
  2. make your orbited gameobject a child of this empty object .
  3. Rotate the empty game object with a Z angle ( 2D way ) .

This can only be done using unity.

Once again thank's for everyone, i've learned a lot .

NOt realy, you cane establish excenrtig behaviour upon those values (super acceleratred black hole horizont objects/ or, objects that escape observable universe in microseconds). Simulations of newton (no time) law are interesting. Though, this law seems to truly express tendence of attractions if no time is considered towards functional universe

If you are after silly circle orbiting stuff, you can just use sine and cosine relations towards distance.

A(z,y)= (sin(x),cos(x)) = position...... what is a unit circle still a demanding analisis explenational group relation

Believe me deltaweb, it's a much more rewarding experience to actually learn the theory, implement it yourself and see it work than just hack something toghether that only looks real. I should have seen this coming, but I overlooked the angry birds warning. :-)

Okay, here's the full code that i'am using to implement the circular orbit from the physics side :


using UnityEngine;
using System.Collections;

public class Orbit : MonoBehaviour {
	
	Vector2 dist = new Vector2 ();
	Vector2 tdist;
	float r;
	float mBody;
	float mPlanet;
	public GameObject Planet;
	public float G;





	void Start () {

		mPlanet = Planet.rigidbody2D.mass;
		mBody = rigidbody2D.mass;

	}

	
	void FixedUpdate () {
		circularOrbit(); 

	}

	void applyGravity(){
		dist = new Vector2 (Planet.renderer.bounds.center.x - renderer.bounds.center.x, Planet.renderer.bounds.center.y - renderer.bounds.center.y);
		r = dist.magnitude;

		float force =  G * mPlanet * mBody / (r * r);
		rigidbody2D.AddForce (dist.normalized * force);


	}
	void circularOrbit(){
		// Get the distance between the two object centers .
		dist = new Vector2 (Planet.renderer.bounds.center.x - renderer.bounds.center.x, Planet.renderer.bounds.center.y - renderer.bounds.center.y);
		r = dist.magnitude;
		tdist = new Vector2 (dist.x, -dist.y).normalized; // 2D vector prependicular to the dist vector .
		float force = Mathf.Sqrt (G * ((mBody + mPlanet) / r)); // Calculate the velocity .
		rigidbody2D.velocity =  tdist * force;


	}
}

The important part is in the circular Orbit function, and here is a small gif of what's hapenning :

http://makeagif.com/i/-JxBG8

As you can see the monster is not orbited by the planet, here are some infos :

  • Monster mass : 1 unit;
  • Planet mass : 5 unit ;
  • Gravity of the monster is off ;
  • G is 1, but the probleme stay the same for all value that i've tested : 0.1 , 0.01 , 1 , 5 , 20....

All i want to do is to be able to make the monster get in a circular orbit, for the angry bird part, this is a whole another story, that i will work on once i get the circular orbit working .

Best of luck.

I've solved the issue : my perpendicular vector was (dist.x,-dist.y) where it should be (-dist.y, dist.x), more here : http://stackoverflow.com/questions/29704731/circular-orbit-formula-is-not-working-in-unity-3d/29704896#29704896

I'm not sure why your velocity code works.

Normally you would calculate the acceleration (where force F = G*M*m / r^2 = m*acceleration ---> acceleration = F / m = G*M / r^2) and then integrate that into the velocity.

ie:

velocity' = velocity + acceleration*(unit length vector pointing from gravitated object toward gravitating object)*dt

position' = position + velocity*dt

...

where dt is your time step size.

This was covered in the gaffer Euler integration tutorial... but I get the distinct feeling that you aren't interested. :)

Well i think that it's working, because the physics engine take care of all the other stuff, like the time step .

What I don't get is why this works: float force = Mathf.Sqrt (G * ((mBody + mPlanet) / r));

Have you read up on the subject of dimensional analysis?

http://en.wikipedia.org/wiki/Dimensional_analysis#The_fundamental_physical_constants

http://www.efm.leeds.ac.uk/CIVE/CIVE1400/Section5/dimensional_analysis.htm

For instance, let's analyze the equation for circular orbit velocity v = sqrt(GM/r)

G has dimensions of Length^3 Mass^-1 Time^-2

M has dimensions of Mass^1

r has dimensions of Length^1

GM has dimensions of Length^3 Mass^-1 Time^-2 Mass^1

GM/r has dimensions of Length^3 Mass^-1 Time^-2 Mass^1 Length^-1 which simplifies to Length^2 Time^-2

v = sqrt(GM/r) has dimensions of Length^1 Time^-1 (ie. metres per second, which stands to reason).

The problem that I have with your equation force = sqrt(G * ((mBody + mPlanet) / r)) is that it has dimensions of Length^2 Time^-2, which is not the dimensions of force (which is Mass Length Time^-2) -- in other words, the equation force = Mathf.Sqrt (G * ((mBody + mPlanet) / r)); makes no physical sense. I invite you to double-check my work, in case I made an error during the analysis of your equation. Plus, the practice wouldn't kill you. :)

This topic is closed to new replies.

Advertisement