How to get collision detection of a rotating object in libgdx
I have a game object that will 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 (); // say wt i means borders
with the help of float rot, the object will rotate. since the boundaries are given by a fixed point and since the boundaries have no rotation. my problem is that this object starts to rotate when my charac touches it. something has to happen. thanks u.
+3
source to share
1 answer
You can create a polygon from your rectangle. Then apply rotation to the polygon. To check if a point is inside, simply use polygon.contains (x, y) and to check if it overlaps with another polygon, use the Intersector class. http://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/math/Intersector.html
+3
source to share