game collision check when rectangle moves from one position to another if it misses collision in the distance travelled

StackOverflow https://stackoverflow.com/questions/20691571

Question

I know this question is similar to others, but if I have a rectangle bounded game object. Which moves position. How can I check along the line if it intersects with any items in between?

In an extreme scenario. [x = 2, x = 1, width = 1, height = 1]A moves to [x = 4, y = 1, width = 1, height = 1]. Where the rectangle B exists at [3,1,0.5,0.5] it would get missed out.

I have read about scalar and cross product but they are single lines if i read correctly. This is due to Android game development on slow devices with low frame rate. I am getting it falling into objects. I check intersects using this code below.

public boolean testIntersection(GameVector lowerLeftMain, float mainWidth, float            mainHeight, GameVector lowerLeftCollider,
float colliderWidth, float colliderHeight){

    boolean intersect = false;

    if(lowerLeftMain.x < lowerLeftCollider.x + colliderWidth+0.08f && //checks left collision
            lowerLeftMain.x + mainWidth > lowerLeftCollider.x-0.08f && //checks right collision
            lowerLeftMain.y < lowerLeftCollider.y + colliderHeight+0.08f &&//checks top collision
            lowerLeftMain.y + mainHeight > lowerLeftCollider.y-0.08f )//checks bottom collision
        intersect = true;
    return intersect;
}

Please can someone point me in the right direction should I give up on rectangles and concentrate on ray cast line collision style?

Thanks in advance.

Était-ce utile?

La solution

Thanks for the links great links will post my code to help others in the future.

My Separating Axis Theorem in java. Only to test if overlaps. I went for this algorithm due to efficiency and potential to see the min and max overlap vectors.

public GameVector[] getVertices(GameObject obj){
    final GameVector topLeft = new GameVector( obj.mPosition.x-0.06f - (obj.mWidth/2), obj.mPosition.y+0.06f +(obj.mHeight/2) );
    final GameVector topRight = new GameVector(   obj.mPosition.x+0.06f + (obj.mWidth/2),obj.mPosition.y+0.06f +(obj.mHeight/2) );
    final GameVector bottomLeft = new GameVector(  obj.mPosition.x-0.06f - (obj.mWidth/2), obj.mPosition.y-0.06f -(obj.mHeight/2));
    final GameVector bottomRight = new GameVector(  obj.mPosition.x+0.06f + (obj.mWidth/2), obj.mPosition.y-0.06f -(obj.mHeight/2));

    //order here matters
    GameVector[] vertices = { topLeft, topRight, bottomRight, bottomLeft }; 
    return vertices;
}

public GameVector[] getAxis(GameObject shape){

    GameVector[] vertices = getVertices(shape);

    GameVector[] axes = new GameVector[vertices.length];
    // loop over the vertices
    for (int i = 0; i < vertices.length; i++) {
        // get the current vertex
        GameVector p1 = vertices[i];
        // get the next vertex if i+1 == vertices length set back to vertices [0]
        GameVector p2 = vertices[i + 1 == vertices.length ? 0 : i + 1];
        // subtract the two to get the edge vector
        GameVector edge = p1.subtract(p2.x, p2.y);
        // get either perpendicular vector
        GameVector normal;
        //get the left side normal of the vector due to clock wise positions
        normal = new GameVector(edge.y, -edge.x);//edge.perp();
        axes[i] = normal;
    }
    return axes;
}

public float dotProduct(GameVector a, GameVector b){
    float dp = a.x*b.x + a.y*b.y;
    return dp;
}

public class Projection {

    private final float min;
    private final float max;

    public Projection(float min, float max) {
        this.min = min;
        this.max = max;
    }

    public boolean doesOverlap(final Projection other) {
        return !(this.min > other.max || other.min > this.max);

    }
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top