Question

Title says it all pretty much. I want the hitbox to be slightly smaller than the player character sprite, so he'd be hugging the walls a bit closer (even overlapping a bit) and not dying to bullets that only barely hit him.

I am able to change the height and width of the rectangle but not the bottom left x and y coordinates with this declaration in my character's class:

Rectangle bounds = new Rectangle((getPosition().x+0.45f), (getPosition().y+1.25f), 0.52f, 0.64f);

By doing that, the rectangle gets smaller from the right edge, so it becomes displaced as the left edge remains unaffected. The height can be adjusted in this way as needed, of course.

There remains an empty space between the character and the wall when approaching a wall from the right. I would like the collision box to be more narrow as to make the character able to "hug the walls" no matter from which direction she approaches the wall. Other than that, everything works well so far.

Was it helpful?

Solution

You can change the size of the rectangle and center it like this:

//suppose originalwidth, and originalheight as the starting size, and that you want to set it 40% smaller.
bounds.setSize(originalwidth*0.6F, originalheight*0.6F);

bounds.setPosition( bounds.x+(originalwidth-bounds.width)/2F, bounds.y+(originalheight-bounds.height)/2);

And if you dont want to change the height (it would look like the feet go through the floor), then just do not affect the height:

bounds.width = originalwidth*0.6F

bounds.x += (originalwidth-bounds.width)/2F;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top