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; }
. , (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).
, .
, . , .
: , , - /.
RunPhysics? ? . t , . , .
, :
: b.at(i) , .
Ball* CurrentBall = b.at(i);
, ! , , , , , . , , : O : , ( ), , . Y, , Delta T, verry. - , . , Elipse() Win32, LONG, . , , , - , , , ( ). , DOUBLE Ball, ( ) . RenderFrame() , elipse, . , STACKOVERFLOW PEOPLE ROCK!!!
dBounceCof > 1, . , .
b.at(i)->dVelocityY += (o.dGravity)/o.REFRESH;
v=v0+gt - , dGravity*DT dGravity/REFRESH_FREQ.
v=v0+gt
dGravity*DT
dGravity/REFRESH_FREQ
: p = p0+v + 1/2gt^2.
p = p0+v + 1/2gt^2
!!! , , RunPhysics PeekMessage. , , , . , 1 , . , DT , , ??? , 0,9
, , .
.. , , / ( "" ), , "" , .
W.r.t. .
, , , . , , . ( . , .)
( ). , , , , .
(dt), . , ( ), . .
Source: https://habr.com/ru/post/1708107/More articles:WebBrowser event properties? - c #How can I return IQueryable to C # 3.0 without knowing its type? - linq-to-sqlCan you include only certain forms at compile time - c #How to set attributes for SOAP elements in Delphi? - soapWhere is the best place to place business rules in a Silverlight MVVM RIA Services application? - mvvmJQuery: вопрос Ajax.load - jqueryКак выполнить строку за строкой и символом по символу diff на текст? - c#Django template includes - pythonEffectively modeling MruList in C # or Java - java`autoload` causes an error, but` require` is not (ruby) - ruby | fooobar.comAll Articles