1
\$\begingroup\$

My platformer has both rectangular tiles and right-triangle slopes with rectangular hitboxes. My sprites are also rectangular.

Collision with only rectangular tiles works well. Even if there are multiple rectangular tiles, collision still works pretty well.

Sprites can collide with individual slopes correctly as well, however, they collide at a different point. In order for the character's feet to be walking down the slope instead of hovering over it, the collision point for slopes is the bottom-center point. For rectangles, it is either corner.

The problem is moving from a rectangular tile onto a sloped one. The sprite resolves by the corners first, since it is resting atop a rectangular tile. However, the center point is resting above the slope, and the sprite will not move onto the slope until it is entirely moved off of the rectangular tile.

Here's a diagram:

A diagram.

How can I reliably use two different collision points for different tiles in a way that allows sprites to walk between rectangular and sloped tiles?

\$\endgroup\$
2
  • \$\begingroup\$ This question has been asked a lot on here. Did you try a search? gamedev.stackexchange.com/search?q=slope+collision \$\endgroup\$
    – bogglez
    Jul 17, 2014 at 21:41
  • \$\begingroup\$ Yeah, I've looked pretty extensively. I haven't really found too many sources talking about the differences between rect and triangle collision; specifically, the checks of different collision points for different shapes. \$\endgroup\$
    – Celarix
    Jul 17, 2014 at 21:55

2 Answers 2

1
\$\begingroup\$

Consistency is crucial for smooth collision detection. I recommend using the bottom center point for rectangular collision as well as slope collision when colliding against the ground.

For colliding against walls, you can use points that are in the center left and center right of your sprite.

Another way to think of it is as though your sprite has a diamond collision shape (i.e., a square rotated 45 degrees).

\$\endgroup\$
1
  • \$\begingroup\$ I like this method, but I do have one question - what if a sprite has to "catch a corner" of a tile - that is, it needs to collide with a tile on its own corner because it can't move fast enough to collide with its own center in time? \$\endgroup\$
    – Celarix
    Jul 17, 2014 at 21:56
0
\$\begingroup\$

Your problem statement is not consistent...

You say you don't want rectangle corners to cross, so you have check collisions by corners.

But when there's a slope, you want your guy to descend the slope, and let rectangle corners cross. The "desired position" rectangle shows this crossing.

A solution is to keep checking the corners, and have your collision shapes not necessarily match your drawing shapes. Draw your slope beginning further to the right of the collision slope.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .