# 3d mouse movement

4 replies to this topic

GMC Member

• GMC Member
• 368 posts
• Version:Unknown

Posted 09 June 2012 - 06:54 PM

I`ve been trying for a day now to make a good mouse movement system in 3d, but it was no good. I found the 2d-3d conversion scripts made by Yourself. I am trying to project the mouse position in 3d space, but the mouse cursor and it`s position in 3d do not overlap. I`m trying to achieve a Diablo/Torchlight type of movement.

I think it`s way easier if I just put a link with the gmk so here you go:

Sorry if it`s messy please bear with me. Things really don`t go very well as you can see in the gmk, the mouse cursor, the projected in 3d version (the circle) and the reprojected to 2d one (the square) are all in different places! I simply have no idea left.

Thank you for your time!

Edited by mads2194, 09 June 2012 - 06:56 PM.

• 0

### #2 Phantom107

Phantom107

Engineer

• GMC Member
• 2599 posts
• Version:GM:Studio

Posted 10 June 2012 - 08:57 AM

I took a quick look in your GMK and it was solved easily.

First thing I noticed is that your Convert_2d script is not the original. So now wonder it misbehaves.... replace it with this, the correct one:

```// Convert_2d(x,y,xfrom,yfrom,zfrom,view)
// The script returns the 3d x and y coordinates at z = 0

screenx = 2*argument0/view_wview[argument5]-1;
screeny = 1-2*argument1/view_hview[argument5];
mX = dX + uX*screeny + vX*screenx;
mY = dY + uY*screeny + vY*screenx;
mZ = dZ + uZ*screeny + vZ*screenx;

if mZ != 0
begin
x_3d = argument2 - argument4 * mX / mZ;
y_3d = argument3 - argument4 * mY / mZ;
end;
else
begin
x_3d = argument2 - argument4 * mX;
y_3d = argument3 - argument4 * mY;
end;```

Secondly, no wonder it's not accurate, you're having your game world's surface at z = 32, while the 2d-to-3d calculates the 3d position at z = 0, genius!
What you need to do is translate the conversion scripts to be 32 z-units higher. Pretty easy, just replace the conversion bit in the step event with this:

```Convert_Prepare(xfrom,yfrom,zfrom-32,xto,yto,zto-32,xup,yup,zup,angle,view_wview/view_hview)
Convert_2d(mouse_x,mouse_y,xfrom,yfrom,zfrom-32,0);
global.mx = x_3d;
global.my = y_3d;```

Thirdly, don't you think it's kind of pointless to do a 3d-to-2d to find out where the cursor position on the screen is? It's just mouse_x mouse_y!

Lastly, I would suggest you use scripts next time in the GMK. You can't have multiple code windows open with loose pieces of code, which is slowing down your oversight and development progress.

Good luck
• 1

GMC Member

• GMC Member
• 368 posts
• Version:Unknown

Posted 10 June 2012 - 09:16 AM

I took a quick look in your GMK and it was solved easily.

First thing I noticed is that your Convert_2d script is not the original. So now wonder it misbehaves.... replace it with this, the correct one:

```// Convert_2d(x,y,xfrom,yfrom,zfrom,view)
// The script returns the 3d x and y coordinates at z = 0

screenx = 2*argument0/view_wview[argument5]-1;
screeny = 1-2*argument1/view_hview[argument5];
mX = dX + uX*screeny + vX*screenx;
mY = dY + uY*screeny + vY*screenx;
mZ = dZ + uZ*screeny + vZ*screenx;

if mZ != 0
begin
x_3d = argument2 - argument4 * mX / mZ;
y_3d = argument3 - argument4 * mY / mZ;
end;
else
begin
x_3d = argument2 - argument4 * mX;
y_3d = argument3 - argument4 * mY;
end;```

Secondly, no wonder it's not accurate, you're having your game world's surface at z = 32, while the 2d-to-3d calculates the 3d position at z = 0, genius! :tongue:
What you need to do is translate the conversion scripts to be 32 z-units higher. Pretty easy, just replace the conversion bit in the step event with this:

```Convert_Prepare(xfrom,yfrom,zfrom-32,xto,yto,zto-32,xup,yup,zup,angle,view_wview/view_hview)
Convert_2d(mouse_x,mouse_y,xfrom,yfrom,zfrom-32,0);
global.mx = x_3d;
global.my = y_3d;```

Thirdly, don't you think it's kind of pointless to do a 3d-to-2d to find out where the cursor position on the screen is? It's just mouse_x mouse_y!

Lastly, I would suggest you use scripts next time in the GMK. You can't have multiple code windows open with loose pieces of code, which is slowing down your oversight and development progress.

Good luck

1. It`s pretty much he same thing most of the time. I really did not find any difference between view_wview and view_wview[0]. I think view_wview is equal to view_wview[current_view] which suits my needs perfectly as I only have one view.

To each his own I guess.

2. I guess I was stupid enough to not see this, wow thanks a bunch it would`ve taken me forever to find this one out. +reps

3. Haha, I just put that there to showcase how after a 2d->3d and back to 2d projection of the mouse position, the mouse cursor and the projection through the script don`t line up.

the mouse cursor, the projected in 3d version (the circle) and the reprojected to 2d one (the square) are all in different places

It was just my misuse of the scripts it seems.

I know that everything is messy, I simply put blocks of code from here and from there and didn`t have the patience to arrange everything as this issue was driving me nuts

Thanks again!
• 0

### #4 Phantom107

Phantom107

Engineer

• GMC Member
• 2599 posts
• Version:GM:Studio

Posted 10 June 2012 - 09:32 AM

One last note,

You will probably need 3d-to-2d at some point. The script for that is unfortunately hardcoded to use a viewing angle of 45. So in your camera, with an angle of 60, you need to mod the Convert_3d script. Look in the script at the lines 23 and 25. Just replace the value 45 with whatever your angle is.
• 1

GMC Member

• GMC Member
• 368 posts
• Version:Unknown

Posted 10 June 2012 - 09:59 AM

One last note,

You will probably need 3d-to-2d at some point. The script for that is unfortunately hardcoded to use a viewing angle of 45. So in your camera, with an angle of 60, you need to mod the Convert_3d script. Look in the script at the lines 23 and 25. Just replace the value 45 with whatever your angle is.

Good point. Now it works like a charm, thanks a bunch!
• 0

#### 0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users