私は現在、Win32 APIとC++のプログラミングコース用の小さなボール物理エンジンを書いています。私はGDIのバックバッファーレンダラーとGUI全体(いくつかの調整が必要なもの)を終えましたが、私は完了までに非常に近いです。最後にボールからボールに衝突する唯一の大きな障害(しかし、私はこれを自分で修正することができます)が、彼らの最大の問題はボールのバウンスです。何が起こるかは、ボールを投げると本当に落ちるが、いったんバウンスすると、それがリリースされたポイントより高くバウンスするだろうか?面白いことは、それは特定の高さ以下の場合にのみ発生します。 (あなたが任意のより多くのコードや説明が必要な場合は、お問い合わせください、しかし、あなたたちは私のコードを見ていることができれば、私はそれを大幅に感謝。)エネルギ保存規則に準拠しない跳ねるボール
#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は1/o.REFRESHに設定され、後でo.REFRESH * DTで使用されるため、論理的なエラーがあるか、または単に削除することができます。 – schnaader