Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Code Snippets / Mouse-Matrix Coordinates

Author
Message
JamesBlond
22
Years of Service
User Offline
Joined: 27th Aug 2002
Location: Germany
Posted: 21st Jul 2003 00:38
All right, since the demand is so high here you go, you can have it. I wrote this shortly after D-Man and I wrote the code for the flat matrix. It basically calculates the mouse vector from the camera into open space and then travels along it until it hits the matrix.
Feel free to use this code, but please give me and D-Man some credits.

To get the mouse coordinates call the function like this:
eg. mmcoord(mousex(),mousey(),1,4000,60)

- scrx# and scry# are the screen coordinates
- matnum is the matrix number
- r# is the length of the vector and should ideally be equal to the camera range or distance
- FOV# is the field of view of the camera (make sure you get this value right!)
Smile and be happy, things could be worse!
So I smiled and was happy, and things got worse...
JamesBlond
22
Years of Service
User Offline
Joined: 27th Aug 2002
Location: Germany
Posted: 21st Jul 2003 17:19
Here's a usage example.

Smile and be happy, things could be worse!
So I smiled and was happy, and things got worse...
sinny
21
Years of Service
User Offline
Joined: 7th Feb 2003
Location:
Posted: 24th Jul 2003 07:22
ReD_eYe
21
Years of Service
User Offline
Joined: 9th Mar 2003
Location: United Kingdom
Posted: 24th Jul 2003 15:37
smooth

You know whats weird???
Donald duck never wore pants, yet everything time he got out of the shower he put a towel around his waist...
Whats with that???
AzureZeal
21
Years of Service
User Offline
Joined: 10th May 2003
Location:
Posted: 29th Jul 2003 20:57
After realizing my 3d maths/vector knowledge isnt quite up to par, I decided to take another look at this code. Seem perfect, many thanks James. Just had a couple questions. I want to understand it before I just use it in my program.

Theres just a few values in the function itself I dont quite understand (mainly because my sin/cos/tan knowledge isnt quite up to par either). Can you/anyone help me understand what each of these values represent and how they are used?

hmax# = r#*tan(FOV#/2)
mh# = (((scrh#/2-scry#)*2)/scrh#)*hmax#
wmax# = r#*scrw#/scrh#*tan(FOV#/2)
mw# = (((scrx#-scrw#/2)*2)/scrw#)*wmax#

cl# = r#*cos(ax#)
ch# = -r#*sin(ax#)

sl# = mh#*cos(90-ax#)
sh# = mh#*sin(90-ax#)

ox# = camera position x() + (cl#+sl#)*sin(ay#) + mw#*cos(ay#)
oy# = camera position y() + (ch#+sh#)
oz# = camera position z() + (cl#+sl#)*cos(ay#) - mw#*sin(ay#)

Thanks in advance for any info. The code works great

JamesBlond
22
Years of Service
User Offline
Joined: 27th Aug 2002
Location: Germany
Posted: 31st Jul 2003 01:44
Maybe this helps.



Smile and be happy, things could be worse!
So I smiled and was happy, and things got worse...
AzureZeal
21
Years of Service
User Offline
Joined: 10th May 2003
Location:
Posted: 1st Aug 2003 19:39
Ah cool thanks james. I realize I should have paid more attention to my maths in highschool, so I checked out some geometry/trig books from the library the other day. heh

AzureZeal
21
Years of Service
User Offline
Joined: 10th May 2003
Location:
Posted: 2nd Aug 2003 06:24
Another question (forgive my newbness), this time about mh/mw. Whats with the "scrw#/scrh#" when calculating wmax#? Unless im mistaken the result is always 1.33..., but i couldnt for the life of me figure the significance of that number (its not supposed to have anything to do with pie is it?).

PS - Can you confirm I have these names right? Im assuming this is what you had in mind when you came up with the abbreviations for each variable...

hmax# = "height max" - meaning the max height from the center to the edge of the screen (in 3d space)?
wmax# = "width max" - meaning the max width from the center to the edge of the screen (in 3d space)?

cl# = "click length?" - meaning not quite sure...
ch# = "click height?" - meaning not quite sure...

sl# = "no idea" - meaning not quite sure...
sh# = "no idea" - meaning not quite sure...

oxyz = "not sure what the o stands for..." - meaning the point in 3d space the 'bullet' is aiming at, and will eventually hit.

Thanks in advance for any info. This code is golden, so I want to make sure I understand it perfectly

AzureZeal
21
Years of Service
User Offline
Joined: 10th May 2003
Location:
Posted: 3rd Aug 2003 04:59
Ok I got it now. cl and ch stands for camera length (or depth) and camera height, sl and sh stand for screen lenght (or depth) and screen height. After I realized that, just needed a few more looks to figure it out. Thanks again James, this is sooo golden!

JamesBlond
22
Years of Service
User Offline
Joined: 27th Aug 2002
Location: Germany
Posted: 3rd Aug 2003 20:35
Yup, that's right. It seems that you understood my highly artistical sketch .

I basically projected the mouse coordinates onto an imaginary plane which faces the camera and is r# units away from it. hmax# and wmax# are the height and width of that plane. Then I imagined theres a 2d coordinate system on that plane and used linear interpolation to get the mousecoordinates on that plane (mw#,mh#). Then I converted these coordinates into length and height from the camera to the center of the plane (cl#,ch#) and from the center of the plane to the mouse coordinates on that plane (sl#,sh#). Then I threw these lengths and heights into 3d space taking into acount the camera rotation to get a point in actual 3d space ox#,oy#,oz#. Finaly I travelled alond the line between the camera and that point using linear interpolation again until I hit the matrix.

Oh yeah, scrw#/scrh# is the screen ratio. I used that because FOV# represents the height of the screen, but I also needed the width.

Smile and be happy, things could be worse!
So I smiled and was happy, and things got worse...

Login to post a reply

Server time is: 2024-11-27 19:27:39
Your offset time is: 2024-11-27 19:27:39