如何使精灵在重力作用下弹跳?

发布于 2024-12-09 06:45:58 字数 1457 浏览 2 评论 0原文

好的,所以我一直在关注有关浮动的教程,并使精灵以一定速度移动 每帧低于 1 像素。很容易。现在,我得到以下任务:

在新坦克位置的计算中添加重力影响, 让它自动落下而不是在顶部弹起 每次。

我该如何制作这个重力的东西?我只学会了如何添加 x 和 y... 当然,简单地添加每一帧的 y 值并不能很好地工作,而且实际上也不是这样 重力。我真的不明白我应该在这里做什么。

要了解简短教程,请点击以下链接。这很容易。 http://www.devmaster.net/articles /intro-to-c++-with-game-dev/part6.php

这是我的错误代码,用于使坦克向四个方向移动并使其不可见 下降时。由于教程,我添加了浮动。

 Sprite theSprite( new Surface("assets/ctankbase.tga"), 16 );


        float SpriteX = 50;
    float SpriteY = 0;    //the sprite starts at the top of the screen (2D coords system)
    float screenBottom = 400; //I'm assuming your screen is 200 pixels high, change to your size
    float speedY = 0.01; //the initial speed of the sprite
    float accelY = 0.01;  //the change in speed with each tick
    bool  Bottom = false;

    void Game::Tick( float a_DT )
    {
        m_Screen->Clear( 0 );

        theSprite.Draw(SpriteX, SpriteY, m_Screen ); // Draws sprite


        if(SpriteY >= screenBottom) //when we hit bottom we change the direction of speed and slow it
        {
            speedY = -speedY/2;
            Bottom = true;
        }

        if(Bottom == false)
        {
        SpriteY += speedY;
        }

        speedY += accelY;

    }

那么我该如何用不太迟缓的代码让这个坦克在“重力”作用下在屏幕上弹跳呢? 谢谢。抱歉,如果这是一个愚蠢的问题,但我没有看到它。

Ok, so I've been following a tutorial about floats, and making a sprite move at a speed
lower than 1 pixel per frame. Pretty easy. Now, I get the following assignment:

Add a gravity influence to the calculation of the new tank's position,
to make it come down automatically instead of bouncing against the top
every time.

How would I make this gravity thing? I only learned how to add to the x and y...
Simply adding to the y value every frame doesn't work well, of course, and isn't really
gravity. I don't really get what I'm expected to do here.

To get an idea of the short tutorial, here's the link. It's easy.
http://www.devmaster.net/articles/intro-to-c++-with-game-dev/part6.php

Here's my bad code for making the tank move in four directions and making it invisible
when going down. I added floats because of the tutorial.

 Sprite theSprite( new Surface("assets/ctankbase.tga"), 16 );


        float SpriteX = 50;
    float SpriteY = 0;    //the sprite starts at the top of the screen (2D coords system)
    float screenBottom = 400; //I'm assuming your screen is 200 pixels high, change to your size
    float speedY = 0.01; //the initial speed of the sprite
    float accelY = 0.01;  //the change in speed with each tick
    bool  Bottom = false;

    void Game::Tick( float a_DT )
    {
        m_Screen->Clear( 0 );

        theSprite.Draw(SpriteX, SpriteY, m_Screen ); // Draws sprite


        if(SpriteY >= screenBottom) //when we hit bottom we change the direction of speed and slow it
        {
            speedY = -speedY/2;
            Bottom = true;
        }

        if(Bottom == false)
        {
        SpriteY += speedY;
        }

        speedY += accelY;

    }

So how would I make this tank bounce around the screen with 'gravity', in less retarded code?
Thanks. Sorry if this is a stupid question, but I'm not seeing it.

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

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

发布评论

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

