# 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.

### DarkBASIC Discussion / 3D Rotation Matrix

Message
Posted: 6th Feb 2011 23:49 Edited at: 7th Feb 2011 19:58
Ok. I worked on a object selection function a long time ago. I thought I had it working and when I tried to use it in a program I'm currently working on. I discovered there were errors in it. I got it to work if the X Y and Z coordinates are at 0. The angles can be whatever, but the moment its x y or z coordinates change, it stops working. I lost the page that I had gotten my information for the rotation matrix from and now all I have found decently is this.

http://en.wikipedia.org/wiki/Rotation_matrix

Here's a knockup program I put the function in. I know its not great.

Here's the function. Please help. the section I'm concerned with is the xpos#=code : ypos#=code : zpos#=code

EDIT:
Nvm, got it. It was a real simple solution although i'm not sure how it worked.

Posted: 7th Feb 2011 23:43 Edited at: 7th Feb 2011 23:48
Hi Pictionary, you seem to have prempted my response but I'll post this anyway.

I've been having a look at your pick object function (pretty much all I've done since I got home from work) and to be honest I don't understand the maths.

I even went through your old post http://forum.thegamecreators.com/?m=forum_view&t=171199&b=10 where other people went through it all.

I still don't understand the maths.

However, I tracked down this post http://forum.thegamecreators.com/?m=forum_view&t=154696&b=10 where someone came up with an object picker using the in-built DBC collision commands.

I lifted the code out, with a couple of minor modifications that I've highlighted (mainly so it will work as you originally intended) and it works perfectly (he says with a certain amount of trepidation).

The code snippet contains all the collision functions, so you won't need an include file (just the dll).

I still don't understand the maths.
Posted: 8th Feb 2011 05:37 Edited at: 8th Feb 2011 06:00
In the example you posted they use a rotation matrix like me, but thhe part with px and py i don't understand. I don't understand what these points signify.

In mine. I can explain. In 3D space, the screen is projected from the eye with a certain field of view. What you can do to find the distance from the eye to the screen is use right triangle math by taking the width and height of the screen and divided them by 2, then take the fov and divide it by 2 since we're only using half our screen. We then use SohCahToa and divide half the screen by the tangent of half the fov angle. Like so:

We then use right triangle math again and divide the distance from the eye to the screen by the distance from the mouse to the center of the screen to find the tangent(angle). Of course we need the angle without the tangent. So we'd just use atanfull(distance eye to screen,distance from mouse to center of screen)
Like so:

Since we now have the angle our ray cast is going to sit on. we would use right triangle math one last time and take the distance of the eye to screen,plus the distance we want to cast out and divide it by the tangent of the angle we just figured out to get the 2d position of the point we just casted to in 3d space.
Like so:

Then we use rotation matrix math kind of like we would with an image trying to keep the offset of the plane of the screen we just casted out to stay with the angle of the camera.

Then we would just move the point our ray is casting as many spaces over as our camera is from the origin. We then have our 3d Coordinates. Next we cast the ray from our camera to the point and return the first object, if any, that the ray collides with.

EDIT:
And yes... I'm aware these aren't the best sketchups ever.

EDIT 2:
I whipped up some functions

These are to cast a ray out and get the x, y, and z coordinates for it. Just in case you weren't trying to collide with any objects and just wanted to position an object a certain distance in 3d space where your mouse is. Say a waypoint or something.

These are some functions to rotate a 3d point around the origin.(To move it to the point your trying to rotate around, just add those coordinates to it)

Posted: 8th Feb 2011 20:08
Quote: " px#=(((dist#*.8)+.8)*2)/screen width()
py#=(((dist#*.6)+.6)*2)/screen height()

X1#=(mx#*px#)-((dist#*.8)+.8)
Y1#=((dist#*.6)+.6)-(my#*py#)
Z1#=dist#+1"

This is a hard coded version of finding the endpoints of the mouse vector. The derivation of the .8 and .6 seem to be based on the ratio of 800:600. But it seems to reflect a constant FOV (it's hardcoded). A loose proof of where these values come from might be:

I'm not 100% sure that this is where the values come from, but it makes sense because the proof takes into account the TOA part of SohCahToa right triangle math.

Posted: 8th Feb 2011 20:35
Yea true that. I'll have to have a better look at your pseudo code when i get back on. Maybe it can help my code to be more accurate.

Posted: 1st Apr 2011 10:09
Hey just wanted to post back on this thread. I got it working perfectly. Kind of jumbled what latch said, what 29 games posted and what I had. The way of getting the ratios. Latch you were correct. His ratios weren't dead on the money and only worked for screens that had a 4 to 3 ratio. I changed it taking your advice on the FOV and made it so it give the correct ratios. Then I just put it to a point I understood. I still can't understand why my way failed me. But regardless. I've figured it out now. No bother.

pickobject function:

3D ray casting functions:

Posted: 6th Apr 2011 21:56
All of those work really well.
Posted: 8th Apr 2011 00:26
thank you. here's the fixed 3drotation matrix functions too. you would just have to add your current positions to the output if you were planning on doing it elsewhere

Posted: 8th Apr 2011 01:58
this is a quick question for latch. Why is the mouse position a little offset like that. it explains why my right triangle math wasnt working. the mouse has a 2.5... offset. i can't see the reasoning behind this

Posted: 9th Apr 2011 20:21
@pictionary
Quote: "this is a quick question for latch. Why is the mouse position a little offset like that. it explains why my right triangle math wasnt working. the mouse has a 2.5... offset. i can't see the reasoning behind this"

There's something not quite right about the ratio of width to height and the mouse position. I can't quite see it because the order of your equations are different than what I'm used to. The end position of the ray is too high on the y axis. This may be what is skewing other things.

There is something to keep in mind as screen resolutions and camera views change. If you change the camera view, for example, to 800 x 200 , the FOV will still be the default 62 degrees. However, because the half y and half x resolutions will have a different ratio (400 x 100 ) you would need to calculate TWO Z distances to the viewing plane or else there would be skewing.

1 z distance based on the width
1 z distance based on the height

For each x and y screen position calculation, you would have to use the respective z distance in the calculations.

Does that make any sense?