跳跃与。重力

发布于 2024-12-18 16:21:53 字数 1198 浏览 2 评论 0原文

我正在开发我的第一个 XNA 2D 游戏,但遇到了一些问题。 如果我跳,我的精灵会跳但不会掉下来。 我还有另一个问题,用户可以按住空格键跳到他想要的高度,但我不知道如何阻止他这样做。这是我的代码: 跳跃:

    if (FaKeyboard.IsKeyDown(Keys.Space))
        {
            Jumping = true;
            xPosition -= new Vector2(0, 5);
        }

        if (xPosition.Y >= 10)
        {
            Jumping = false;
            Grounded = false;
        }

非常简单的基本重力:

    if (!Grounded && !Jumping)
        {
            xPosition += new Vector2(1, 3) * speed;
        }

这里是接地设置为 True 或 False 的碰撞

    Rectangle MegamanRectangle = new Rectangle((int)xPosition.X, (int)xPosition.Y, FrameSizeDraw.X, FrameSizeDraw.Y);
        Rectangle Block1Rectangle = new Rectangle((int)0, (int)73, Block1.Width, Block1.Height);
        Rectangle Block2Rectangle = new Rectangle((int)500, (int)73, Block2.Width, Block2.Height);

        if ((MegamanRectangle.Intersects(Block1Rectangle) || (MegamanRectangle.Intersects(Block2Rectangle))))
        {
            Grounded = true;
        }
        else
        {
            Grounded = false;
        }

接地布尔和重力已经过测试并且正在工作。 有什么想法吗? 预先致谢,如果您需要本准则的另一部分,请随时询问。

I'm working on my first XNA 2D game and I have a little problem.
If I jump, my sprite jumps but does not fall down.
And I also have another problem, the user can hold spacebar to jump as high as he wants and I don't know how to keep him from doing that. Here's my code:
The Jump :

    if (FaKeyboard.IsKeyDown(Keys.Space))
        {
            Jumping = true;
            xPosition -= new Vector2(0, 5);
        }

        if (xPosition.Y >= 10)
        {
            Jumping = false;
            Grounded = false;
        }

The really simple basic Gravity:

    if (!Grounded && !Jumping)
        {
            xPosition += new Vector2(1, 3) * speed;
        }

Here's where's the grounded is set to True or False with a Collision

    Rectangle MegamanRectangle = new Rectangle((int)xPosition.X, (int)xPosition.Y, FrameSizeDraw.X, FrameSizeDraw.Y);
        Rectangle Block1Rectangle = new Rectangle((int)0, (int)73, Block1.Width, Block1.Height);
        Rectangle Block2Rectangle = new Rectangle((int)500, (int)73, Block2.Width, Block2.Height);

        if ((MegamanRectangle.Intersects(Block1Rectangle) || (MegamanRectangle.Intersects(Block2Rectangle))))
        {
            Grounded = true;
        }
        else
        {
            Grounded = false;
        }

The grounded bool and The gravity have been tested and are working.
Any ideas why?
Thanks in advance and don't hesitate to ask if you need another Part of the Code.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

无所的.畏惧 2024-12-25 16:21:53

处理跳跃的一种相对简单的方法是将重力实现为矢量,并将跳跃/移动实现为脉冲。

例如:

foreach (Object obj in GameWorld)
{
    obj.Velocity *= 0.5; // Slow it down slightly
    obj.Velocity += GravityPerSecond * FrameTime; // Gravity adjusted for time
    obj.Move(obj.Velocity); // Handle collision and movement here
}

速度应该是与世界具有相同维度的向量,而Move是在给定向量中将对象移动到尽可能远的方法。

跳跃代码:

OnKeyDown(Key k)
{
    if (k == Key.Space)
    {
        obj.Velocity += Vector2(0, 10); // Add an upward impulse
    }
}

这将导致对象向上移动一点,然后开始下降。您可以将类似的脉冲用于其他效果(移动、爆炸、碰撞)。当两个物体碰撞时,您需要碰撞修改速度以进行弹跳。

这是一个极其简单的物理系统,将使未来的变化变得更加简单,并且可以允许一些有趣的关卡设计(改变重力等)。它通过将复杂的 IsJumping 代码抽象为单个机制来简化跳跃处理。

为了防止在不站在物体上的情况下跳跃,您需要进行碰撞测试,或跟踪物体向下碰撞/停止碰撞的时间。还可以检查物体的垂直速度是否为零并仅允许跳跃(防止在坠落或向上移动时跳跃)。

A relatively simple way to handle jumping is to implement gravity as a vector and jumping/moving as an impulse.

For example:

foreach (Object obj in GameWorld)
{
    obj.Velocity *= 0.5; // Slow it down slightly
    obj.Velocity += GravityPerSecond * FrameTime; // Gravity adjusted for time
    obj.Move(obj.Velocity); // Handle collision and movement here
}

