2 registered members (AndrewAMD, 7th_zorro),
1,285
guests, and 4
spiders. |
Key:
Admin,
Global Mod,
Mod
|
|
|
Rotate an object with mouse
#453447
07/27/15 09:13
07/27/15 09:13
|
Joined: Jan 2006
Posts: 968
EpsiloN
OP
User
|
OP
User
Joined: Jan 2006
Posts: 968
|
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:
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:
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.
|
|
|
Re: Rotate an object with mouse
[Re: Ch40zzC0d3r]
#453449
07/27/15 09:35
07/27/15 09:35
|
Joined: Jan 2006
Posts: 968
EpsiloN
OP
User
|
OP
User
Joined: Jan 2006
Posts: 968
|
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.htmlBut I dont understand java that much and I dont have access to the polygons of the sphere to implement that 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...
|
|
|
Re: Rotate an object with mouse
[Re: EpsiloN]
#453451
07/27/15 11:21
07/27/15 11:21
|
Joined: Jun 2007
Posts: 1,337 Hiporope and its pain
txesmi
Serious User
|
Serious User
Joined: Jun 2007
Posts: 1,337
Hiporope and its pain
|
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.
#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!
|
|
|
|