Posted By: EpsiloN
Rotate an object with mouse - 07/27/15 09:13
Again, hi all.
So, apparently I skipped a few years of math from highschool about vectors and angles here we go again...
To describe the problem better, I want to 'drag' a point of the sphere surface, thats under my mouse pointer, to a new location on the sphere surface. I'm either close, but visibly inaccurate or completely off...
I'm tracing two rays from mouse pos to a sphere (gizmo) and I get the two points coordinates relative to the model origin with:
"My" is the sphere.
And I'm trying to rotate my gizmo so that oldContactPos will move to the new contactPos location.
All attempts failed, here's the last two:
Anyone know why both methods fail? I have no idea what I'm doing wrong, but this must be my 20-th attempt doing the same stuff and always wrong...
Thanks in advance.
So, apparently I skipped a few years of math from highschool about vectors and angles here we go again...
To describe the problem better, I want to 'drag' a point of the sphere surface, thats under my mouse pointer, to a new location on the sphere surface. I'm either close, but visibly inaccurate or completely off...
I'm tracing two rays from mouse pos to a sphere (gizmo) and I get the two points coordinates relative to the model origin with:
Code:
vec_set( m3dStartPos , vector( mouse_pos.x , mouse_pos.y , 1 ) ); vec_set( m3dEndPos , vector( mouse_pos.x , mouse_pos.y , 1000 ) ); vec_for_screen( m3dStartPos , camera ); vec_for_screen( m3dEndPos , camera ); you = puppet; result = c_trace( m3dStartPos , m3dEndPos , IGNORE_YOU | USE_POLYGON ); if( result > 0 ) { // Get local point position (relative to my sphere) vec_diff( oldContactPos , hit.x , my.x ); } wait(1); vec_set( m3dStartPos , vector( mouse_pos.x , mouse_pos.y , 1 ) ); vec_set( m3dEndPos , vector( mouse_pos.x , mouse_pos.y , 1000 ) ); vec_for_screen( m3dStartPos , camera ); vec_for_screen( m3dEndPos , camera ); you = puppet; result = c_trace( m3dStartPos , m3dEndPos , IGNORE_YOU | USE_POLYGON ); if( result > 0 ) { // Get local point position (relative to my sphere) vec_diff( contactPos , hit.x , my.x ); }
"My" is the sphere.
And I'm trying to rotate my gizmo so that oldContactPos will move to the new contactPos location.
All attempts failed, here's the last two:
Code:
if( vec_dist( oldContactPos , contactPos ) > 0 ) { VECTOR dir1 , dir2 , axis1 ; ANGLE oldAng , newAng , diffAng ; // // Using vec_to_angle to orient two angles // and get the difference needed to rotate the gizmo /* vec_set( dir1 , contactPos ); vec_set( dir2 , oldContactPos ); vec_to_angle( oldAng , oldContactPos ); // Here roll becomes 2xxx, so I reset it to 0 // But I think I need roll too, to be a realistic rot.? oldAng.roll = 0; vec_to_angle( newAng , contactPos ); newAng.roll = 0; ang_diff( diffAng , newAng , oldAng ); ang_add( my.pan , diffAng ); ang_add( my.local_pan , diffAng ); */ // // Using ang_for_axis to rotate the objects angles // by the angle difference of the two points // around axis - cross product of the two points // (relative to local object space) vec_set( dir1 , oldContactPos ); vec_set( dir2 , contactPos ); vec_cross( axis1 , dir1 , dir2 ); vec_set( newAng , my.pan ); vec_normalize( dir1 , 1 ); vec_normalize( dir2 , 1 ); ang_for_axis( newAng , axis1 , acosv( vec_dot( dir1 , dir2 ) ) ); ang_add( my.pan , newAng ); ang_add( my.local_pan , newAng ); }
Anyone know why both methods fail? I have no idea what I'm doing wrong, but this must be my 20-th attempt doing the same stuff and always wrong...
Thanks in advance.