programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Jeanne Boyarsky
• Ron McLeod
• Paul Clapham
• Liutauras Vilda
Sheriffs:
• paul wheaton
• Rob Spoor
• Devaka Cooray
Saloon Keepers:
• Stephan van Hulst
• Tim Holloway
• Carey Brown
• Frits Walraven
• Tim Moores
Bartenders:
• Mikalai Zaikin

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

Greenhorn
Posts: 13
• Number of slices to send:
Optional 'thank-you' note:
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.

Bartender
Posts: 5470
212
• 1
• Number of slices to send:
Optional 'thank-you' note:
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

 Consider Paul's rocket mass heater.