Game rectangles & OpenGL vertices

Posts: 30
Joined: 2010.08
Post: #1
In my game I tried adding rotation to one of the objects, but this led to a problem: Before all my textured rectangles were at the origin, with the corner at (0,0). But to rotate them I realized they would need to be centered around the origin. But all my game rectangles use the upper-left corner of the rectangle as their origin, so translating the matrix by the game rectangle's coordinate results in the center being at where the corner should be. I guess I could just translate by rect.origin.x + rect.size.width / 2, but... I don't know this doesn't feel as clean as it was before somehow. Is there any other solution to this?
Quote this message in a reply
Posts: 86
Joined: 2008.04
Post: #2
If you are using textured quads, it is probably best to render them centered to facilitate rotation. This should not be hard to do, just use square verts, e.g.:
static const GLfloat squareVertices[] = {
-SZ, -SZ,
SZ, -SZ,
-SZ, SZ,

Also remember that typically you apply both a translation and a rotation to your game objects - and the order matters.
glTranslate followed by glRotate results differently than glRotate followed by glTranslate.

Hope that helps...
Quote this message in a reply
Posts: 30
Joined: 2010.08
Post: #3
Yea that's how I was doing it. I think the thing I didn't like was how I was working with matrices. I'm using legacy-free OpenGL code so no glTranslate and friends. I'm writing my own, with a 2d game in mind, but I don't think I have it right. OpenGL always multiplies the matrix for even glTranslate, but isn't 64 multiplications and 48 additions a waste when usually all you need is two additions? Also some of my game objects only need a rectangle, others need a rectangle and a velocity, others need that plus a rotation, so I'm not sure if I should make those combinations all different structs. But there's no point in calculating rotation for something that never rotates.
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  squares[] and sorting vertices into arrays... mikey 3 3,711 Sep 11, 2009 11:21 AM
Last Post: mikey
  Skewed Vertices with Quaternions, Matrices, and Good Ol' Trig Oddity007 4 4,651 May 12, 2009 03:13 PM
Last Post: Oddity007
  Finding Outer Vertices of Shapes Nick 18 11,263 Nov 27, 2006 07:01 AM
Last Post: Nick