# The vector from the 2D position of the mouse to an object in 3D space

## Recommended Posts

Hi. I need to put an object in space relative to the position of the mouse on the screen. screenToWorld is not suitable because it returns the position relative to the terrain. Is it possible to somehow get the mouse position through positionCameraToWorld
So far, I'm stuck on this piece of code:

```_startASL = agltoasl ( positionCameraToWorld [ 0,0,0 ] ) ;
getMousePosition params ["_xpos","_ypos"];
_Xoffset = _xpos;
_Yoffset = - _ypos;
_Zoffset = 10; //Depending on z, the x and y offset will be different. The position relative to the mouse is best seen in small _Zoffset values, but it is still inaccurate
_endASL = agltoasl ( positionCameraToWorld [ _Xoffset,_Yoffset,_Zoffset ] ) ;

_ins = lineIntersectsSurfaces [ _startASL , _endASL , player , objNull , true , 1 , "VIEW" , "NONE" ] ;```

I think I need to use FOV, angles, extra vectors or something like that. But I have no idea how to do it and my math skills are bad. Please help me!!!

##### Share on other sites

to make sure im understanding this correctly

you want to place something on the players mouse position, but projected, for example, 2 meters in front of the players face? floating in mid air?

##### Share on other sites
5 minutes ago, j0nes said:

to make sure im understanding this correctly

you want to place something on the players mouse position, but projected, for example, 2 meters in front of the players face? floating in mid air?

Not exactly. I'm trying to make it so that the object was put in the position of the intersection point, as in the example code below, but only with respect to the position of the mouse

```_ins = lineIntersectsSurfaces [
AGLToASL positionCameraToWorld [0,0,0],
AGLToASL positionCameraToWorld [0,0,1000],
player
];
if !(count _ins isEqualTo 0) exitWith {arrow setPosASL (_ins select 0 select 0)};```

But because of the use of screenToWorld, which returns a position relative to the terrain, I encounter a problem when the object is not put above a certain position (or something like that)

Spoiler

##### Share on other sites

still working on a solution. but the reason youre encountering problems with "screentoworld" is the horizon. if theres no terrain to project the coordinates on it doesnt return proper values

##### Share on other sites

Also mind camera coordinates are using [x,z,y] format.

Cheers

##### Share on other sites

As we already discussed in Discord. The biggest problem here is that he wants "mouse position" not the aim position.
Getting what the center of the screen is aimed at is easy. But getting the position the mouse cursor is on..

I can't think of a solution, except mathing out the direction the line needs to go from the eyePos, for which you need to take the FOV and screen resolution into account.

Math out where the 4 corners of the screen point towards, then use mouse position to interpolate between these 4 directions.

##### Share on other sites

I don't understand what a "mouse position" is, if not in a display like arsenal/inventory or else.

Center of the screen is understandable.

Aiming cursor also. But then, what is the "mouse position" not the aim position ?

What is the reference for camera? you, in front of your screen, or the unit "player" ?

Anyway, if I understand, test that:

```MGI_boo = createVehicle ["Sign_Arrow_F",[0,0,0],[],0,"can_collide"];
["aim","onEachFrame", {
_start = eyePos player;
_trtdir = eyeDirection player;
_ins = lineIntersectsSurfaces [_start, _start vectorAdd (getCameraViewDirection player vectorMultiply 1000),player,objNull,true,1,'FIRE','NONE'];
_pos = if (count _ins > 0) then [{_ins select 0 select 0},{ _start vectorAdd (getCameraViewDirection player vectorMultiply 1000)}];
MGI_boo setposASL _pos;

##### Share on other sites
15 hours ago, pierremgi said:

Aiming cursor also. But then, what is the "mouse position" not the aim position ?

Mouse cursor position. On a UI dialog.