Java – how to move objects at speed correctly?

I recently started playing Android and decided to try to make a basic physical simulator, but I encountered a small problem

I have ball's object. Each ball has a velocity vector. The way I move it is to add the position of each ball to the position of the ball. It works well before I notice the problem with this method

When I tried to apply gravity to the ball, I noticed that when the two balls were close to each other, one of them was fast

After some debugging, I found the reason for this. The following is an example of my calculation of 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));
        }

That's the problem – when the ball gets very close, the gravity becomes very high (as it should be), so the speed becomes very high, but because I add a vector to each scale position, and the value of the vector is very high, a ball is ejected

This reminds me of my question - is there a better way to move objects than adding vectors? Also, is there a better way to deal with gravity?

I appreciate any help you offer

resolvent:

You can try this:

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

(acceleration, velocity, displacement and position) is the vector of the ball

*Note (DT = delta time, that is, the difference time between the current frame and the previous frame

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>