评论(3

原谅我要高飞 2024-12-16 06:45:58

当我需要某种“重力”(也可以称为仅向下的加速度)时,我通常做的是定义位置速度加速度变量。每个刻度线都将速度添加到位置,这样你的精灵在每个刻度线都会向给定方向移动,现在您还需要更改速度运动方向逐渐改变,这就是加速度的用武之地,您可以将其添加到速度每个刻度。

伪代码将如下所示:

var posy = 50;
var speedy = 5;
var accely = -0.5;
tick() {
    posy += speedy;
    speedy += accely;
}

这样您的速度最终将变为负值,因此将开始移动精灵底部而不是向上。

注意:我的值是任意的,您必须使用它们才能达到您真正需要的效果

这是我认为适合您的情况的代码,出于简单原因,我假设你的球仅在 Y 轴上移动。您必须自己添加 X 轴运动。

float SpriteX = 50;
float SpriteY = 100;    //the sprite starts at the top of the screen (2D coords system)
float screenBottom = 200; //I'm assuming your screen is 200 pixels high, change to your size
float speedY = 0.23; //the initial speed of the sprite
float accelY = 0.02;  //the change in speed with each tick

void Game::Tick( float a_DT )
{
    m_Screen->Clear( 0 );

    theSprite.Draw(SpriteX, SpriteY, m_Screen ); // Draws sprite


    if(SpriteY >= screenBottom) //when we hit bottom we change the direction of speed and slow it
    {
        speedY = -speedY/2;
    }
    SpriteY += speedY;
    speedY += accelY;
}

该代码根本没有经过测试,因此可能需要使用它才能使其实际工作。值也是任意的,但逻辑保持不变。我尝试删除所有与弹跳不直接相关的内容,让我知道它是如何运作的。

希望这有帮助。

What I usually do when I need sorts of "gravity" (which may also be referred as acceleration directed down only), is define a position, speed and acceleration variables. Each tick you add speed to position, that way your sprite moves in a given direction every tick, now you also need to change the speed for the direction of motion to change gradually, that's where acceleration comes in, you add it to speed every tick.

A pseudo code will look like this:

var posy = 50;
var speedy = 5;
var accely = -0.5;
tick() {
    posy += speedy;
    speedy += accely;
}

This way your speed will eventually become negative, and thus will start moving the sprite bottom instead of up.

(Note: my values are arbitrary, you'll have to play with them in order to achieve the effect you really need)

Here's the code that I assume will fit your case, for simplicity reasons I'm assuming your ball moves only on the Y axis. You'd have to add the X axis motion by yourself.

float SpriteX = 50;
float SpriteY = 100;    //the sprite starts at the top of the screen (2D coords system)
float screenBottom = 200; //I'm assuming your screen is 200 pixels high, change to your size
float speedY = 0.23; //the initial speed of the sprite
float accelY = 0.02;  //the change in speed with each tick

void Game::Tick( float a_DT )
{
    m_Screen->Clear( 0 );

    theSprite.Draw(SpriteX, SpriteY, m_Screen ); // Draws sprite


    if(SpriteY >= screenBottom) //when we hit bottom we change the direction of speed and slow it
    {
        speedY = -speedY/2;
    }
    SpriteY += speedY;
    speedY += accelY;
}

This code is not tested at all, so will may have to play with it to make it actually work. Also values are arbitrary, but logic stays the same. I tried to remove all stuff that's not directly related to bouncing, let me know how it works out.

Hope this helped.

高冷爸爸 2024-12-16 06:45:58

这只是简单的物理学。

您需要一个位置和速度变量。速度会随着时间线性增加,因此您只需在每一帧中添加一个常数即可。然后将速度添加到该位置,您将获得正确的重力行为。

Its just simple physics.

You need a variable for the position and for the speed. Speed will increase linear over time, so you just add a constant to it every frame. Then you add the speed to the position, and you will get a corrent gravitation behavior.

七月上 2024-12-16 06:45:58

您需要跟踪精灵的当前速度。

每次迭代时,你都必须在速度上加上一个常数(这个常数就是加速度),然后将精灵绘制成按照当前速度移动,即在当前位置上加上速度。当然,为了获得好看的结果,速度和加速度应该是二维向量。

在地球上,重力是指向地球的加速度,其值为 9.81 m/(sec^2)。这意味着物体在地球方向上的速度每秒增加9.81 m/s

you will need to keep track of the current speed of your sprite.

at each iteration, you have to add a constant to the speed (this constant is the acceleration), then draw the sprite as if it has moved according to the current speed, that is add the speed to the current position. of course, for a good looking result, speed and acceleration should be 2 dimensional vectors.

on earth, gravity is an acceleration with a value of 9.81 m/(sec^2) directed toward the earth. that means at each second the speed of an object increases by 9.81 m/s in the direction of the earth.

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