Sign in to follow this  
Photon Man

Rotation error

Recommended Posts

I can't pull off this rotation of quads. I simply try to rotate 4 vertexes of a rect around the center axis of the quad while translating the quad. I got part of it to work. It will rotate fine when I haven't added MousePos. But every time I add MousePos to the x and y, the quad's axis never moves. It is like the radius is being lengthened (or shortened, at times), instead of the axis following the literal center of the quad. I'm using DX 9.0.

	// Set aside the vertices info
	SQUAREVERTEX* vertices;

	//Lock the vertex buffer
	vertexBuffer->Lock(0, 0, (void **)&vertices, NULL);
	
        // Update the vertices to follow the mouse.
		x +=  MousePos1;						
		y +=  MousePos2;

	//Setup vertices				  
	vertices[0].Color = color0;
	vertices[0].x   = x + 1.5f;
	vertices[0].y   = y + 1.5f;
	vertices[0].rhw = 1.0f;
	vertices[0].u   = 0.0f;
	vertices[0].v   = 0.0f;

	vertices[1].Color = color1;
	vertices[1].x   = (float)x + width - 0.5f;
	vertices[1].y   = (float)y + 1.5f;
	vertices[1].rhw = 1.0f;
	vertices[1].u   = 1.0f;
	vertices[1].v   = 0.0f;

	vertices[2].Color = color2;
	vertices[2].x   = (float)x + width - 0.5f;
	vertices[2].y   = (float)y + height - 0.5f;
	vertices[2].rhw = 1.0f;
	vertices[2].u   = 1.0f;
	vertices[2].v   = 1.0f;

	vertices[3].Color = color3;
	vertices[3].x   = (float)x + 1.5f; 
	vertices[3].y   = (float)y + height - 0.5f;
	vertices[3].rhw = 1.0f;
	vertices[3].u   = 0.0f;
	vertices[3].v   = 1.0f;


  // Rotation
  if (rotate != 0.0f)
  {			
	 
      RECT rOrigin;
      static float centerX, centerY;

      //Find center of destination rectangle
      centerX = (float)(x + width)  / 2;
      centerY = (float)(y + height) / 2;

      //Translate destination rect to be centered on the origin
      rOrigin.top    = (float)x  - (centerY);
      rOrigin.bottom = (float)x + height - (centerY);
      rOrigin.left   = (float)y  - (centerX);
      rOrigin.right  = (float)y + width  - (centerX);

      //Rotate vertices about the origin
      vertices[0].x = (float)rOrigin.left * cosf(rotate) -
                                rOrigin.top * sinf(rotate);
      vertices[0].y = (float)rOrigin.left * sinf(rotate) +
                                rOrigin.top * cosf(rotate);

      vertices[1].x = (float)rOrigin.right * cosf(rotate) -
                                    rOrigin.top * sinf(rotate);
      vertices[1].y = (float)rOrigin.right * sinf(rotate) +
                                    rOrigin.top * cosf(rotate);

      vertices[2].x = (float)rOrigin.right * cosf(rotate) -
                                    rOrigin.bottom * sinf(rotate);
      vertices[2].y = (float)rOrigin.right * sinf(rotate) +
                                    rOrigin.bottom * cosf(rotate);

      vertices[3].x = (float)rOrigin.left * cosf(rotate) -
                                    rOrigin.bottom * sinf(rotate);
      vertices[3].y = (float)rOrigin.left * sinf(rotate) +
                                    rOrigin.bottom * cosf(rotate);
							   
      //Translate vertices to proper position
      vertices[0].x += (float)centerX;
      vertices[0].y += (float)centerY;
      vertices[1].x += (float)centerX;
      vertices[1].y += (float)centerY;
      vertices[2].x += (float)centerX;
      vertices[2].y += (float)centerY;
      vertices[3].x += (float)centerX;
      vertices[3].y += (float)centerY;		  
  }									

	x -=  MousePos1;						
	y -=  MousePos2;

	// Unlock the Vertex buffer
	vertexBuffer->Unlock();

