Mouse tracking
3 posters
Mouse tracking
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.
mordenkaim- Posts : 21
Join date : 2009-05-19
Re: Mouse tracking
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:
You need these definitions and function for that to work:
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
Permissions in this forum:
You cannot reply to topics in this forum
|
|