How does offset in Hitbox work?


(Jean) #1

So, when we create a Hitbox we need to specify new Hitbox(width, height, xOffset, yOffset).

I’m interested in this since my character has a big head(its larger than the rest of the body) and there is something above the head of the character that I want to ignore aswell.

The image above shows what I want to do: my image is the whole square, but I want that it’s hitbox to be reduced only on top and on both horizontal borders of it. I tried playing around with the values in the Hitbox constructor, but nothing what I tried worked. Any solutions for this? (even if I don’t use Hitbox object?)


(Mike Evmm) #2

You’ll probably want to specify the width and height of the beige box, and an x and y offset like this:


(Jean) #3

Ty for the better image, I was at the work so I did this stupid and simple image. Will edit my with your image.


(Mike Evmm) #4

I’m confused? I was actually trying to provide a solution. Here’s what I meant in code:

setHitbox(img.width-X_BORDER*2, img.height-Y_BORDER,X_BORDER,Y_BORDER);

imgbeing your entity’s image (the whole square).


(Jean) #5

Ah LOL Sorry for not understanding you So, seeing your solution, the offset in the 3rd and 4th parameter works only on the top and on the left of the image right?


(Mike Evmm) #6

No problem! Yes, offsets are calculated from the top left, if I’m not mistaken.


Who came up with the idea of an inverted Y axis?
(Ultima2876) #7

If that isn’t right you can try using negative values. I’m not sure if this is still the case, but when I was super active with FlashPunk I recall origins and offsets being somewhat inconsistent (and we didn’t want to change it as it would break backwards compatibility).


(Zachary Lewis) #8

Making Sense of Origins & Offsets

There’s nothing mystical or incorrect with how origins and offsets are calculated and dealt with in FlashPunk.

By default, an image will be loaded with an origin of (0, 0) positioned at (0, 0).

Setting the image’s x and y will offset the image.

Setting the image’s origin doesn’t update the images position values, but will change where it is drawn. If I center the image’s origin to the center of the image and leave the image position at (0, 0), it will look like this.

Applying the same offset as before, you get this.

Setting the x and y position of a hitbox works similarly, moving its origin (the top-left corner of the hitbox) by the given offset.


(Mike Evmm) #9

My image was insanely one-uped. Great to know though, I’d never been quite sure how that worked either!


(Ultima2876) #10

I was pretty sure there was one of them that worked the other way around (positive values moved it the opposite way to the others). Guess not :stuck_out_tongue:


(Zachary Lewis) #11

Setting a positive offset to an image appears to move it up and left. Setting a negative position to an image appears to move it up and left. That may be what you’re thinking of.


(Ultima2876) #12

Most likely. Glad those diagrams make it clear what’s going on - maybe they should be added to the tutorials under an advanced topic?


(Jean) #13

That helped me a lot now, everything is working with my big headed jumping character. It was ridicously confusing, since FP.console stills draws my hitbox from the origin after setting centerOriginXY().

The other problem it’s because I had to do more adds and minus since Height, Width and it’s halfs were getting me some trouble getting stuck on things.

Just a last question… I believe that I still at 1.6.? version of FP, is there any problem I could have with those simple functions?


(Zachary Lewis) #14