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

Started by
27 comments, last by deltaweb 8 years, 11 months ago
I am working on a 2D game, and I am working on adding some space physics : I need to find a way to create a circular orbit and an orbit like angry birds space ( the spiral trajectory ) . So there is 2 question
First Porblem :
I want to create a circular orbit, to make my game object move on a circular way around a planet, I am using the old famous formula :
V = sqrt(G * ((m1 + m2 ) / r))

This formula is the classic real world one, V is the velocity of the game object .
Once i code it, it doesn't move on a circular way, it just being attracted by the planet, like normal gravity . Here's my code (i'am using Unity 3D ) :

  dist = new Vector2 (Planet.renderer.bounds.center.x - renderer.bounds.center.x, Planet.renderer.bounds.center.y - renderer.bounds.center.y);
r = dist.magnitude;
       rigidbody2D.velocity = dist.normalized * (Mathf.Sqrt (0.5f * ((mBody + mPlanet) / r)));
Dist is a 2D vector and r is a float, The G value is 0.5 ( I used this one because 6.673E-11 is very small and it's not what i want )
So the question is is how can i make my gameobject move on a circular orbit like a satellite around earth ?
One thing to note, is that my gameobject is kicked by the player, so it get a speed of 12 . (i am using addForce() ) .
2nd problem :
I want also to be able to make the gameObject to be attracted by the planet in the same way of angry birds space or something like that, i need it to be attracted in a spiral way, which i can't find any ressource on the web to do that . I am using a simple code that use the Famous Gravitional law of Newton :

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 =  0.5f * mPlanet * mBody / (r * r);
rigidbody2D.AddForce (dist.normalized * force);
0.5 is the value of G, Actually this is a port of a code in C++ that'ive found on a BOX2D forum .
This work very well with attracting object, but what i want is that the gameObject follow an angry birds space like trajectory once it enter the gravity field of the planet :
74725_400x300.jpg
So how can achieve a similar effect ?
Please explain in beginner friendly way if you answer , I don't have advanced physics and math knowledge (I am only a high school student ) .
Thank's !
Advertisement

Hi there,

Regarding your first problem, it seems that you have forgotten to apply the velocity perpendicular to the distance vector. Rather it seems like you are applying it in the direction of the attracting body. You do this by making the normalized vector perpendicular: n(x, y) -> n(y, -x) or n(-y, x) depending on wether you want a clockwise or ccw orbit.

Regarding the second problem, angry birds space has very little to do with actual physics, so there can't be a definitive answer. Can you describe the desired effect in a little more detail? (Haven't played the game much).

Cheers,

Mike

Well if youve got it attracting to the planet you just need to go around it now. Simplest way would be to just apply a force to move perpendicular to the vector from the object to the planet.

vec2 dirToPlant = normalize(planetPos - objectPos);

side = vec2(-dirToPlant.y, dirToPlant.x);

object.AddForce(side * someForce); // <-- recalc side and do this each frame while inside the attracting region

For the 'orbital decay' due to atmosphere (2nd problem), you can just apply some drag if the object is in the atmosphere.

Calculate the resistance force and apply it in the direction opposite to the velocity vector (as in slow down using that force)

IIRC

drag = objectCrossSectionalArea * objectSpeed^2 * airDensity * someConstant

where airDensity is a function of distance from planet center (or surface, doesnt really matter), see wiki article (seems complicated, so maybe you can just try a few simple functions like constant, linear, exponential etc. and see what works best).

You might want to not apply atmospheric effects above a certain altitude (define some cut-off point), so anything above that altitude wont be slowed down due to atmospheric drag.

A simple model would be constant air density + cut-off at some height (like in that angry birds pic, you could draw similar 'atmosphere circle' with constant air density inside and no air outside), which might be how angry birds does it.

o3o

Thank's for every one on their answers, actually i've forgot to apply a vector prependicular to dist vector, But when i did that, i got another problem :

The orbiting body is going to far while moving in a circular way, but i don't know why , here's my code again :


dist = new Vector2 (Planet.renderer.bounds.center.x - renderer.bounds.center.x, Planet.renderer.bounds.center.y - renderer.bounds.center.y).normalized;
r = dist.magnitude;
Vector2 t = new Vector2 (-dist.x,dist.y);
		
float f = Mathf.Sqrt ((5.00f * (mPlanet + mBody)) / r);
rigidbody2D.velocity = t * f ;

Dist is the normalized distance vector, t is the prependicular vector to the dist one, f is the velocity which is calculated from wikipedia : http://en.wikipedia.org/wiki/Circular_orbit#Velocity

Please tell me where i'am wrong ?

thank's

r always has the value of one, because you normalized dist :-) Hence the equation applies the velocity needed for a circular orbit one unit away from mass center.

Maybe you should reform your celestial bodies movement.

Quote

V = sqrt(G * ((m1 + m2 ) / r))

This formula is the classic real world one, V is the velocity of the game object .

There exists no formula for speed upon absolute values in general (speed is a property), this is a deviation of newton formula for force of gravitation between two objects at a moment in space, which is F=G*((m1*m2)/(r*r)), and speed at a time equals V=Vo+(F/m)*t where Vo is current speed vector (tendence) of attracted object, F is gravity force it is attracted by, m is the object's mass and t is time step that F takes effect - until you compute new F vector again pointing to new direction since it sticks to mass center while object has moved (the same also applies on the other object since gravitational force applies on both of the objects, but you can omit the second object if its mass is extremly larger than of the first object)

If there are also other attractors acting on the object, you simply only add also those other forces to the orginal speed of object, and after time step recompute them (vector variables are italic):

V=Vo+(F1/m)*t+(F2/m)*t.....+(Fi/m)*t

I would reform your code like this:

dist = new Vector2 (Planet.renderer.bounds.center.x - renderer.bounds.center.x, Planet.renderer.bounds.center.y - renderer.bounds.center.y)/*.normalized*/;

float rdist=dist.magnitude;

//r = dist.magnitude; beware, you have normalized previous subtraction, this would be just equal to 1.0, not real distance

dist=dist.normalized;

//Vector2 t = new Vector2 (-dist.x,dist.y);

Vector2 t = new Vector2 (dist.x,dist.y);

float forcesize=consG*((mPlanet * mBody) / (rdist*rdist));

Vector2 theforce=new Vector2 (t.x*forcesize,t.y*forcesize);

//float f = Mathf.Sqrt ((5.00f * (mPlanet + mBody)) / r);
//rigidbody2D.velocity = t * f ;

rigidbody2D.velocity = rigidbody2D.velocity + theforce*(1.0/mBody)*fixedtimestep ;

establish original rigidbody2D.velocity vector, choose timestep, and estbalish both(all) attracted objects masses

If you will wish for circular orbit, then establish your distance D, masses m1 and m2, and the velocity of circulary orbitting object based upon those values would be

V*t=(unit 2d vector orthogonal to distance)* cos(arcsin( (D-G*m1*m2*1/(r*r)*t) /D ) )*D

pick t as a second likely,

I've tried what everyone said to get a circular orbit, but the gameobject is still going too far to the left while the planet is in the right, I don't know why ?

deltaweb, I'm not certain what you mean, could you make a more detailed description? does the gameobject go into orbit even if it's not a circular one? Can the central planet move by applying force to it, or is it fixed in place?

This topic is closed to new replies.

Advertisement