1

It is not much information out there how to do a 2d camera in Pyglet. I assume its not a good idea to move all sprites so I look for something similar to a Surface in Pygame that I could move instead. But the is no such thing in Pyglet? Is this the way it should be done in openGl? or am I missing something important? I don't know what the gl command do but it is working.

def on_draw(self):
    #camera start

    glMatrixMode(gl.GL_PROJECTION)
    glLoadIdentity()
    glOrtho(self.camera.x, self.camera.x2, self.camera.y, self.camera.y2, -1, 1)

    #camera end
    self.clear()
    self.batch.draw()# draw stuff
Johannes
  • 25
  • 3
  • You should move all sprites. Doing it in software is typically not a good idea though so you should do it on the GPU (via OpenGL). You move sprites by multiplying their position with a [GL_MODELVIEW](https://www.khronos.org/opengl/wiki/Viewing_and_Transformations#How_does_the_camera_work_in_OpenGL.3F) matrix (view is basically the 'camera'). I don't remember how the old pipeline works. – Ted Klein Bergman Oct 12 '21 at 11:17
  • As I said, you're using the old, fixed pipeline. I'd suggest trying the programmable version instead. It's a bit harder but gives you more control and is more explicit. Then you can follow along with [Learn OpenGL](https://learnopengl.com) (although they use C++). I made a simple step by step that goes over the programmable pipeline in pyglet [here](https://github.com/Naxaes/OpenGLpython/tree/master/tutorial), although it's just code and small comments, so you need to go over the basics elsewhere. – Ted Klein Bergman Oct 12 '21 at 11:24
  • then ill move the sprites. I thought it would be more cpu intensive thx – Johannes Oct 12 '21 at 14:11
  • It will be quite CPU intensive for many sprites, which is usually why you do it on the GPU. It can be done on the CPU (unless you have thousands of sprites), as shown in [this popular pygame example](https://stackoverflow.com/questions/14354171/add-scrolling-to-a-platformer-in-pygame/14357169#14357169). – Ted Klein Bergman Oct 12 '21 at 14:46
  • Pyglet sprite has an update function do change its position. ill try that, should be no problem. I used that camera on some of my Pygame projects but it is as you say a bit limited if confronted with bigger tile maps. If just the documentations were clearer what is hardware accelerated and what not. Your Code ist interesting but hard to understand for me because of other libraries I never used. Ill try to code like I did with Pygame and try to get more performance with pypy – Johannes Oct 12 '21 at 15:06

1 Answers1

2

There is a camera example in the pyglet/examples folder: https://github.com/pyglet/pyglet/blob/pyglet-1.5-maintenance/examples/window/camera.py

EDIT: 2.0 was released and requires a slight change to camera. See the master branch instead: https://github.com/pyglet/pyglet/blob/master/examples/window/camera.py

Charlie
  • 680
  • 4
  • 11