[libGdx]rotating group element cause rectangle misplacement

Hello everyone,

I want to know how to check collision between elements inside a group and other element of the games, in other word, i want to know how to draw a rectangle around elements of the group, cause so far every time i try the rectangle is always in the wrong position, i tried using stageToLocalCoordinates but the result was always messed up (sometimes i get the rectangle in the correct position but when i move the group the rectangle seems to have a “mirror effect” {always in the opposit direction} )

thank you

i have added some code in this pastebin, maybe it helps a little bit ?

okay so i made some progress and the problem seems to appear only when am rotating the group, but when am moving it in the X or Y then everything works fine, to give you more insight, here is a GIF that represent the issue

http://s4.postimg.org/bp4rmg159/group_Trble.gif

and here is how am doing it

this is method is to setup the stage, group and actor :

private void createThePaddle() {
		// Create a stage
		stage = new Stage(new StretchViewport(game.WIDTH, game.HEIGHT));

		// Create a group and add it to the stage.
		group = new Group();
		stage.addActor(group);

		
		Sprite sprite = new Sprite(circleImg);
		// this is what the paddle class take (i'll post it below)
		redPad = new Paddle(redCol, "red", sprite, new Vector2(0, 0), 1f);
		// adding the actor 
		group.addActor(redPad);
		// setting the position relative to the stage
		redPad.setPosition(0, 0);
		// setting the group position in the middle of the screen
		group.setPosition(RgbX.WIDTH / 2, game.HEIGHT / 2);
	}

this method is to update the group :
(if i enable the commented part and disable the rotation everything works fine)

private void groupUpdate() {
		if (Gdx.input.isKeyPressed(Keys.LEFT)) {
			group.rotateBy(rotSpeed);
			//group.setX(group.getX() - 100 * Gdx.graphics.getDeltaTime());
		}
		if (Gdx.input.isKeyPressed(Keys.RIGHT)) {
			group.rotateBy(-rotSpeed);
			//group.setX(group.getX() + 100 * Gdx.graphics.getDeltaTime());
		}
		if (Gdx.input.isKeyPressed(Keys.UP)) {
			//group.setY(group.getY() + 100 * Gdx.graphics.getDeltaTime());
		}
		if (Gdx.input.isKeyPressed(Keys.DOWN)) {
			//group.setY(group.getY() - 100 * Gdx.graphics.getDeltaTime());
		}
	}

and this is the render method :

@Override
	public void render(float delta) {
		// TODO Auto-generated method stub
		Gdx.gl.glClearColor(0, 0.0f, 0.0f, 0);
		Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

		groupUpdate();

		stage.act(Gdx.graphics.getDeltaTime());
		stage.draw();

		Vector2 pos = redPad.localToStageCoordinates(new Vector2(0, 0));

		float[] poly = rectangleToVertices(pos.x, pos.y,
				redPad.sprite.getWidth(), redPad.sprite.getHeight());
		
		
		shape.setProjectionMatrix(stage.getCamera().combined);// this doesn't make any difference
		shape.begin(ShapeType.Line);
		shape.polygon(poly);
		shape.end();

		
	}

and finally this is the Paddle class :

package com.aladine.rgbx;

import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.scenes.scene2d.Actor;

public class Paddle extends Actor {

	public Color color;
	public String type;
	public Vector2 position;
	public Sprite sprite;
	public float scale;
	public Rectangle rectangle;

	public Paddle(Color color, String type, Sprite sprite, Vector2 position,
			float scale) {

		this.color = color;
		this.type = type;
		this.position = position;
		this.sprite = sprite;
		sprite.setColor(color);
		sprite.setSize(sprite.getWidth() * scale, sprite.getHeight() * scale);
		sprite.setOriginCenter();

	}

	@Override
	public void draw(Batch batch, float alpha) {
		sprite.draw(batch);
	}

	public Rectangle getBound() {

		return sprite.getBoundingRectangle();
	}

	public Color getColor() {
		return color;
	}

	public void setColor(Color color) {
		this.color = color;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public Vector2 getPosition() {
		return position;
	}

	public void setPosition(Vector2 position) {
		this.position = position;
	}

	public Sprite getSprite() {
		return sprite;
	}

	public void setSprite(Sprite sprite) {
		this.sprite = sprite;
	}

}

NOTE: i tried using polygon instead of rectangle but i always got the same result (someone told me rectangles doesn’t rotate {am not looking to rotate the rectangle anyway})
thank you

up please

I don’t really see what is your problem. Is the problem that your group is being rotated around bottom-left corner? Well, you just have to change pivot/center point of the group to rotate it’s children around.

Finally ;D !! someone has decided to interfere!!
Thnx a lot man!!!

And nope, that is not the problem. if you have a couple of minutes to read this blog post of mine then you will understand exactly what am willing to do.
but let me re-explain again :slight_smile:

say that i have a group named G, this group contain 3 objects A, B and C
the goal is to rotate both 3 objects of the group as they are a single object, basically they are all rotating around the group pivot no matter where they are in space, and if you have visited the link above, the Group feature of Scene2D has completely solved my problem.
BUT for some strange reason, i cannot find a way to have a rectangles for collision, remember the A,B and C object ?
i want each one of them to have Ar, Br and Cr (r for rectangle) that behave exactly like A, B and C, i simply need rectangles around these objects to check for collision.

i’ve been searching a lot to solve this problem, and am still out of luck, am actually starting to thing to drop the Group approach and use Box2D and one of it’s grouping features ? (joints or whatever)

thank you again and have a great day

I know it’s not what are you asking but, why don’t you use circle collisions instead of rectangles?

because i want to know why the rectangle are acting like this, i am 100% sure that am missing a very simple thing, there is a gap between positioning the graphics of the actor and the collision rectangles, and am waiting for someone with enough experience to tell me what am messing ::slight_smile: