Moving crosshair in 3rd person

Hi,

i would like to move the crosshair in a 3rd person view along with the mouse.



for this purpose i need the position of the mouse on the screen.



Vector2f mp = input.getCursorPosition();

crosshair.setLocalTranslation(mp.x,mp.y,0);



gave me a crash.



Thank you for your support :slight_smile:

A stack trace would really help anyone trying to help you here :/. I’m going to assume that it crashed because you haven’t defined one or more variables (input, crosshair). If you can paste the output of your application here in code blocks, that would be great.

public class BlockManipulation extends SimpleApplication

implements ActionListener,

PhysicsCollisionListener,

AnimEventListener{







//crosshair

BitmapText ch;



// initiate Input Manager

InputManager input;



protected void initCrossHairs() {

// guiNode.detachAllChildren();

guiFont = assetManager.loadFont(“Interface/Fonts/Default.fnt”);

ch = new BitmapText(guiFont, false);

ch.setSize(guiFont.getCharSet().getRenderedSize() * 2);

ch.setText("+"); // fake crosshairs :slight_smile:

ch.setLocalTranslation( // center

settings.getWidth() / 2 - guiFont.getCharSet().getRenderedSize() / 3 * 2,

settings.getHeight() / 2 + ch.getLineHeight() / 2, 0);

guiNode.attachChild(ch);

}





@Override

public void simpleUpdate(float tpf) {







// update Crosshair-Position



Vector2f mp = input.getCursorPosition();

ch.setLocalTranslation(mp.x,mp.y,0);

}



error-Log:







INFO: Camera created (W: 1, H: 1)

20.07.2011 12:28:25 com.jme3.app.Application handleError

SCHWERWIEGEND: Uncaught exception thrown in Thread[LWJGL Renderer Thread,5,main]

java.lang.NullPointerException

at Testing.BlockManipulation.simpleUpdate(BlockManipulation.java:633)

at com.jme3.app.SimpleApplication.update(SimpleApplication.java:255)

at com.jme3.system.lwjgl.LwjglAbstractDisplay.runLoop(LwjglAbstractDisplay.java:144)

at com.jme3.system.lwjgl.LwjglDisplay.runLoop(LwjglDisplay.java:185)

at com.jme3.system.lwjgl.LwjglAbstractDisplay.run(LwjglAbstractDisplay.java:218)

at java.lang.Thread.run(Unknown Source)



i guess, its some dumb error i cant find…



thanks :slight_smile:

Err, you just have an empty field called “input” sitting there, thats not the inputManager.

yes… :slight_smile:



now it works, i had 2 different inputmanager and used the wrong one…



thanks