开发者

How to make a sprite bounce, with gravity?

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开发者_Python百科 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.


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.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