Advertisement

ok, got another good question.....

Started by February 11, 2003 05:28 PM
28 comments, last by jverkoey 22 years ago
Ok, now i know what you want and heres what i got:

First heres what i think you want: You''ve got some piece of terrain and you want your character to walk over it and not through it. To do that were going to translate the character to its x and z co-ordinate and use those values to calculate the y coordinate of the character from your terrain.

First thing you have to change is that your terrain must be made up of triangles. Squares arn''t planer so we cant interpolate easily between them. Ok, in my solution, your terrain needs to be made up of triangles and to make things easier im going to use a system like Trent''s game tutorials.

The terrain mesh will look like this:

  _________________|\|\|\|\|\|\|\|\||\|\|\|\|\|\|\|\||\|\|\|\|\|\|\|\||\|\|\|\|\|\|\|\||\|\|\|\|\|\|\|\||\|\|\|\|\|\|\|\||\|\|\|\|\|\|\|\|-----------------  


It is important that you draw the triangles going from upper left to bottom right - you''ll see why in a minute. One important thing to note is that each square is made up of two planes - in this case there is the one made by the bottom left triangle and the one made by the upper right triangle.

Ok, now lets write a function for your terrain that takes two variables, your character''s x and z position and have it generate a y position. The function i write will make it unable to translate your terrain because we are going to compare the world co-ordinates of the character to the terrain - you can easily compensate for this. Our terrain will be stored as an array of vertices like so: Data[j]. It must also have world coordinates in it''s vertices.



  //the distance between the vertices of the terrain - terrain must be a square#define DIST_Vfloat y TERRAIN::GenY(float X,float Z){//first thing we have to do is find out which triangle our//character is onint i,j;bool Tri = false, done = false;//loop through all the rows values of the terrainfor(i = 0; loop < (NumI - 1); i ++){   for(j = 0; j < (NumJ -1); j ++)   {      //check to see if our point is in the square      if((X < Data[i+1][j+1].x) && (Z < Data[i+1][j+1].z))      {          //now check to see which triangle in the square the           //point is          //we assume that it is in the bottom left corner          //but we check the top right here          float tx = (X - Data[i][j].x), tz = (Z < Data[i][j].z);          if( (tx + tz) > DIST_V)             Tri = true;          done = true;          break;      }   }   if(done)      break;}//ok, now the values of i and j represent the lower left vertex//of the plane that our character is on//we can use these values to generate our y value//there are two situations - the plane we have to check is the //lower left or it is the upper right - our Tri variable will//tell usfloat t1 = 0, t2 = 0, answer = 0;//this means its in the upper rightif(Tri){   //this a bit complicated - you need planer geometry   t2 = ( ( (Data[i+1][j].x - Data[i+1][j+1].x)*(Z - Data[i+1][j+1].z) - (Data[i+1][j].z - Data[i+1][j+1].z)*(X - Data[i+1][j].z) ) / ( (Data[i+1][j].x - Data[i+1][j+1].x)*(Data[i][j+1].z - Data[i+1][j+1].z) - (Data[i+1][j].z - Data[i+1][j+1].z)*(Data[i][j+1].x - Data[i+1][j+1].x) ) );   t1 = ( X - t2 * *(Data[i][j+1].x - Data[i+1][j+1].x) - Data[i+1][j+1].x ) / (Data[i+1][j].x - Data[i+1][j+1].x);   answer = t1 * (Data[i+1][j].y - Data[i+1][j+1].y ) + t2 * (Data[i][j+1].y - Data[i+1][j+1].y) + Data[i+1][j+1];   return answer;}else{   //this a bit complicated - you need planer geometry   //hopefully this works :)   t2 = ( ( (Data[i+1][j].x - Data[i][j].x)*(Z - Data[i][j].z) - (Data[i+1][j].z - Data[i][j].z)*(X - Data[i+1][j].z) ) / ( (Data[i+1][j].x - Data[i][j].x)*(Data[i][j+1].z - Data[i][j].z) - (Data[i+1][j].z - Data[i][j].z)*(Data[i][j+1].x - Data[i][j].x) ) );   t1 = ( X - t2 *(Data[i][j+1].x - Data[i][j].x) - Data[i][j].x ) / (Data[i+1][j].x - Data[i][j].x);   answer = t1 * (Data[i+1][j].y - Data[i][j].y ) + t2 * (Data[i][j+1].y - Data[i][j].y) + Data[i][j];   return answer;}}  


