# Which is a better way for moving 3d objects in OpenGL?

I'm learning OpenGL, and I'm a bit confused. I've just learned about projection and modelview matrices which can be used for moving things. This is the first way, which is convenient if I want to move everything. The other way is to calculate the new coordinates, and rebuffer the data. The second one seems a bit inefficient, because I have to rebuffer every single moved objects 60 times per frame. This has an advantage: I know the exact coordinates, and I can check collision, and other stuffs. If I move my character with manipulating the matrix, how do I know if I hit the wall or not? Should I make the same calculation as the shader, and get the 'real' position?

Which is more practical and efficient solution?

• Aug 8, 2019 at 18:53

## 1 Answer

Mix of the both: move object for game logic, move the origin/camera (modify modelview matrix) for drawing.

As you said, recalculating the vertex buffer every frame is not going to be efficient, to put it lightly. Imagine a 3D RTS game with hundreds of units on the map. One way is to create vertex array for each unit. You'll need to prepare and send [number of vertices] x [number of units] values to GPU. Other way is to store the model in GPU RAM buffer, and tell the GPU to draw the contents of the buffer at specific coordinates/rotation (so you only send 4x4 matrix for each unit, regardless of its model's number of vertices).

First one should be fine for a small game, but if you scale it up, second is the way to go (especially if you start using shaders, uniforms, skeletal animation and other nice looking stuff).

• If I understand you well, you are saying the following: let's say I have a [2;2;2] position. I want it to be on the [3;3;3] position, so I store the second on cpu side, the first one stays at the gpu's memory, and I pass a matrix to the shader to look like it's on the [3;3;3]?
– user130653
Aug 8, 2019 at 19:05
• Exactly. Objects in your game are made of position, colliders (or anything else you need for physics), and model/sprite. Each frame you update position and resolve physics on CPU, and then tell the GPU to draw the model/sprite on specific position. If your model has its origin at the feet, and by objects position you mean its feet, then this way you'll be able to easily draw it correctly. Aug 8, 2019 at 19:27