Author Topic: Mouse position to world coords (in script)  (Read 2572 times)

I know this should be done in C++ but I'm wondering how to get the 3D world coordinates from the 2D mouse position.

here's what i have so far (which doesn't work very well):
Code: [Select]
// commandToServer('GetMouse3DPos', canvas.getCursorPos(), getRes());

function serverCmdGetMouse3DPos(%client, %clientMousePos, %clientRes) {

%Pos2D = (firstWord(%clientRes) / 2) SPC (getWord(%clientRes, 1) / 2);

%x = firstWord(%clientMousePos) - firstWord(%Pos2D);
%y = getWord(%clientMousePos, 1) - getWord(%Pos2D, 1);
// The cursor position is based off of the top-left corner of the screen
// This makes x and y relative to the center of the screen

%delta = "0" SPC (%x / 10) SPC (-%y / 10);
// If you click above your player, for example, this will be about:
// 0 0 1
// The z value is how far above your player you clicked

error(%delta);
%Vec3D = VectorAdd(%client.player.getForwardVector(), %delta);
%Vec3D = VectorAdd(%Vec3D, %client.player.getPosition());
messageClient(%client, '', '3D position is: %1', %Vec3D);
}
« Last Edit: August 29, 2007, 03:35:22 PM by makaan »

You're not intending to make something like a RPS?

what's that?

I'm trying to make an object selection tool in script.

Pretty much impossible to implement this well.  The Mission Editor works because you can select objects within your field of view.  The only way i can foresee getting good coordinates on all three planes would be to incorporate use of the zoom control.

You should be able to do it in script if you knew the 2d position of where the client clicked and the client's resolution.

Look at this picture:
« Last Edit: August 29, 2007, 06:35:37 PM by makaan »

You seriously don't expect to just pick some arbitrary point on the Y axis do you? You need to have some sort of scalable zoom method to let you choose how near or far you are wanting to click.  It would also be good to have a display as your zooming that shows the distance from the player to the x, y (zoom), z point.  Then there's the question of the player's angle of view.  You would have to make some sort of alteration to each of the values based on the player's angle of view.
« Last Edit: August 29, 2007, 07:12:28 PM by Trader »

what i'm saying is as long as you have x and z of the delta vector, you don't need y (you can ray cast until you hit an AIPlayer). In other words, instead of a zoom method like you were talking about, it just finds the first AIPlayer on the line extending forward from the point where the player clicked.

Meh, there really needs to be a limit.  That's why zooming would work great.

No zoom = 10 clicks or less from player.
Full zoom = 100 clicks from player.

as long as I can get help to capture the 3d position, i can script the rest

Position of %client.Camera.  Units of width = horizontal resolution / ~15.  Units of height = vertical resolution / ~15.

So,

Code: [Select]
%Pos3D = VectorAdd(%client.camera.getPosition(), VectorAdd((%x / 15) SPC "0" SPC (%y / 15), %client.player.getForwardVector()));

=

camera pos + ("x/15 0 y/15" + forward vector)

Well, I would do it like this:

%cursorpos = position of mouse cursor
%cursorX = getWord(%cursorpos,0);
%cursorY = getWord(%cursorpos,1);
%resolution = current screen resolution
%resolutionX = getWord(%resolution,0);
%resolutionY = getWord(%resolution,1);
%camerapos = %client.Camera.position
%cameraX = getWord(%camerapos,0);
%cameraY = getWord(%camerapos,1);

if(%cursorX < %resolutionX / 2)
{
          %x = %cameraX - (%resolutionX / 2 - %cursorX) / 15;
}
else if(%cursorX > %resolutionX / 2)
{
          %x = %cameraX + (%cursorX - %resolutionX / 2 ) / 15;
}
else
{
          %x = %cameraX;
}

if(%cursorY < %resolutionY / 2)
{
          %z = %cameraY - (%resolutionY / 2 - %cursorY) / 15;
}
else if(%cursorY> %resolutionX / 2)
{
          %z = %cameraY + (%cursorY - %resolutionY / 2 ) / 15;
}
else
{
          %z = %cameraY;
}

Then you need some way to scale the z value (e.g. zoom).  Obviously the x and y values in the code above would need to be swapped based on rotation of the client's camera.  Also, I'm not sure how well this would work if the client's camera was not rotated on the z axis at 0,90,180, or 270 degrees.

You might be able to make modifications using the forward vector, but I don't know for sure.  I'm at work right now, I'll have to check when I get home.
« Last Edit: August 30, 2007, 12:16:52 PM by Trader »

Why not use a weapon, the "Pointy Finger", cast an invisible projectile?

Edit this, maybe? Use a ray cast not a projectile.

This was both confusing and interesting to read.

Grats u wins