[OPENGL-Find the bug]Spawning objects

Started by
6 comments, last by 3Dgonewild 16 years, 10 months ago
Hi again!!! After alot of work , i've just finished my object_cache class! which creates and draws objects (some triangles!!) at runtime(yes, you guessed right , im trying to make a mini-level editor). The rendering works fine. The only problem that im having is with objects position! To make things easier , i'll post some code : Thats the function that adds the new object to our vector...

 void sOBJECT_CACHE::Add_object(GLfloat tx,GLfloat ty,GLfloat tz)
{
       TRIANGLE TEMP;//a normal 3d triangle..
 triangle_list.push_back(TEMP);//add the temp object
 triangle_list[TOTAL].repos(tx,ty,tz);//set the new position based on camera's 
                                    //position!

 TOTAL+=1;//increase the list..
}


Camera class:

#include "cCAMERA.h"
#include <math.h>
//WARNING!SOME CODE IS TAKEN FROM NEHE'S TUTORIALS!!
CAMERA::CAMERA()
{ 
heading=0.0f;
    walkbias = 0;
    walkbiasangle = 0;
    lookupdown = 0.0f;
    z=-20.0f;	 
    xpos=0.0f;
    zpos=0.0f;
    lookupdown=0.0f;
  
}
void CAMERA::CAM_DO_UPDATE()
{
    GLfloat xtrans = -xpos;
    GLfloat ztrans = -zpos;
    GLfloat ytrans = -walkbias - 0.25f;
    GLfloat sceneroty = 360.0f - yrot;
    glRotatef( lookupdown, 1.0f, 0.0f , 0.0f );
    glRotatef( sceneroty, 0.0f, 1.0f , 0.0f );
    glTranslatef( xtrans, ytrans, ztrans );
}

void CAMERA::CamBW()
{
	    xpos += ( float )sin( yrot * PI180 ) * 0.05f;
	    zpos += ( float )cos( yrot * PI180 ) * 0.05f;
	    if ( walkbiasangle <= 1.0f )
                walkbiasangle = 359.0f;
	    else
                walkbiasangle -= 10;
	    walkbias = ( float )sin( walkbiasangle * PI180 ) / 10.0f;
	   
}
void CAMERA::CamFW()

{
        xpos -= ( float )sin( yrot * PI180 ) * 0.05f;
	    zpos -= ( float )cos( yrot * PI180 ) * 0.05f;
	    if ( walkbiasangle >= 359.0f )
		walkbiasangle = 0.0f;
	    else
		walkbiasangle+= 10;
	    walkbias = ( float )sin( walkbiasangle * PI180 ) / 10.0f;
	    
 
 }
 void CAMERA::CamLeft()
{
   // yrot+=1.5f;
      	  heading += 1.5f/2;	
		 	yrot = heading;
 
}
 void CAMERA::CamRight()
{
      //yrot-=1.5f;
   	 heading -= 1.5f/2;	
	yrot = heading;
		 
}
void CAMERA::CamScrollRight()
{
xpos-= - (0.5f/10);
}
void CAMERA::CamScrollLeft()
{
xpos+= - (0.5f/10);

}
GLfloat CAMERA:: GET_X()
{
     return -xpos;
}
GLfloat CAMERA::  GET_Z()
{
     return -zpos;
}
GLfloat CAMERA::  GET_Y()
{
     return -0.0f;//....Any way to fix this  ? xD
}


...AND THE MOST IMPORTANT PART...

//thats how im doing it..
if( EVENTS.button.button == SDL_BUTTON_LEFT )
{
OBJECT_CACHE_CLASS.Add_object(c_CAMERA.GET_X(),c_CAMERA.GET_Y(),c_CAMERA.GET_Z());
}


Can someone tell me what im doing wrong/??????????????????????????????!!! edited Here's a picture: http://img443.imageshack.us/img443/4623/damnitse3.jpg
Advertisement
Not even one reply? :/!
All im asking is how to create the object at camera's position!
I have all the functions ready , but there must be something wrong with GET_X() ,GET_Y() and GET_Z() functions(camera class)....everything else works just fine..
I can't gather from the screenshot what's wrong. Is the object being created in front of the camera as opposed to exactly where it is?
Are you using gluLookAt() for the camera?

If so, remember that the camera's position (first 3 floats in gluLookAt()) is actally behind the view frustum.

you could use the veiw vector (second 3 floats in gluLookAt()) to move the spawning point forward of the cam (normalize the view vector then multiply the result by the forward distance, add it to the cameras position, that's your spawn pos). Or you could use something like your camera walk movement to pick your spawn point.


can you tell me where, in relation to the camera, or anything else, your objects are actually spawning?

Quote:Original post by TheUmpteenth
Are you using gluLookAt() for the camera?

If so, remember that the camera's position (first 3 floats in gluLookAt()) is actally behind the view frustum.

you could use the veiw vector (second 3 floats in gluLookAt()) to move the spawning point forward of the cam (normalize the view vector then multiply the result by the forward distance, add it to the cameras position, that's your spawn pos). Or you could use something like your camera walk movement to pick your spawn point.


can you tell me where, in relation to the camera, or anything else, your objects are actually spawning?


No , im not using GLUlookAT() , im using GLtranslatef & rotatef(check out the camera class...thats what im using..).

I've uploaded here:
http://www.gigasize.com/get.php/-1100118408/TEST.zip
A test demo 200kb only (sdl.dll included).
Please test it , and you'll understand why im so confused.


Controls:
LEFT CLICK : Create a new object
UP,DOWN & MOUSE for movement..
bump
Okay, I downloaded it and I really have no clue what it's doing. But looking at the code did give me one idea, though that couldn't possibly account for the weirdness simply by itself.

You are translating by (-xtrans, -ytrans, -ztrans), but your GET_X and GET_Z functions return -xtrans and -ztrans respectively. They should return xtrans and ztrans (no negatives). Why? Because a camera you have to move in the opposite direction of where it is. You aren't actually moving the 'camera' (there really is not camera), you're moving the rest of the world in the opposite direction. So the location of the camera is actually the negative of what you translate by, and hence just (xtrans, ztrans). (ignoring ytrans, since you seem to have disabled movement in the y axis, but the same would go for it)

Experimenting with the actual program shows that this does seem to be a problem (moving backwards makes them appear further forwards, moving to the side makes it move to the other side), but there are certainly some other issues going on.
Holy crap ! , it works!
I had to create a log manager to track all actions to locate the bug!

Thanks to whoever replied ;) !!!!

This topic is closed to new replies.

Advertisement