KlemenPl
KlemenPl

Reputation: 355

How can I position mouse cursor in the middle of the screen and move the object according to mouse movement?

I am working on a 3d game where the cursor(crosshair) is in the center of the screen and when you move your mouse the object will rotate according to the mouse(like in fps games).

I've already replaced the default crosshair with my own, but I am having trouble with centring it on the screen. I've already determined the middle position of cursor placement using:

cursorPosition=new Vector2((Gdx.graphics.getWidth()-cursorSize.x)/2,(Gdx.graphics.getHeight()-cursorSize.y)/2);

And then I apply that position each time the render() method is called using this:
Gdx.input.setCursorPosition((int)cursorPosition.x,(int)cursorPosition.y);
It does not work as I expected. If I move my mouse fast the position of the cursor still moves and then it resets to the middle of the screen.
I've also tried setting cursor catched to true, but that only makes cursor invissble. Gdx.input.setCursorCatched(true);

I want the mouse cursor to be placed in the middle of the screen at all times and then 3d move the object according to mouse movment.

Upvotes: 1

Views: 1363

Answers (1)

Keval
Keval

Reputation: 1602

Use Gdx.input.setCursorCatched(true) but then draw your mouse cross-hair to the center of the screen while the mouse is catched and remove it when you uncatch it.

Depending on how you've set up your game, your crosshair should be one of the last things you render:

public void render() {
    ...
    spriteBatch.render(cursor, Gdx.graphics.getWidth()-cursorSize.x)/2,(Gdx.graphics.getHeight()-cursorSize.y)/2);
    spriteBatch.end();
}

Upvotes: 1

Related Questions