0

I am currently working on a game in which i need pixel perfect collision with objects. The only obstacle i had gotten to however, is with actually getting a mask of the object. I thought about rendering a sprite of an object (which is properly scaled and transformed) to FrameBuffer, which then i'd convert to a pixmap, but i had no luck doing it (tried many methods, including this)

So, my question is: Is there a way of rendering a single sprite to a pixmap in LibGDX?

Community
  • 1
  • 1
themorfeus
  • 277
  • 1
  • 3
  • 17

1 Answers1

3

Sounds like putting the horse behind the cart. A Sprite is a region of a Texture, which in turn is an image loaded from file aka Pixmap. If you just want to load the image from file then you can do: new Pixmap(Gdx.files.internal("yourfile.png"));. You can also scale and transform your coordinates without rendering to a FBO first.

That said; getting the Pixmap of a FrameBuffer is as "simple" as taking a screenshot while the FBO is bound, because it is exactly that.

fbo.begin();
Gdx.gl.glClear(...);
... //render to the FBO
Pixmap pixmap = ScreenUtils.getFrameBufferPixmap(0, 0, fbo.getWidth(), fbo.getHeight());
fbo.end();

Note that this will look like it is up-side-down.

But you are probably only interested in the pixel data, so you might as well skip the Pixmap step in that case and use the ScreenUtils.getFrameBufferPixels method.

However, that said; using the pixel data for collision detection is most likely not the best solution for whatever it is you are trying to achieve. Instead I'd advise to look into other options, for example have a look at this tool which can be used to convert your images into a collision shape.

trinity420
  • 670
  • 7
  • 19
Xoppa
  • 7,983
  • 1
  • 23
  • 34
  • +1 for the tool. But loading the texture to a pixmap wont work for me, since the image i am loading is 4 times as small, and wont reatin the rotation that i am giving to a sprite. I will check out the ScreenUtils method, though – themorfeus Dec 08 '15 at 21:27
  • Sounds like you're making an assumption. Pixel perfect means exactly that: pixel perfect. Any transformation you do when you render your sprite, you can do also when you dont render it (and more). – Xoppa Dec 08 '15 at 21:30