Search for triangle points after rotation

I am working on a (fairly) simple 2D project in OpenGL. This is some kind of asteroid clone.

The ship is basically an isosceles triangle of height H, and the base has a length of H / 2.

The way I have done so far is simply to save the center point (CP) of the triangle and then calculate the end positions of the vertices on the fly. "Point" of the ship (vectors x, y) (CP.x, CP.y + H / 2). The other two points are (CP.X - H / 4, CP.Y - H / 2) and (CP.X + H / 4, CP.Y - H / 2).

To get the ship in the right direction, I first call glRotate at the current angle of rotation.

This part is working fine, but I am facing conflict detection problems. Currently, I am trying to perform collision detection of a triangular plane, but for this I first need to find out the actual points of the tops of the vessel after rotation. I tried using trigonometry to calculate these points, however I failed.

I tried using the cosine rule to find the distance between a non-rotating triangle and a triangle after rotation. To give an example, as follows, as I tried to calculate the position of the “pointy” vertices after rotation:

//pA is a vector struct holding the position of the pointy vertex of the ship (centerPoint.x, centerPoint.y + height / 2) //Distance between pA and the rotated pointy vertex - using the cosine rule float distance = sqrt((2 * pow(size / 2, 2)) * (1 - cosf(rotAngle))); //The angle to the calculated point float newPointAngle = (M_PI / 2) - rotAngle; float xDif = distance * cosf(newPointAngle); float yDif = distance * sinf(newPointAngle); //Actually drawing the new point glVertex2f(pA.x - xDif, pA.y - yDif); 

Any idea what I can do wrong?

+4
source share
3 answers

Thanks for the help guys, but I think these explanations were too technical for me. However, you made it clear to me that there is no special case for a triangle (which, looking back, I should have known), so I tried my hand at searching and after several methods, I found one that worked for me.

A post from estain on the GameDev forums did the trick. To quote his post (sorry for c & p, but may be useful for someone else who is facing a similar problem):

Without going into general solutions and mathematics (as mentioned above in these posters and in numerous articles), I could give you an example of how to solve the problem of “turning point A around point B by C degrees”.

Now. First of all, as I described in a previous article, a point located on the X axis, the distance L from the beginning, rotates degrees C around the origo

x = L * cos (C)

y = L * sin (C)

Similarly, the formula for the perpendicular vector is x = -y | y = x, which means that the point located on the Y axis (again, L from origo) will be rotated to C using the formula

x = - L * sin (C)

y = L * cos (C)

As shown in the above image, the final solution is the sum of the rotations of the projected vectors, so we can get the formula

x '= x * cos (C) - y * sin (C)

y '= y * cos (C) + x * sin (C)

... but you already knew that, right? the problem is that this formula only revolves around ori. So, we need to move the coordinate system, which we rotate around to Oriu, rotate, and then go back. This can be done quickly with complex numbers or in general solutions with matrices, but we will stick with vector math on this to make it simple.

first step; move the starting point.

x '= Ax - Bx

y '= Ay - By

second step, turn

(C) = (Ax-Bx) * cos (C) - (Ay-By) * sin (C) (C) = (Ay-By) * cos (C) + (Ax-Bx) * sin (C)

third and final step, return the coordinate frame

x '' '= x' '+ Bx = (Ax-Bx) * cos (C) - (Ay-By) * sin (C) + Bx

y '' '= y' '+ By = (Ay-By) * cos (C) + (Ax-Bx) * sin (C) + By

And presto! we have our own rotation formula. I will give this to you without all these calculations:

Rotation of point A around point B by angle C

Ax '= (Ax-Bx) * cos (C) - (Ay-By) * sin (C) + Bx

Ay '= (Ay-By) * cos (C) + (Ax-Bx) * sin (C) + By

If you follow me here (and I'm a pretty lousy teacher, sorry if you didn’t), you can say that the order in which you perform these operations is very important. Try mixing steps 3 and 1 and see the difference in the formulas that you get.

Good luck and all!

+4
source

Rotation calculations must be centered at the origin, so you may need to first translate the coordinates so that the center of rotation is aligned with the origin.

Then use the new points to get the rotated coordinates:

 x1 = x cos f - y sin f y1 = y cos f + x sin f 

where f is the angle of rotation.

Then you translate the new coordinates to where you started (the reverse side of the first translation.

Check out this article for some diagrams and explanations.

+2
source

Calculating new points is relatively simple. Assume that x and y are the coordinates of some point on the triangle (i.e., Vertices) relative to the rotation point (or center).

You must convert the coordinates to a square shape and an angular component:

 float dist, angle; dist = (x*x) + (y*y); angle = atan(abs(x)/abs(y)); // a little more code is required for the different quadrants that the angle could be in. 

Then turn:

 angle+=rotation_angle; 

Then go back:

 new_x = sqrt(dist)*sin(angle*pi/180); new_y = sqrt(dist)*cos(angle*pi/180); 
+1
source

All Articles