Skip to main content
Cleanup
Source Link
DMGregory
  • 136.3k
  • 22
  • 247
  • 373

Free fall motion simulation Bouncing ball with Euler integration loses speed over time

Hi guys I am trying to implement free fall motion of a ball towards a plane. 

I implemented collision detection and if collision happens, I change sign of velocity of a ball so it bounces off and climbs back up. 

Now, it should climb back to its original position if there is no loss of energy, but after some testing I realized that after each bounce I have less speed than the bounce before.

Part of my code related to this:

if (collision)
    {   
        velocity.y = -velocity.y;

        std::cout << velocity.y << std::endl;
    }

    velocity.y += gravityAccel * ts.getSeconds();
    

    position.x += velocity.x * ts.getSeconds() + ent.getComponent<RigidBody>().InitVelocity.x * ts.getSeconds();
    position.y += velocity.y * ts.getSeconds() + ent.getComponent<RigidBody>().InitVelocity.y * ts.getSeconds();
    position.z += velocity.z * ts.getSeconds() + ent.getComponent<RigidBody>().InitVelocity.z * ts.getSeconds();
 

I think that my problem lies in the calculations that happen right before ball reaches its maximum height. I think that he ball has some speed that is less than the speed change calculated by the next time step, so what I get is negative speed and reversal of motion direction along y axes a bit before it should actually happen. If this is so, I am trying to figure out bestthe best way to solve this problem and have relatively nice free fall motion. 

I know, you could say that this is not a bug but a feature since I have energy loss without asking for it. :)

Free fall motion simulation

Hi guys I am trying to implement free fall motion of a ball towards a plane. I implemented collision detection and if collision happens I change sign of velocity of a ball so it bounces off and climbs back up. Now, it should climb back to its original position if there is no loss of energy but after some testing I realized that after each bounce I have less speed than the bounce before.

Part of my code related to this:

if (collision)
    {   
        velocity.y = -velocity.y;

        std::cout << velocity.y << std::endl;
    }

    velocity.y += gravityAccel * ts.getSeconds();
    

    position.x += velocity.x * ts.getSeconds() + ent.getComponent<RigidBody>().InitVelocity.x * ts.getSeconds();
    position.y += velocity.y * ts.getSeconds() + ent.getComponent<RigidBody>().InitVelocity.y * ts.getSeconds();
    position.z += velocity.z * ts.getSeconds() + ent.getComponent<RigidBody>().InitVelocity.z * ts.getSeconds();
 

I think that my problem lies in the calculations that happen right before ball reaches its maximum height. I think that he ball has some speed that is less than speed change calculated by next time step so what I get is negative speed and reversal of motion direction along y axes a bit before it should actually happen. If this is so, I am trying to figure out best way to solve this problem and have relatively nice free fall motion. I know, you could say that this is not a bug but a feature since I have energy loss without asking for it :)

Bouncing ball with Euler integration loses speed over time

I am trying to implement free fall motion of a ball towards a plane. 

I implemented collision detection and if collision happens, I change sign of velocity of a ball so it bounces off and climbs back up. 

Now, it should climb back to its original position if there is no loss of energy, but after some testing I realized that after each bounce I have less speed than the bounce before.

Part of my code related to this:

if (collision)
{   
    velocity.y = -velocity.y;

    std::cout << velocity.y << std::endl;
}

velocity.y += gravityAccel * ts.getSeconds();


position.x += velocity.x * ts.getSeconds() + ent.getComponent<RigidBody>().InitVelocity.x * ts.getSeconds();
position.y += velocity.y * ts.getSeconds() + ent.getComponent<RigidBody>().InitVelocity.y * ts.getSeconds();
position.z += velocity.z * ts.getSeconds() + ent.getComponent<RigidBody>().InitVelocity.z * ts.getSeconds();

I think that my problem lies in the calculations that happen right before ball reaches its maximum height. I think that he ball has some speed that is less than the speed change calculated by the next time step, so what I get is negative speed and reversal of motion direction along y axes a bit before it should actually happen. If this is so, I am trying to figure out the best way to solve this problem and have relatively nice free fall motion. 

I know, you could say that this is not a bug but a feature since I have energy loss without asking for it. :)

Source Link

Free fall motion simulation

Hi guys I am trying to implement free fall motion of a ball towards a plane. I implemented collision detection and if collision happens I change sign of velocity of a ball so it bounces off and climbs back up. Now, it should climb back to its original position if there is no loss of energy but after some testing I realized that after each bounce I have less speed than the bounce before.

Part of my code related to this:

if (collision)
    {   
        velocity.y = -velocity.y;

        std::cout << velocity.y << std::endl;
    }

    velocity.y += gravityAccel * ts.getSeconds();
    

    position.x += velocity.x * ts.getSeconds() + ent.getComponent<RigidBody>().InitVelocity.x * ts.getSeconds();
    position.y += velocity.y * ts.getSeconds() + ent.getComponent<RigidBody>().InitVelocity.y * ts.getSeconds();
    position.z += velocity.z * ts.getSeconds() + ent.getComponent<RigidBody>().InitVelocity.z * ts.getSeconds();

I think that my problem lies in the calculations that happen right before ball reaches its maximum height. I think that he ball has some speed that is less than speed change calculated by next time step so what I get is negative speed and reversal of motion direction along y axes a bit before it should actually happen. If this is so, I am trying to figure out best way to solve this problem and have relatively nice free fall motion. I know, you could say that this is not a bug but a feature since I have energy loss without asking for it :)