0
\$\begingroup\$

I'm working on a simple Box2D platformer, and I was wondering, what is the best shape to use for the player's box? I'm currently using a circle with a radius of 10, but that doesn't work well, as when the player collides with a corner, they can get stuck on it, or won't be able to move forwards properly and it feels really awkward. What should I use instead?

I've heard ovals work well for this kind of thing, but there's no easy way to create an oval in B2D without creating 3 circles on top of each other. I've also seen rectangles used, but aren't those less accurate?

\$\endgroup\$
2
  • 1
    \$\begingroup\$ This will depend a lot on your game, so it's hard to answer in general what every 2D platformer "should" use. Instead, if you can edit your question to include an image of your current player setup and typical level geometry where you're encountering a problem, we can suggest ways to fix that particular problem for your specific game. \$\endgroup\$ – DMGregory Jan 17 '17 at 4:28
  • 1
    \$\begingroup\$ Hey I have edited your post to remove the second question as it was a different issue. You can create a new question post and ask it separately! \$\endgroup\$ – Vaillancourt Jan 17 '17 at 11:06
3
\$\begingroup\$

Every collision shape has it's pros and cons:

The axis aligned bounding box is very cheap to do collision against, but it's very rough and it's not possible to rotate it, thus it may need to be recalculated every frame.

The circle is nice if your shape rotates, because you don't need to recalculate the shape every frame, but it's the least exact.

Convex polygons are very good for complex shapes, but they are costy in terms of CPU usage.

Concave polygons are the best in terms of match. They can define any shape, but they are very very hard to do collisiom against them.

Choosing a shape for your character depends on how complex collisions you want. For a platformer or RPG you may want to stick with the AABB, but if you want a game, where precision is key, then you should go with one of the polygon types.

\$\endgroup\$
2
\$\begingroup\$

Using primitives like rectangles and circles is obviously the fastest, but don't fit every object exactly so you end up with less accuracy. If you need some more accuracy you can use multiple primitives to define the collision area of an object. A much more expensive way is to create a polygonal shape for your character but it's wise to first check with a box and if there is collision with it you then check again for the polygonal object.

A nice tool to create a polygonal object.

The Intersector class has all kind of handy methods for checking collisions on different types such Poly vs Point, Poly vs Poly, Ray vs Triangles, etc.

\$\endgroup\$
1
\$\begingroup\$

As mentioned by others, this depends a lot of the proportions of a character. In a sprite editor you can try drawing over various hit box shapes to see how they look. How "accurate" the shape is will depend on how close it comes to covering the entire shape, without some parts sticking out of the shape.

An oval might work well around the head, but miss the side of the feet (making it hard to stand half on a ledge). If your character has a giant head for example, you might instead want to use two separate boxes (one for the head and the body).

A rectangle is a simple collision box that roughly fits the shape of "human" characters. However, if you find it getting caught on corners, you can actually make the hitbox smaller than the character. This means part of the player's sprite will be able to overlap with the level geometry (like the player's arms), but the core (torso) is what will get caught. (The bottom of the rectangle should be lined with the bottom of the feet).

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.