Sign in to follow this  
jaymystro

Translation and Collision problem

Recommended Posts

Hi, The problem with my very basic colision system is that when the glTranslate matrix is used, the vertex points dont seem to be translating to their new locations. I tested this by assigning a variable to one of the verticies, and then incrementing the vertex, when it reached a certain point on the screen (representing a pixel location), I triggered a response. This method works but is stupidly slow. I need help gettting this to work. Below is some of my code which is supposed to create a sort of bounding box around one of the object. I have set up my collisions like so: code: //Object 1 coordinates x1 = 0; //Top left x y1 = 100; //Top left y x2 = 100; //Bottom right x y2 = 0; //Bottom right y //Object 2 coordinates dx1 = 0; //same layout as above.. dy1 = 300; dx2 = 100; dy2 = 200; //Object 1 glBegin(GL_QUADS); glVertex2i(0,0); glVertex2i(x2,y2); glVertex2i(100,100); glVertex2i(x1,y1); glEnd(); //Object 2 glTranslatef(0.0f, Translate, 0.0f); glBegin(GL_QUADS); glVertex2i(0,200); glVertex2i(dx2,dy2); glVertex2i(100,300); glVertex2i(dx1,dy1); glEnd(); Now, this draws two squares with the top left and bottom right corners set as variables. When I press a key I increment the 'Translate' variable which moves my square down towards the other one. My question is, how come when I begin translating, the coordinate variables dont change but the object moves. My collision test is very simple: code: if ((dx1 < x2) && (dy1 < y2)) //supposed to be a square region Collided! It works if i set up each point in each square with a variable, then increment each one with a keypress. But this means 18 variables and 18 input parameters in the function, which is wasteful. I know its been posted before but ive not seen any actual code posted. Please can some one clarify for me. Thanks a lot

Share this post


Link to post
Share on other sites
Not entirely clear what the problem is, but I'll just point out that glTranslate*() has no effect on any of the variables in your program; it just tells OpenGL where to draw the geometry. For collision detection, you have to track and update the geometry yourself.

You mentioned concerns about speed and/or memory, but unless your resources are extremely limited, these are not issues you need to worry about. Just get it working, and if at some point you run into performance problems you can look into improving the algorithm.

Share this post


Link to post
Share on other sites
Right. glTranslate is ONLY for drawing. Collision detection is your problem.

I also noticed that you are not drawing you box "right". you should have something more like this:

// set Object 1 coordinates
float x1 = 0; // left
float y1 = 100; // top
float x2 = 100; // right
float y2 = 0; // bottom

// create some offset variables ("d" stands for "Delta" or "change in")
float dx = 0;
float dy = 0;

// draw the object with this code:
glBegin(GL_QUADS);
glVertex2f( x1 + dx, y1 + dy ); // top left
glVertex2f( x2 + dx, y1 + dy ); // top right
glVertex2f( x2 + dx, y2 + dy ); // bottom right
glVertex2f( x1 + dx, y2 + dy ); // bottom left
glEnd();

So every frame, you want to update to the dx and dy variables. Those two variables describe your x/y "movement". You want to keep your x1,x2,y1,y2 variables always the same. Those variables describe the geomtry of your object. If your geometry doesn't change (if it's always a square), those variables don't need to change. Only change the offsets to describe the location / movement.

Share this post


Link to post
Share on other sites
ok thanks for your replies, i understand that glTranslate does not affect the actualy vertex data. I have tried what you said using dx, and it seems to work pretty good.

However, I am having the poerformance problems when I come to load my textures for the background. I am loading TGA images and texturing them to quads. Do you have any ideas on how to avoid this. I have watched the task manager while my program is running and the memory just keeps decreasing. Im pretty stuck on this, im not sure where to call destructors or delete texture objects.

Thanks,

Jay

Share this post


Link to post
Share on other sites
I do not know what you are using to actually load the images, but images only need to be loaded once, of course. If you are using a temporary image surface, such as when you use SDL, you need to delete that after you create the texture.

After you create the openGL texture, it needs no further attention. When you are done using the texture and have no further need for it, you can make a call to glDeleteTextures().

Also remember that if you use multiple objects that use the same image, you only want to create one texture and have all the objects use it. You do NOT want to load the same texture many times, one for each object.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this