Tell me more ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I'm been working on 2D rectangle collision for weeks and still cannot get this problem fixed. The problem I'm having is how to adjust a player to obstacles when it collides. I'm referencing this link. The player sometime does not get adjusted to obstacles. Also, it sometimes stuck in obstacle guy after colliding. Here, the player and the obstacle are inheriting super class Sprite

I can detect collision between the two rectangles and the point by ;

public Point getSpriteCollision(Sprite sprite, double newX, double newY) {
    // set each rectangle
    Rectangle spriteRectA = new Rectangle(
        (int)getPosX(), (int)getPosY(), getWidth(), getHeight());
    Rectangle spriteRectB = new Rectangle(
        (int)sprite.getPosX(), (int)sprite.getPosY(), sprite.getWidth(), sprite.getHeight());

    // if a sprite is colliding with the other sprite
    if (spriteRectA.intersects(spriteRectB)){
        System.out.println("Colliding");
        return new Point((int)getPosX(), (int)getPosY());
    }
    return null;
}

and to adjust sprites after a collision:

// Update the sprite's conditions 
public void update() { // only the player is moving for simplicity
    // collision detection on x-axis (just x-axis collision detection at this moment)
    double newX = x + vx; // calculate the x-coordinate of sprite move

    Point sprite = getSpriteCollision(map.getSprite().get(1), newX, y);// collision coordinates (x,y)
    if (sprite == null) { // if the player is no colliding with obstacle guy
        x = newX; // move
    } else { // if collided
        if (vx > 0) { // if the player was moving from left to right
            x = (sprite.x - vx); // this works but a bit strange    
        } else if (vx < 0) {
            x = (sprite.x + vx); // there's something wrong with this too
        }
    }
    vx=0;
    y+=vy;
    vy=0;
}

I think there is something wrong in update() but cannot fix it. Now I only have a collision with the player and an obstacle guy but in future, I'm planning to have more of them and making them all collide with each other. What would be a good way to do it?

Thanks in advance.

share|improve this question
 
Are there limitations on the way your character can move? What is the movement like? Also, I don't think your method of getting the collision point between two Sprites is correct. It looks like it's just returning the position of one of the Sprites. –  ktodisco Oct 17 at 15:53
 
@ktodisco, I have "void move(int dir)" which if the dir is left vx = -SPEED, if right vx = SPEED and so on in Sprite class. *SPEED is the amount the sprite can move. um.. getSpriteCollision returns a point where two sprites intersect each other. The reason I used the method to get the collision point was easy to implement. but Im willing to get suitable ways to do it too. –  neko Oct 17 at 17:20
 
I mean to say that your algorithm for calculating the collision point appears to be incorrect. You are returning new Point((int)getPosX(), (int)getPosY()), which appears to be the position of one of the Sprites. This is not your point of collision. –  ktodisco Oct 17 at 19:00

Know someone who can answer? Share a link to this question via email, Google+, Twitter, or Facebook.

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.