The best way to define/move Rect in OpenGL

Started by
3 comments, last by Aardvajk 13 years, 6 months ago
Hi, I just want your quick opinion on how you define Rects in your OpenGL/DirectX Apps.

class Vector{public:       float x;       float y;};


class Rect {public:          // Center of the Rect      Vector CenterVertex;              // 4 Vertices of the Rect      Vector Vertex1;      Vector Vertex2;      Vector Vertex3;      Vector Vertex4;                              // Width and Height      float w;      float h;        ~Rect(){}      Rect(){}          Rect(float x, float y, float W, float H){          CenterVertex.x = x;          CenterVertex.y = y;                    w = W;          h = H;                    moveRect();      }                        void moveRect(){          Vertex1.x = CenterVertex.x - w/2;          Vertex1.y = CenterVertex.y + h/2;          Vertex2.x = CenterVertex.x + w/2;          Vertex2.y = CenterVertex.y + h/2;                                        Vertex3.x = CenterVertex.x + w/2;          Vertex3.y = CenterVertex.y - h/2;                    Vertex4.x = CenterVertex.x - w/2;          Vertex4.y = CenterVertex.y - h/2;      }};


then somewhere in the code if you want to move the rect
you just update it's center vertex like

// Update myRect positionmyRect.CenterVertex.x += 1;myRect.CenterVertex.y += 1;// Call update function to update the position on the screenmyRect.moveRect();


is that a good way of actually moving the rect?

Thanks for your time guys!
Juris.L. - Technical Director, Lead Programmer.ANN-Tech. Interactive.

Http://www.ann-tech.com

Advertisement
You could certainly make that work that way, but I'd always be slightly wary of functions where you have to "finalize" things that were started before. Its another step where your data can be out of sync and an easy way to cause bugs.

For example if you move your rect center, but then want to check it for collisions before you call "moveRect", then you're going to have problems as your rect class is now telling you two different things about where your rect is.

Unless you have a need to do it this way, I'd try to make sure that your data is always consistent (move the center and the vertices at the same time).

[size=2]My Projects:
[size=2]Portfolio Map for Android - Free Visual Portfolio Tracker
[size=2]Electron Flux for Android - Free Puzzle/Logic Game
I'd personally be more inclined to have a centre and half-width and half-height and compute the actual positions on the fly as I needed them. As the above poster points out, this abolishes the risk of your data becoming invalid.

Given that we are talking about additions/subtractions, I'd need to see some very compelling evidence that there was a bottleneck before the convenience of having the literal positions pre-computed outweighed not having to remember to call the move method.
Thanks a lot guys! i was just thinking if this is a good way of actually moving Rects around?

2 things we could improve:

1) pre-compute half-width and half-height when the Rect is created/reszed
2) create a func like:

void updateRect(float x, float y);


and update CenterVertex + all 4 Vertices in 1 func.

Can you think of a more efficient/faster way of updating OpenGL Rect?
And one more thing, how do you enable OpenGL glVertex2d(); to use
1 pixel to move istead of this 1.0f format? so for example i do:

myRect.x += 1;


and it moves 1 pixel.

Thanks for your time guys!
Juris.L. - Technical Director, Lead Programmer.ANN-Tech. Interactive.

Http://www.ann-tech.com

Quote:Original post by Annihilator
Can you think of a more efficient/faster way of updating OpenGL Rect?


In the absence of some profiling data, consider it irrelevant and do whatever leads to the most clean and reliable code. The chances that optimisation of this will have any effect on anything is negligible.

This topic is closed to new replies.

Advertisement