Calculating vertices of a rotated rectangle

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

User avatar
short
ES Beta Backer
ES Beta Backer
Posts: 548
Joined: Thu Apr 30, 2009 2:22 am
Current Project: c++, c
Favorite Gaming Platforms: SNES, PS2, SNES, SNES, PC NES
Programming Language of Choice: c, c++
Location: Oregon, US

Re: Calculating vertices of a rotated rectangle

Post by short »

GyroVorbis wrote:That is the equation for rotating something with respect to the origin. You aren't translating your entity before you rotate, are you?

That must be applied to your vertices before any translation.

Also, check to make sure that your local coordinates (since I'm guessing you've decided to store them) are (assuming a rect):

top left - (-0.5, -0.5)
top right - (0.5, -0.5)
bottom left - (-0.5, 0.5)
bottom right - (0.5, 0.5)

AND NOT

top left - (0, 0)
top right - (1, 0)
bottom left - (0, 1)
bottom right - (1, 1)
I created a rect at 0,0,0 with width=1, height=1 and indeed the coordinates are
top left - (-0.5, -0.5)
top right - (0.5, -0.5)
bottom left - (-0.5, 0.5)
bottom right - (0.5, 0.5)
As for rotating, here's the basic setup of my draw loop:

glPushMatrix()

glOrtho()

// call draw rectangle function (member function of rect class)
draw rectangles:
1) push matrix
2) glTranslatef(m_rectangle->getPosition().x, m_rectangle->getPosition().y, m_rectangle->getPosition().z);
glRotatef(m_rectangle->getOrientation(), 0,0,1);
glTranslatef(-m_rectangle->getPosition().x, -m_rectangle->getPosition().y, -m_rectangle->getPosition().z);

3) draw quad (rectangle)
4) pop matrix
// now back to main draw loop
// immediately draw this line
glBegin(GL_LINES);
glColor3f(0.0f, 1.0f, 0.0f);
glVertex3f(spriteArray[0].m_rectangle->getWorldVertices()[BOTTOM_LEFT].x,
spriteArray[0].m_rectangle->getWorldVertices()[BOTTOM_LEFT].y,
spriteArray[0].m_rectangle->getWorldVertices()[BOTTOM_LEFT].z);
glVertex3f(800,600,0);
glEnd();
glFlush();

glPopMatrix()

// end draw loop

So to answer your question, the only place I call rotate is inside the drawRect function, and i push/pop the matrix at the beginning/end of the function. So I thought I was handling the translations correctly inside the drawRect function within the push/pop calls, but I could be mistaken?

edit: the only place I call rotate (or translate) is inside the drawRect....
My github repository contains the project I am currently working on,
link: https://github.com/bjadamson
User avatar
cypher1554R
Chaos Rift Demigod
Chaos Rift Demigod
Posts: 1124
Joined: Sun Jun 22, 2008 5:06 pm

Re: Calculating vertices of a rotated rectangle

Post by cypher1554R »

GyroVorbis wrote:I really can't argue whether the transformation matrix math is happening in the CPU or GPU (I've been told that the CPU is responsible), but it is really independent of my argument. On something like a Dreamcast, a PSP, or an embedded system it is happening on the CPU (and will eventually happen there anyway for physics). You don't have cute little glAnything() to handle your transforms for you. My method boasts platform independence.
The matrix itself in CPU, per vertex in GPU.
I don't use cute little glAnything, but have my own matrix class and each function for each transformation type (like DX), use glMultMatrix(myMatrixClass.m) and draw models with pointers using glDrawElements(...) <- talk about speed here.
GyroVorbis wrote:If you are doing it your way and use dynamic memory allocation, you could trash the heap (and have really bad memory holes). Or even if you did it with the stack, you still have less room for persistent data (that lingers for more than one frame). So why the hell not just store your vertices?
Let's just agree to disagree. :)
The PhysX API does everything for me.. okay!! Now leave me alone! I just want puppies, warm and fuzzy, that cuddle against me and just love me no matter what. :(
GyroVorbis wrote:
cypher wrote:instead of trig. approach uses matrices,
Oh, I'm sorry, I didn't realize that a rotation matrix was independent of ... trigonometry. XD
Oh, come on! I was talking about it being practical. :) You don't really think I'm that stupid.
GyroVorbis wrote:Although I have to admit that all-in-all, you aren't exactly going to see a performance difference between both of our approaches (on a PC with OpenGL). If you prefer your way, there's nothing wrong with it. I'm just arguing that my way is better for practical reasons. :D
But that's exactly what I'm trying to say about my way ;D

But then I read your signature and think "Ahaaa.." :lol:
User avatar
avansc
Respected Programmer
Respected Programmer
Posts: 1708
Joined: Sun Nov 02, 2008 6:29 pm

Re: Calculating vertices of a rotated rectangle

Post by avansc »

short here is how i do it.

this is in ortho view..

Code: Select all

