How to move cursor in shooter

Hello. At first, sorry fo my bad english.

I am writing a game, where player turning by mouse. So player rotating proportionally to cursor moving. So i need to always move cursor to center of window, but if i will use Java.awt.Robot, my moving cursor to center will be as the gamer moved it.So, how to hold cursor in the center, whithout changing code, which response to anolise input from mouse such as in this tutorial

If you are just trying to make a first person shooter take a look at the flyCam which is the default for a simpleApplication. I don’t think you have to change much for your game if you use it, it already captures mouse movement and hides the cursor for you.

Thanks for your response, baalgarnaal, but it is not what i need.Becase in fact, my game is a multiplayer spaceships shooter and the player camera located over players’s ship, so i can not use flyCam. I think to see slyCam sources to get the information to my problem, but i think smb will help me before i will decide to do so.)

Sorry for my bad english again.

With best wishes, PolarHare.

If i understand you correctly, you want the mouse to stay at the center all the time? And mouse movements rotate the spaceship?

you can take the code from ChaseCamera.java and adapt it a bit



this makes the cursor invisible

[java]inputManager.setCursorVisible(false);[/java]



then u could add mouse listeners that keep track of the rotation

[java]

private float targetRotation = 0f;

private float targetVRotation = 0;



this.getInputManager().addMapping("mouseDown", new MouseAxisTrigger(1, true));

this.getInputManager().addMapping("mouseUp", new MouseAxisTrigger(1, false));

this.getInputManager().addMapping("mouseLeft", new MouseAxisTrigger(0, true));

this.getInputManager().addMapping("mouseRight", new MouseAxisTrigger(0, false));

this.getInputManager().addListener(

new AnalogListener() {

public void onAnalog(String name, float value, float tpf) {

if (name.equals("mouseLeft")) {

targetRotation += value * rotationSpeed;

} else if (name.equals("mouseRight")) {

targetRotation -= value * rotationSpeed;

} else if (name.equals("mouseUp")) {

targetRotation += value * rotationSpeed;

} else if (name.equals("mouseDown")) {

targetVRotation -= value * rotationSpeed;

}

}

},

new String[]{"mouseLeft", "mouseRight", "mouseUp", "mouseDown"}

);

[/java]





finally update the rotation of the target in the simpleUpdate(float tpf) method of your application

[java]

float[] angles= new float[] {targetVRotation, targetRotation, 0}

Quaternion rotation = new Quaternion().fromAngles(angles)

//i hope that this will work, i dint thest it though

SPACESHIP.setRotation(rotation);

[/java]



EDIT: updated the rotational computation, was invalid

1 Like

Thank you for your post.Yes, it is what i need, but how to hold the cursor at the center all the time?(I did not see this in your option-maybe I missed something?)

I have done so:(i get it from flycam)(and i do not see much difference between your offer and mine.)

[java]

public void initKeys(InputManager inputManager) {



inputManager.addMapping(Config.turnLeft, new MouseAxisTrigger(MouseInput.AXIS_X, true));

inputManager.addMapping(Config.turnRight, new MouseAxisTrigger(MouseInput.AXIS_X, false));

inputManager.addListener(analogListener, new String[]{Config.accelerate, Config.breakAcc,

Config.stepLeft, Config.stepRight, Config.turnLeft, Config.turnRight});



Joystick[] joysticks = inputManager.getJoysticks();

if (joysticks != null && joysticks.length > 0) {

Joystick joystick = joysticks[0];

joystick.assignAxis(Config.turnRight, Config.turnLeft, joystick.getXAxisIndex());

}

}



public void onAnalog(String name, float value, float tpf) {

if (name.equals(Config.turnLeft)) {

input(Config.ControlCommands.TURN_LEFT, value);

} else if (name.equals(Config.turnRight)) {

input(Config.ControlCommands.TURN_RIGHT, value);

} else …

}



public void input(Config.ControlCommands command, float delta) {

switch (command) {

case TURN_LEFT: {

rotationSpeed=deltaConfig.rotationSpeed;

break;

}

case TURN_RIGHT: {

rotationSpeed=-delta
Config.rotationSpeed;

break;

}



}

}





[/java]

I looked through flycam again and I have not found how to hold cursor. :frowning: And i do not want to use Java.awt.Robot, because if our window now in fullscreen mode-we dont know where the center.

I understood that, inputManager.setCursorVisible(false); not just hide cursor-it hold cursor somewhere(i think that nego mean it)-so it is the solution!(thanks a lot nego and baalgarnaal)