# Terrain slope

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;
double triangleNormal;
double V;
double mag = sqrt(pow(triangles[currentIndex].N,2) + pow(triangles[currentIndex].N,2) + pow(triangles[currentIndex].N,2));

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

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

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

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

//player directional vector y always 0
userVector = 0.0;

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

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

//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 >= 0.5){
pass = true;
}
return pass;
}

``````