Ball rebound that does not comply with the energy conservation rule

I'm currently busy writing a small physical ball engine for my programming course in Win32 API and C ++. I have finished the GDI buffer visualizer and the entire GUI (a couple of additional steps to configure), but I am very close to completion. The only serious obstacles that recently have been balls colliding with a ball (but I can fix it myself), but the biggest problem of them is the rebound of the balls. What happens is that I throw the ball and it really falls, but as soon as it bounces, it will bounce higher than the point, I let it go ??? the funny thing is, it happens only at a certain height. This part is a physical code: (If you need any more code or explanation, please ask, but I would really appreciate it if you guyscould see my code.)

#void RunPhysics(OPTIONS &o, vector<BALL*> &b)
{ 
    UINT simspeed = o.iSimSpeed;
    DOUBLE  DT; //Delta T
    BOOL bounce; //for playing sound

    DT= 1/o.REFRESH;

    for(UINT i=0; i<b.size(); i++)
    {
        for(UINT k=0; k<simspeed; k++)
        {           
            bounce=false;

            //handle the X bounce
            if( b.at(i)->rBall.left <= 0 && b.at(i)->dVelocityX < 0 ) //ball bounces against the left wall
            {
                b.at(i)->dVelocityX = b.at(i)->dVelocityX * -1 * b.at(i)->dBounceCof;
                bounce=true;
            }
            else if( b.at(i)->rBall.right >= SCREEN_WIDTH && b.at(i)->dVelocityX > 0) //ball bounces against the right wall
            {           
                b.at(i)->dVelocityX = b.at(i)->dVelocityX * -1 * b.at(i)->dBounceCof;
                bounce=true;
            }
            //handle the Y bounce
            if( b.at(i)->rBall.bottom >= SCREEN_HEIGHT && b.at(i)->dVelocityY > 0 ) //ball bounces against the left wall
            {
                //damping of the ball
                if(b.at(i)->dVelocityY < 2+o.dGravity/o.REFRESH)
                {
                    b.at(i)->dVelocityY = 0;
                }

                //decrease the Velocity of the ball according to the bouncecof
                b.at(i)->dVelocityY = b.at(i)->dVelocityY * -1*b.at(i)->dBounceCof;
                b.at(i)->dVelocityX = b.at(i)->dVelocityX * b.at(i)->dBounceCof;

                bounce=true;
            }


            //gravity
            b.at(i)->dVelocityY += (o.dGravity)/o.REFRESH;
            b.at(i)->pOrigin.y += b.at(i)->dVelocityY + (1/2)*o.dGravity/o.REFRESH*DT*METER; 
            //METER IS DEFINED GLOBALLY AS 100 which is the amount of pixels in a meter

            b.at(i)->pOrigin.x += b.at(i)->dVelocityX/o.REFRESH*METER; 

            b.at(i)->UpdateRect();
        }
    }
    return;
}
+3
9

. , (DT) . , , , Y:

  b.at(i)->pOrigin.y += b.at(i)->dVelocityY + (1/2)*o.dGravity/o.REFRESH*DT*METER; 

, , DT. :

  b.at(i)->pOrigin.y += b.at(i)->dVelocityY * DT; 

, ( METER).

+4

, .

, . , .

: , , - /.

+1

RunPhysics? ? . t , . , .

, :

b.at(i)->pOrigin.y += b.at(i)->dVelocityY + (1/2)*o.dGravity/o.REFRESH*DT*METER;

: b.at(i) , .

Ball* CurrentBall = b.at(i);
+1

!! !! !! , : - (

, ! , , , , , . , , : O : , ( ), , . Y, , Delta T, verry. - , . , Elipse() Win32, LONG, . , , , - , , , ( ). , DOUBLE Ball, ( ) . RenderFrame() , elipse, . , STACKOVERFLOW PEOPLE ROCK!!!

+1

dBounceCof > 1, . , .

0

, :

 b.at(i)->dVelocityY += (o.dGravity)/o.REFRESH;

v=v0+gt - , dGravity*DT dGravity/REFRESH_FREQ.

 b.at(i)->pOrigin.y += b.at(i)->dVelocityY + (1/2)*o.dGravity/o.REFRESH*DT*METER; 

: p = p0+v + 1/2gt^2.

  • *, .
  • /, . METER
  • , .
0

!!! , , RunPhysics PeekMessage. , , , . , 1 , . , DT , , ??? , 0,9

0

, , .

.. , , / ( "" ), , "" , .

W.r.t. .

0

, , , . , , . ( . , .)

( ). , , , , .

(dt), . , ( ), . .

0

Source: https://habr.com/ru/post/1708107/


All Articles