Advertisement

Trouble rotating a 2d polygon

Started by August 24, 2000 03:02 PM
2 comments, last by dj9781 24 years, 4 months ago
Im trying to rotate a polygon but Im having trouble. Here's my code:
        

int CPolygon2D::Rotate_Polygon2D(int theta)
{
 	float xr, yr;
	// test for negative rotation angle

	if (theta < 0)
	    theta+=360;
	
	// loop and rotate each point

	for (int curr_vert=0; curr_vert<num_verts; curr_vert++)
        {
	    // perform rotation

	    xr =(float)vlist[curr_vert].x*cos_look[theta] - 
		(float)vlist[curr_vert].y*sin_look[theta];
		
	    yr =(float)vlist[curr_vert].x*sin_look[theta] + 
		(float)vlist[curr_vert].y*cos_look[theta];
		
	    // store result back

	    vlist[curr_vert].x = xr;
	    vlist[curr_vert].y = yr;
		
        } // end for curr_vert

	
	// return success

	return(1);
	
} 

Heres my CPolygon2D class (only the relevant stuff):

class CPolygon2D
{
public:
	int state;        // state of polygon	 

        int num_verts;    // number of vertices	

        int x0,y0;        // pos of center of polygon

  	int xv,yv;        // initial velocity	

        DWORD color;      // could be index or PALETTENTRY	

        VERTEX2DF *vlist; // pointer to vertex list

			
        int Rotate_Polygon2D(int theta);
	int Draw_Polygon2D(UCHAR *vbuffer, int lpitch);

        CPolgon2D(const CPolygon2D &init) //copy constructor

	{
		state       = init.state;
		num_verts   = init.num_verts;  
		x0          = init.x0; 
		y0          = init.y0;
		xv          = init.xv;
		yv          = init.yv;
		color       = init.color;
		vlist       = new VERTEX2DF [init.num_verts];

		for(int index=0; index<init.num_verts; index++)
		     vlist[index] = init.vlist[index];
	}
};        
        
First I make a polygon object and inititialize all its data members like this: CPolygon2D Shape; //global polygon object VERTEX2DF vertices[8] = {33,-3, 9,-18, -12,-9, -21,-12, -9,6, -15,15, -3,27, 21,21}; Shape.state = 1; // turn it on Shape.num_verts = 8; Shape.x0 = SCREEN_WIDTH/2; // position it Shape.y0 = SCREEN_HEIGHT/2; Shape.xv = 0; //initial velocity Shape.yv = 0; Shape.color = 255; // white Shape.vlist = new VERTEX2DF [Shape.num_verts]; for (int index = 0; index < Shape.num_verts; index++) Shape.vlist[index] = vertices[index]; Then I call the function like this: Shape.Draw_Polygon2D(back_buffer, back_lpitch); Shape.Rotate_Polygon2D(5); Only problem is instead of rotating the polygon it just draws a dot on the screen. I know it's not the Draw_Polygon2D fault because if I comment out the rotate it draws the polygon fine. I think maybe it has to do with my class not copying the vertex list but im not sure. Anyone have any ideas what Im doing wrong? Edited by - dj9781 on 8/24/00 3:04:15 PM
How have you defined VERTEX2DF? You reference an x and y point as part of vlist in your Rotate function, but reference vlist as a simple array when initializing and filling it with data.

Also, you should only be initializing vlist in your class constructor. You shouldn''t have to do shape.vlist = new outside the constructor. Pass the number of vertices to the constructor on creation of the object, as well as the data to fill it with. If you need to change the size and data after creation, provide another public method to do that in your Shape class.

I''m also not sure of the logic behind your rotation calculations. If they''re some sort of optimized rotation formula, I''d like to know how it works. My own rotation calculations are pretty basic. I calculate the distance the point is from the origin, and it''s angle with respect from the origin to the positive X axis. I then add theta to this base angle and recalculate my new X and Y coords using the new angle and the distance.

new_X = cos(new_theta) * dist
new_Y = sin(new_theta) * dist

I store all my 2D poly objects in both cartesian and polar coordinates. Rotation is faster using the polar coordinates. Collision detection is easier using the cartesian coordinates. I haven''t found an easy way to calculate line normals using polar coordinates.

One more point, you''ll want to check for new_theta > 360 degrees. I know trig functions handle angle greater then 360 degrees, but your lookup table likely doesn''t.
Advertisement
thanks alot for the help. It turns out I forgot to initialize my lookup tables. So all the vertices ended up being zero. I got the rotation algorithm from tricks of win prog gurus. Its on pg 440. he says its very similar to polar only rotation with conversion to cartesian coordinates at the end.
Sounds like a handy book to have. Does the book discuss how the conversion actually works?

This topic is closed to new replies.

Advertisement