# Circular orbit formula on JMonkey3

I am doing a planet + big object circular orbit simulation, the idea is that the big object will get closer to the planet using a random force, and then it should orbit it.
I found some similar problems in the internet : http://stackoverflow.com/questions/29704731/circular-orbit-formula-is-not-working-in-unity-3d?rq=1
I am planning to apply this same formula : v = sqrt( G * ((m1 + m2)/r )) on the big object, but just don’t know how to do it.
Since the planet has mass 0, I gets confused, did anyone implements anything close to it ?
What did you use ? Set an force or linear velocity ? Did you use this same formula ?
Any help is appreciate thanks !

I made a program close to what you’re trying to acheive about half a year ago. I think the most simple way to explain this would be like this: Imagine that you’re working with forces working on the object. The central thing that you need to orbit around (for example the planet) applies a force on your object by a vector that is pointed from the object to the centre of the planet. Its length is determined by the formula you mentioned. That force needs to be applied each frame/phyics tick - to mimic gravitational acceleraton. Once you got this done i’ts easy to add more attractors by just adding up all the forces.

Your object does need specific (orbital) velocity when the simulation starts, otherwise it’ll fall straight down towards the planet like it should. Getting this velocity right is tricky but can be calculated by some equations that i’ve never looked into. Too much and you’ll get an eccentric orbit, too little and the object will crash into the planet.

Thanks @MoffKalast , it helps me to understand.
I may be doing something very wrong thought, instead of attracting the object its actually repealing :

``````        float speed = FastMath.sqrt( 100 / distSphere );
distObjRigid.applyForce(gravityVector.normalize().mult(speed), nuclePosition);
``````

distSphere is the distance of the object to the orbit sphere.

Using this formula this way, if the object is close, the force is small, if the object is far, the force is infinite… It should be the opposite, what I am missing ?

Missing: the concept of how gravity works. Gravity is strongest when you are close and weakest when you are far. The moon orbiting a planet around alpha centauri has almost no effect on us where as our own moon can affect tides.

I know this concept, the problem is that some how my implementation is getting the opposite result, I tried to simplify it but no success. I may be swamping some variable.
As I understand, it will calculate the constant speed that will make an object with some kind of vector in constant orbit to another object, so it suppose to at least attract the objects.
I think I am just applying the wrong force, maybe its wrong to mult this vector by this speed ?
Or maybe its the wrong vector, I just want to confirm that this implementation is right, so I can test some perpendicular vectors on it.

I apologize. I shouldn’t have waded into this thread but I forgot how bad communication is in these.

You said:

And I interpreted that as the “the code I showed you has infinite force up close but small force far away”. Which is true, it does. And I interpreted the rest as “the code should do the opposite”… but it shouldn’t the code does what it says it does.

So I’m going to bow out of this thread as they get too frustrating too quickly with miscommunications and too little code so that we are left to guess what half of it actually means. Like how is gravityVector calculated, etc…

Hopefully someone else has more patience as I don’t have the time these days. Sorry.

Anyway, my problem is just in the applyForce(gravityVector.normalize().mult(speed)) , I just get confused sometimes with this functions. Supposing the formula is right and speed has the right value, it should be the right implementation right ? That is the question of this post.
I dont think this solution will fix my problem since my object will colide with others and change the force, getting out of orbit because of this, and I need a fixed orbit dosent matter what.
But its an interesting to post here for others that are trying to understand better how to apply forces etc.
Also, I just notice I typed wrong, I already fix it, sorry for that, you are right about the communications, I shall take more care on this next time. Its just that I am having so bad time with distance constraint and solutions like that with Jm3, I know that its not jm3 fault, the problem is on bullet, but its taking so much time to fix it, its something I already done in other engines very easily and fast.

I see, so you need an object that is “on rails” so to speak. The equations that you’re using are more suited for dynamic objects which can be bounced around. For making a sort of static orbit I think its possible to make the body kinematic or something that will prevent it from changing its velocity vector by other objects, but I haven’t really looked into that yet so I can’t say anything for sure. The other option is calculating the object’s position using conics - a circle or an elipse and setting its position by setLocalTranslation or setPhysicsLocation. The latter might be more suited if you want to use bullet collisions.

