[SOLVED] Camera Node and Spot Light and Mouse Cursor - How?

[quote=“Torsion”]
So I presume you have to first translate cursor screen coordinates into your scene coordinates and then point light onto those you got?[/quote]

Except from the fact that I’m using spotlight instead of pointlight, yes…

[quote=“mathiasj”]Everything you need is in the wiki:http://wiki.jmonkeyengine.org/doku.php/jme3:advanced:mouse_picking

Take a look at Pick a Target Using the Mouse Pointer[/quote]

And how would You understand this lines:

Vector3f click3d = cam.getWorldCoordinates(new Vector2f(click2d.x, click2d.y), 0f).clone();

Vector3f dir = cam.getWorldCoordinates(new Vector2f(click2d.x, click2d.y), 1f).subtractLocal(click3d).normalizeLocal();

Because they look like using cam object world coordinates to do the job and I don’t have this object because I don’t use FlyByCamera (maybe I’m wrong but in every other tutorial cam is an instance of default FlyByCamera).

Well, then you need the direction, which can be calculated from two points, one being that you calculated after translation, and second is your cam’s position. Or cameranode is something else that doesn’t have camera position?

You’re guessing right but the whole point is translating this mouse coordinates into world/scene coordinates. How? Can You give me formula/algorithm to do that? One that doesn’t use cam because I don’t have it.

Since you don’t have camera and still see something in your scene this must be from some default position implied by JME. I have no idea what are the defaults here cause I always use explicitly defined cam… maybe somebody from developers team will help…

A position on the screen is a ray in the 3d world… Theres methods on camera to get a 3d position on that ray (see the picking tutorial). From what you said I thought you already had some kind of 3d cursor thingy in your world though.

Just in case the issue is simpler than we may think, The default camera's location is Vector3f(0.0f, 0.0f, 10.0f). as stated here

Ok so once again. What I have:
a) default camera disabled (which I presume means that no cam objects exists) and by default only cursor I got is Windows default cursor that works both in my jM window and outside it,
b) camera node created and I can control it but mouse have nothing to do with it so let’s assume I have stationary View without any camera at all,
c) now to make light follow a cursor I need to have cursor position in my 3d scene and what I got with inputmanager is only its 2d position in jM window,

Again, look at the picking tutorial, it shows how to create a 3d position from the cursor…

Also look at this:
https://www.mikeash.com/getting_answers.html

My IDE says implicitly that I don’t have CAM object to refer to. No! Nada! Nicht! It’s not that I’m on defensive or anything, I tried, it just don’t work for me, even if I copypaste whole damn tutorial. Dunno if it’s related but I have everything related to lights in appstate.

How can I use this (or any other) picking tutorial without it?

Theres always a cam object in SimpleApplication.

I did the tutorial on it’s own (without my code) to try reverse engineering it part by part.
a) It works on it’s own, HOORAY!!!
b) It doesn’t do what I need it to do - when I created spotlight inside this tutorial, light shoots from the center of the screen to the desired point. I need light to shoot from the cursor just if cursor was a flashlight fixed in one direction (Z). This is how it looks.

//This is inside update loop:
Vector2f click2d = inputManager.getCursorPosition();
Vector3f click3d = cam.getWorldCoordinates(new Vector2f(click2d.x, click2d.y), 0f).clone();
Vector3f dir = cam.getWorldCoordinates(new Vector2f(click2d.x, click2d.y), 1f).subtractLocal(click3d).normalizeLocal();
spotlight.setPosition(click3d);
spotlight.setDirection(dir);

c) I also tried to move tutorial code to appstate I created for this purpose (still working in tutorial code not mine) and it doesn’t work again. CAM object not found… ARGHHHH!!!

What is this CAM anyway? Isn’t default camera and flybycamera same thing? I need to flyCam.setEnabled(false); everytime so it has to be default right?

b) So make direction just Vector3f.UNIT_Z
c) No, flyCam is just a control that moves the camera based on input.

Awesome, we start to get somewhere! :smiley: Now there are only 2 things left:
a) I still need to move this light code to my appstate. How do I make appstate recognize CAM object?

b) With Vector3f.UNIT_Z spotlight moves insanely slow. I mean it does “follow” the cursor but whenever I move cursor the whole window (800 pix) light just moves like 20 pix maximum… It’s like light and the cursor are related now but not glued to each other.

P.S. Several people here on the forums are calling flybycamera a camera instead of control. That made me confused (it’s much clearer now thanks to You :wink: All CameraNode’s, ChaseCams, FlyCams, etc. are just controls while actual camera is the same. Someone should really change entity naming here :stuck_out_tongue:

To access the cam object in your app state, you can create an instance variable in the app state and set it to the cam object that you should pass in the constructor. So
new AppState(cam)
and
this.cam =cam in the app state

Are You native english speaker? Sry but I didn’t understand half of what You wrote… Could You please give complete example of what goes where?

Also for the B point…

Are you a Java coder? If you were you’d have understood what he said. Learning Java while learning how to write a game is an almost impossible task and will only lead to frustration.

1 Like

Not as long as there are people willing to help with that. Code snippets here code snippets there and it works… That’s how I learn! Now can You please help me finish those last things? :smile:

I didn’t say the frustration would necessarily be on your side…

3 Likes

That was subtle… And nice…

Could You please at least follow up on the B point and help me fix this light so it moves exactly as mouse cursor does? Not slower, not faster just identical as if my mouse cursor and flashlight were one entity…

While I take my sweet time to google around to found a way to pass this goddamn cam to my appstate…
[EDIT] Did it like this… Is it proper way to do stuff (I mean it works but dunno if it’s best solution)? I believe cam itself belongs to another (default) appstate so no idea how to reach those directly.

//Inside MyAppState constructor
this.theapp = app;
//And the vector stuff in update loop in MyAppState
Vector3f click3d = theapp.getCamera().getWorldCoordinates(new Vector2f(click2d.x, click2d.y), 0f).clone();
Vector3f dir = theapp.getCamera().getWorldCoordinates(new Vector2f(click2d.x, click2d.y), 1f).subtractLocal(click3d).normalizeLocal();

Anyway I can’t deal with B point alone :frowning: It’s not just googling…

I still not sure on what you need.
Take a look on my old thread :

http://hub.jmonkeyengine.org/t/solved-it-seens-quaternions-got-me-again/

You may find some answers.