// it looks like this
//
//    0--------1
//    |       /|
//    |      / |  
//    |     /  | 
//    |    #   |  
//    |   /    |
//    |  /     |         
//    | /      |      
//    2--------3
//
//    # = Center Axis
//
// But when I add MousePos, I get this:
//
//                        0--------1
//                        |       /|
//                        |      / |  
//                        |     /  | 
//       #                |    /   |  
//                        |   /    |
//                        |  /     |         
//                        | /      |      
//                        2--------3
//
// (given the rect is moving around the # in a big circle)

Share this post


Link to post
Share on other sites
I think you should add the mouse position after you rotate the quad about it`s axis. I hope I get what you`re trying to do...

Share this post


Link to post
Share on other sites
I tried adding the MousePos after the rotation, before rotation, everywhere. I tried putting the

//Setup vertices
vertices[0].Color = color0;
vertices[0].x = x+0.5f;
vertices[0].y = y+0.5f;
vertices[0].rhw = 1.0f;
vertices[0].u = 0.0f;
vertices[0].v = 0.0f;

vertices[1].Color = color1;
vertices[1].x = x+0.5f;
vertices[1].y = y+0.5f;
vertices[1].rhw = 1.0f;
vertices[1].u = 1.0f;
vertices[1].v = 0.0f;

vertices[2].Color = color2;
vertices[2].x = x+0.5f;
vertices[2].y = y+0.5f;
vertices[2].rhw = 1.0f;
vertices[2].u = 1.0f;
vertices[2].v = 1.0f;

vertices[3].Color = color3;
vertices[3].x = x+0.5f;
vertices[3].y = y+0.5f;
vertices[3].rhw = 1.0f;
vertices[3].u = 0.0f;
vertices[3].v = 1.0f;



after the rotation, and that failed too.

All I got, was either nowhere, or the rects never spun.

[Edited by - Photon Man on June 23, 2009 12:04:19 PM]

Share this post


Link to post
Share on other sites
Is this what you're trying to do? I hope I've used your variables correctly, if not you should be able to see where I'm coming from. It's crucial that you do *all* the rotation stuff first, *then* you translate. If you set up the vertices relative to the centre point in the first place, then you can just go ahead and rotate away.


// Set aside the vertices info
SQUAREVERTEX* vertices;

//Lock the vertex buffer
vertexBuffer->Lock(0, 0, (void **)&vertices, NULL);


//Setup vertices
vertices[0].Color = color0;
vertices[0].x = -width*0.5f;
vertices[0].y = -height*0.5f;
vertices[0].rhw = 1.0f;
vertices[0].u = 0.0f;
vertices[0].v = 0.0f;

vertices[1].Color = color1;
vertices[1].x = width*0.5f;
vertices[1].y = -height*0.5f;
vertices[1].rhw = 1.0f;
vertices[1].u = 1.0f;
vertices[1].v = 0.0f;

vertices[2].Color = color2;
vertices[2].x = -width*0.5f;
vertices[2].y = height*0.5f;
vertices[2].rhw = 1.0f;
vertices[2].u = 1.0f;
vertices[2].v = 1.0f;

vertices[3].Color = color3;
vertices[3].x = width*0.5f;
vertices[3].y = height*0.5f;
vertices[3].rhw = 1.0f;
vertices[3].u = 0.0f;
vertices[3].v = 1.0f;


// Rotation
if (rotate != 0.0f)
{
SQUAREVERTEX tempvertex;
float cosr = cosf(rotate), sinr = sinf(rotate);
for(UINT i=0;i<4;++i)
{
//Store temporary vertex position
tempvertex.x = vertices[i].x;
tempvertex.y = vertices[i].y;
//Rotate vertices about the origin
vertices[i].x = tempvertex.x * cosr - tempvertex.y * sinr;
vertices[i].y = tempvertex.x * sinr + tempvertex.y * cosr;
}
}

// Update the vertices to follow the mouse.
for(UINT i=0;i<4;++i)
{
//Translate vertices to proper position
vertices[i].x += (float)MousePos1;
vertices[i].y += (float)MousePos2;
}

// Unlock the Vertex buffer
vertexBuffer->Unlock();


Share this post


Link to post
Share on other sites
Thanks! That is it! I had to mod it a little, but it is up and running. You guys were right about putting the translation after the rotation. Once I did that, it panned out.

I had put this:

x += MousePos1;
y += MousePos2;

// Which I should have put this
vertices[].x += MousePos1;
vertices[].y += MousePos2;

Thanks for pointing that out, shaolinspin.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this