FORUM CLOSED, PLEASE REGISTER AT FORUM.SIO2INTERACTIVE.COM
Would you like to react to this message? Create an account in a few clicks or log in to continue.

Mouse tracking

3 posters

Go down

Mouse tracking Empty Mouse tracking

Post  mordenkaim Sun Jul 26, 2009 9:48 pm

Are there any good examples in a tutorial or otherwise for mouse tracking? Specifically I am trying to make a 3D object follow the mouse on a 2D plane when it is clicked and held. I've been trying to code this and I am running into issues converting screen position for the touch (touch(0).x) into the float for location (_SIO2transform->loc->x). I'm thinking that SIO2UnProject is a good place to look based on the forums, but I can't find a good example for it's use... I'll play around with it and see if I can get it working. Thanks in advance for any help. Smile

mordenkaim

Posts : 21
Join date : 2009-05-19

Back to top Go down

Mouse tracking Empty Re: Mouse tracking

Post  Faikus Tue Jul 28, 2009 3:04 am

Something like this might be what you are looking for.

In the functions touchesBegan, if the position of your camera in 3D space is the btVector3 cameraPos and your plane is given by the Plain struct defined below, use these lines:

Code:

btVector viewDirection = getTouchDirection ( [[touches anyObject] locationInView: view], camerPos);
Line line = { viewDirection, camerPos };

btVector3 pointOnPlane =  intersection(plane, line);

You need these definitions and function for that to work:
Code:

btVector3 getTouchDirection(CGPoint touch, const btVector3& cameraPos)
{
   GLfloat model[ 16 ];
   glGetFloatv( GL_MODELVIEW_MATRIX, model );
   GLfloat proj[ 16 ];
   glGetFloatv( GL_PROJECTION_MATRIX, proj );
   GLint viewport[ 4 ];
   glGetIntegerv( GL_VIEWPORT, viewport );

   GLfloat worldX1, worldY1, worldZ1;
   
   GLfloat x = touch.x;
   GLfloat y = viewport[ 3 ] - touch.y;
   
   sio2UnProject( x, y, 0.11,
              model, proj, viewport,
              &worldX1, &worldY1, &worldZ1 );
   
   return btVector3(   worldX1 - cameraPos.x(),
                worldY1 - cameraPos.y() ,
                worldZ1 - cameraPos.z()  );
}


// point: point in plane, normal: the plane's normal
typedef struct {
   btVector3 normal;
   btVector3 point;
} Plane;



typedef struct {
   btVector3 direction;
   btVector3 point;
} Line;


btVector3 intersection(const Plane &plane, const Line& line)
{
   GLfloat t = dot(plane.normal, plane.point - line.point) / dot(plane.normal, line.direction);
   return line.direction * t + line.point;
}

Faikus

Posts : 23
Join date : 2009-05-26
Location : Berlin

Back to top Go down

Mouse tracking Empty Re: Mouse tracking

Post  sio2interactive Tue Jul 28, 2009 4:46 am

Check tutorial062 in the latest sdk...
sio2interactive
sio2interactive

Posts : 1526
Join date : 2008-08-26
Age : 44
Location : Shanghai

http://sio2interactive.com

Back to top Go down

Mouse tracking Empty Re: Mouse tracking

Post  Sponsored content


Sponsored content


Back to top Go down

Back to top


 
Permissions in this forum:
You cannot reply to topics in this forum