[SOLVED] SDL LazyFoo Moving an Image Help

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

User avatar
xx6heartless6xx
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 80
Joined: Wed Feb 02, 2011 9:42 pm

Re: SDL LazyFoo Moving an Image Help

Post by xx6heartless6xx »

D-e-X wrote:I would give the class a bool collision; in the move function you should do this instead: collision = check_collision(blah...);
then in the show function do this: if(collision) apply_surface(bleh...);
When I do this I see the image popping on the screen as I want to, but it only stays for a split second. How can I keep this image on the screen?
krilik
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 25
Joined: Sat Apr 18, 2009 11:24 pm
Favorite Gaming Platforms: SNES,PS1

Re: SDL LazyFoo Moving an Image Help

Post by krilik »

Its only appearing for a split second because the collision returns back to false after you have a collision.
if( ( box.x < 0 ) || ( box.x + MOVER_WIDTH > SCREEN_WIDTH ) || ( check_collision( box, wall ) ) )
{
//Move back
box.x -= xVel;
}

//If the Mover went too far up or down or has collided with the wall
if( ( box.y < 0 ) || ( box.y + MOVER_HEIGHT > SCREEN_HEIGHT ) || ( check_collision( box, wall ) ) )
{
//Move back
box.y -= yVel;
}
}
The two bold underlined lines are always going to make your box "bounce" off whatever it is colliding with, making the collision become false again. Which is why you are only seeing the image for a split second and then it disappears. You are going about it the right way, but because you are checking for a collision each time you move the statement is no longer true once you do have a collision because the box "bounces" off the wall. Honestly, that collision code isn't that optimal for returning whether or not a collision is happening because it causes that "bounce" effect.

Doing it this way will draw your image as long as you are having a "collision". i.e you need to be moving the box into the wall.

Code: Select all

void Mover::move()
{
    //Move the Mover left or right
    box.x += xVel;

    //If the Mover went too far to the left or right or has collided with the wall
    if( ( box.x < 0 ) || ( box.x + MOVER_WIDTH > SCREEN_WIDTH ) || ( check_collision( box, wall ) ) )
    {
        //Move back
        box.x -= xVel;
        apply_surface( 200, 300, dot, screen );
    }

    //Move the Mover up or down
    box.y += yVel;

    //If the Mover went too far up or down or has collided with the wall
    if( ( box.y < 0 ) || ( box.y + MOVER_HEIGHT > SCREEN_HEIGHT ) || ( check_collision( box, wall ) ) )
    {
        //Move back
       box.y -= yVel;
       apply_surface( 200, 300, dot, screen );
    }
}
Alternatively you could store the collision in a bool flag

Code: Select all

//somewhere in your main.cpp
bool collision = false;

Code: Select all

void Mover::move()
{
    //Move the Mover left or right
    box.x += xVel;

    //If the Mover went too far to the left or right or has collided with the wall
    if( ( box.x < 0 ) || ( box.x + MOVER_WIDTH > SCREEN_WIDTH ) || ( check_collision( box, wall ) ) )
    {
        //Move back
        box.x -= xVel;
        collision = true;
    }

    //Move the Mover up or down
    box.y += yVel;

    //If the Mover went too far up or down or has collided with the wall
    if( ( box.y < 0 ) || ( box.y + MOVER_HEIGHT > SCREEN_HEIGHT ) || ( check_collision( box, wall ) ) )
    {
        //Move back
       box.y -= yVel;
       collision = true;
    }
}

Code: Select all

//somewhere in your main loop
if (collision) 
{ 
     apply_surface( 200, 300, dot, screen ); 
}
Post Reply