Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I am a bit confused about what I need to move a basic square. Should I use a translation matrix or just change the object vertices? Which one is better?

I use a simple vertex shader, gl_Position = myPMVMatrix * a_vertex, along with a VBO.

share|improve this question
add comment

1 Answer

The best approach is to change the value of your matrix. It is a parameter that is sent to the shader once per object and costs very little to update; changing all your vertices would mean updating the VBO with a very high bandwith cost.

share|improve this answer
    
This is the only way it should be done. Your original object exists in its own local space, usually built around (0,0,0), and then it's placed into the world with the translation/rotation matrix. In this way you can display many copies of the original object at many different locations and also very cheaply move the visual copy with a super cheap matrix change. –  Patrick Hughes Aug 14 '11 at 18:25
2  
@Patrick: This is not always true for quads. Since each object has only a tiny number of vertices (4-6), changing the transformation matrix can be a toss-up in terms of bandwidth compared to re-sending the vertices; and changing the transformation requires you to separate draw calls, which can be worse than re-uploading all the vertices if you can get them cheaply transformed (e.g. from the physics system or other game logic) and doing it all in fewer draw calls. r2d2rigo is right that this is probably the best approach; but it's not the only way to do it for 2D games. –  user744 Aug 14 '11 at 21:49
    
Joe just read my mind, sometimes manually updating the VBO is better than drawing single objects; XNA does this with its SpriteBatch class. –  r2d2rigo Aug 14 '11 at 23:02
    
That seems a little obscure an optimization for someone just looking to move stuff, I figure to point out the overwhelmingly common case that will scale so that in two weeks he doesn't hit the "why don't my boxes run fast?" wall and have to rewrite everything. But you are right, there are many ways to accomplish the task. I'll edit that! –  Patrick Hughes Aug 15 '11 at 1:05
    
Follow up: I built a test and tried the matrix versus set-the-vertices race and, at least on my system, matrices win out in small counts while with larger numbers setting the data directly wins. Counts will vary per system, so if you're targeting a baseline configuration that's your test bed. –  Patrick Hughes Aug 18 '11 at 17:45
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.