Page 1 of 1

Crafter map problem

Posted: Thu Apr 07, 2011 4:18 pm
by N64vSNES
Okay I'm going to go insane with this :evil:

You've probably seen my newest side project Crafter (a minecraft remake) and it's my first real attempt at anything related to the 3D world (or at least from complete scratch).

I'm not sure if I was right to do this but I decided the best way to store the map would be similar to 2D tile games, so in a 3D array.

My problem is hard to explain so I'll try and explain it as I go along.
Here is how the map gets rendered:

Code: Select all

void Crafter::Map::Render() {
	void Crafter::Map::Render() {
	//glPolygonMode(GL_FRONT_AND_BACK,GL_LINE);
	glEnable(GL_TEXTURE_2D);
	glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
	glTexEnvf(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_DECAL);
	glBindTexture(GL_TEXTURE_2D,t_BlockSet.tex);
	crop = t_BlockSet.Crop;
	width = t_BlockSet.fWidth;
	height = t_BlockSet.fHeight;
	crop.w = crop.h = 16;

	 float camX = GetCamera()->GetX();
	 float camZ = GetCamera()->GetZ();
	 float camY = GetCamera()->GetY();

	printf("x- %f\ny- %f\nz- %f\n\n",camX,camY,camZ);

	i_x = 0, i_z = 0;

	float bs = 4.0f; // size of cubes

	glPushMatrix();
	glColor3f(1,1,1);
	glBegin(GL_QUADS);
	for ( y = 0;y < 64;y++ ) {
		if ( y < 0 ) {
			y = 0;
		}
		for (  x = camX - 12;x < camX + 12;x += 4.0f ) {
			for (  z = camZ - 12;z < camZ + 12;z += 4.0 ) {
					i_z = (int)z;
					i_x = (int)x;
					if (i_x < 0 ) {
						i_x -= i_x*2;
					}
					if (i_z < 0 ) {
						i_z -= i_z*2;
					}
					int id = Blocks[i_x][i_z][y]; // get the id of the block
					if ( id == 0 ) {
						continue; // the is no block here
					}

					crop.x = (float)(id % 8 * 16) - 16; // clipping on the sheet (-16 to back up a single tile)
					crop.y = (float)(id / 8 * 16);

                        /******************
                         Vertex crap
                        *******************/
						}
					}

			}
		}
	}
	glEnd();
	glPopMatrix();
}
But the important parts, or at least he parts I'm having problems with is here:

Code: Select all

 float camX = GetCamera()->GetX();
	 float camZ = GetCamera()->GetZ();
	 float camY = GetCamera()->GetY();

	printf("x- %f\ny- %f\nz- %f\n\n",camX,camY,camZ);

	i_x = 0, i_z = 0;

	float bs = 4.0f; // size of cubes

	glPushMatrix();
	glColor3f(1,1,1);
	glBegin(GL_QUADS);
	for ( y = 0;y < 64;y++ ) {
		if ( y < 0 ) {
			y = 0;
		}
		for (  x = camX - 12;x < camX + 12;x += 4.0f ) {
			for (  z = camZ - 12;z < camZ + 12;z += 4.0 ) {
i_x and i_z are just for the index of the array...I think that could be to do with the problem.

My brain is literally gone to sleep with this so here's a video trying to explain it:


I've probably done a really bad job of explaining this, so any questions please ask.

Thanks.

EDIT:
So if I do like

Code: Select all

for ( x = 0;x < camX + 20; x += 4.0 )
That's fine
But if I do:

Code: Select all

for ( x = camX - 20; x < camX + 20;x += 4.0 )
It twitches around weirdly. :|

Re: Crafter map problem

Posted: Thu Apr 07, 2011 4:32 pm
by eatcomics
I saw no explaining what so ever....

Re: Crafter map problem

Posted: Thu Apr 07, 2011 4:45 pm
by xiphirx
Why is this here?

Code: Select all

for ( y = 0;y < 64;y++ ) {
      if ( y < 0 ) { <----
         y = 0; <----
      } <----
...

Re: Crafter map problem

Posted: Thu Apr 07, 2011 5:17 pm
by N64vSNES
xiphirx wrote:Why is this here?

Code: Select all

for ( y = 0;y < 64;y++ ) {
      if ( y < 0 ) { <----
         y = 0; <----
      } <----
...
Oh the y is for the the y axis obviously and the if statement was because I was doing it like this before:

Code: Select all

for ( y = camY - 20;y < camY + 20;y++ ) {
      if ( y < 0 ) { <----
         y = 0; <----
      } <----
It's pretty untidy at the minute :lol:

Re: Crafter map problem

Posted: Fri Apr 08, 2011 1:03 pm
by krilik
I'm pretty sure the problem has to do with casting that x float value into an int to get the array index for your blocks. When you use x = 0, its going to return an integer value, so when you cast it to an int the value it is going to be accurate. When you use x = camX, its going to return a float, so the float value when casted to an int will return a less accurate value because you can have an in between integer values and its going to round up or down.

But honestly, I'm just guessing based on looking at it. I'm in class right now so I don't have time to look into it extensively.