C++ SDL Player 跳跃时不会掉落
C++ SDL Player doesn't fall when jumps
我会尽量解释的简单一点,我的角色会跳,但是当 Space 按钮被释放时它不会掉下来。我该如何解决?
这是我的跳转代码:
if (keystate[keys[7]]) //Keys 7 = SPACE Button
{
jumping = true;
}
if (jumping) //If is jumping
{
positionRect.y += jumpVel;
jumpVel -= gravity;
maxjumpheight++;
}
if (maxjumpheight >= 40)
{
positionRect.y += maxjumpheight * delta;
std::cout << "maxjumpheight: " << maxjumpheight << std::endl;
jumping = false;
if (maxjumpheight == 40)
{
maxjumpheight == 0;
}
}
Here 是 Player.cpp 文件的全部来源!
如果你想让角色在释放时停止跳跃。将 else 添加到第一个带有 jumping = false 的 if 语句。
处理这个问题的正常方法是在 y 方向上给玩家一个速度脉冲并使用一些物理方法。如果jumpVel
是y方向的速度,则变成如下(我也会添加一些文体提示):
if (keystate[keys[7]]) //Keys 7 = SPACE Button
{
jumping = true;
// unnecessary to set jumping to true and just check it directly afterwards. Just add it here instead
jumpVel = 1.0; // Set this to some value, this is the impulse you give the player when he jumps
}
// Okay, time to update the position. Lets limit the player so he can't go below 0
if (positionRect.y > 0) {
positionRect.y += jumpVel * delta; // This is how you calculate the new position given speed and change in time
}
// gravity affects the speed with which we fall
jumpVel -= 9.8 * delta;
像那样!现在,如果你按住 space 条,上面的实现允许玩家飞行,但如果你放开它,你应该会看到这个家伙掉下来。如果你想限制最大高度,我会把它留作练习:)
编辑:您的 y 轴是倒置的,因此我们必须相应地进行更改:
if (keystate[keys[7]]) //Keys 7 = SPACE Button
{
jumping = true;
jumpVel = 1.0;
}
// the character is above the bottom of the screen
if (positionRect.y > 480) {
positionRect.y -= jumpVel * delta;
// note: ^ we just flip the axis
}
jumpVel -= 9.8 * delta;
EDIT2:好的,让我们暂时跳过物理学。实现您想要的行为的最简单方法是
if (positionRect.y > 0 && positionRect.y < 480) {
// We are inside the screen bounds
if (keystate[keys[7]]) {
positionRect.y -= 1.0; // jump
} else {
positionRect.y += 1.0; // fall
}
}
删除 jumping
/falling
/maxjumpheight
标志。
EDIT3:啊,当然。再修改一次。
if (keystate[keys[7]]) {
if (positionRect.y > 0) positionRect.y -= 1.0; // jump if below the top
} else {
if (positionRect.y < 480) positionRect.x += 1.0; // fall if above the bottom
}
我会尽量解释的简单一点,我的角色会跳,但是当 Space 按钮被释放时它不会掉下来。我该如何解决?
这是我的跳转代码:
if (keystate[keys[7]]) //Keys 7 = SPACE Button
{
jumping = true;
}
if (jumping) //If is jumping
{
positionRect.y += jumpVel;
jumpVel -= gravity;
maxjumpheight++;
}
if (maxjumpheight >= 40)
{
positionRect.y += maxjumpheight * delta;
std::cout << "maxjumpheight: " << maxjumpheight << std::endl;
jumping = false;
if (maxjumpheight == 40)
{
maxjumpheight == 0;
}
}
Here 是 Player.cpp 文件的全部来源!
如果你想让角色在释放时停止跳跃。将 else 添加到第一个带有 jumping = false 的 if 语句。
处理这个问题的正常方法是在 y 方向上给玩家一个速度脉冲并使用一些物理方法。如果jumpVel
是y方向的速度,则变成如下(我也会添加一些文体提示):
if (keystate[keys[7]]) //Keys 7 = SPACE Button
{
jumping = true;
// unnecessary to set jumping to true and just check it directly afterwards. Just add it here instead
jumpVel = 1.0; // Set this to some value, this is the impulse you give the player when he jumps
}
// Okay, time to update the position. Lets limit the player so he can't go below 0
if (positionRect.y > 0) {
positionRect.y += jumpVel * delta; // This is how you calculate the new position given speed and change in time
}
// gravity affects the speed with which we fall
jumpVel -= 9.8 * delta;
像那样!现在,如果你按住 space 条,上面的实现允许玩家飞行,但如果你放开它,你应该会看到这个家伙掉下来。如果你想限制最大高度,我会把它留作练习:)
编辑:您的 y 轴是倒置的,因此我们必须相应地进行更改:
if (keystate[keys[7]]) //Keys 7 = SPACE Button
{
jumping = true;
jumpVel = 1.0;
}
// the character is above the bottom of the screen
if (positionRect.y > 480) {
positionRect.y -= jumpVel * delta;
// note: ^ we just flip the axis
}
jumpVel -= 9.8 * delta;
EDIT2:好的,让我们暂时跳过物理学。实现您想要的行为的最简单方法是
if (positionRect.y > 0 && positionRect.y < 480) {
// We are inside the screen bounds
if (keystate[keys[7]]) {
positionRect.y -= 1.0; // jump
} else {
positionRect.y += 1.0; // fall
}
}
删除 jumping
/falling
/maxjumpheight
标志。
EDIT3:啊,当然。再修改一次。
if (keystate[keys[7]]) {
if (positionRect.y > 0) positionRect.y -= 1.0; // jump if below the top
} else {
if (positionRect.y < 480) positionRect.x += 1.0; // fall if above the bottom
}