Posted By: paracharlie
Here's a camera collision with camera distance scroll and fading - 02/19/14 00:32
This code originally came from Kingdom of hearts and it's part of my snippets folder that I use for everything. It is camera collision, camera distance scrolling using mouse wheel and fades in and out on whatever entity.
Code:
var camera_distance = 200; //How far away the initial distance is. var temp; VECTOR* tempV = {x=0;y=0;z=0;} var fade_start = 90; //at what distance to start fading var fade_end = 60; //at what distance to end the fading var fade_toggle = 1; //0 = off; 1 = on ////////////////////////////// //Stable/rigid camera collision ////////////////////////////// function handle_camera() { camera_distance -= mickey.z * time_step; //middle mouse scroll camera_distance = clamp(camera_distance, 0, 700); //put a clamp on the total camera distance for mouse scrolling camera.pan -= (key_cul - key_cur) * 12 * time_step; camera.tilt += (key_cud - key_cuu) * 8 * time_step; camera.tilt = clamp(camera.tilt, -60, -10); //I like putting clamps on the amount of camera tilt also temp = fcos(camera.tilt, -camera_distance); vec_set(camera.x, vector(my.x + fcos(camera.pan, temp),my.y + fsin(camera.pan, temp),my.z + 35 + fsin(camera.tilt, -camera_distance))); vec_diff(tempV.x, camera.x, my.x); //find the vector from the player to the camera vec_normalize(tempV.x, 16); //get the magnitude of it's vector to 16 quants and store it in temp vec_add(tempV.x, camera.x); //add the vector (from player to camera) of a magnitude of 16 quants and add it to the camera's position. result = c_trace(my.x, tempV.x, IGNORE_ME|IGNORE_PASSABLE); //from the player trace to 16 quants behind the camera. if(result > 0) //c_trace returns a 1 if polygon target is hit { vec_diff(tempV.x, my.x, target.x); //find the vector from the point the trace hit to the player vec_normalize(tempV.x, 16); //get the magnitude of this vector to 16 quants and store in temp vec_set(camera.x, target.x); //place the camera at the trace hit point vec_add(camera.x, tempV.x); //move the camera away from the wall by the vector temp, 16 quants towards the player } //start the alpha fade on the character for zooming in and out *********************** if(fade_toggle == 1) { vec_diff(tempV.x, my.x, camera.x); //get the difference between me and camera and copy it to tempV if(fade_start > 0) { tempV.x = vec_length(tempV.x); // distance from camera to target if(tempV.x < fade_start) { set(my, TRANSLUCENT); // fade to zero depending distance to fade end reset(my, SHADOW); //NOTE: use 1st max to avoid negative alpha //use 2nd max to avoid dreaded divide by zero my.alpha = 100 * maxv(0, ((tempV.x - fade_end) / maxv(1,(fade_start - fade_end)))); } else { reset(my, TRANSLUCENT); set(my, SHADOW); } }//endif fade_start }//endif fade_toggle == 1 }//endfunction