SDL rotation

Feb 3, 2010 at 10:44pm
I want to know how to rotate a shape, I declare my shapes like this:

1
2
3
4
5
shape foo[]={
     0,0,//vertex 0
     100,100,//vertex 1
     0,100 //vertex 2
};


I was wondering how to rotate it?

(I am using vertices so that when I move to 3d it will be easier(hopefully))

Also while I was looking up rotation I ran into this: http://en.wikipedia.org/wiki/Vertex_%28geometry%29#Definitions
Last edited on Feb 3, 2010 at 10:47pm
Feb 3, 2010 at 10:49pm
Rotate all the vertices around it's center using sine/cosine functions
Feb 3, 2010 at 10:49pm
So what does this have to do with SDL?
Feb 3, 2010 at 11:03pm
@Bazzy
This what I had before:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
void rotate(shape* poly, double radians){
	//count the number of vertices ...
	int numVert;
	int i = 0;
	while(poly->vertices[i]->x != NULL) ++i;
	numVert = i;

	i = 0;
	while(i < numVert){
		int x, y ;
		x = poly->vertices[i].x * cos(radians) - poly->vertices[i].y * sin(radians);
		y = poly->vertices[i].x * sin(radians) + poly->vertices[i].y * cos(radians);
		poly->vertices[i].y = y;
		poly->vertices[i].x = x;
		++i;
	}	
}


@helios
Actually it has very little to do with SDL, I guess I wanted to say I was using graphics.
Feb 3, 2010 at 11:11pm
All you need to do to rotate a point is provide a pivot point. Then you convert the rectangular coordinates to polar in relation to that point, add the rotation to the angle component, then convert back to rectangular.
Feb 5, 2010 at 3:37am
Ok, so first conversion from rectangular to polar:

r = √ (x2 + y2)
r = 2*(x*2 + y*2)

θ = atan( y / x )
theta = atan(y/x)

Is that correct so far?




Feb 5, 2010 at 4:17am
Uh... You do realize that x^.5 is not the same as 2*x, right? And that formula is completely wrong, anyway.
r=(x1-x2+y1-y2)^.5
t=atan2(y1-y2,x1-x2)

Determining an angle from two sets of coordinates is such a common operation that atan2() exists just to do that. It even handles the cases when !x. If !x && !y, the function fails.
Last edited on Feb 5, 2010 at 4:17am
Feb 5, 2010 at 6:09am
The discussion of rotation is incomplete without quaternion.
Feb 5, 2010 at 7:06pm
Sorry, I am horrible at geometry, thankyou.
Feb 5, 2010 at 7:06pm
Sorry, I am horrible at geometry, thankyou.
Topic archived. No new replies allowed.