How to have an object follow the cursor

I want to have an object match the location of the cursor on the screen (in the way that if I hid the cursor, the object would act as a visual stand in for it).

I am used to the ‘object.worldPosition = mouse.hitPosition’ python script method that works like a charm in the BGE, but I can’t quite figure out how to duplicate the effect in Armory.

The way I am approaching this is:

But the second the mouse location changes, the object disappears. I’ve also tried splitting up the XYZ coordinates and playing with keeping two of the mouse coordinates while setting the third coordinate tozero, but to no avail.

When I print the location of the object and the cursor, they are the same… so I think there is a mismatch of how I interpret the screen space vs the world space. Any tips?

Hi,
I’m too lazy tonight to give you the complete solution, but here is my contribution.
First, if you place the object on the screen plane, you won’t see anything, unless your set a two sided material. Then you will see the inside of the object. To see the whole object, you have to go in edit mode, then translate all vertices away from the center of the object.
Secondly, I join a blend file that almost work when you are on the top left corner. There is a scale problem that can surely be solved with width/2 and height/2 of the screen.
object_on_mouse.blend (597.6 KB)
I said I was lazy, but I hope it will help you :slight_smile:

2 Likes

Thank you for the insight! I didn’t know about the material issue, so that’s good to know for sure. I’ve been messing around with the blend file and am getting…closer to a more accurate scale. I’ll keep trying until it works! :slight_smile:

I spend all my afternoon to find a solution, but I can’t.
I thing something is missing. The coords given by “Screen To World Space” are to big, and we don’t have information about screen dimensions to scale them.
Hope someone can help us on this problem…

This needs mathematics!

Specifically something to do with the camera View Matrix, camera Projection Matrix, Mouse co-ordinates, and the Window resolution. I know these are the parameters needed, but I still dont know how to put them together to get the result.

1 Like

This needs mathematics!

Fortunately the work as been done already and it’s in the Raycaster class from iron’s math.

This a function I’ve written to fix the twin stick template’s player mouse look:

It cast a ray from the mouse coords projected in the screen and it’s intersected with a plane that is more or less at the height of the player hip (0, 0 , 1).

If you don’t want to have a plane “limiting” you and instead have the height vary, you can use getDirection from the same class:
(this was a previous version of those last two lines in the project_mouse_pos function back when I was fixing that template)

var physics = PhysicsWorld.active;
RayCaster.getDirection(start, end, input.x, input.y, camera);
var hit = physics.rayCast(camera.transform.world.getLoc(), end);
return hit;

Hopefully someone can make it into logic nodes for the OP to use, although I’m not sure if that was what the OP needed.

2 Likes

Doesn’t this example do what you want to achieve?

If you want object to follow mouse, than you can create some sort of ground and than ray-cast it, if it hit, than set object location to it

And, what is the end result you are trying to achieve?

1 Like