## rotate with touch

Apprentice
Posts: 9
Joined: 2010.02
Post: #1
hi,
i try to rotate a sprite with touch, works well, but when the rotation-angle is grater than 360Â° the rotation place in the opposite direction, here is my code:

Code:
```-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {     UITouch *touch = [touches anyObject];     CGPoint touchPoint = [touch locationInView:self];     _touchPoint1 = touchPoint;     _p1 = makeVector((self.bounds.size.width) - self.bounds.size.width*0.5, touchPoint.y-(self.bounds.size.height*0.5) , 0.0); } - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {     UITouch *touch = [touches anyObject];     CGPoint touchPoint = [touch locationInView:self];     _touchPoint2 = touchPoint;          _p2 = makeVector( touchPoint.x - (self.bounds.size.width*0.5) , touchPoint.y - (self.bounds.size.height*0.5) , 0.0);          _p1 = normalizeVector(_p1);     _p2 = normalizeVector(_p2);          _angle = angleBetweenVectors(_p1, _p2);     _angle = _angle*360/PI; } . . . draw: glRotatef(_angle, 0.0f, 0.0f, 1.0f); . . .```
Sage
Posts: 1,487
Joined: 2002.09
Post: #2
What does your angleBetweenVectors() function look like? I'm guessing the problem is in there.

Scott Lembcke - Howling Moon Software
Author of Chipmunk Physics - A fast and simple rigid body physics library in C.
Apprentice
Posts: 9
Joined: 2010.02
Post: #3
Skorche Wrote:What does your angleBetweenVectors() function look like? I'm guessing the problem is in there.

sorry, I forgot it:

Code:
```double angleBetweenVectors(const Vector v1, const Vector v2) {     float dot = dotProduct(v1, v2);     return asin( dot); }```
Member
Posts: 446
Joined: 2002.09
Post: #4
Apprentice
Posts: 9
Joined: 2010.02
Post: #5

thanks for suggestions, i moved to quaternions, but the behavior is the same, here what i have:

Code:
```float matrix[16]; _rotationY = createQuaternionFromAxisAngle(0, 0, 1, _angle); convertQuaternionToMatrix(_rotationY, matrix); glMultMatrixf(matrix); . . . render createQuaternionFromAxisAngle(const float x, const float y, const float z, const float degree) {     Quaternion v;          float angle = (float)((degree / 180.0f) * PI);          float result = (float)sin( angle / 2.0f );          v.w = (float)cos( angle / 2.0f );          v.x = (float)(x * result);     v.y = (float)(y * result);     v.z = (float)(z * result);          return v; } convertQuaternionToMatrix(Quaternion q, float *matrix) {     if(!matrix) return;     // First row     matrix[ 0] = 1.0f - 2.0f * ( q.y * q.y + q.z * q.z );     matrix[ 1] = 2.0f * (q.x * q.y + q.z * q.w);     matrix[ 2] = 2.0f * (q.x * q.z - q.y * q.w);     matrix[ 3] = 0.0f;            // Second row     matrix[ 4] = 2.0f * ( q.x * q.y - q.z * q.w );       matrix[ 5] = 1.0f - 2.0f * ( q.x * q.x + q.z * q.z );     matrix[ 6] = 2.0f * (q.z * q.y + q.x * q.w );       matrix[ 7] = 0.0f;            // Third row     matrix[ 8] = 2.0f * ( q.x * q.z + q.y * q.w );     matrix[ 9] = 2.0f * ( q.y * q.z - q.x * q.w );     matrix[10] = 1.0f - 2.0f * ( q.x * q.x + q.y * q.y );       matrix[11] = 0.0f;            // Fourth row     matrix[12] = 0;       matrix[13] = 0;       matrix[14] = 0;       matrix[15] = 1.0f; }```
Apprentice
Posts: 9
Joined: 2010.02
Post: #6
i have got it :-). The problem is not to use quaternions (in my case). I change the angle-calculation to:
Code:
```_angle = angleBetweenVectors(_p1, _p2); _angle = _angle*180/PI; double angleBetweenVectors(const CFXVector v1, const CFXVector v2) {    return (atan2(v1.x*v2.y - v2.x*v1.y   ,   v1.x*v2.x+v1.y*v2.y) ); }```

thanks for the tipps
Member
Posts: 446
Joined: 2002.09
Post: #7
The cool and useful thing about Quaternions or Spinors is that you don't have to screw around with angles and matrixes (at least not until render time). Converting an angle into a quaternion, then converting that to a matrix is completely pointless

Glad you got it working regardless.