Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get collision detection of a rotating object in libgdx

Tags:

android

libgdx

I have a game object which is going to rotate.

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;

public class Object extends AbstractGameObject{// AbstractGameObject is a class to apply physics on the object
private TextureRegion object;
float rot;
public Object(){
    init();
}
private void init() {
    dimension.set(2f, 2f);
    object = Assets.instance.object.object;//it just retrieves the texture region from assets class
    // Set bounding box for collision detection
    bounds.set(0,0, dimension.x, dimension.y);
}
@Override
public void render(SpriteBatch batch) {
    TextureRegion reg = null;
    final float degressPerSecond = 120.0f;
    rot = (rot + Gdx.graphics.getDeltaTime() * degressPerSecond) % 360;
    reg = object;

    batch.draw(reg.getTexture(),
            position.x, position.y,
            origin.x, origin.y,
            dimension.x, dimension.y,
            scale.x, scale.y,
            rot,
            reg.getRegionX(), reg.getRegionY(),
            reg.getRegionWidth(), reg.getRegionHeight(),
            false, false);
}

}

Rectangle bounds = new Rectangle();//to say wt i mean by bounds

by the float rot the object is going to rotate. by as the bounds is set to fixed point and as bounds doesn't have a rotation. my problem is wen this object starts rotating when my charac touches it. something should happens. thank u.

like image 411
Sunny Sabhanam Avatar asked Nov 24 '25 15:11

Sunny Sabhanam


1 Answers

You can create a polygon from your rectangle. Then, apply the rotation to the polygon. To check if a point is inside just use polygon.contains(x,y) and to check if it is overlapping with other polygon, use the Intersector class. http://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/math/Intersector.html

like image 62
Boldijar Paul Avatar answered Nov 27 '25 04:11

Boldijar Paul



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!