Jump to content
• Advertisement

# Rotation question

This topic is 4409 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

## Recommended Posts

I have been working on a good algorithm to detect collisions between convex polygons. The algorithm works perfectly for triangles. So to demonstrate it a little better I want to be able to rotate one of the triangles using one vertex. as its rotation point. Again the rotation works perfectly with one strange effect the triangle shrinks the more it spins. Here is my code for the rotation, does anyone see what I am doing wrong?
void Triangle::Rotate(float radians)
{
D3DXVECTOR2 translation;
translation.x = vertices[0].x;
translation.y = vertices[0].y;
for(int i = 0; i < 3; i++)
{
//Translate the vertex relative to vertex[0]
vertices.x = vertices.x - translation.x;
vertices.y = vertices.y - translation.y;

vertices.x = vertices.x*cos(radians) - vertices.y*sin(radians);
vertices.y = vertices.y*cos(radians) + vertices.x*sin(radians);

vertices.x += translation.x;
vertices.y += translation.y;
}
vertices[3] = vertices[0];
}


Thanks in advance for the help I am using the ID3DXLine interface for drawing the triangles that is why I have four points instead of 3.

#### Share this post

##### Share on other sites
Advertisement
The first problem I see is this:
vertices.x = vertices.x*cos(radians) - vertices.y*sin(radians);vertices.y = vertices.y*cos(radians) + vertices.x*sin(radians);
The original value of vertices.x is overwritten in the first line, therefore the result of the computation in the second line is incorrect. This can be fixed by using local temporaries.

As for the rest, I'm not sure (as the code is out of context), but it looks you're applying cumulative transformations to the triangle, which could cause it to degenerate over time. This could be the cause of the shrinking you mentioned (perhaps along with the incorrect rotation code). For this reason and others, it's usually better to store one copy of the polygon or model in local space, and transform the local copy to the desired world position and orientation each frame.

#### Share this post

##### Share on other sites
That was the problem, I need to save the x value so it could be used to calculate the y value.

Your right it is better to build the object on the local coordinate system and then translate it to world space. The problem I had was that I already implemented the Triangle class before I decided I wanted it to rotate. I had everything else using 2D screen coordinates so I just figured I translate based on vertex[0]. I should probably go through and rebuild the triangle class when I get time or when I use this algorithm in a game.

#### Share this post

##### Share on other sites

• Advertisement
• Advertisement

• ### Popular Contributors

1. 1
2. 2
JoeJ
21
3. 3
4. 4
5. 5
frob
13
• Advertisement

• 9
• 10
• 13
• 9
• 23
• ### Forum Statistics

• Total Topics
632195
• Total Posts
3004712

×

## Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!