You are not logged in.

#1 2009-06-02 11:12:42

AdrenalineJunky
Member
Registered: 2009-05-03
Posts: 149

2d game physics/programming quesiton... C++/SDL

For the last couple of days i've been trying to learn C++/SDL by wprking on making a 2d platformer.

the basics of what i'm working with (stripped down to just the relevant components)

game loop

while( SDL_PollEvent( &event ) )
      {
        myHero.handle_input();
            //If the user has Xed out the window
            if( event.type == SDL_QUIT )
            {
                //Quit the program
                quit = true;
            }
        }

   
    myHero.move( blahbox );
   
    //Set the camera
    myHero.set_camera();


handle_input()

    if( event.type == SDL_KEYDOWN )
    {
        
            if( SDLK_LEFT ) { xForce -= HERO_WIDTH / 2; }
        
            if( SDLK_RIGHT ){ xForce += HERO_WIDTH / 2; }
        

    }
    else if( event.type == SDL_KEYUP )
    {

            if( SDLK_LEFT ) { xForce = 0; }
            if( SDLK_RIGHT ) { xForce = 0; }
       

    }

move()

void Hero::move(SDL_Rect blahbox)
{
    //Move the dot left or right
    if( xForce != 0 ) { xAcc = xForce / HERO_MASS; }
    if( xForce == 0 ) { xVel = xAcc = 0; }
    xVel = xVel + xAcc;
    box.x = box.x + xVel;


    //If the dot went too far to the left or right or touched a wall
    if( ( box.x < 0 ) || ( box.x + HERO_WIDTH > LEVEL_WIDTH ) || check_collision( box, blahbox) )
    {
        //move back
        box.x = box.x - xVel;
    }

}

what i was going for is force is applied when a key is pressed, 

accelleration = Force / Mass
Velocity = Velocity + Accelleration
etc...

when no force is being applied, set velocity and accelleration to 0

now, i know thats a little unrealistic, i was going to try to set it to slow down etc later, but i can't get this working.

for whatever reason, when i lift the key my guy just keeps cruising along at whatever speed he was going before...

anybody know what i'm doing wrong?

Last edited by AdrenalineJunky (2009-06-02 11:13:50)

Offline

#2 2009-06-02 13:05:37

AdrenalineJunky
Member
Registered: 2009-05-03
Posts: 149

Re: 2d game physics/programming quesiton... C++/SDL

figured out my problem, i saved a backup copy and didn't realize i was working on the backup copy.....

so i kept recompiling my old code without any changes i was making.

after compiling the correct code it does exactly what i thought it should....

Offline

#3 2009-06-02 13:46:55

whoops
Member
Registered: 2009-03-19
Posts: 891

Re: 2d game physics/programming quesiton... C++/SDL

Great, I'm not alone!

Little trick on the side: try to make more typos. You already know some-thing's wrong when you don't get an error message  trying the program for the first time after a major addition/change! wink

Offline

#4 2009-06-02 13:54:33

AdrenalineJunky
Member
Registered: 2009-05-03
Posts: 149

Re: 2d game physics/programming quesiton... C++/SDL

^^^ thats actually what made me realise what was going on.

all the sudden it dawned on me... i haven't had a compiler error in a while... something can't be right smile

Offline

#5 2009-06-02 14:12:26

Runiq
Member
From: Germany
Registered: 2008-10-29
Posts: 1,053

Re: 2d game physics/programming quesiton... C++/SDL

And here's another (forum-related) trick: Try putting [ code ] and [ /code ] tags around your code, makes it more readable. smile

Offline

#6 2009-06-02 14:46:12

AdrenalineJunky
Member
Registered: 2009-05-03
Posts: 149

Re: 2d game physics/programming quesiton... C++/SDL

thanks for the tip ^^

ok, so i have another question...

i put in a simple method of checking if the character is moving vertically and disable jumping if he is ( i should probably make it check if he is touching the ground... but i'll get there )

and that works - if the character is in the air and i press the jump key he doesn't jump again. problem is if i hold the jump key in the first place he will continue going up till he reaches the top of the level.

basically i have this:

    

if( yForce != 0 ) { yAcc = yForce / HERO_MASS; } 
if( yForce == 0 ) { yAcc = 3;  } // for gravity
if( yVel >= 10 ){ yVel = 10; } 
if( yVel <= -10 ){yVel = -10;}
yVel =  yVel + yAcc;


oldbox = newbox;
//Move the dot up or down
newbox = box.y = box.y + yVel;
if (newbox == oldbox) {jumping = false;}
else { jumping = true; }

switch( event.key.keysym.sym )
{
    case SDLK_UP: if( jumping == false ) { yForce -= 20; break; }
    if( jumping == true ){ yForce = yForce + 3; break;} // supposed to slow down and stop vertical movement. but doesn't work.
}

Last edited by AdrenalineJunky (2009-06-02 14:46:37)

Offline

#7 2009-06-02 15:48:37

AdrenalineJunky
Member
Registered: 2009-05-03
Posts: 149

Re: 2d game physics/programming quesiton... C++/SDL

nevermind, got it figured out

Offline

#8 2009-06-03 09:21:33

Themaister
Member
From: Trondheim, Norway
Registered: 2008-07-21
Posts: 652
Website

Re: 2d game physics/programming quesiton... C++/SDL

Heyhey smile Another beginner SDL/C++ coder. Me and a friend have recently started on an SDL/C++ game. A 2D-platformer too tongue I'm a beginner when it comes to SDL/C++ though, but we have a googlecode page up at least.
http://code.google.com/p/the-escapist/
Still in consept phase though. The code is merely just some basic physics stuff (and being able to have a scrolling background (marioish)).

Next step would probably be adding collition detection with arbitary platforms around the map.

Last edited by Themaister (2009-06-03 09:38:26)

Offline

Board footer

Powered by FluxBB