The last part of this is sort of unclear. All you really have to do is relate those i and j values to this:


  //if we had a square the the index values would be here:i+1,j_____ i+1,j+1     |   |     |   |i,j  ----- i, j+1  


now all you have to do is recode that and then if you move your character''s x and z coordinates and use this to generate the y then it should walk over the cliffs
i already got it working, I''m gonna uploading the working copy, FINALLY, to my site thanks SKY!!!

bloodright.150m.com //For the latest on all of the IVGDA projects!
Advertisement
If you want to put me you could put Endless Psyke - Skyler Clark or something like that...

What's your game about?
How far along is it?

EDIT: By the way, at your site in the entrance page thing it want's me to install Gator. I'm not sure if you know this, but Gator is spyware, so if you know why its doing that you should take it off so other people that are ignorant to this fact dont accidently put spyware on their computer


[edited by - Sky on February 12, 2003 9:06:25 PM]
oh, i can't help that, really sorry, but 150m.com is stupid and has MANDATORY pop-ups and stuff, that's why i just use it as a link to my FREE brinkster account (which doesn't have popups, lol)

ok, I'm gonna upload it in about 5 minutes

bloodright.150m.com //For the latest on all of the IVGDA projects!

-EDIT #1-

uploading..........

[edited by - jverkoey on February 12, 2003 9:18:20 PM]

-EDIT #2-

oh, and Sky, the game is a 1st person RPG, so far the game itself is just a huge tilemap with some chairs, tables, and doors, lol, I've been working on the models, and I actually measured them off of real-life things, the chair for instance, is measured exactly like a chair in my school's library, lol

Oh, and all the models so far have been hand-coded (in OpenGL, and yes for all you skeptics, I know this isn't a smart idea, but it works for now, lol)

[edited by - jverkoey on February 12, 2003 9:20:05 PM]

-EDIT #3-
Both the debugger and the level editor are now on the site, bloodright.150m.com, current_projects

check it out, and make a map or two, hehe

[edited by - jverkoey on February 12, 2003 9:27:18 PM]
I spent half an hour typing my response and by that time sky had posted his }:0) Its essentially the same stuff.
sorry bout that, thanks for helping though

bloodright.150m.com //For the latest on all of the IVGDA projects!
Advertisement
quote:
Original post by llvllatrix
I spent half an hour typing my response and by that time sky had posted his }:0) Its essentially the same stuff.


Ya it is, the only difference is the number of triangles... I think its easier on the graphics card to draw 2 triangles rather then 4, and its the exact same thing, find the triangle, find the plane, plugin points and solve for y.

oh, well, the reason why i''m using 4 triangles is because that way, I can have much higher definition in the levels than just 2 triangles would give

bloodright.150m.com //For the latest on all of the IVGDA projects!
ah, i was about to say, the texture for the ground looks very nice. just dizzying.

Hehe... its fun to run around the level, makes me slightly dizzy though (all the up-down-ness,weeeee)

Its pretty nice, you should add jumping, the collision detection would use the exact same thing with the planes and triangles....

Twas fun to help, I've learned alot. It will be very usefull when i get to writing my own engine.

Oh, and are you still working on the jetpack game? or is that dead?




[edited by - Sky on February 12, 2003 10:03:51 PM]
still workin'' on that, not as much as i was though (my hard drive crashed, and with it so did some of my will to work on jetpack jack)

but JetPack Jack WILL make a comeback sooner or later, I''ll be in the mood to work on it in a while...lol, just not right now

bloodright.150m.com //For the latest on all of the IVGDA projects!

This topic is closed to new replies.

Advertisement