PDA

View Full Version : Terrain slope



OctopusPrime314
05-15-2012, 04:29 PM
I want to have user movement on a terrain limited by the slope of each individual triangle.
I already have user height adjusted based on the equation of a plane in 3D space.

I have been fiddling with vectors to try and get this problem solved by doing dot product and cross product routines to try and find a pattern but nothing is coming up.
What I have been doing is having a user vector direction that is confined to the y plane (y plane is my height as in opengl) at y = 0.
While the normal vector of the plane of the triangle is my other vector. I tried just having a limit of whenever a plane has a normalized y component of say less than 0.5 I can't travel up its slope.
We could say that a flat surface with no slope would have a normal y component of 1 and a wall would have a y normal component of 0. And the rest of the possible slopes in between.

My issue is that if I enter one of these triangles I technically should be able to back up because gravity naturally lets me retreat to a flatter surface. I can't, I get stuck because I can't move at all if I am in a triangle of large slope.
So I therefore realized If I need to go backwards my directional vector should flip 180 degrees. This is where I thought my cross product would flip my result and allow me to go down a negative slope. Nope!
Here is my code if you don't understand what I am trying to do.



//angle changes if user is moving forward or backward ie backward adds an offset of 180 degrees


bool Terrain::findSlope(double angle){
bool pass = false;
double userVector[3];
double triangleNormal[3];
double V[3];
double mag = sqrt(pow(triangles[currentIndex].N[0],2) + pow(triangles[currentIndex].N[1],2) + pow(triangles[currentIndex].N[2],2));

//triangle x coordinate normal normalized
triangleNormal[0] = triangles[currentIndex].N[0]/mag;

//triangle y coordinate normal normalized
triangleNormal[1] = triangles[currentIndex].N[1]/mag;

//triangle z coordinate normal normalized
triangleNormal[2] = triangles[currentIndex].N[2]/mag;

//player directional vector x
userVector[0] = sin(angle*3.14/180.0);

//player directional vector y always 0
userVector[1] = 0.0;

//player directional vector z
userVector[2] = cos(angle*3.14/180.0);


//cross product to get normal of vectors not sure what to do here
V[0]=userVector[2]*triangleNormal[1]-userVector[1]*triangleNormal[2];
V[1]=userVector[2]*triangleNormal[0]-userVector[0]*triangleNormal[2];
V[2]=userVector[1]*triangleNormal[0]-userVector[0]*triangleNormal[1];


//testing if the normal is greater than 0.5 ie 0.5 is between a wall and a flat surface relative to user
if(V[1] >= 0.5){
pass = true;
}
return pass;
}