im too good for this community.
I took some of my old code, and put it together in a camera script with collision.
Now i dont wanna hear im stubborn anymore!
Code:
entity* car;
//Environment Defines
var earth_gravity[3] = 0,0, -887;
var camera_distance = 400;//change to whatever
var temporary_distance;
var distance_traced;
function avoid_obstacles()
{
car.skill42 = camera_distance;
trace_mode = ignore_me + ignore_passable;
vec_set(temporary_distance.x, camera.x);
temporary_distance.z -= 50;
distance_traced = c_trace(car.x, temporary_distance.x, ignore_models);
if (distance_traced == 0) // Nothing in the way?
{
if (camera_distance < car.skill42)
{
camera_distance += 1;
}
}
else
{
distance_traced -= 5;
camera.x = car.x - distance_traced * cos(camera.pan); // place the camera behind the player
camera.y = car.y - distance_traced * sin(camera.pan); // at the new distance given by distance_traced
}
}
//Put this in "function main" Simple_TestphCar_Camera();
//"Function Simple_TestphCar_Camera" needs to be below the entity* car;
Function Simple_TestphCar_Camera
{
var vCameraPos_Angle; // Angle the camera is at in relation to the cars angle
while (!car) {wait (1);} // wait for car to be created
vCameraPos_Angle = car.pan; // Start behind
// Follow behind the car
while(1)
{
while(freeze_mode == 1){wait(1);}
// Find the difference in the cameras heading compared to the cars heading
temp.x = ang (car.pan - camera.pan);
temp.x = clamp (temp.x, -3.5 * time, 3.5 * time);
vCameraPos_Angle += temp.x; // Adjust the viewing angle
// Position camera behind car from its viewing angle
temp.x = -450;
temp.y = 0;
temp.z = 100;
vec_rotate (temp, vector(vCameraPos_Angle, 0, 0));
vec_add (temp, car.x);
vec_set (camera.x, temp.x);
// face the car
vec_set (temp, car.x);
vec_sub (temp.x, camera.x);
vec_to_angle (camera.pan, temp.x);
avoid_obstacles();
wait (1);
}
}
action physics_ent
{
car = me;
phent_settype(my, PH_RIGID, PH_BOX);
ph_setgravity( earth_gravity );
phent_setmass(my, 7, PH_BOX);
phent_setfriction(my,20);
phent_setelasticity(my, 25, 1);
phent_setdamping(my, 30, 15);
control();
}
function control
{
var velocity;
var velocity3[3];
while(1)
{
phent_getvelocity(my, velocity3, nullvector);
velocity = vec_length(velocity3);
if(key_w == 1){vec_set(temp.x, my); vec_normalize(temp, 5000); phent_addforcelocal(my, temp, nullvector);}
if(key_s == 1){vec_set(temp.x, my); vec_normalize(temp, -2500); phent_addforcelocal(my, temp, nullvector);}
if(key_a == 1 && velocity > 60){phent_addtorqueglobal (my, vector(0, 0, 200));}
if(key_d == 1 && velocity > 60){phent_addtorqueglobal (my, vector(0, 0, -200));}
wait(1);
}
}
function main
{
video_switch(7, 32, 2);
fps_max = 60;
level_load("nascar_test.wmb");
wait(5);
Simple_TestphCar_Camera();
}
youre all welcome
regards,