我目前工作的二维弹跳球物理,反弹的球上下。物理行为很好,但在最后速度保持+3,然后0不停止,甚至球已经停止反弹。我应该如何修改代码来解决这个问题?
下面的视频显示了它是如何工作的。注: Bandicam不能记录-3到0之间的速度跃迁。所以,当球停止弹跳时,它就会显示出-3。
https://www.youtube.com/watch?v=SEH5V6FBbYA&feature=youtu.be
下面是生成的报告:https://www.dropbox.com/s/4dkt0sgmrgw8pqi/report.txt
ballPos = D3DXVECTOR2( 50, 100 );
velocity = 0;
acceleration = 3.0f;
isBallUp = false;
void GameClass::Update()
{
// v = u + at
velocity += acceleration;
// update ball position
ballPos.y += velocity;
// If the ball touches the ground
if ( ballPos.y >= 590 )
{
// Bounce the ball
ballPos.y = 590;
velocity *= -1;
}
// Graphics Rendering
m_Graphics.BeginFrame();
ComposeFrame();
m_Graphics.EndFrame();
}发布于 2013-10-21 14:19:09
当球停止弹跳时,放置一个isBounce标志,使速度达到零。
void GameClass::Update()
{
if ( isBounce )
{
// v = u + at
velocity += acceleration;
// update ball position
ballPos.y += velocity;
}
else
{
velocity = 0;
}
// If the ball touches the ground
if ( ballPos.y >= 590 )
{
if ( isBounce )
{
// Bounce the ball
ballPos.y = 590;
velocity *= -1;
}
if ( velocity == 0 )
{
isBounce = false;
}
}发布于 2013-10-21 13:10:44
只有当球不倒在地上时,才加速:
if(ballPos.y < 590)
velocity += accelaration;顺便说一句,如果你发现了碰撞,你不应该把球的位置设置为590。相反,把时间倒转到球落地的那一刻,倒转速度,快进你后退的时间。
if ( ballPos.y >= 590 )
{
auto time = (ballPos.y - 590) / velocity;
//turn back the time
ballPos.y = 590;
//ball hits the ground
velocity *= -1;
//fast forward the time
ballPos.y += velocity * time;
}https://stackoverflow.com/questions/19475213
复制相似问题