Following Mouse...

Hi everyone,

I am absolutely newbie to opengl. I have just started learning opengl es 1.1 & 2.0 on iPhone platform using a book. So here is problem:

I created a simple project. It contains a 2D triangle. What I want to do is that whenever user touches the iphone screen, triangle’s head (top) should start pointing towards the finger touch location. I calculate angle from touch x,y coordinates and rotate triangle with that angle. But triangle doesn’t rotate correctly. Some of the code is in objective-c, but understandable. Below are relevant functions…

-(void) touchedAt:(CGPoint)point{

float radius;
float dX,dY,cX,cY;
float pX=2*(point.x/320)-1; //normalize to opengl coordinates (left corner is -1, right corner is +1)
float pY=2*(point.y/480)-1; //similarly, bottom is -1, top is +1

//below, I calculate center point of triangle.
pY=-pY; //opengl’s y coordinate is inverted relative to iphone’s

radius=sqrtf(dX*dX+dY*dY); //calculate distance between center & touch position

float theta=acosf(pX/radius); //calculate angle of touch position.

theta = theta*180/3.14;   //convert to degrees
NSLog(@"ThetaDegree: %f",theta);

if(pX>0)        //if touch is in right-half of screen then make angle negative




-(void) render{

glClearColor(0.5f, 0.5f, 0.5f, 1.0f);


glRotatef(destAngle, 0, 0, 1);

glVertexPointer(2, GL_FLOAT, sizeof(float)*6, &vertices[0].position[0]);
glColorPointer(4, GL_FLOAT, sizeof(float)*6, &vertices[0].color[0]);

GLsizei count=sizeof(vertices)/(sizeof(float)*6);
glDrawArrays(GL_TRIANGLES, 0, count);




Please help me. I have always wanted to do this stuff. But never found it easy. I have another doubt about this code. Answer this: triangle is in object-space. Touch happens in screen space. How can I translate between these two. Touch angle is an absolute value. But triangle is rotated relatively to its previous position. I only need guidance.

print out the vales for the touch. I don’t think the values will be normalized and instead from 0 to the width/height of the screen. You might want to do the same for the vertex positions. Usually printing out the data can give you a clue and let you see if the values are in the same space. I do it all the time to make sure when things go weird.