2D Collision Detection issue with SAT. Entity moving up and down.

Started by
5 comments, last by Gtadam 3 months, 2 weeks ago

I'm having trouble with my newly developed collision detection routine.

It's based on a SAT guide I found at http://dyn4j.org/2010/01/sat/.

Everything

Advertisement
public Vector2D[] updateAxes(final Vector2D[] vertices) {
for(int i = 0; i < vertices.length; i++){
final Vector2D vector1 = vertices[i];
final Vector2D vector2 = vertices[i+ 1 == vertices.length ? 0: i+1];

final Vector2D tempVector = axes[i].set(vector1);
final Vector2D edgeVector = tempVector.subtract(vector2);
final Vector2D normalVector = edgeVector.perp();

axes[i] = normalVector.normalize();
}
return axes;
}

Everything works good as long as i dont rotate objects. But if i rotate the player 25 degrees the char starts to move up and down in a jittering movement.

public static MinimumTranslationVector calOverlapSAT(final Shape shape1, final Shape shape2){
float overlap = 9999999f;
Vector2D minVector = null;
final Vector2D[] axes = shape1.axes;

for(int i = 0; i < axes.length;i++){

final Projection proj1 = shape1.project(shape1.wsVertices, axes[i]);
final Projection proj2 = shape2.project(shape2.wsVertices, axes[i]);

if(!proj1.overlap(proj2)){
return null;
}
else{
	float currOverlap = proj1.getOverlap(proj2);
if (currOverlap < overlap) {
	overlap = currOverlap;
	minVector = axes[i];
}
}
}

Don't know if it helps, can attach the full code if someone wants to take a look at it.

    public void handleOverlap(final Entity that){
        MinimumTranslationVector overlap = Shape.getOverlapSAT(this, that, false);

       if(overlap != null){
           this.x = x - overlap.min.x*overlap.overlap;
           this.y = y - overlap.min.y*overlap.overlap;

           if(mVelocity.y != 0f){
               mVelocity.y = 0;
               if(overlap.min.y * overlap.overlap > 0f) { //feet
                   mIsOnGround = true;
               }
           }
        }
    }

Given an MTV vector how should i use it? Should i always subtract it from the current x and y?

I have now identified what seems to be the problem. The issue is that my player collides with multiple other entities. They are ground tiles. It means the players collision routine is can be called multiple times each iteration, but with different coliders. I'm not sure but i think it makes the player bounce a bit.

This topic is closed to new replies.

Advertisement