1

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.

Turbosheep
  • 183
  • 1
  • 2
  • 13

3 Answers3

2

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.

MeLight
  • 5,454
  • 4
  • 43
  • 67
  • Thanks for your answer. Could you show this to me in relation to the code that I wrote, or is that too much work? I don't really get how to implement this. Mind you, I'm completely new to this coding stuf. – Turbosheep Oct 09 '11 at 10:36
  • @Turbosheep There are some things I don't really get in your code. Like why are you doing this: `SpriteY += 0.01;` at the start of `tick()` and then change the value of `SpriteY` again if it moves up or down... Can you please explain that part a bit? An explanation about your constraints (what are the x/y limits of motion) would also be nice. – MeLight Oct 09 '11 at 11:11
  • Of course. The SpriteY += 0.01; can be ignored. I edited it out of my post now. I left that experiment in by error, I'm sorry. The constraints are there because I needed some conditions to move the sprite right, down, left and up. I needed to move the sprite in four conditions for an assignment, so... But I wouldn't know how to actually bounce the sprite or anything. – Turbosheep Oct 09 '11 at 12:19
  • Edited answer to include somewhat real code. Let me know how it works out. – MeLight Oct 09 '11 at 12:39
  • Edited again for ball to keep bouncing after hitting ground, check the new code. – MeLight Oct 09 '11 at 12:50
  • Thanks so much for your help! Interesting as it is, I had something else in mind. Do you know that old windows screensaver that makes a ball bounce across the screen? I want that, but with gravity. So that the ball moves closer to the ground with each bounce. Anyway, I was playing with your code and saw that the sprite bounce from the ground (nice!) but it kept bouncing eternally. I guess because the tick event kept adding the SpriteY. – Turbosheep Oct 09 '11 at 13:29
  • Check my first post, it shows what I added to your code. This makes the tank stop bouncing completely though. – Turbosheep Oct 09 '11 at 13:35
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/4118/discussion-between-melight-and-turbosheep) – MeLight Oct 09 '11 at 13:37
  • Are you dividing the speed by two when inverting it? – MeLight Oct 09 '11 at 13:38
  • I checked, it stops becuase you're not resetting the `Bottom` variable. You don't need this variable, as your ball should be moving all the time (either up or down, but moving). Which means you add speed to position all the time, only the speed varies. – MeLight Oct 09 '11 at 13:43
0

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.

Sibbo
  • 3,796
  • 2
  • 23
  • 41
0

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.

Adrien Plisson
  • 22,486
  • 6
  • 42
  • 73