Libgdx, Box2D, Box2dLight

Hello there

I have a little problem with Box2dLight. I want it to rotate to the direction where the hand is pointing (follow the mouse pointer).

So I made a little gif to show ya how it’s behaving.

As u can see, it’s pointing opposite direction :l.

So this is how I create my light:



			light = new ConeLight(ray,100, Color.WHITE, 200,arm.getPosition().x,arm.getPosition().y, 360, 20);
			light.attachToBody(arm);
			light.setColor(Color.BLUE);

 

This is how I rotate the arm:



public void rotate() {
		Vector3 worldCoords = camera.unproject(new Vector3(Gdx.input.getX(),Gdx.input.getY(), 0));
		Vector2 vec2 = new Vector2(arm.getWorldCenter().x - worldCoords.x,arm.getWorldCenter().y- worldCoords.y);
		vec2.set(vec2.x, vec2.y);
float angle = (float) Math.atan2(arm.getWorldCenter().y - worldCoords.y, arm.getWorldCenter().x - worldCoords.x);
                arm.setTransform(new Vector2(arm.getPosition().x, arm.getPosition().y), angle);
}


And this is where rotating is happening:



	public boolean mouseMoved(int screenX, int screenY) {
		
		mx = Gdx.input.getX();
		my = Gdx.input.getY();

			light.setPosition(arm.getPosition().x, arm.getPosition().y);

			System.out.println(Math.toDegrees(arm.getAngle()));

		rotate();
		
		return false;
	}


So what I want, is that the licghtcone would point always to a mouse pointer. I read that, when attaching light to a body, it will automatically rotate to bodys direction. I have tried like changig the light direction and so, but it always seems to rotate just 180 and to an opposite direction. If u have any ideas, I would be glad :).

Cheers

Tumppu

Rotate 180… 1pi

Sorry, I see you tried that.

Have you tried just putting the body to the opposite point? Like at width - X and height - y? Seems that would be a way of butchering things into what you want?

(Sorry, I don’t have a lot of experience with box2d, it didn’t produce an effect I had found desirable and ripped it out, just that’s something I would try)

Hey, and thanks for your input.

Sorry, really didn’t understand what u mean. Do u mean the light at the opposite point? My char is built up from 4 bodies (body, head, weapon & arm). Or do u mean the arm should be attached like the way u discribed? Sorry I’m little confused :). Wierd thing thou, the arm is working nicely and rotating all the 360 degrees. Can’t just understand why the light won’t behave the same way.

Cheers

Tumppu

Also tried somethin:



	public void rotate() {
		Vector3 worldCoords = camera.unproject(new Vector3(Gdx.input.getX(),Gdx.input.getY(), 0));
		Vector2 vec2 = new Vector2(arm.getWorldCenter().x - worldCoords.x,arm.getWorldCenter().y- worldCoords.y);
		vec2.set(vec2.x, vec2.y);
		
		float angle = (float) Math.atan2(arm.getWorldCenter().y - worldCoords.y, arm.getWorldCenter().x - worldCoords.x);
		
		arm.setTransform(new Vector2(arm.getPosition().x, arm.getPosition().y), angle);
		
		float newAngle = (float) Math.PI / 180 * angle;
		light.setDirection(newAngle);
		System.out.println(newAngle);
		
		
		//System.out.println(angle);
		/** double angle2 = arm.getAngle();
		angle2 = normalRelativeAngle(angle2);
		System.out.println(angle2);
		light.attachToBody(arm); **/
		
	    /**  final float angle3 = arm.getAngle();

	      final float sin = MathUtils.sinDeg(angle3);
	      final float cos = MathUtils.cosDeg(angle3);
	      final float x = arm.getPosition().x + 1 * cos - 1 * sin;
	      final float y = arm.getPosition().y + 1 * sin + 1 * cos;
	      light.setPosition(x, y);
	      light.setDirection(angle3); **/
		
		
			
	}


Output is intredasting



-0.04592159
-0.04593443
-0.045944814
-0.0459527
-0.045958187
-0.045961812


Just can’t get it work :confused:

Cheers

Tumppu

newAngle = angle + (float)Math.PI

Hi there, okay thanks :). I will test it out after a few hours when I’m back home.
I will report here how’s it going :).

Cheers

Tumppu

Damn, still not working :l

code:



	public void rotate() {
		Vector3 worldCoords = camera.unproject(new Vector3(Gdx.input.getX(),Gdx.input.getY(), 0));
		Vector2 vec2 = new Vector2(arm.getWorldCenter().x - worldCoords.x,arm.getWorldCenter().y- worldCoords.y);
		vec2.set(vec2.x, vec2.y);
		
		float angle = (float) Math.atan2(arm.getWorldCenter().y - worldCoords.y, arm.getWorldCenter().x - worldCoords.x);
		
		float newAngle = angle + (float)Math.PI;
		arm.setTransform(new Vector2(arm.getPosition().x, arm.getPosition().y), newAngle);
		
		light.attachToBody(arm);
		//light.setDirection(arm.getAngle() + 135);
		light.setDirection(newAngle);
		

		
		//System.out.println(angle);
		/** double angle2 = arm.getAngle();
		angle2 = normalRelativeAngle(angle2);
		System.out.println(angle2);
		light.attachToBody(arm); **/
		
	    /**  final float angle3 = arm.getAngle();

	      final float sin = MathUtils.sinDeg(angle3);
	      final float cos = MathUtils.cosDeg(angle3);
	      final float x = arm.getPosition().x + 1 * cos - 1 * sin;
	      final float y = arm.getPosition().y + 1 * sin + 1 * cos;
	      light.setPosition(x, y);
	      light.setDirection(angle3); **/
		
		
			
	}


Cheers

Tumppu

My hypothesis of the issue, you’re doing
arm.setTransform(new Vector2(arm.getPosition().x, arm.getPosition().y), newAngle);
and you’re ALSO doing
light.setDirection(newAngle);
My interpretation of Orange451’s suggestion, and assumptions of the box2d api, would suggest you shouldn’t be flipping (adding PI) to both the arm and the light, as that would just put the light back where it started. Try flipping only the light, not the arm. That is, revert line 9 to use angle
arm.setTransform(new Vector2(arm.getPosition().x, arm.getPosition().y), newAngle);

Other notes,

does vec2.set(vec2.x, vec2.y); do anything? I’m not familiar with box2d, but assuming I understand correctly, that code seems redundant.

float angle = (float) Math.atan2(arm.getWorldCenter().y - worldCoords.y, arm.getWorldCenter().x - worldCoords.x);
can be replaced by
float angle = (float) Math.atan2(vec2.y, vec2.x);
in order to make it both more efficient, and more importantly, easier to read.

You have some formatting issues. It’s a good idea to format your spacing and such before posting online. After all, you’re asking other people to offer their time and experience, the least you could do is make it easier for them to understand your problem. Most IDE’s have a keyboard shortcut to auto-format.

Thanks for the input, yeah the code is a little messy right now (sorry bout that). I will try those suggestions.

Cheers

Tumppu