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?