The velocity should be a vector with the same dimensions as the world, and Move a method that moves the object as far as possible in the vector given.

Jump code:

OnKeyDown(Key k)
{
    if (k == Key.Space)
    {
        obj.Velocity += Vector2(0, 10); // Add an upward impulse
    }
}

This will cause the object to move up for a bit, then begin to fall. You can use similar impulses for other effects (movement, explosions, collision). You will need to have collision modify the velocity when two objects collide, for bouncing.

This is an extremely simple physics system that will make future changes much simpler, and could allow some interesting level design (change gravity and such). It simplifies handling jumping by abstracting it out from complex IsJumping code to a single mechanic.

To prevent jumping without standing on an object, you'd need to do a collision test, or track when objects collide/stop colliding for the down direction. It might also be possible to check if the object's vertical velocity is zero and allow jumping only then (would prevent jumping while falling or moving up).

青春有你 2024-12-25 16:21:53

我相信这可能是答案:

将您的更改

    if (FaKeyboard.IsKeyDown(Keys.Space))
    {
        Jumping = true;
        xPosition -= new Vector2(0, 5);
    }

    if (xPosition.Y >= 10)
    {
        Jumping = false;
        Grounded = false;
    }

    if (!Jumping && FaKeyboard.IsKeyDown(Keys.Space))
    {
        Jumping = true;
        Grounded = false;
    }

    if (Jumping)
    {
        xPosition -= new Vector2(0, 5);
    } 

    if (xPosition.Y >= 10)
    {
        Jumping = false;
    }

速度给出的其他答案确实是更好的方法,但这可能足够接近,可以让您重新开始。

I believe this might be the answer:

change your

    if (FaKeyboard.IsKeyDown(Keys.Space))
    {
        Jumping = true;
        xPosition -= new Vector2(0, 5);
    }

    if (xPosition.Y >= 10)
    {
        Jumping = false;
        Grounded = false;
    }

to

    if (!Jumping && FaKeyboard.IsKeyDown(Keys.Space))
    {
        Jumping = true;
        Grounded = false;
    }

    if (Jumping)
    {
        xPosition -= new Vector2(0, 5);
    } 

    if (xPosition.Y >= 10)
    {
        Jumping = false;
    }

The other answers given with velocity are really better approaches, but this might be close enough to get you started again.

∞梦里开花 2024-12-25 16:21:53

这是一个非常简单的解决方案,可以使跳跃向上和向下移动:

const float SCALE = 20.0f; //SPEED!!!
if (jump <= 15 && jump != 0)
            {
                humanPosition.Y -= SCALE * speed;
                jump++;
            }
            else if (jump > 15)
            {
                humanPosition.Y += SCALE * speed;
                jump++;
                if (jump == 32)
                {
                    jump = 0;
                    humanPosition.Y = 307;
                }
            }

并通过以下方式激活它的作用

            else if (keyboard.IsKeyDown(Keys.Up))
        {
            jump = 1;
            humanPosition.Y -= SCALE * speed;
        }

是通过将其设置为 1 来启动跳跃
另外,如果您想限制仅在角色位于地面时跳跃,您可以添加与此类似的条件

if (humanPosition.Y != GROUNDLEVEL){}

Here is a very rustic solution to making a jump go up and down:

const float SCALE = 20.0f; //SPEED!!!
if (jump <= 15 && jump != 0)
            {
                humanPosition.Y -= SCALE * speed;
                jump++;
            }
            else if (jump > 15)
            {
                humanPosition.Y += SCALE * speed;
                jump++;
                if (jump == 32)
                {
                    jump = 0;
                    humanPosition.Y = 307;
                }
            }

And is activated by

            else if (keyboard.IsKeyDown(Keys.Up))
        {
            jump = 1;
            humanPosition.Y -= SCALE * speed;
        }

What this does is set in motion the jump by setting it to 1
Also if you want to restrict jumping to only when the character is on the ground you can add a condition similar to this

if (humanPosition.Y != GROUNDLEVEL){}
夜夜流光相皎洁 2024-12-25 16:21:53

跳跃不应该是布尔值,除非您需要它用于动画或只能在跳跃时使用的能力之类的东西。

相反,您应该使用矢量运动,当玩家接触地面并按下跳跃按钮时,为您的运动添加向上跳跃矢量。

然后,重力需要成为作用于玩家的向下矢量,并且它会自行处理。

Jumping shouldn't be a boolean unless you need it for something like an animation or an ability that can only be used while jumping.

Instead, you should be using vector movement and when the player is touching the ground and press their jump button, add an upward jump vector to your motion.

Then, gravity needs to be a downward vector acting against the player and it will handle itself.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文