Moving into a maze..

Anything related to SDL

Moving into a maze..

Postby Nimerion » Sat May 12, 2012 11:31 am

Hi again.
I have another stupid question..
I want to make a moving object inside of a maze, using Tims build. (from the tuts)
I figured out that if I want to find a way out of a maze, I should follow one of the walls (left or right), then every time when I reach a wall in front of me I will just check the next direction (if right is a wall, I would go down etc).

I know that in function OnMove, we check for collision at every new position.
I'm talking about this:

Code: Select all
if(PosValid((int)(X + NewX), (int)(Y))){
            X += NewX;
         }
         else
         {
            SpeedX = 0;
         }

         if(PosValid((int)(X), (int)(Y + NewY)))
         {
            Y += NewY;
         }
         else
         {
            SpeedY = 0;
         }


I change it like this:
Code: Select all
if( PosValid( (int)(X + NewX), (int)(Y) ) ){
                X += NewX;
         }
         else{
                SpeedX = 0;
                if( PosValid( (int)(X), (int)(Y + NewY) ) ){
                    Y += NewY;
                }
                else{
                    SpeedY = 0;
                    if( PosValid( (int)(X + NewX), (int)(Y) ) ){
                       X -= NewX;
                    }
                    else{
                        SpeedX = 0;

                        if( PosValid( (int)(X), (int)(Y + NewY) ) ){
                            Y -= NewY;
                        }
                        else{
                            SpeedY = 0;
                        }
                    }
                }
         }

         if( PosValid( (int)(X), (int)(Y-NewY) ) ){
                Y += NewY;
         }
         else{
                SpeedY = 0;
                if( PosValid( (int)(X-NewY), (int)(Y) ) ){
                    X -= NewX;
                }
                else{
                    SpeedX = 0;
                    if( PosValid( (int)(X), (int)(Y + NewY) ) ){
                       Y -= NewY;
                    }
                    else{
                        SpeedY = 0;

                        if( PosValid( (int)(X+NewX), (int)(Y) ) ){
                            X -= NewX;
                        }
                        else{
                            SpeedX = 0;
                        }
                    }
                }
         }


Then in the CEntity::OnLoop I move right by 1:
Code: Select all
OnMove(1, 0);


When I run the program, the player walks to the right side, until he collides with a wall.
After that nothing happens.
I can't figure out, how can I change the direction after the object collides with a wall..
Nimerion
 
Posts: 22
Joined: Sat Dec 03, 2011 5:55 pm

Re: Moving into a maze..

Postby marvin » Mon Jun 04, 2012 8:09 pm

what i notice is this in several places:
Code: Select all
if( PosValid( (int)(X + NewX), (int)(Y) ) ){
                       X -= NewX;

here you check if (X + newX) is valid, and then do (X - NewX)
you need to change those checks to check for position you want to go to, in this example:
Code: Select all
if( PosValid( (int)(X - NewX), (int)(Y) ) ){
                       X -= NewX;


Not sure that will solve your problem, but this is certainly wrong in your code.
42
User avatar
marvin
 
Posts: 49
Joined: Mon Oct 24, 2011 10:25 am

Re: Moving into a maze..

Postby Nimerion » Mon Jun 25, 2012 7:57 am

Thank you marvin, I will check my code after I get back from work.. :)
Nimerion
 
Posts: 22
Joined: Sat Dec 03, 2011 5:55 pm


Return to SDL

Who is online

Users browsing this forum: Google [Bot] and 0 guests

cron