# How to rotate a 2d square using maths equations through OpenGL

Michelle Ruth

Greenhorn

Posts: 13

posted 1 year ago

So I need to create a square and make it rotate when the letter R is pressed and then make it stop when the letter R is pressed again. I need make it rotate using a maths equation that I can't figure it out myself right this moment and would greatly appreciate any help.

Code for square:

Code to control the square with Key Events

In the Square code there is a method to write an equation to rotate it but I can't figure that part out and would appreciate if anyone can shed some light on this and help me out. Thanks in advance.

Code for square:

Code to control the square with Key Events

In the Square code there is a method to write an equation to rotate it but I can't figure that part out and would appreciate if anyone can shed some light on this and help me out. Thanks in advance.

Piet Souris

Rancher

Posts: 1630

35

posted 1 year ago

- 1

hi Michelle,

you can rotate your square, no problem, but apart from the rotation angle, you will also need a center of rotation.

Since your square can have any four points as its vertices, I don't think you can assume this center to be always (0,0).

Let us know if you need any help with getting that matrix set up.

Another way to make your square rotate in opengl is to simply let opengl do all the work. You would then specify

a rotation matrix and use that in your opengl 'display' method, or even easier, use the glRotated(angle, x, y, z)

or the glRotatef(angle, x,y,z) functions.

Greetz,

Piet

you can rotate your square, no problem, but apart from the rotation angle, you will also need a center of rotation.

Since your square can have any four points as its vertices, I don't think you can assume this center to be always (0,0).

Let us know if you need any help with getting that matrix set up.

Another way to make your square rotate in opengl is to simply let opengl do all the work. You would then specify

a rotation matrix and use that in your opengl 'display' method, or even easier, use the glRotated(angle, x, y, z)

or the glRotatef(angle, x,y,z) functions.

Greetz,

Piet