2

I've written some code which lets an arbitrary sized rectangle collide with a grid-based terrain setup (for a platformer game). The way I do it is something like this:

For each tile the rectangle intersects with, do:
    Calculate the primary axis that this tile is on with respect to the rectangle
    Calculate the interpenetration of this tile into the rectangle along the primary axis (factoring in previous position offsets from other tiles)
    If this tile is solid, add that interpenetration to a total collision resolution vector
Adjust the rectangle's position by the total calculated collision resolution vector

Which works just fine, except i run into random "hang-ups" as my rectangle gets pulled into the ground just over the border of two tiles, my code decides that it needs to resolve the collision with this new tile by pushing it in the X axis, thus stopping the rectangle's motion unless it is manually pushed out of the terrain to get over it.

I've tried only resolving the collision on one axis at a time (so it ignores any x axis collision resolution if the Y axis resolution is the largest and vice versa), but that results in jittering when the rectangle is being pressed into a corner (as this is a situation that actually needs both axes resolved at once).

In short, what method can I use to fix both of these problems at once?

RCIX
  • 38,647
  • 50
  • 150
  • 207

1 Answers1

0

This is very hard problem because it has infinity interactions in some cases.

To choose between speed and accuracy:

1.add interaction counter for every object (rectangle)

  • before collision detection reset all counters to zero

2.if any collision detected increment counters for all objects in collision

3.if counter value exceeds limit value stop computation of interaction for that object

Beware that this approach can also create some hiccups if forced to but will not hang up.

Spektre
  • 49,595
  • 11
  • 110
  • 380