how to fit a body to a sprite avoiding transparent space?

i´ve been looking arround and i can´t get a way to do that in code, a way would be to modify the .png files before loading them on the memory, but i supose it has to be some way to detect the transparent space wich is arround and modify the sprite to fit his size :-\

What do you mean by loading .png files into memory?

You mean OpenGL or Java2D or something else?

i mean to remove that part manually with an image editor software, what i´m looking for is a way to do it with code

So you want to crop an Image according to the sprite’s minimum bounds?

  • Iterate over the source image’s pixels, recording the coordinates of the left-most, top-most, right-most & bottom-most non-transparent pixels.
  • create a new image, sized according to these coordinates and copy over the source image, offset according to the left/top coords.

How are you loading your .png file right now?

i´m loading it from a texture atlas created with texture packer, i set all the regions i need to create an animation, the thing is that i want to change the player body dimension dynamicly acording to the actual region i´m using… so… i scale the image to use it with box2d, and then i destroy the fixture to create a new one every frame with the new size, but … i don´t know why and from where comes an area arround between the sprite and the body… i´ve been some days looking arround to figure out if i´m missing something with any result.

i´ve checked the region size without finding anything wrong so i guess i am doing something wrong setting up the shape acording to the sprite width and height ???

ok i found what was my problem, i had to divide width and height by 2

Don’t do that, if your character changes size dynamically, simply attach fixtures to represent the larger sizes and set them as sensors.

Say the player can shrink to half his size, have 2 fixtures. 1 for normal size and 1 for half size that is a sensor but default, whenever the player shrinks, set the full size one to sensor and the half size one to not sensor.

Creating a new fixture every frame is a waste of power, especially if you are doing it for more than 1 entity.

You´re right, in fact i updated my code to just change the fixture shapes acording to the sprite size, I thing that´s a better practice and it will perform better thanks for your help :wink: