Skip to main content
Tweeted twitter.com/StackGameDev/status/941679445383041024
deleted 28 characters in body
Source Link

Background


I have the following png file that contains the player sprite.

As with any png file, the non-colored pixels are transparent.

player.png

(Width: 16px; Height: 16px)

Currently, I can erect a rectangle (collision-bounds), using java.awt.Rectangle around the image using the image's width and height, as seen below.

image-based width and height bounds

I would like to modifycrop the image so that the width and height are changed to the edges of the colored portion of the image, much like what is shown below.

non-zero-based pixel width and height bounds

This will allow for more accurate collision-detection between entities or objects in a game.

I want to construct a method that handles this modification. So, I created a method to calculate this dynamically, as seen below.

//Method calculates how far in from [top, bottom, left and right] 
//the [non-zero] or [transparent] pixel-data of an image are
//in order to construct a new image that starts at the edges
//of the colored portion of the image.
public BufferedImage adjustImage(BufferedImage image)
{
    BufferedImage result = null;
    


    return result;
}

Problem


However, I am uncertain as to the algorithm to use to achieve the result above.

Can anyone give a step-by-step on how to detect these bounds and how to implement such a fuction as described above?

Background


I have the following png file that contains the player sprite.

As with any png file, the non-colored pixels are transparent.

player.png

(Width: 16px; Height: 16px)

Currently, I can erect a rectangle (collision-bounds), using java.awt.Rectangle around the image using the image's width and height, as seen below.

image-based width and height bounds

I would like to modify the image so that the width and height are changed to the edges of the colored portion of the image, much like what is shown below.

non-zero-based pixel width and height bounds

This will allow for more accurate collision-detection between entities or objects in a game.

I want to construct a method that handles this modification. So, I created a method to calculate this, as seen below.

//Method calculates how far in from [top, bottom, left and right] 
//the [non-zero] or [transparent] pixel-data of an image are
//in order to construct a new image that starts at the edges
//of the colored portion of the image.
public BufferedImage adjustImage(BufferedImage image)
{
    BufferedImage result = null;
    


    return result;
}

Problem


However, I am uncertain as to the algorithm to use to achieve the result above.

Can anyone give a step-by-step on how to detect these bounds and how to implement such a fuction as described above?

Background


I have the following png file that contains the player sprite.

As with any png file, the non-colored pixels are transparent.

player.png

(Width: 16px; Height: 16px)

Currently, I can erect a rectangle (collision-bounds), using java.awt.Rectangle around the image using the image's width and height, as seen below.

image-based width and height bounds

I would like to crop the image so that the width and height are changed to the edges of the colored portion of the image, much like what is shown below.

non-zero-based pixel width and height bounds

This will allow for more accurate collision-detection between entities or objects in a game.

I want to construct a method that handles this modification dynamically, as seen below.

//Method calculates how far in from [top, bottom, left and right] 
//the [non-zero] or [transparent] pixel-data of an image are
//in order to construct a new image that starts at the edges
//of the colored portion of the image.
public BufferedImage adjustImage(BufferedImage image)
{
    BufferedImage result = null;
    


    return result;
}

Problem


However, I am uncertain as to the algorithm to use to achieve the result above.

Can anyone give a step-by-step on how to detect these bounds and how to implement such a fuction as described above?

Source Link

How do I detect a given sprite's bounds automatically using Java?

Background


I have the following png file that contains the player sprite.

As with any png file, the non-colored pixels are transparent.

player.png

(Width: 16px; Height: 16px)

Currently, I can erect a rectangle (collision-bounds), using java.awt.Rectangle around the image using the image's width and height, as seen below.

image-based width and height bounds

I would like to modify the image so that the width and height are changed to the edges of the colored portion of the image, much like what is shown below.

non-zero-based pixel width and height bounds

This will allow for more accurate collision-detection between entities or objects in a game.

I want to construct a method that handles this modification. So, I created a method to calculate this, as seen below.

//Method calculates how far in from [top, bottom, left and right] 
//the [non-zero] or [transparent] pixel-data of an image are
//in order to construct a new image that starts at the edges
//of the colored portion of the image.
public BufferedImage adjustImage(BufferedImage image)
{
    BufferedImage result = null;
    


    return result;
}

Problem


However, I am uncertain as to the algorithm to use to achieve the result above.

Can anyone give a step-by-step on how to detect these bounds and how to implement such a fuction as described above?