Crafter map problem

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
N64vSNES
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 632
Joined: Thu Aug 12, 2010 11:25 am

Crafter map problem

Post 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. :|
Last edited by N64vSNES on Thu Apr 07, 2011 4:33 pm, edited 1 time in total.
User avatar
eatcomics
ES Beta Backer
ES Beta Backer
Posts: 2528
Joined: Sat Mar 08, 2008 7:52 pm
Location: Illinois

Re: Crafter map problem

Post by eatcomics »

I saw no explaining what so ever....
Image
User avatar
xiphirx
Chaos Rift Junior
Chaos Rift Junior
Posts: 324
Joined: Mon Mar 22, 2010 3:15 pm
Current Project: ******** (Unkown for the time being)
Favorite Gaming Platforms: PC
Programming Language of Choice: C++
Contact:

Re: Crafter map problem

Post by xiphirx »

Why is this here?

Code: Select all

for ( y = 0;y < 64;y++ ) {
      if ( y < 0 ) { <----
         y = 0; <----
      } <----
...
StarCraft II Zerg Strategy, open to all levels of players!

Looking for paid work :< Contact me if you are interested in creating a website, need a web design, or anything else you think I'm capable of :)
N64vSNES
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 632
Joined: Thu Aug 12, 2010 11:25 am

Re: Crafter map problem

Post 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:
krilik
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 25
Joined: Sat Apr 18, 2009 11:24 pm
Favorite Gaming Platforms: SNES,PS1

Re: Crafter map problem

Post 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.
Post Reply