How to move objects in speed?

I recently started playing with android and decided to try making a basic physical simulator, but I ran into a small problem.

I have a Ball object, each ball has a velocity vector, and the way I move it is to add the specified vector to the ball with each tick. It worked well enough until I noticed a problem with this approach.

When I tried to apply gravity to the balls, I noticed that when two balls are approaching each other, one of the balls starts at a great speed.

After some debugging, I found the reason for this. Here is an example of how I calculate gravity and acceleration:

    //for each ball that isn't this ball
    for (Ball ball : Ball.balls)
        if (ball != this) {
            double m1 = this.getMass();
            double m2 = ball.getMass();
            double distance = this.getLocation().distance(ball.getLocation());
            double Fg = 6.674*((m1*m2)/(distance * distance));
            Vector direction = ball.getLocation().subtract(this.getLocation()).toVector();
            Vector gravity = direction.normalize().multiply(Fg / mass);
            this.setVeloctity(this.getVelocity().add(gravity));
        }

- , ( ), , , , , .

, - , ? , ?

, , , .

+4
2

:

acceleration.y = force.y / MASS; //to get the acceleration
force.y = MASS * GRAVITY_Constant; // to get the force
displacement.y = velocity.y * dt + (0.5f * acceleration.y * dt * dt); //Verlet integration for y displacment
position.y += displacement.y * 100; //now cal to position
new_acceleration.y = force.y / MASS; //cau the new acc
avg_acceleration.y = 0.5f * (new_acceleration.y + acceleration.y); //get the avg
velocity.y += avg_acceleration.y * dt; // now cal the velocity from the avg

(, , ) .

* note (dt = Delta Time - .

+3

. , , .

, dt

velocity = velocity + acceleration * dt

, , , , . ( ) () .

0

All Articles