开发者

example of point-based gravity in c++ [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers. 开发者_如何学Go

We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.

Closed 1 year ago.

Improve this question

I am trying to figure out how to implement gravity into my application i'm creating. I have a sphere in Opengl and im wanting to give it gravity like a planet. so any small objects near it will "fall" to the surface of it.

Im wondering if anyone could point me in the direction of a example in c++.


Maybe it is too late for this, but at least others might get something from this.

Here is a program I just wrote in C++ and OpenGL of gravity simulation. Hope it will help. The source code is in the description of the video and can be found here as well.

http://www.youtube.com/watch?v=TXY6NJm5se0&list=UU0OHvV8fkDcKlWAm1vFqc1w&index=1&feature=plcp


You need to write a function that implements the gravity formula, for example:

const float g = 9.81f;  // Gravity of Earth in m/s²
float gravity(Vec3 p1_pos, Vec3 p2_pos, float p1_mass, float p2_mass)
{
    float distance = (p2_pos - p1_pos).length();
    return g * p1_mass * p1_mass / (distance*distance);
}

Multiply the magnitude of the force by the unit vector parallel top2_pos - p1_pos to give the force a direction. Then, simply compute the acceleration on the object using F = ma

struct object
{
    Vec3 pos;
    Vec3 vel;
    float mass;

    void add_force(Vec3 force);
};

void object::add_force(Vec3 force, float dt)
{
    vel += (force / mass) * dt;
}

Be sure to multiply the acceleration by dt, the number of seconds per frame. This allows your simulation to progress at a regular speed regardless of the speed of the computer. I have written an NBody simulation that uses a technique quite similar to the one above to simulate an arbitrary number of planets and calculates the force they attract each other with. For every object that you want to simulate, use the gravity function to get the magnitude of the force and call add_force() on the object to push it. You will need to substitute Vec3 for your own vector class, and make sure it has operator overloading. OpenGL probably provides one.


I think there is a mistake. The formula that calculates the force between object A of mass mA and object B of mass mB at distance r is: [g * (mA * mB)] / (r^2)

Same as above until now. The mistake is that when using meters for distances and kg for mass there is a constant that makes the force porportional with the measurments in real world. That constant is in my fomula noted as 'g'. This 'g' is not 9.81 . This 'g', the gravitational constant. is equal with: 6.67300 × 10^-11 m^3 / (kg * s^2) .

So, for objects A and B both having masses of 1 kg, and the distance 'r' being 1 meter the force between them will be: 6.67300 × 10^-11 (kg * m) / (s^2) , or 6.67300 × 10^-11 Newtons . This is a very small force :) Our planet hass mA HUGE :D . This mass is what gives at the surface a force of 9.81 Newtons on a body with mass of 1 kg.


You might not want to use a physics library, but if you take a look at the source code for one, it would probably help you to understand the formula's you keep seeing a bit better. Box2D is an open source physics engine that you might want to take a look at. Alternately, Bullet is an open source 3d physics engine.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