1 registered members (TipmyPip),
18,619
guests, and 5
spiders. |
Key:
Admin,
Global Mod,
Mod
|
|
|
Re: rotation around a sphere
[Re: Carlos3DGS]
#391533
01/13/12 17:24
01/13/12 17:24
|
Joined: Mar 2006
Posts: 3,538 WA, Australia
JibbSmart
Expert
|
Expert
Joined: Mar 2006
Posts: 3,538
WA, Australia
|
Almost -- entAng needs to be the player's actual angle (and not just a representation of it), since that will get directly changed by the function. That is: alignToVec(my.pan, vec, vector(0, 0, 1), 1); // you got vec right, as well Also, to smooth things out, you might want to have the last parameter be time_step. What this will do is align your model to the normal by its local z axis by changing its orientation as little as possible -- so really, whatever your model's previous angle was will determine the direction it is ultimately facing. Then you can use ang_add/ang_rotate (I always forget which one) afterwards if you want to change the ship's pan relative to its current orientation. It'll make more sense as you put it into practice and fiddle with it, I think. The planet generator looks really cool 
Formerly known as JulzMighty. I made KarBOOM!
|
|
|
Re: rotation around a sphere
[Re: JibbSmart]
#391547
01/13/12 19:24
01/13/12 19:24
|
Joined: Oct 2008
Posts: 513
Carlos3DGS
OP
User
|
OP
User
Joined: Oct 2008
Posts: 513
|
Thanks for the compliment on the random planet generator! It took alot of work to figure out all the math for that, I still consider it (and the shader) a work in progress but it's pretty close to what I want. I got the rotations to work with your function, thanks alot! Only one problem though... Now I somehow managed to mess up the positioning of my vehicle! This is starting to turn into a huge headache (literally). The function:
void align_to_surface(ENTITY* Planet, ENTITY* Vehicle)
{
VECTOR start_trace;
VECTOR Temp_Vec;
//get the surface position
vec_set(start_trace,Vehicle.x);
vec_sub(start_trace,Planet.x);
vec_normalize(start_trace,vec_length(start_trace)*2);
vec_add(start_trace,Planet.x);
me=Vehicle;
c_trace(start_trace, Planet.x,IGNORE_ME);
//place vehicle on surface
//(taking into account vehicle min_z)
vec_set(Temp_Vec,hit.x);
vec_sub(Temp_Vec,Planet.x);
vec_normalize(Temp_Vec,vec_length(Temp_Vec.x)-Vehicle.min_z);
vec_add(Temp_Vec,Planet.x);
vec_set(Vehicle.x,Temp_Vec);
//rotate towards planet core
vec_sub(Temp_Vec,Planet.x);
alignToVec(Vehicle.pan, Temp_Vec, vector(0, 0, 1), 1);
}
JibbSmart's AlignToVec funtion:
function alignToVec(ANGLE* entAng, VECTOR* vec, VECTOR* axis, var factor) {
vec_rotate(axis, entAng);
vec_normalize(axis, 1);
vec_normalize(vec, 1);
VECTOR rotAxis;
vec_cross(rotAxis, vec, axis);
var angle = -acos((float)vec_dot(axis, vec)) * factor * vec_length(rotAxis);
ANGLE rotAngle;
ang_for_axis(rotAngle, rotAxis, angle);
ang_add(entAng, rotAngle);
}
My movement code:
c_move(vehicle,vector(key_w-key_s,0,0),nullvector,IGNORE_MODELS);
ang_rotate(vehicle.pan,vector(key_a-key_d,0,0));
align_to_surface(Planet, Vehicle);
I know its ugly withought any time_step in there, but its just for testing till I get it to work properly. This is the result: http://www.youtube.com/watch?v=1Y2uTAf8twIEDIT: it looks as if it was detecting the planet as a box or something... But I have set it's POLYGON flag, I swear! set(Planet,POLYGON);
Last edited by Carlos3DGS; 01/13/12 19:30.
|
|
|
Re: rotation around a sphere
[Re: Carlos3DGS]
#391557
01/13/12 20:26
01/13/12 20:26
|
Joined: Mar 2006
Posts: 3,538 WA, Australia
JibbSmart
Expert
|
Expert
Joined: Mar 2006
Posts: 3,538
WA, Australia
|
Weird. I would debug the trace visually -- I'd draw_point3d at the hit position, and use DEBUG_VAR to show vec_length(Temp_Vec) before and after you use vec_normalize.
Formerly known as JulzMighty. I made KarBOOM!
|
|
|
Re: rotation around a sphere
[Re: JibbSmart]
#391562
01/13/12 21:07
01/13/12 21:07
|
Joined: Oct 2008
Posts: 513
Carlos3DGS
OP
User
|
OP
User
Joined: Oct 2008
Posts: 513
|
debugging function:
void align_to_surface(ENTITY* Planet, ENTITY* Vehicle)
{
VECTOR start_trace;
VECTOR Temp_Vec;
//get the surface position
vec_set(start_trace,Vehicle.x);
vec_sub(start_trace,Planet.x);
vec_normalize(start_trace,vec_length(start_trace)*2);
vec_add(start_trace,Planet.x);
me=Vehicle;
c_trace(start_trace, Planet.x,IGNORE_ME);
//DEBUGGING
draw_line3d(start_trace,NULL,100);
draw_line3d(start_trace,COLOR_RED,100);
draw_line3d(Planet.x,COLOR_RED,100);
draw_point3d(hit.x,COLOR_RED,100,16);
//place vehicle on surface
//(taking into account vehicle min_z)
vec_set(Temp_Vec,hit.x);
vec_sub(Temp_Vec,Planet.x);
//DEBUGGING
DEBUG_VAR(vec_length(Temp_Vec), 100);
vec_normalize(Temp_Vec,vec_length(Temp_Vec.x)-Vehicle.min_z);
//DEBUGGING
DEBUG_VAR(vec_length(Temp_Vec), 120);
vec_add(Temp_Vec,Planet.x);
vec_set(Vehicle.x,Temp_Vec);
//rotate towards planet core
vec_sub(Temp_Vec,Planet.x);
alignToVec(Vehicle.pan, Temp_Vec, vector(0, 0, 1), 1);
}
RESULT: http://www.youtube.com/watch?v=r3j5MKgXWR8
|
|
|
Re: rotation around a sphere
[Re: msmith2468]
#391564
01/13/12 21:11
01/13/12 21:11
|
Joined: Oct 2008
Posts: 513
Carlos3DGS
OP
User
|
OP
User
Joined: Oct 2008
Posts: 513
|
checked it... planet is using polygon  EDIT: I am soooooooooooooo stuuuuuupid!!! Since I am not using my planet generator code... you cannot see the water (another translucent sphere the same size as planet) so I forgot it was even there! I set polygon flag for water too and it works now... Time to go buy another brain! Anyway thanks alot for your comment, you got me thinking in the right direction when i went to double check planet's polygon flag! Now I'm off to check the results with my randomly generated planets, and mabe i will consider making the code take into account surface normals...
Last edited by Carlos3DGS; 01/13/12 21:19.
|
|
|
Re: rotation around a sphere
[Re: JibbSmart]
#391588
01/14/12 00:07
01/14/12 00:07
|
Joined: Oct 2008
Posts: 513
Carlos3DGS
OP
User
|
OP
User
Joined: Oct 2008
Posts: 513
|
Got it working also with non-spheric surface normals. Wasn't hard at all, I just had to change Temp_Vec for hit.nx Anyway, I just thought you guys might want to see it working after helping me out: http://www.youtube.com/watch?v=DJJD1jZ2Ob0Two new objectives (but they will have to wait till monday): 1-Making it walk to places (no pathfinding or anything complicated, just simple "look towards objective - move forward"), either to another object on the planet surface or to a mouse click on the surface... 2-Fixing those random crashes that the shader produces (I'm pretty sure its the shader, not assigning the material stops the random crashes)
|
|
|
Re: rotation around a sphere
[Re: Carlos3DGS]
#391595
01/14/12 01:18
01/14/12 01:18
|
Joined: Mar 2006
Posts: 3,538 WA, Australia
JibbSmart
Expert
|
Expert
Joined: Mar 2006
Posts: 3,538
WA, Australia
|
Great work  It looks good! 1- Shouldn't be too hard. If the object is aligned according to its spherical co-ordinates rather than the normal beneath it, you can just translate the destination into a position relative to the object's orientation (vec_rotateback) and rotate the object's pan (relative to its z axis) towards that point. Since you use actual surface normals, we can set up a temporary angle that uses the spherical co-ordinates instead. 2- A shader shouldn't be able to crash a program. Divide by zero and square root of a negative number, the obvious suspects in traditional programming, don't cause crashes in shaders in my experience -- we just get zero instead. It could be a driver problem. Then again, it could be both -- perhaps the driver's supposed to not crash, but it does, and perhaps the code that triggers it is undesirable anyway. Anyway, that's just food for thought. Enjoy your weekend and forget about this for now.
Formerly known as JulzMighty. I made KarBOOM!
|
|
|
Moderated by mk_1, Perro, rayp, Realspawn, Rei_Ayanami, rvL_eXile, Spirit, Superku, Tobias, TSG_Torsten, VeT
|