[LibGDX] Check for mouse position every frame

I am in the process of implementing a torch onto a character in a game using Box2D and Box2D lights, currently I have it funcional to a degree.

It will follow the cursor when I move it, problem is when I move the character and keep the cursor still it will remain at the same angle as before. If that makes sense?

Basically I want the torch to constantly look at the location where the mouse is, as if the cursor is being moved.

Here is my code:

Called when the mouse is moved

@Override
	public boolean mouseMoved(int screenX, int screenY) {
		touchDown = new Vector3(screenX, screenY, 0);
		WorldRenderer.box2dCam.unproject(touchDown);
		torchPos = new Vector3(torch.getPosition(), 0);
		torchPos.sub(touchDown);
		tmpVec = new Vector2(-torchPos.x, -torchPos.y);
		torchDirection = tmpVec.angle();
		return true;
	}
torch.setDirection(torchDirection);
		torch.setPosition(getBody().getPosition().x, body.getPosition().y + 1f);

Called in the players update method, called each frame.

I also have this in my update method:

if (Gdx.input.isKeyPressed(Keys.A)) {
			setFacing(State.FACING_LEFT);
			setCurrentState(State.MOVING);
			setSpeed(-5, 0);
			animatedBox2DSprite.play();
		} else if (Gdx.input.isKeyPressed(Keys.D)) {
			setFacing(State.FACING_RIGHT);
			setCurrentState(State.MOVING);
			setSpeed(5, 0);
			animatedBox2DSprite.play();
		} else {
			setCurrentState(State.IDLE);
			animatedBox2DSprite.stop();
			setSpeed(0, 0);
		}

Now I have tried setting the tmpVec to the last know location of the cursor using

Gdx.input.getX

but this gives me some eh, weird results with the torch going crazy if I move the mouse and move at the same time.

I think all I need is some way to store the last know coordinate that the mouse was registered as moving on and have the torch look at this coordinate until I move the mouse, regardless of the player position.

Any ideas?

  1. Is the character always at 0,0? Because vec.angle() returns the angle from 0,0 to the vector. I think what you want is to get the angle from the character to the mouse pos.

/** return angle in degrees */
	public static float getAngleFromAtoB(Vector2 a, Vector2 b) {
		return MathUtils.radiansToDegrees * MathUtils.atan2(b.y - a.y, b.x - a.x);
	}

  1. Gdx.Input.getX() and Gdx.Input.getY() should do exactly what you need. Just remember to project those coords using the camera projection)

  2. You’re making new Vector2/Vector3s in each mousemoved call, that’s gonna make the GC kick in a lot. It’s best to either pool your vectors or use a vector outside the stack.

This looks good, will have a look at it when I am home. I move the code around last night, no longer creating new vector every mouse movement but instead just reusing the same ones.

[quote]1. Is the character always at 0,0? Because vec.angle() returns the angle from 0,0 to the vector
[/quote]
This would be true however I have set that vector position to the position of the torch, which is anchored on top of my sprites head.

So the origin is wherever the player is, basically.

Don’t get me wrong what I have the now works ok but the movement is a little jerky if you don’t move the mouse or move it too much lol.