Posted By: Carlos3DGS
rotation around a sphere - 01/12/12 21:57
I am playing around with spheric levels instead of flat ones, and am having a bit of trouble with player movement around the sphere. I have an almost working code, but it gets strange results at the poles (top & bottom).
Placing the player model on the sphere's surface is pretty straightforward. For a sphere with a radius of 500 for example I do the following:
place_on_surface(My_Planet, My_Vehicle, 500);
Now here comes the tricky part I am having trouble with, the player's rotations. So far this attempt is pretty close to what I want. but I get very strange results when the player reaches the top or bottom of the sphere:
rotate_on_surface(My_Planet,My_Vehicle,my_pan);
I keep the desired absolute orientation (pan I want the vehicle to look at) in a separate variable so it dosn't get messsed up during the calculations.
The resuts are "almost" what I need, but even separating the pan like I do does not make my vehicle cross the planet surface in a straight line unless it is going straight across the equator... The farther it is from the middle (equator) the more it turns towards the poles (north/south), kind of like traveling in circles orbiting the poles and turning more and more towards them! And when walking directly on the north/south poles it just goes crazy! (it does stay aligned to the planet, but the pan just freaks out)
Movement Code:
Has anyone tried something similar before?
Can you spot what I am doing wrong here?
Any advice would be great.
EDIT:
Uploaded an example video to youtube:
http://www.youtube.com/watch?v=KjgIwm75e8U
(keep in mind this is just placeholder art to test the code)
EDIT2:
Added a second video with a free camera further away so its easier to see whats going on with the vehicle withought getting dizzy:
http://www.youtube.com/watch?v=NXkbrUEMpfQ
Placing the player model on the sphere's surface is pretty straightforward. For a sphere with a radius of 500 for example I do the following:
place_on_surface(My_Planet, My_Vehicle, 500);
Code:
void place_on_surface(ENTITY* Planet, ENTITY* Vehicle, var planet_radius) { VECTOR relative_pos; var desired_distance; //get vehicle position relative to the planet center vec_diff(relative_pos.x,Vehicle.x,Planet.x); //set desired distance from radius desired_distance=abs(Vehicle.min_z)+planet_radius; vec_normalize(relative_pos,desired_distance); vec_add(relative_pos,Planet.x); //place the vehicle on the planet surface vec_set(vehicle.x,relative_pos); }
Now here comes the tricky part I am having trouble with, the player's rotations. So far this attempt is pretty close to what I want. but I get very strange results when the player reaches the top or bottom of the sphere:
rotate_on_surface(My_Planet,My_Vehicle,my_pan);
Code:
void rotate_on_surface(ENTITY* Planet, ENTITY* Vehicle, var desired_pan) { VECTOR relative_pos; ANGLE relative_ang; ANGLE result_ang; //get vehicle position relative to the planet center vec_diff(relative_pos.x,Vehicle.x,Planet.x); //tranform relative position to angle vec_to_angle(relative_ang.pan,relative_pos.x); //build new angle vec_set(result_ang,vector(0,0,90)); ang_rotate(result_ang,vector(0,-relative_ang.pan-90,0)); ang_rotate(result_ang,vector(0,0,-relative_ang.tilt)); ang_rotate(result_ang,vector(desired_pan,0,0)); //rotate vehicle vec_set(vehicle.pan,result_ang); }
I keep the desired absolute orientation (pan I want the vehicle to look at) in a separate variable so it dosn't get messsed up during the calculations.
The resuts are "almost" what I need, but even separating the pan like I do does not make my vehicle cross the planet surface in a straight line unless it is going straight across the equator... The farther it is from the middle (equator) the more it turns towards the poles (north/south), kind of like traveling in circles orbiting the poles and turning more and more towards them! And when walking directly on the north/south poles it just goes crazy! (it does stay aligned to the planet, but the pan just freaks out)
Movement Code:
Code:
while(1) { c_move(My_Vehicle,vector(key_w-key_s,0,0),nullvector,IGNORE_MODELS); my_pan=cycle(my_pan+key_a-key_d,0,360); rotate_on_surface(My_Planet,My_Vehicle,my_pan); place_on_surface(My_Planet, My_Vehicle, 500); wait(1); }
Has anyone tried something similar before?
Can you spot what I am doing wrong here?
Any advice would be great.
EDIT:
Uploaded an example video to youtube:
http://www.youtube.com/watch?v=KjgIwm75e8U
(keep in mind this is just placeholder art to test the code)
EDIT2:
Added a second video with a free camera further away so its easier to see whats going on with the vehicle withought getting dizzy:
http://www.youtube.com/watch?v=NXkbrUEMpfQ