Posted By: draculaFactory
Clamp to screen. - 04/24/13 22:04
I have this code that someone gave me a long time ago. I recently began to use it again. The code works perfectly; it clamps the player to the view, not allowing movement outside of the view. The problem is that when I begin to rotate the camera, which is in a top-down position, the player position messes up. Why won't this work for all angles of the camera in the top-down view?
Code:
function clamp_to_view(VECTOR* vec) { var hull_h, hull_v; VECTOR temp_vec, h1, h2, v1, v2; vec_set(temp_vec.x,vec.x); vec_to_screen(temp_vec.x,camera); // set horizontal border position vec_set(h1.x,vector((camera_border * 0.5),temp_vec.y,temp_vec.z)); vec_set(h2.x,vector(screen_size.x - (camera_border * 0.5),temp_vec.y,temp_vec.z)); // set vertical border position vec_set(v1.x,vector(temp_vec.x,0,temp_vec.z)); vec_set(v2.x,vector(temp_vec.x,screen_size.y,temp_vec.z)); // set border position to world value vec_for_screen(h1.x,camera); vec_for_screen(h2.x,camera); vec_for_screen(v1.x,camera); vec_for_screen(v2.x,camera); // get hull size hull_h = (vec_dist(vector(my.min_x,0,0),vector(my.max_x,0,0)) / 2); hull_v = (vec_dist(vector(0,my.min_y,0),vector(0,my.max_y,0)) / 2); // clamp to on-screen position vec.x = clamp(vec.x,h1.x + hull_h,h2.x - hull_h); vec.y = clamp(vec.y,v2.y + hull_v,v1.y - hull_v); }