Code that should work.

Started by
8 comments, last by TheUmpteenth 16 years, 6 months ago
Can someone tell me why this code doesn't do what i must do? Supposdly, when i press "a" the rectangle should go further, but it just desappears.


typedef float dpi_vertex[3];
typedef float dpi_matrix44[16];

void cDPI_Frame::getMatrix44(dpi_matrix44 m)
{
	dpi_matrix44 x;
	dpi_crossProduct(x, front, up);
	
	m[0]=x[0];
	m[1]=x[1];
	m[2]=x[2];
	m[3]=0;
	
	m[4]=up[0];
	m[5]=up[1];
	m[6]=up[2];
	m[7]=0;
	
	m[8]=front[0];
	m[9]=front[1];
	m[10]=front[2];
	m[11]=0;
	
	m[12]=origin[0];
	m[13]=origin[1];
	m[14]=origin[2];
	m[15]=1;
	
}

void cDPI_Frame::getOrigin(dpi_vertex v)
{
	v[0]=origin[0];
	v[1]=origin[1];
	v[2]=origin[2];
}

void cDPI_Frame::MoveFoward(float p)
{
	origin[0]+=front[0]*p;
	origin[1]+=front[1]*p;
	origin[2]+=front[2]*p;
}

void dpi_crossProduct(dpi_vertex result, dpi_vertex vectora, dpi_vertex vectorb)
{
	result[0]=(vectora[1]*vectorb[2])-(vectorb[1]*vectora[2]);
	result[1]=-(vectora[0]*vectorb[2])+(vectorb[0]*vectora[2]);
	result[2]=(vectora[0]*vectorb[1])-(vectorb[0]*vectora[1]);
}

cDPI_Frame camera;
dpi_matrix44 temp;
dpi_vertex v;

int result=0;
result=SDL_Init(SDL_INIT_VIDEO);
if(result!=0)
	return false;
result=(int)SDL_SetVideoMode(width, height, 32, SDL_OPENGL);
if(result!=0)
	return false;

glViewport(0, 0, 640, 480);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();

glOrtho(-100, 100, -100, 100, -100, 100);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

for(;;)
	{
		glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
		input->update();
		
		if(input->isKeyPressed(SDLK_ESCAPE))
			break;
		
		
		if(input->isKeyPressed(SDLK_a))			
		{
			printf("origin=(%f, %f, %f)\n", v[0], v[1], v[2]);
			camera.MoveFoward(0.01);
		}
		
		camera.getMatrix44(temp);
		camera.getOrigin(v);
		glPushMatrix();
		glMultMatrixf(temp);
		glTranslatef(-v[0], -v[1], -v[2]);
		
		
		glBegin(GL_TRIANGLES);
		glColor3f(1.0, 0.0, 0.0);
		glVertex3f(-0.5, 0.5, 0.0);
		glVertex3f(-0.5, -0.5, 0.0);
		glVertex3f(0.5, -0.5, 0.0);
		glVertex3f(0.5, 0.5, 0.0);
		glEnd();
		
		glPopMatrix();
		render->update();
	}	

There are only relevant pieces of code. Any doubt about it tell me. I have used gluLookat too and some other tricks but all of them do the same stuff, make the cube desappear instead of move foward.
Advertisement
I think that your issue is that you do not take into account the framerate your box is running at, the code

camera.MoveFoward(0.01);


moves your cube forward by 0.01 units per _frame_, but you do see the cube before you hit 'a' right? use the SDL call SDL_GetTicks() {it returns how much time in thousandth's of a second has passed since SDL was started up} to see how much time has passed between frames and use that value to scale your 0.01f.

Best Regards
Close this Gamedev account, I have outgrown Gamedev.
Well, the problem is that i have used:

camera.MoveFoward(0.001);
and
camera.MoveFoward(0.0001);

both with the saame effect.
At that rate there should be something visible.

Also, after debugging, i saw that the square desappear when the counter reachers -1.00
Before that it stays.
Might not be helpful, but

You're moving your camera, not the box.

You're using glOrtho(), which I've only ever used for 2d and masking. I'd use gluPerspective(). Make sure your box isn't outside of the clipping planes.

If I'm thinking straight, the glOrtho() thing doesn't show perspective (objects don't get smaller the further they are from the camera). Think of it as a cuboid as opposed to a pyramid with the top cut off.

so that's possibly it.
Hmm... if I may ask, what are you initialising "front"'s values to?

Other than that, I would tend to agree with kRogue - it may simply be that your code is executing so fast that even using a step of 0.1*front or 0.0001*front is moving the object out of your viewing space too quickly.

I would second his suggestion to base the degree of forward movement on the time between frames - even if this is not your current problem, it should produce movement that better matches what I imagine you expect.

MWAHAHAHAHAHAHA!!!

My Twitter Account: @EbornIan

Sorry, i forgot the constructor:

cDPI_Frame::cDPI_Frame(){	origin[0]=0.0;	origin[1]=0.0;	origin[2]=0.0;		front[0]=0.0;	front[1]=0.0;	front[2]=-1.0;		up[0]=0.0;	up[1]=1.0;	up[2]=0.0;		}


By the way, i want to say that the FPS i not an issue. As you can see there is a printf just above the MoveFoward member. And believe me, it doen't go THAT fast.

I can agree with the theory of the ortho-problem.
I will try moving to the sides instead and after i have what i want i may change it to perspective.
Hmm... You do make a good point about the frame rate.

However, I do notice something else:

You appear to be moving the object along the z-axis, if I'm not much mistaken, in which case, since you are viewing the scene orthographically, it seems to me now that TheUmpteenth makes a good point. I would expect, given this, that you should see the object sit still for a moment, and then disappear as it exits the viewing volume. Note that, as TheUmpteenth says, an orthographic projection should not show any perspective.

If it disappears immediately, try changing the third value of "front" to "1" - if it remains on-screen for a longer period and then disappears, then you would seem to be moving the object behind the camera on the first step (since it would seem that you have the camera and object placed at the same point in space).

If you want the object to move across the screen, then I believe that you should be moving along x or y (or both, of course), instead of z, as you appear to be doing now.

If you want the object to become smaller as it moves "into" the screen, then I would recommend either a perspective projection or simply scaling the object.

MWAHAHAHAHAHAHA!!!

My Twitter Account: @EbornIan

Well, actually what i wanted to make is to move with the arrows foward and backward, right and left, so i can "explore" the polygons.
Of course, this are just some tests before the real thing.
I'll try moving to other sides and changing to perspective.
thanks
Well, yep the problem was that, the orthographic projection. I tried moving to the sides and it worked.

Thanks for your help.
np :) glad you got it fixed

This topic is closed to new replies.

Advertisement