Advertisement

Got a good question for everyone

Started by February 10, 2003 09:02 PM
1 comment, last by jverkoey 22 years ago
ok, here''s a test of how good you guys can problem solve...I''ve been trying to figure this out, and maybe I''ll figure it out sometime, but if someone out there already knows, all the better. ok, here''s the problem, I have a tile, made up of a triangle strip. Now, what I''m doing is making a terrain for my game and a level editor to go along with it, the only thing that changes is the height of each corner (there are 30x30 of these tiles). this then raises that corner, and by some equation, moves the two adjacent sides up too, but leaves the far corner put. Here is a pic (if you can''t see it, just type in the link) http://www32.brinkster.com/jetpackjack/squarepic.gif Ok? you with me so far? well, now here''s a screenshot of a tile that I made (not the same one, but you can apply what I just said easily) http://www32.brinkster.com/jetpackjack/sshot2.gif and if you will notice, the far corner is raised up (it''s kinda hard to see, but it''ll be too much of a hassle to get the whole exe online) and the other corners are at 0 or slightly above. OK, now that we should have everything straight, this is what I want to be able to do. I know how to find the character''s exact pixel coordinates (in float) on each tile. The tiles are all 88x88 pixels. I also know WHICH tile I''m on, so I''ve got all of that down, but here''s the clincher: I need to be able to take the FOUR corners of each tile in to account, and plug in the character''s pixelcoord on the tile, and find out how much I need to change their Y-value...ok? here''s a sample of what I have so far (only works for the far side...and it acts as if that far side extends across the entire tile, so as soon as you enter that tile, you kinda jump up):
  
	float rise1,rise2,slope,x=0;

	rise1=level[P1.tile.Y] [P1.tile.X];
	rise2=level[P1.tile.Y] [P1.tile.X+1];
	slope=(rise2-rise1)/88;
		
	x= slope*P1.tilecoord.x+rise1;

	glTranslatef(C1.xpos,C1.ypos-P1.crouching-x,C1.zpos);
  
the above code does this: it find the top left point, then the top right point, by looking at the matrix with the y-values. Then, it calculates the slope of the two points, and then we have a y=mx+b equation (b=rise1 because rise1 is on the right, and, well, it works) The x value is the character''s position on the tile (P1.tilecoord.x), the resulting value is what we want to add to our character''s y-value (in this case we subtract it because the camera translates everything in relation to it, therefor everything is opposite of the actual value) well, that''s a big post, now, let''s see if anyone can take the challenge and help me out, (i''ll include you in the credits). -thanks in advance jverkoey
ok, I''ll try and explain this again, I know how to find the slope of all four sides, plus the 2 diagonals, ok?

now, what I need to know is how, taking those 4 slopes in to consideration and also the character''s x,z pos, how do i know what the resulting y-value is?????

someone please at least try and reply
Advertisement

  float World::HeightAt(float x,float y){    // x and y should be in 0<=x<((CHUNK_W+1)*5),0 <= y < ((CHUNK_H+1)*5)    // and rounded down    if(x < 0 || x >= ((64-1)*HORIZ_SCALE))        return 0;    if(y < 0 || y >= ((64-1)*HORIZ_SCALE))        return 0;    int tilex = (int)floor( (x / HORIZ_SCALE) );    int tiley = (int)floor( (y / HORIZ_SCALE) );//        Log::Print("%i,%i t\n",tilex,tiley);    float dx = (x - ((float)tilex * HORIZ_SCALE))/HORIZ_SCALE;    float dy = (y - ((float)tiley * HORIZ_SCALE))/HORIZ_SCALE;    if(dx > 1 || dx < 0 || dy > 1 || dy < 0)        Log::Print("%f,%f!\n",dx,dy);    float h[2][2];    h[0][0] = ((float)hmap[tilex+(tiley*64)] / 256.0) * UP_SCALE;    h[1][0] = ((float)hmap[tilex+1+(tiley*64)] / 256.0) * UP_SCALE;    h[0][1] = ((float)hmap[tilex+((tiley+1)*64)] / 256.0) * UP_SCALE;    h[1][1] = ((float)hmap[tilex+1+((tiley+1)*64)] / 256.0) * UP_SCALE;  //  Log::Print("%f,%f,%f,%F t\n",h[0][0],h[0][1],h[1][0],h[1][1]);    float height=0;    if(dx+dy < 1)    {        vector3 s1(1,h[1][0] - h[0][0],0);        vector3 s2(0,h[0][1] - h[0][0],1);        vector3 co = -Cross(s1,s2);        co.Normalize();        float d = Dot(co,vector3(0,h[0][0],0));        if(d < 0)            Log::Print("1:D <\n");        //height = (( (1-(dx))*(1-(dy)) )*h[0][0]) + (dx*(1-dy)*h[1][0]) + (dy*(1-dx)*h[0][1]);        height = - ( (co[0] * dx) + (co[2] * dy) - d ) / co[1];    }    else    {        //dx = 1-dx;        //dy = 1-dy;        vector3 s2(0,h[1][0] - h[1][1],-1);        vector3 s1(-1,h[0][1] - h[1][1],0);        vector3 co = -Cross(s1,s2);        co.Normalize();        float d = Dot(co,vector3(1,h[1][1],1));        if(d < 0)            Log::Print("2:D <\n");        height = - ( (co[0] * dx) + (co[2] * dy) - d ) / co[1];//        height = ((1-(dx))*(1-(dy))*h[1][1]) + (dx*(1-dy)*h[0][1]) + (dy*(1-dx)*h[1][0]);    }//    Log::Print("%f\n",height);    return height;}  


The idea is to create a linear combination using the x,y position within the tile (thats dx and dy). You can then work out which triangle the point is in and use those three points in a plane equation to find the height.

I''ve just dumped an example from an old demo of mine so hopefully you''ll be able to figure out what is going on.

This topic is closed to new replies.

Advertisement