Mouse tracking

View previous topic View next topic Go down

Mouse tracking

Post  mordenkaim on 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

View user profile

Back to top Go down

Re: Mouse tracking

Post  Faikus on 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

View user profile

Back to top Go down

Re: Mouse tracking

Post  sio2interactive on Tue Jul 28, 2009 4:46 am

Check tutorial062 in the latest sdk...

_________________
SIO2 Interactive
Free Open Source 3D Game Engine for iPhone and iPod Touch
http://sio2interactive.com
avatar
sio2interactive

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

View user profile http://sio2interactive.com

Back to top Go down

Re: Mouse tracking

Post  Sponsored content


Sponsored content


Back to top Go down

View previous topic View next topic Back to top

- Similar topics

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