Hello Gatleos, I was meaning to answer your question much earlier as I kinda said would be posting some different intersection code snippets, but I sadly had a case of a female friend demanding my attention for the night.
Anyway. I finally think I understand what you are trying to do:
You are trying to, move the box towards the slope, and it should "slide" up the slope based on projecting the vector upon the "slope vector"?
If I understood correctly then you are going about collision response a bit wrong, especially with what you want to do.
Anyway, I will write in c++ psuedo code what you should probably do to resolve the collision. Please note, I am assuming that the vector * vector type translates to dot product.
//The will calculate a movement vector based on if it collides with a slope.
vect MoveResolveAABBSlopeSolveCollision(rect & A, rect & tri, vect & move){
if(move.x == 0) return move;
//Returns intersection point between the AABB A and slope tri, based on vector move.
//Here we are essentially just setting up some variables, if you wan't to know what is going on I could explain, but it's a lengthily explanation so I think I'll just skip it for now.
vect h_inverted(- tri.h, tri.w);
float hm = 1 / (h_inverted * move);
float hp = h_inverted * tri.point;
float t; //Here we are going to store the factor the movement vector needs to be to intersect.
//Calculate intesection distance between the movement and slope.
if(dir.x < 0){ //We come from right
t = (hp - (A.p) * h_inverted) * hm;
}
if(dir.x > 0){ //We come form left
t = (hp - (A.p + vect(A.w, 0)) * h_inverted) * hm;
}
if(t >= 1 && t < 0){ //The movement vector and slope do not intersect
return move;
}
//Alright, now that we know they intersect. Let's project the movement vector onto the slope.
vect h(tri.w, tri.h);
vect intersection_p = ((h * move) / (h*h)) * h + tri.p;
//We return a new movement vector projected onto the slope for a "sliding" effect.
return intersection_p;
}
//How to use the code.
//This is basic physics, movement should be calculated after all forces have been summed. This means force should be reset before each update!
player.movementVector = vect(0,0);
//....//
//Handle input!
if(keydown(left))
player.movementVector += vect( player.speed, 0);
if(keydown(right))
player.movementVector += vect(- player.speed, 0);
//....//
tile & tile_we_moved_to = FindTileOnGrid(player.position + player.speed);
//....//
//I assume this is the collision resolving code
if(tile_we_moved_to == slope){
//Alright, we collided with a slope. Let's move our object towards the slope:
player.position += MoveResolveAABBSlopeSolveCollision(player.boundingbox, tile_we_moved_to.boundingbox, player.movementVector);
}
Please note, the code is not really "checked" as such, but rather I wrote the code from thinking about the problem and solving it. It should work as I've done something similar in the past. Also, this can be done much more effectively, but it's simplified so it's easier to follow.
If you can't get it to work, you are free to ask if you have any questions.