Rotate an object with mouse

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 grin 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.
Posted By: Ch40zzC0d3r

Re: Rotate an object with mouse - 07/27/15 09:29

Just rotate it arround the distance your cursor moved in pixels from click position until now? o.o
Posted By: EpsiloN

Re: Rotate an object with mouse - 07/27/15 09:35

I will provide that method too in my app, but I need the 'realistic' rotation too, as if you're rotating the object by hand...

This article is my goal:
http://www.quantimegroup.com/solutions/pages/Article/Article.html

But I dont understand java that much and I dont have access to the polygons of the sphere to implement that laugh so I'm trying to figure it out on my own, but I'm failing...

I dont see anything wrong with my script, but it is not working, both methods...or my previous attempts...
Posted By: txesmi

Re: Rotate an object with mouse - 07/27/15 11:21

Hi,
I built same method of you (the second) and it is working.

I realized that the sphere entity needs the POLYGON flag set. Otherway the hit position vector differs from the point it should hit visually.

Code:
#include <acknex.h>

void fncRotate ()
{
	VECTOR vPos, vTarget;
	vec_set ( &vPos, &camera->x );
	vec_set ( &vTarget, &mouse_dir3d );
	vec_scale ( &vTarget, 1000 );
	vec_add ( &vTarget, &vPos );
	c_trace ( &vPos, &vTarget, NULL );
	if ( HIT_TARGET )
	{
		ENTITY *ent = you;
		VECTOR vDir1;
		vec_diff ( &vDir1, &hit->x, &you->x );
		vec_normalize ( &vDir1, 1 );
		ANGLE angOld;
		vec_set ( &angOld, &you->pan );
		while ( mouse_left )
		{
			vec_set ( &vPos, &camera->x );
			vec_set ( &vTarget, &mouse_dir3d );
			vec_scale ( &vTarget, 1000 );
			vec_add ( &vTarget, &vPos );
			c_trace ( &vPos, &vTarget, NULL );
			if ( HIT_TARGET )
			{
				if ( you == ent )
				{
					draw_point3d ( &hit->x, COLOR_WHITE, 100, 2 );
					VECTOR vDir2, vAxis;
					vec_diff ( &vDir2, &hit->x, &you->x );
					vec_normalize ( &vDir2, 1 );
					vec_cross ( &vAxis , &vDir1 , &vDir2 );
					ANGLE angRot;
					ang_for_axis( &angRot, &vAxis, acosv ( vec_dot ( &vDir1 , &vDir2 ) ) );
					vec_set ( &you->pan, &angOld );
					ang_add( &you->pan, &angRot );
				}
			}
			
			DEBUG_VAR ( ent->pan, 10 );
			DEBUG_VAR ( ent->tilt, 40 );
			DEBUG_VAR ( ent->roll, 70 );
			wait(1);
		}
	}
}

void main ()
{
	mouse_mode = 4;
	wait(1);
	
	level_load ( "" );
	camera->x -= 100;
	ENTITY *ent = ent_create ( "sphere.mdl", nullvector, NULL );
	ent->flags |= POLYGON;
	
	on_mouse_left = fncRotate;
	
	while ( !key_esc )
		wait(1);
	
	ent_remove ( ent );
	sys_exit ( NULL );
	
}



Salud!
Posted By: EpsiloN

Re: Rotate an object with mouse - 07/27/15 12:51

I havent tested it yet, but thanks again for saving my ass laugh
Posted By: txesmi

Re: Rotate an object with mouse - 07/28/15 05:47

Glad to help laugh
© 2024 lite-C Forums