Hey, thanks alot for your suggestions. Everything that you said to me made sense, and I have optimized my code. It works just as it did before. I felt a little silly when you had pointed out my errors with the stack (that is supposed to make sense to a guy that understands ASM). Also, I do have a basic understanding of the pipe line, and I see what you were talking about with sending unneccesary code through it (which would bog things down when I got more elaborate with things later on). And last but not least, I understand the HTML tags.
Thanks very much
JC
[edited by - LostBoy on October 2, 2002 1:27:47 AM]
Please Help - Simple collision detection problem
look.....I know what everyone is saying and it is ALL right. But here is the deal. If you have one color for the ball and one color for the wall...all you need to do is to write this as a global variables
GLfloat wallcolor[3]={1.0f,1.0f,1.0f} //white as test color
GLfloat ballX;
GLfloat ballY;
GLfloat pixels[3];
GLint viewport[4];
then in the main drawing function do this:
glGetIntegerv(GL_VIEWPORT,viewport);
glReadPixels(ballX,ballY,1,1,GL_RGB, GL_FLOAT, (void *)pixels);
if(pixels[0]==wallcolor[0] && pixels[1]==wallcolor[1] && pixels
[2]==wallcolor[2])
{
WALL COLLISION HAPPENED!!!!!!!!!!!!!!!!!
}
else
KEEP ROLLING!!!!!!!!!!!!!!!!!!!!
this just gets tricky if you have different colored golf-balls or different colored walls(on each hole).Email me if you have any questions.
GLfloat wallcolor[3]={1.0f,1.0f,1.0f} //white as test color
GLfloat ballX;
GLfloat ballY;
GLfloat pixels[3];
GLint viewport[4];
then in the main drawing function do this:
glGetIntegerv(GL_VIEWPORT,viewport);
glReadPixels(ballX,ballY,1,1,GL_RGB, GL_FLOAT, (void *)pixels);
if(pixels[0]==wallcolor[0] && pixels[1]==wallcolor[1] && pixels
[2]==wallcolor[2])
{
WALL COLLISION HAPPENED!!!!!!!!!!!!!!!!!
}
else
KEEP ROLLING!!!!!!!!!!!!!!!!!!!!
this just gets tricky if you have different colored golf-balls or different colored walls(on each hole).Email me if you have any questions.
Wow! Thanks very much for that code ! That was my original idea (to use the colors for colision detection), I just didn''t know how to code it. It seems to be the most simple approach for what I want to do.
Thanks very much
JC
Thanks very much
JC
I''m still having problems. Here is my code :
First my global variables :
Now the drawing routine :
My red ball just keeps on going through the white diagonal line towards the top of my course. What I''m trying to do here is make decay = 0 if they touch so that ballY + decay = 0 and my ball will stop. This is just to test functionality. If I can get the damn thing to at least respond I can get on with it. This is the only bad obstacle that I have run into. With help from others on the forum, my previous problems were easily solved.
I might also add that I am getting 2 warnings when I compile since I have added the collision detection. My variable initialization looks OK. I am thinking that it is in the drawing routine.
Thanks everyone
JC
First my global variables :
GLfloat ballX=-.508f; // Initialize ball coordinate horizontalGLfloat ballY=-.542f; // Initialize ball coordinate verticalGLfloat decay=.005f;GLfloat delay=1000.0f;GLuint texture[1]; // Storage For One TextureGLUquadricObj *quadratic;// Storage For Our Quadratic Objects GLfloat wallcolor[3]={1.0f,1.0f,1.0f}; // Line colorGLfloat pixels[3]; GLint viewport[4];
Now the drawing routine :
int DrawGLScene(GLvoid) // Here''s Where We Do All The Drawing{ // Clear Screen And Depth Buffer glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glLoadIdentity();// Reset The Current Modelview Matrix glTranslatef(0.0f,0.0f,-2.0f); // Move Into The Screen 2 glEnable(GL_TEXTURE_2D);// Enable Texture Mapping glBindTexture(GL_TEXTURE_2D, texture[0]);// Binds our //BMP to next object glBegin(GL_QUADS);// Draw A Quad (our corse) //Texture and quad coordinates // Top Left glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 0.0f); // Top Right glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 0.0f); // Bottom Right glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f,-1.0f, 0.0f); // Bottom Left glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f,-1.0f, 0.0f); glEnd();// Done Drawing Quad glDisable(GL_TEXTURE_2D); glBegin(GL_LINE_STRIP); glColor3f(1.0f,1.0f,1.0f); glVertex3f( -0.334f,-0.612f,.0f);//corner 1 glVertex3f( -0.681f,-0.612f,.0f);//corner 2 glVertex3f(-0.681f,.415f,.0f);//corner 3 glVertex3f( -0.332f,0.765f,.0f);//corner 4 glVertex3f( 0.29f,0.765f,.0f);//corner 5 glEnd(); GLfloat radius=0.02f; glTranslatef(ballX,ballY,0.0f);// Move to ball position glColor3f(1.0f,0.0f,0.0f); // Load red for our ball gluSphere(quadratic,radius,32,32);//Draw a spher(ourball) glColor3f(1.0f,1.0f,1.0f); // Reset color delay=delay-5.0f; if (delay<=0) { delay=0;//This is temporary, just to set ball in motion ballY=ballY+decay;// increment ytravel decay=decay-.00001f;// Decrement decay } glGetIntegerv(GL_VIEWPORT,viewport); glReadPixels(ballX,ballY,1,1,GL_RGB, GL_FLOAT,(void *) pixels); if(pixels[0]==wallcolor[0] && pixels[1]==wallcolor[1] && pixels[2]==wallcolor[2]) decay=0; if (decay<=0)// Eventually stop/prevent going backwards decay=0; return TRUE;// Keep Going}
My red ball just keeps on going through the white diagonal line towards the top of my course. What I''m trying to do here is make decay = 0 if they touch so that ballY + decay = 0 and my ball will stop. This is just to test functionality. If I can get the damn thing to at least respond I can get on with it. This is the only bad obstacle that I have run into. With help from others on the forum, my previous problems were easily solved.
I might also add that I am getting 2 warnings when I compile since I have added the collision detection. My variable initialization looks OK. I am thinking that it is in the drawing routine.
Thanks everyone
JC
If you encounter problems hitting the wall, you could try to enlarge the wall line (because the line is too thin). Call glLineWidth(4.0f) for instance.
And for you compilation warnings, I guess the best thing you could do is posting them here.
And for you compilation warnings, I guess the best thing you could do is posting them here.
Thanks for the advice, but it still doesn't work. This is rough territory for an OpenGl beginner.
Here are the warnings :
c:\program files\microsoft visual studio\myprojects\golf\golf.cpp(199) : warning C4244: 'argument' : conversion from 'float' to 'int', possible loss of data
c:\program files\microsoft visual studio\myprojects\golf\golf.cpp(199) : warning C4244: 'argument' : conversion from 'float' to 'int', possible loss of data
[edited by - LostBoy on October 2, 2002 4:07:28 PM]
Here are the warnings :
c:\program files\microsoft visual studio\myprojects\golf\golf.cpp(199) : warning C4244: 'argument' : conversion from 'float' to 'int', possible loss of data
c:\program files\microsoft visual studio\myprojects\golf\golf.cpp(199) : warning C4244: 'argument' : conversion from 'float' to 'int', possible loss of data
[edited by - LostBoy on October 2, 2002 4:07:28 PM]
Are you sure that your ballX and ballY values correspond to SCREEN ccordinates ?
and why do you write :
Does it mean that it is the only border where there is the problem, and there's no problems for other walls ?
Your warning are simply telling that converting from float to int may lose decimals if your floating value is too precise, and if your floating value is too high you won't be able to convert it in a decent way. Generally you can skip the warning by casting explicitally your floating expression.
For instance, if the warning appears in such line :
my_integer = my_float_value;
Replace it with :
my_integer = (int)my_float_value;
And that should do the trick for most compilers (if not all)
[edited by - vincoof on October 2, 2002 4:29:07 PM]
and why do you write :
quote:
My red ball just keeps on going through the white diagonal line towards the top of my course.
Does it mean that it is the only border where there is the problem, and there's no problems for other walls ?
Your warning are simply telling that converting from float to int may lose decimals if your floating value is too precise, and if your floating value is too high you won't be able to convert it in a decent way. Generally you can skip the warning by casting explicitally your floating expression.
For instance, if the warning appears in such line :
my_integer = my_float_value;
Replace it with :
my_integer = (int)my_float_value;
And that should do the trick for most compilers (if not all)
[edited by - vincoof on October 2, 2002 4:29:07 PM]
Hey man, I know about 10 problems with the code I gave you.It will work ,but it will miss on certain things the code needs to be changed a little.
First off---the code will miss if you resize the scene.You need to add in a variable for the screen width and height.
Second off---the color collision is a very tricky way to do things.It will work and probably will be the best approach for you, but you are going to need to test which way the ball is moving and modify the glReadPixels to test the side of the ball closest to the wall.You are also going to need to test the ball as a ball(test the diameter/circumcerence) and not as a QUAD for accuracy.
Id like to take a look at the code and help you more...but going back and forth in the forums gets kinda tricky.
First off---the code will miss if you resize the scene.You need to add in a variable for the screen width and height.
Second off---the color collision is a very tricky way to do things.It will work and probably will be the best approach for you, but you are going to need to test which way the ball is moving and modify the glReadPixels to test the side of the ball closest to the wall.You are also going to need to test the ball as a ball(test the diameter/circumcerence) and not as a QUAD for accuracy.
Id like to take a look at the code and help you more...but going back and forth in the forums gets kinda tricky.
For the love of God, please help me man ! (laughs, but only half joking). This is driving me nuts !
1. I''m not sure as to what was vincoof talking about.-
(my_integer = (int)my_float_value
2. How do I make my ballX and ballY correspond to screen
coordinates ?
3. How can I make each pair of vertices in my line drawing equal
one object ? In other words, how can I make a given line
equal to a single variable ? If I could do this, then I could
test to see if ballX,ballY is touching this line, or has
passed this line.
4. Please explain things in a way that a beginner can
understand. I am not familiar with the advanced math being
thrown around in some of the posts. This is my first opengl
project, and I was trying to avoid complex measures. I will
get to the harry math soon enough, and don''t really want to
climb that mountain yet. Not to mention I am still learning
C++ as well. These are all reasons that I chose such a simple
project. (laughs - at least I thought it was simple).
1. I''m not sure as to what was vincoof talking about.-
(my_integer = (int)my_float_value

2. How do I make my ballX and ballY correspond to screen
coordinates ?
3. How can I make each pair of vertices in my line drawing equal
one object ? In other words, how can I make a given line
equal to a single variable ? If I could do this, then I could
test to see if ballX,ballY is touching this line, or has
passed this line.
4. Please explain things in a way that a beginner can
understand. I am not familiar with the advanced math being
thrown around in some of the posts. This is my first opengl
project, and I was trying to avoid complex measures. I will
get to the harry math soon enough, and don''t really want to
climb that mountain yet. Not to mention I am still learning
C++ as well. These are all reasons that I chose such a simple
project. (laughs - at least I thought it was simple).
1. In C++, if you write :
there will be a warning because converting a float to an int may lose some precision. In that example, ''my_int'' will receive the value ''1'', not ''1.5'' thus losing the decimals. You can''t store the value ''1.5'' in an integer.
But if your code is something like that :
The warning is still here but you can ignore it since you do not lose precision. It''s not a problem to store ''18'' in an integer.
2. What I mean is that glReadPixels does use screen coordinates. If you don''t really know how to project the coordinates, I''d recommend not to use glReadPixels. Though if you really want to convert from world coordinates to screen coordinates, you may take a look at gluProject and gluUnproject functions.
3. I''m sorry but I don''t understand your question. I''m afraid you should rephrase it or argue with schemes please.
4. In fact the math behind the algorithms described here are very simple. If it doesn''t seem simple, that just mean we''re not very good teachers. Really, it''s very rare to have an algorithm easier than line collision detection in 2D !
float my_float=1.5f;int my_int;my_int = my_float;
there will be a warning because converting a float to an int may lose some precision. In that example, ''my_int'' will receive the value ''1'', not ''1.5'' thus losing the decimals. You can''t store the value ''1.5'' in an integer.
But if your code is something like that :
float my_float=18.f;int my_int;my_int = my_float;
The warning is still here but you can ignore it since you do not lose precision. It''s not a problem to store ''18'' in an integer.
2. What I mean is that glReadPixels does use screen coordinates. If you don''t really know how to project the coordinates, I''d recommend not to use glReadPixels. Though if you really want to convert from world coordinates to screen coordinates, you may take a look at gluProject and gluUnproject functions.
3. I''m sorry but I don''t understand your question. I''m afraid you should rephrase it or argue with schemes please.
4. In fact the math behind the algorithms described here are very simple. If it doesn''t seem simple, that just mean we''re not very good teachers. Really, it''s very rare to have an algorithm easier than line collision detection in 2D !
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement