Click here to Skip to main content
12,999,081 members (48,909 online)
Rate this:
Please Sign up or sign in to vote.
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
void Object(float objX, float objY)

arrow keys handler(i just mention left arrow key)
void mySpecialKeyFn(int key, int mx, int my)
        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)

void display(void)

function in headerfile RGBA.h
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)
               return true;
               return false;
Posted 3-Oct-11 7:30am
Sweety Khan 3-Oct-11 13:31pm
if i wrote any thing wrong n unclear, plz ask me inspite of closing my question
Manfred R. 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...
johny10151981 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. ...
Sweety Khan 10-Oct-11 10:43am
actually i use binary tree algorthm to build maze. its the property of algo tht it does not store positions, every time we access cell, it is independent of others. it only has a 2D array whose elements represent walls n paths in maze, i hope im little clear. thts why i m using this approach for detecting floor.
GPUToaster™ 4-Oct-11 5:51am
Instead of using opengl inbuilt function glREctf, use your own geometry. Try to do ray tracing. Checking with pixel wont help you, and even if you get some result, wont be accurate and your program will become slower. After all its all math.

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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

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[^]

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.
Niklas Lindquist 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!
Sweety Khan 14-Oct-11 1:59am
its dOne. thanx for forcing me to do collision detection by storing positons :)
Stefan_Lang 14-Oct-11 4:27am
Grats, and you're welcome *swings whip for emphasis* ;-)
Sweety Khan 10-Oct-11 12:50pm
yeh checking pixels has waste my time and my marks :(
the way u r saying seems vry difficult to me but now i know there is no other way. i will try it

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web01 | 2.8.170622.1 | Last Updated 5 Oct 2011
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100