Well, little bouncing is ok for me, but the thing is, what I understood of this equation is that if some force take the object out of the orbit speed, it will fall into the planet nucle or into the space, am I correct ?
Anyway, do you see anything wrong in use the force that way ?

float speed = FastMath.sqrt( 100 / distSphere );
distObjRigid.applyForce(gravityVector.normalize().mult(speed), nuclePosition);

Did you remember if it was the way you implemented it ?

Well I did it in a bit different way since it was in 2D and in JavaFX but the backend equations should be the same everywhere. I took a look at my code and if I modify it a bit it’d look something like this:

``````float hyp= Math.sqrt((Math.pow(x2-x1,2)+Math.pow(z2-z1,2)));
float vx = (x2-x1)/Math.pow(hyp, 2);
float vz = (z2-z1)/Math.pow(hyp, 2);
distObjRigid.applyForce(new Vector3f(vx,0,vz));
``````

Where x1 and z1 are the coordinates of the object and x2 and z2 of the attractor. The hyp was supposed to be some kind of hypotenuse.

Thanks @MoffKalast for sharing !
I changed your code to apply this force on the existing force :

``````    Vector3f nuclePosition = centerDistNode.getWorldTranslation();
Vector3f objposition    = distObjRigid.getPhysicsLocation();
Vector3f force = distObjRigid.getLinearVelocity();
Vector3f gravityVector = nuclePosition.subtract( objposition );
float vx = (gravityVector.x-force.x)/(hyp*hyp);
float vy = (gravityVector.y-force.y)/(hyp*hyp);
float vz = (gravityVector.z-force.z)/(hyp*hyp);
Vector3f newforce = new Vector3f(vx,vy,vz);
distObjRigid.applyForce(newforce, nuclePosition);
``````

It seens to be doing the job right, I guess it do the same from the v = sqrt( G * ((m1 + m2)/r )) , since when I tried to apply some random forces it sometimes goes into the planet nucle or into the space, but depending on the force is applied it keeps the orbit.

if you need fixed orbi, why using forces at all btw?

Just use a cinematic rigidbody and pull it over a calculated circle in update.

I have object A and B. Object A is a planet, and have infinity mass, and dont move.
I have a radius constant that defines an fixed orbit sphere ( an sphere where I want the objectb to be at the edge of it ). And object B, is subject to random forces from colisions from other objects, so there is forces acting on it already. How to make object B to have the same distance from object B ( keep in the radius in the sphere ) just using forces ? I know there is some complex calculations on Jacobian equations but I am tryting to avoid that. Currently I am doing :
Its preaty much the same of an distance constraint, but since there is no such thing in jmonkey or bullet I need to simulate it in other way.
Currently I am doing :

``````    Vector3f nuclePosition = centerDistNode.getWorldTranslation(); // nucle of the planet, its in 0,0,0
Vector3f objposition   = distObjRigid.getPhysicsLocation();    // object position
float radius = 2f;					       // the fix radius of the planet orbit sphere

Vector3f gravityVector = nuclePosition.subtract( objposition ); // define the force to be applied
float currentDistance = gravityVector.length();			// the distance from the core of the planet
float distSphere = currentDistance-radius;			// the distance from the orbit sphere

// Aply the gravity force
if(currentDistance<radius) gravityVector=gravityVector.negate();  // if it pass trought into the planet core just invert it
distObjRigid.applyForce(gravityVector.mult(distObjRigid.getMass()), nuclePosition);	// apply the force
``````

If I just do this, the object goes into the direction of the orbit sphere as intend, but its just bouncing too much…
There is another simple way to do it or maybe an correction on the force if the distance is smaller ?
Maybe using setlinear or setangular ( I am not sure how setangular works ).

Its an simulation, I was looking to a equation to help me fix the issue from another post.
Obs: It seens normen join the threads and make it only one thought.