Sign in to follow this  
ov3r_dr1v3

on or off tile???

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
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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this