Propeller Spin

I'm almost done with my project. The last thing I need to do is make my propeller stay on my airplane and spin like propellers do. I just can't figure out how to make it rotate and do that. It's very frustrating. Can some one help?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
class Airplane
{
private:
	//attributes
	double posX;
	double posY;
	double posZ;
	double w;
	double h;
	double d;
	float color1 = (rand() % 100) * .01;
	float color2 = (rand() % 100) * .01;
	float color3 = (rand() % 100) * .01;
	float rotate = 0.0;
	float propSpin;
	float planeAngle;

public:
	//constructor
	Airplane(double px, double py, double pz, double _w, double _h, double _d)
	{
		posX = px;
		posY = py;
		posZ = pz;
		w = _w;
		h = _h;
		d = _d;
		propSpin = 0.0;
		planeAngle = 15.0;
	}

	//methods
	void drawAirplane()
	{
		glPushMatrix();
		glRotatef(planeAngle, -1, 1, 0);

		//body
		glPushMatrix();
		glRotatef(90.0, 0, 0, 1);
		glRotatef(rotate, 1, 0, 0);
		setColor(color1, color2, color3);
		drawRectangle(posX, posY, posZ, w, h, d);
		glPopMatrix();

		//wings
		glPushMatrix();
		glRotatef(90.0, 1, 0, 0);
		glRotatef(-rotate, 0, 0, 1);
		setColor(color1, color2, color3);
		drawRectangle(posX - 200, posY + 300, posZ - 500, w, h, d);
		glPopMatrix();

		//tail lat
		enable3d();
		glPushMatrix();
		glRotatef(0.0, 0, 1, 0);
		glRotatef(rotate, 0, 1, 0);
		glScalef(1.0, 0.3, 0.75);
		setColor(color1, color2, color3);
		drawRectangle(posX - 250, posY + 700, posZ + 100, w, h, d);
		glPopMatrix();

		//tail lon
		glPushMatrix();
		glRotatef(90.0, 1, 0, 0);
		glRotatef(-rotate, 0, 0, 1);
		glScalef(1.0, 0.3, 0.5);
		setColor(color1, color2, color3);
		drawRectangle(posX - 250, posY + 1000, posZ - 750, w, h, d);
		glPopMatrix();

		//glPushMatrix();
		//glRotatef(propSpin, 0, 1, 0);

		//prop lat
		glPushMatrix();
		glRotatef(90.0, 0, 1, 0);
		glRotatef(rotate, 0, 1, 0);
		glScalef(0.5, 0.5, 0.5);
		setColor(color1, color2, color3);
		drawRectangle(posX - 800, posY + 400, posZ - 150, w, h, d);
		glPopMatrix();

		//prop long
		glPushMatrix();
		glRotatef(90.0, 0, 1, 0);
		glRotatef(90.0, 0, 0, 1);
		glRotatef(rotate, 1, 0, 0);
		glScalef(0.4, 0.4, 0.4);
		setColor(color1, color2, color3);
		drawRectangle(posX + 300, posY + 750, posZ - 100, w, h, d);
		glPopMatrix();

		//glPopMatrix();
		glPopMatrix();

		rotate++;
		propSpin++;
	}
};


The couple lines of code that are in // are my attempt to fix the problem, they however didn't work.
Try putting your rotation calls for the propeller after the rotation calls for aligning it with the rest of the plane. Also, to do it like this, you should change your drawRectangle calls to just be the offsets and just throw a glTranslatef at the start of the function to align the plane. Remember, the glRotatef calls rotate around the origin, rather the plane's axis, which isn't what you want for the propeller.
I'm not quite understanding exactly how to change it. What do you mean by offset? We haven't covered those. And yeah glRotatef only does origin, how do I make it so its local?
This is what I mean:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
//...
void drawAirplane() /* const */ {
    glPushMatrix();
        glTranslatef(posX, posY, posZ);
        glRotatef(planeAngle, -1, 1, 0);

        glPushMatrix();
            glRotatef(...);
            glRotatef(...);
            glColor3f(color1, color2, color3);
            drawRectangle(0, 0, 0, w, h, d);
        glPopMatrix();

        glPushMatrix();
            // ...
            drawRectangle(-200, 300, -500, w, h, d);
        glPopMatrix();

        // etc.
    glPopMatrix();
}


I can't actually test if this works, because I don't have your 'enable3d' or 'drawRectangle' functions, but fiddle around with the values (you might need to translate by -posX).

EDIT:
Noticed you update inside your draw method. In general, probably better to move that to a seperate update function - it helps with establishing an even game flow using a game loop.
Last edited on
Topic archived. No new replies allowed.