Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i build a maze. it has walls of brown colour and floor is of white colour. player is a rectangular box controlled by arrows keys. on pressing arrow keys i want to first check tht the area my object will occupy is a floor or not, if it is a floor i allow it to move otherwise not.
i check this by
1. reading pixels of the next occupied area using glReadPixels
2. compare the readed pixels with white color, if it is white increment in the object direction otherwise not

the problem is this when i press left key, the object does not move even if it is on the floor

some of my global variables
float objectx, objecty, objectSize = 20;
RGBApixmap block(10,20);
bool isFloorColor = false;

objectx and objecty is initialzed in generateMaze()function which is in main

this is my object code
C#
void Object(float objX, float objY)
{
    glColor3f(64/255.0,0,64/255.0);
    glRectf(objX,objY,objX+objectSize,objY+objectSize);
}


arrow keys handler(i just mention left arrow key)
C#
void mySpecialKeyFn(int key, int mx, int my)
{
    switch(key)
    {

        case GLUT_KEY_LEFT:
            glReadPixels(objectx-10, objecty, block.nCols, block.nRows, GL_RGBA, GL_UNSIGNED_BYTE,block.getPixmap());
            isFloorColor = block.checkfloor();
            if(objectx-10>95 && isFloorColor==true)
                objectx-=10;
            glutPostRedisplay();
            break;
}



C#
void display(void)
{
....
           Object(objectx,objecty);
           glFlush();
}


function in headerfile RGBA.h
C#
bool checkfloor()
{
           int count = 0;
           for(int i =0 ; i<10; i++)
           {
               if(this->pixel[i].r ==(uchar)255.0 && this->pixel[i].g ==(uchar)255.0 && this->pixel[i].b ==(uchar)255.0)
                   count++;
           }
           if(count>=5)
               return true;
           else
               return false;
}
Posted
Comments
Sweety Khan 3-Oct-11 13:31pm    
if i wrote any thing wrong n unclear, plz ask me inspite of closing my question
Manfred Rudolf Bihy 3-Oct-11 13:52pm    
Repost! Deleting a question which was already answered is really bad style. I hope this will not be repeated by you in the future. I"ll monitor this question.
Sweety Khan 3-Oct-11 13:55pm    
no one answered it. n secondly i didnt delete it was closed by Chris Maunder.
enhzflep 4-Oct-11 3:15am    
To be fair - it was a rather moronic sounding post!

You asked how to do collision detection with the walls, while at the same time stating that you didn't know where the walls were! We're not mind-readers you know.

If you drew the maze, you know where all the walls are, simple as that. If you didn't draw the maze, then how on earth do you expect to be able to do collision detection with it?

An analogy would be drawing a string to the screen - if you've displayed the string in the first place, then of course you know what it says and where it's positioned. There's absolutely no need to perform Text Recognition on an image of the screen.

Now, if you've failed to maintain a variable with the position and another with the string, then that's an oversight on your part. However, you DO know what and where the text is already. The same thing applies for the maze walls...
Mohibur Rashid 3-Oct-11 23:42pm    
I am no graphics programmer or designer but I have a question,
Do you think it will be best idea to check by pixel??? why don't you create a object array that will also hold the position, by checking the position you can figure out where your object is. ...

1 solution

Change
C++
if(this->pixel[i].r ==(uchar)255.0 && this->pixel[i].g ==(uchar)255.0 && this->pixel[i].b ==(uchar)255.0)

to
C++
if(this->pixel[i].r ==255 && this->pixel[i].g ==255 && this->pixel[i].b ==255)

Your mistake:
You used a C-style typecast on constants of an incompatible type (float, in this case). Note that a cast does not convert the binary representation, it just reinterprets it - which in this case produces garbage.

Best practices to follow:
1. NEVER use C-style type casts in a C++ program. Most of the time a cast is just a sign of using the wrong types - change your variable types accordingly!

2. Even if you do really need a cast, use the C++ casts such as dynamic_cast or reinterpret_cast. And use them appropriately: see http://www.cplusplus.com/doc/tutorial/typecasting/[^]

Besides, it is inefficient and error prone to check your output and interpret it back into the model, when instead you could just check the model you used to produce that output.
 
Share this answer
 
Comments
Niklas L 6-Oct-11 7:30am    
To OP: In addition, having the information in the model enables you to change your graphic background to something more interesting, like an image or similar, not having to rely on specific color information.
Sweety Khan 10-Oct-11 10:34am    
sorry i didnt understand
Stefan_Lang 10-Oct-11 11:12am    
The point Niklas was trying to make is that you should not look at the image, you should at the data instead that was used to generate it! Or more to the point, the positions of the walls. That way you are free to paint your walls over a random image (the background image) and do not need to care what color each pixel has. E. g. you could have an image that looks like some rough, rocky floor, or an earthen floor with roots springing up and patches of grass and moss everywhere, and then you paint your walls on top of that.
Sweety Khan 10-Oct-11 10:34am    
oki. one of my mistake is objects x n y are left bottom pt and glread pixels need top left so i corrected it but still the same problem. to observe whts going on i print pixels which are read by glreadpixels and found tht it is reading too top from the point i gave. so i decrement y as required. it works fine in the first line of maze n detect floor well but below tht line it again reads from some where else :(
Stefan_Lang 10-Oct-11 11:07am    
Have you fixed the error I pointed out above? An uchar is NOT a float, and you can NOT cast from float to uchar and expect meaningful values! 255.0 is a float! 255 is not.

But anyway, forget about checking pixels. It is complicated, inefficient and error prone, and will cause you more headaches than it's worth. You mentioned a function generateMaze() in your posting, so you do have the information where the walls are being drawn right in your program! When generateMaze() (randomly?) plazes the walls, store these positions! Then you can use that information to do your collision detection. You just have to iterate over all walls and see if they intersect with your object. Easy, and doesn't require any OpenGL calls at all!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900