voidRy
voidRy

Reputation: 684

Sprite collision detection center region of sprite

I'm working on sprite collision. There is a ball and a bucket, until the ball touches the middle of bucket, point shouldn't be counted.And if it touches the handles of bucket it should bounce back.

Any idea how can this be achieved?

Upvotes: 1

Views: 260

Answers (1)

Shihab Uddin
Shihab Uddin

Reputation: 6931

You can also take a custom Bucket class.Then attach a rectangle onto the center of a bucket. Then Invisible the rectangle. The rectangle should be the child of this bucket.

public class Bucket extends Sprite {

private Rectangle checkRectangle;

public Bucket(float pX, float pY, float pWidth, float pHeight,
        ITextureRegion pTextureRegion,
        ISpriteVertexBufferObject pSpriteVertexBufferObject) {
    super(pX, pY, pWidth, pHeight, pTextureRegion,
            pSpriteVertexBufferObject);
    // Declare rectangle object
    checkRectangle = new Rectangle(41, 54, 4, 4,
            getVertexBufferObjectManager());
    // set invisible
    checkRectangle.setVisible(false);
}

@Override
public void onAttached() {
    super.onAttached();
    super.onAttached();
    if (hasParent()) {
        attachChild(checkRectangle);
    }
}

}

When the ball touches rectangle which is in the center of a bucket you can count point. For, this you need to check in your game scene like:

if (aBucketObj.checkRectangle.collidesWith(aBallSprite)) {

        // do What you want
    }

And finally you can check the collision only for the bucket itself.

if (aBucketObj.collidesWith(aBallSprite)) {

        // // do What you want 
    }

Hope, you it works for you.:)

Upvotes: 3

Related Questions