Berk Kaya
Berk Kaya

Reputation: 470

Triangle rotation causes deformation

I would like to rotate my triangle but there are some problems.

Its default form: enter image description here

I am rotating it with my arrow keys but as you see it has some deformations on shape of triangle: enter image description here

Here is my code:

typedef struct {
point_t pos;    // position of the triangle
float   angle;  // view angle 
float   r;
} weapon_t;


void drawPlayer(weapon_t tw) {

glBegin(GL_TRIANGLES);
glColor3f(0.1, 0.2, 0.3);
glVertex2f(tw.pos.x, tw.pos.y);
glVertex2f(tw.pos.x + 150 * cos(tw.angle * D2R), tw.pos.y + 100 * sin(tw.angle * D2R) + 8);
glVertex2f(tw.pos.x + 150 * cos(tw.angle * D2R), tw.pos.y + 100 * sin(tw.angle * D2R) - 8);
glEnd();
}

void onTimer(int v) {

glutTimerFunc(TIMER_PERIOD, onTimer, 0);

if (right) {
    if (weapon.angle != -45)
        turnWeapon(&weapon, -3);
}
if (left) {
    if (weapon.angle != 45)
        turnWeapon(&weapon, 3);
}

Any idea guys?

Upvotes: 1

Views: 123

Answers (1)

user3692497
user3692497

Reputation:

I don't know where you got your formulas from but they are wrong. To rotate a 2D vector anti-clockwise around angle x you can use the rotation matrix [cos(x), -sin(x) ; sin(x), cos(x)] (you can prove this easily with exp(i*x) = cos(x) + i*sin(x)). You want to rotate the vectors [150, 108] and [150, 92] if you multiply those by the rotation matrix you get [150*cos(x) - 108*sin(x), 150*sin(x) + 108*cos(x)] and [150*cos(x) - 92*sin(x), 150*sin(x) + 92*cos(x)].

Translated into code this looks like this:

float c = cos(tw.angle * D2R);
float s = sin(tw.angle * D2R);
glVertex2f(tw.pos.x + 150*c - 108*s, tw.pos.y + 150*s + 108*c);
glVertex2f(tw.pos.x + 150*c - 92*s, tw.pos.y + 150*s + 92*c);

Upvotes: 3

Related Questions