void drawGeom(cRigidBody *body)
{
	glPushMatrix();
	glColor3f(0.0f, 1.0f, 0.0f);
	
	glTranslated(body->getLocation_wc().x, body->getLocation_wc().y, 0);
	glRotated(body->getOrientation_wc()*180/kPi, 0, 0, 1);
 
	list<cConvex>::const_iterator it;
	list<cVec>::const_iterator vec;
 
	for(it = body->getGeom().begin(); it != body->getGeom().end();it++)
	{
		glBegin(GL_LINE_LOOP);
		for(vec = (*it).begin();vec != (*it).end();vec++)
		{
			//printf("%f\n", (*vec).x);
			glVertex3f((*vec).x, (*vec).y, 0);
		}
		glEnd();
	}
	glPopMatrix();
 }

im not sure why you translate twice. the push translat + whatever else then POP should send you back to where you were before you pushed.
also, why is the glFlush there?
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
User avatar
short
ES Beta Backer
ES Beta Backer
Posts: 548
Joined: Thu Apr 30, 2009 2:22 am
Current Project: c++, c
Favorite Gaming Platforms: SNES, PS2, SNES, SNES, PC NES
Programming Language of Choice: c, c++
Location: Oregon, US

Re: Calculating vertices of a rotated rectangle

Post by short »

avansc, that seems how to draw it.

But how do you get the actual world coordinates in ortho view?

I'm not using the world coordinates to draw the rectangle, but rather for collision detection.

I am using the local coordinates with a translation and rotation for drawing the rectangle.
My github repository contains the project I am currently working on,
link: https://github.com/bjadamson
User avatar
avansc
Respected Programmer
Respected Programmer
Posts: 1708
Joined: Sun Nov 02, 2008 6:29 pm

Re: Calculating vertices of a rotated rectangle

Post by avansc »

short wrote:avansc, that seems how to draw it.

But how do you get the actual world coordinates in ortho view?

I'm not using the world coordinates to draw the rectangle, but rather for collision detection.

I am using the local coordinates with a translation and rotation for drawing the rectangle.
okay. so lets assume you have a array of vecors what make up the geometry of the object
vec[4] geom
and the geometry being a rect you have it as
-1,-1
1,-1
1,1
-1,1

i'll also assume you have a array the same dimentions are geom but just called geom_wcd

okay
so lets also assume that the object also has a vector that stores its wc (worldcoordinate),
vec wc;
and also i'll assume you have a float/double that is its wo(world orientation) and i'll assume you have it stored as radians

so we will declare a function called calcWC()

Code: Select all

void calcWC()
{
    for(int a = 0;a < num_verts;a++)
    {
        //x' = x cos f - y sin f
        this->geom_wc[a].x = this->geom.x[a] * cos(this->wo) - this->geom.y[a] * sin(this->wo);
        //y' = y cos f + x sin f
        this->geom_wc.y[a] = this->geom.y[a] * cos(this->wo) + this->geom.x[a] * sin(this->wo);
        //also dont forget to translate to wc
        this->geom_wc[a].x += this->wc.x;
        this->geom_wc[a].y += this->wc.y
    }
}

now you should have the correct world coordinates that you can use for collisions.

if you just wanted to have wc and not worry with local coords you would first traslate to origin, rotate, and minus the origin_translation vector.
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
User avatar
short
ES Beta Backer
ES Beta Backer
Posts: 548
Joined: Thu Apr 30, 2009 2:22 am
Current Project: c++, c
Favorite Gaming Platforms: SNES, PS2, SNES, SNES, PC NES
Programming Language of Choice: c, c++
Location: Oregon, US

Re: Calculating vertices of a rotated rectangle

Post by short »

Ok, here's what I have been able to come up with:

Code: Select all

for(int i = 0;i<4;i++)
        {
            xNOT = spriteArray[0].m_rectangle->getPosition().x;
            yNOT = spriteArray[0].m_rectangle->getPosition().y;

            o = (GLfloat)DEG_TO_RAD( (-1)*spriteArray[0].m_rectangle->getOrientation() );

            x = spriteArray[0].m_rectangle->getLocalVertices()[i].x;
            y = spriteArray[0].m_rectangle->getLocalVertices()[i].y;

            xNEW = xNOT+(x-xNOT)*cosf(o)+(y-yNOT)*sinf(o);
            yNEW = yNOT-(y-yNOT)*sinf(o)+(y-yNOT)*cosf(o);

          glBegin(GL_LINES);
              glColor3f(0.0f, 0.0f, 1.0f);
              glVertex3f(0,0,0);
              glVertex3f(xNEW,yNEW,0);
          glEnd();
        }
I made my first youtube video of what was happening from the above code, to hopefully show what is going on and maybe clarify exactly what I may be doing wrong:

http://www.youtube.com/watch?v=7ixX2GtaWcM
My github repository contains the project I am currently working on,
link: https://github.com/bjadamson
Post Reply