You are not logged in.
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
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
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!
Offline
^^^ 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
Offline
And here's another (forum-related) trick: Try putting [ code ] and [ /code ] tags around your code, makes it more readable.
Offline
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
nevermind, got it figured out
Offline
Heyhey Another beginner SDL/C++ coder. Me and a friend have recently started on an SDL/C++ game. A 2D-platformer too 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