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 / Click and drag draw a 3d box

Author
Message
Latch
17
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 3rd Nov 2009 10:10
I'm trying to figure out a way to click somewhere on the screen with the mouse, and while holding the mouse button, drag to draw a 3d box object.

Something like this:



But using 3d box objects (in 3d mode!) .

Any ideas would be greatly appreciated. Thank you.

Enjoy your day.
TDK
Retired Moderator
21
Years of Service
User Offline
Joined: 19th Nov 2002
Location: UK
Posted: 3rd Nov 2009 14:48
Just knocked this together for you.

It's not very elegant, but it does work - and might give you a few ideas of your own...



TDK

TheComet
16
Years of Service
User Offline
Joined: 18th Oct 2007
Location: I`m under ur bridge eating ur goatz.
Posted: 3rd Nov 2009 22:13 Edited at: 3rd Nov 2009 22:16
The maths for finding the point of the mouse's position in 3D space is quite easy. Thanks to my WaypointPro Waypoint Editor I found the required knowledge. As long as you don't change the camera FOV, it should work.

I don't guaranty this will work, because I can't compile and run anything right now, but I'll see what comes off the top of my head.

This should position a 3D object at the mouse's position:



As you can see, I offset the mousex and y position to fit the center point of the screen, and I invert mouse y to fit the 3D y axis.

The above code will only position the object at the mouse's position if it's distance to the camera is 400, and the camera is rotated to 0,0,0 and positioned at 0,0,0.

If you would like the object to be closer, you will need to divide the offset and inverted mousex and y values by the factor you change the distance of. I know that if you position the object at half the distance, you also need to half the mousex and y values. So this proves that it is proportional.

So now we have the formula:

x#=(mousex()-320)/400.0*distance#
y#=(240-mousey())/400.0*distance#
z#=distance#


Now comes the tricky part. Changing resolutions. The object coordinates will always be based on a 640*480 display, even if you use 800*600. It's quite hard to explain. What you have to do is multiply the mouse coordinates by the change factor of the screen dimension based on 640*480. That would be:

mousex()*(640/screen width())
mousey()*(480/screen height())


So now, what ever you change the resolution to, the range mousex can go to is always between 0-640, and mousey between 0-480. So lets add this to the formula:

x#=((mousex()*(640/screen width()))-320)/400.0*distance#
y#=(240-(mousey()*(480/screen height())))/400.0*distance#
z#=distance#


And that's it! Remember that this will only work if the camera is rotated at 0,0,0 and the distance of the object is only on the Z axis.

If you would like this to work with a rotating camera, you will need to use matrix transformation calculations. Good luck!

TheComet


Make the paths of your enemies easier with WaypointPro!
Libervurto
17
Years of Service
User Offline
Joined: 30th Jun 2006
Location: On Toast
Posted: 3rd Nov 2009 22:44
@TDK
That's quite a nice simple method. Do you know what the OBJECT SCREEN calculations actually are? It's a shame it has to be an object, I'd like to be able to put any 3D co-ordinate into a function and return the 2D.

Another thing, I hear ABS is slow and I was wondering if using OR would work and would it be quicker?
so...

would become

I can't tell if I'm being clever or stupid here
Oh no it's stupid I'll leave it there though, there must be a way to do that with binary logic.

"With game, we create these elaborate worlds in our minds, and the computer is there to do the bookkeeping." - Will Wright
Latch
17
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 4th Nov 2009 01:23
@TDK
Thank you. That's a great example. That gives me a lot of ideas. Scale the dimensions separately... I like it. It's just so crazy that it might work!

@TheComet
Thank you. I had considered using 2d to 3d and 3d to 2d conversions (and I still may), but when dragging the mouse in 3 dimensions at once, the "into the screen" part of the selection box starts to become ambiguous. I'll keep this method in mind.

Perhaps you have some suggestions for orthogonal projection?

Enjoy your day.
Caleb1994
15
Years of Service
User Offline
Joined: 10th Oct 2008
Location: The Internet you idiot!
Posted: 4th Nov 2009 20:15 Edited at: 4th Nov 2009 20:17
Quote: "Do you know what the OBJECT SCREEN calculations actually are"


3D Projection

the calculation is:

ScreenX = (ObjDistanceFromCamX - ViewerDistanceFromCamX) / (ViewerDistanceFromCamZ / ObjDistanceFromCamZ)
ScreenY = (ObjDistanceFromCamY - ViewerDistanceFromCamY) / (ViewerDistanceFromCamZ / ObjDistanceFromCamZ)

or is the viewer is at 0,0,z then:

ScreenX = ObjXDist / (ViewZDist / ObjZDist)
ScreenY = ObjYDist / (ViewZDist / ObjZDist)

viewer Z distance from camera is usually dependant on the fov and stuff, but you can throw different values in for different effects(zoom) but for the fov being 62 the formula is

viewZdist = HalfScreenHeight / tan(HalfFOV)

Thanks for that one latch! lol

New Site! Check it out \/
Latch
17
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 5th Nov 2009 01:36
Quote: "It's a shame it has to be an object"

For my purposes this is just right. I can just use the built in scale object commands (thanks for the idea TDK) to drag a box however I want. I offset the pivot of the box to one of it's corners. Then drag in whatever dimension I select. This way it behaves like the 2d bounding area box. I can calculate any other corner simply by using the first corner and the object size commands. The best part, it's all in 3d coordinates so I don't have to do any heavy maths.

Enjoy your day.
Yodaman Jer
User Banned
Posted: 6th Nov 2009 19:16
@TDK:

That's a great code snippet for something like a level editor! Once I get DBPro I may just modify this and try a few things out...the reason I'll be using DBPro is for the faster sync rates.

But first I must get my laptop...

"The fools may crash down upon us in thunderous waves, but we shall Jeku slap them back from whence they came"
-BiggAdd, Oct 28th 2009
Caleb1994
15
Years of Service
User Offline
Joined: 10th Oct 2008
Location: The Internet you idiot!
Posted: 6th Nov 2009 20:08
Oh darn. we are probably going to see a hit to the attendance of this board since it's free now... lol

New Site! Check it out \/
TDK
Retired Moderator
21
Years of Service
User Offline
Joined: 19th Nov 2002
Location: UK
Posted: 7th Nov 2009 16:46 Edited at: 7th Nov 2009 16:59
Quote: "we are probably going to see a hit to the attendance of this board since it's free now"


Probably, but maybe not such a big hit.

It may be free, but anyone who isn't good with DBC is going to be worse with DBP as it's more complex.

I have DBP and many of the plugins, but I still prefer to use DBC as I feel 'more comfortable' using it I suppose. It's hard to describe...

The point is, there will be a few who get the free version of Pro, have a bit of a tinker then like me, come back to DBC.

It's a bit premature to announce it officialy, but I am planning a nice DBC Christmas programming competition with prizes, so we might see the free DBP version pushed to one side for a bit...

In the new year maybe I will look at DBC and decide whether it's time to call it a day and force myself to commit fully to DBP. It's very quiet on this board compared to just 6 months ago and I wonder if it's still worth it.

Variation of the 3D box draw snippet which draws in a single Repeat..Until loop:



TDK

Latch
17
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 10th Nov 2009 07:52
Thanks TDK, it's a useful example.

Quote: "In the new year maybe I will look at DBC and decide whether it's time to call it a day and force myself to commit fully to DBP. It's very quiet on this board compared to just 6 months ago and I wonder if it's still worth it."

You mean worth it to participate on the DBC board? DBC is still easy and fun to use. I like the "type and go" functionality. As far as BASIC languages go, it's one of the easiest I've ever used.

Enjoy your day.

Login to post a reply

Server time is: 2024-04-25 14:22:59
Your offset time is: 2024-04-25 14:22:59