Jump to content
  • Advertisement
Sign in to follow this  
ov3r_dr1v3

on or off tile???

This topic is 4235 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey... Im developing a basic 2D racing game, which is tile based, based on a simple 2D array as below...
int track[20][20] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
		0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0,
		0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0,
		0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0,
		0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0,
		0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0,
		0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0,
		0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0,
		0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0,
		0, 0, 1, 1, 1, 1, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 1, 0, 0,
		0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0,
		0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0,
		0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0,
		0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0,
		0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 1, 0, 0,
		0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0};

The block of code below is the method i use to decode and draw this to the screen
void draw_Track()
{
	int tile;
	for(int x = 0; x < 20; x++)
	{
	    for(int y = 0; y < 20; y++)
	    {
                tile = track[x][y];
		glEnable(GL_BLEND);
		glBindTexture(GL_TEXTURE_2D, texture[tile]);
		glBegin(GL_QUADS);
		glColor3f(1.0, 1.0, 1.0);
		glTexCoord2f(0.0f, 0.0f); glVertex3f(float(x), float(y), 0.0f);
	        glTexCoord2f(1.0f,0.0f);glVertex3f(float(x+1),float(y),0.0f);           
	        glTexCoord2f(1.0f,1.0f); glVertex3f(float(x+1),float(y+1),0.0f);
		glTexCoord2f(0.0f,1.0f); glVertex3f(float(x),float(y+ 1),0.0f);
		glEnd();
	    }
	}
}

Im pretty sure in order to do this i have to perform some form of check in my idle method to see whether the current tile the car is on is grass or texture, and then react accordingly...However i'm having trouble determining this... Any help would be great, thanks

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
use coordinates

coordinates divided by tile size = which tile index your coordinate is on top of

Share this post


Link to post
Share on other sites
sorry for being a bit dim, im very tired at the moment lol...

how would i calculate that...

i have two global variables, which hold the x and y position of the car at any one time, and the size of each tile is 1 by 1...

thanks

Share this post


Link to post
Share on other sites
If I were you, I'd build a function that takes an x and y value, and returns a tile value. I'd also wrap the whole int array inside a Track class to keep all the level-related code contained inside one area (as member functions), but I assume you're not familiar with classes so let's leave it at that.


Anyway, detecting the tile value is quite straightforward really:

int TileValueAtLocation(int x, int y)
{
return track[tilewidth / x][tileheight / y];
}

which is basically what the AP said, in code form.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!