Circular orbit formula is not working in unity 3D

I am working on a game where I have to implement some space physics like orbits and gravity....

My main problem is that I'am not able to make a GameObject orbit correctly around a planet (or another object ) , I am trying to do that using an old famous formula from physics textbooks :

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

Where v is the velocity of the orbiting object, G is a constant, m1 and m2 are the masses of the the orbiting object and the planet and r is the distance from the center of m1 and m2 .

Here's my implementation :

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 circularOrbit Function is called each frame inside FixedUpdate that unity provide for physics scripting .

Now here's a small gif that is enough to explain my problem : http://makeagif.com/i/-JxBG8

Here are also some information about my settings :

  • Monster mass is 1 unit and planet mass is 5 units.
  • The gravity for the monster is off .
  • G have a value of 1 (I've tested many values without fixing the problem )
  • Finally, I know that i'am not the first one to do that, but i've searched a lot this week on how to do this, mainly my code is Box2D port with some tweaking and changes, I've posted a question on GameDev.net here but any suggestion worked .

    Please help me by explaining why it's not working in physics beginner way, I'am stuck in this thing for the whole week .


    The perpendicular vectors to (x,y) are multiples of (y,-x), but you have (x,-y). You can check when two vectors are perpendicular by the fact that their dot product x1*x2 + y1*y2 is 0. (1,2).(1,-2) = -3. (1,2).(2,-1) = 2-2 = 0.

    By the way, I don't recommend mixing force and velocity. I would rename what you currently call force to speed.

    链接地址: http://www.djcxy.com/p/66172.html

    上一篇: 为什么我的弹跳球穿过地面?

    下一篇: 圆轨道公式不适用于统一3D