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 / how des 3d selector work

Author
Message
That1Smart Guy
15
Years of Service
User Offline
Joined: 26th Feb 2009
Location: Somewhere...... yep
Posted: 22nd Jul 2009 19:31
in the extras folder there is a file called 3dselector (i think its under the Jason Clogg folder but im not 100 percent sure)

it randomly positions a bunch of boxes then colors any under the mouse

how does it do this?

TheComet
16
Years of Service
User Offline
Joined: 18th Oct 2007
Location: I`m under ur bridge eating ur goatz.
Posted: 22nd Jul 2009 22:31
I don't seem to have that in my extras... Mind sharing? (If it of course isn't against the AUP...)

TheComet


Make the path of your enemies easier with Waypoint Pro!
That1Smart Guy
15
Years of Service
User Offline
Joined: 26th Feb 2009
Location: Somewhere...... yep
Posted: 22nd Jul 2009 22:42
ok heres the code:



and attached is the only image it requires

Attachments

Login to view attachments
Latch
17
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 22nd Jul 2009 23:45
@SmartGuy
The program creates a hidden object box that it places at the camera position. Inside the function getpointer(), it creates a ratio of how far the mouse moves relative to the screen dimensions in relation to the 3d view.

Using matrix rotation math, it calculates a vector that represents the camera view and the mouse projection into the screen. When the mouse moves, the invisible box is positioned with the mouse and then rotated according to the vector. The vector coordinates are stored in the arrays sx#() sy#() and sz#(). The box is rotated by using the command
point object 2,sx#(2),sy#(2),sz#(2)

Using collision detection, the invisible box pointing outward from the camera view into the screen points at the projected mouse position in 3d space and returns a hit if colliding with an object. That's basically it.

It would be better to use sparky's collision dll in place of the DBC collision because DBC reports collision by lower object number priority; meaning object 100 could be in front of object 1 but object 1 would be indicated as being collided with; where as Sparky's reports collision from the first object hit.

One question I have is in regard to the .6 and .8 hard coded into the function math. From what I can tell it's to adjust for the skew in the pixel difference from width to height, other than that it's pretty nice.

One might shuffle the contents of the function around a little bit so it doesn't call the trig functions and camera stats more than once and speed it up a bit:



Enjoy your day.
That1Smart Guy
15
Years of Service
User Offline
Joined: 26th Feb 2009
Location: Somewhere...... yep
Posted: 22nd Jul 2009 23:50
ok i knew it was something similar to that but i didnt understand certain parts of it, thanks latch!!

Latch
17
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 23rd Jul 2009 00:07
No problem.

Enjoy your day.
Zero G Scott
16
Years of Service
User Offline
Joined: 14th Dec 2007
Location: California, USA, Earth, Sol Sys
Posted: 24th Jul 2009 21:26
Quote: "One question I have is in regard to the .6 and .8 hard coded into the function math. From what I can tell it's to adjust for the skew in the pixel difference from width to height"


Hey Latch, does that mean it would need to be adjusted to a different ratio if a resolution is used that isn't a 4:3 ratio? Some default resolutions are a 16:9 ratio. I'm curious what the ratio math would need to be if thats the case.
Latch
17
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 19th Aug 2009 06:09
@Zero G Scott
Didn't see this post when you wrote it. I think because you post so infrequently, your probation status isn't changing and your responses and posts don't show up immediately and may get lost in the mix, so to speak.

Anyway
Quote: "Hey Latch, does that mean it would need to be adjusted to a different ratio if a resolution is used that isn't a 4:3 ratio? Some default resolutions are a 16:9 ratio. I'm curious what the ratio math would need to be if thats the case. "

The ratio is based on the Field Of View (FOV) and the screen dimensions. I'd have to sit down with paper and pencil to figure out the specifics of the calculations used in the code that SmartGuy posted, but the way to calculate the ratio is by finding the Z distance to what we'll call the viewing plane where 1 2d pixel = 1 3d unit.

The default FOV for DBC in radians is 3.14/2.905 = 1.081 radians or about 62 degrees. The FOV could be set to anything and the ratio would have to be adjusted accordingly, but we'll only worry about the default.

Using that FOV, we can use right triangle math to find the distance to the view plane. Since the width and the height of the montior are different, we'll use the smaller of the too to calculate the ratio, that way skewing should be compensated for within DBC.

So, for a right triangle, we need three sides. The first side will be from the middle of the screen to the top of the screen. The second side will be the distance we want to find into the screen. The third side will be from the top of the screen into the screen to the view plane distance. The right angle is formed between the side closest to the viewer and the view plane distance.

The sloping side and the view plane side form our FOV angle. But since we are starting from the center of the screen, the screen is being cut in half. Therefore, we will use half of the FOV angle.

Using right triangle math, we know that
tan(angle)=opposite/hypotenuse

In this case it would be:
tan(fov/2)=(screen height()/2)/distance to the view plane

Since we want to find the distance to the view plane:
distance to the view plane = (screen height()/2)/tan(fov/2)

We'll call distance to the view plane zdist#.
The 3d x position in relation to the mouse x then becomes
distance from camera*((mousex()-(screen width()/2)))/zdist#)

and the 3d y position in relation to the mouse y becomes
object distance from camera*(((screen height()/2)-mousey())/zdist#)

Enjoy your day.

Login to post a reply

Server time is: 2024-04-24 00:45:14
Your offset time is: 2024-04-24 00:45:14