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 Professional Discussion / Work out if a projected 3d point is behind the camera

Author
Message
Sasuke
19
Years of Service
User Offline
Joined: 2nd Dec 2005
Location: Milton Keynes UK
Posted: 30th Oct 2013 23:56
How would you work out if a projected 3d point, so a 3d point in screen space is behind the camera? I've heard of people using the z component of the result of project vector3 in other languages but in DBP, it doesn't go negative so that's out. I just can't see how you would check unless I built my own project vector3 function maybe.

Cheers for help in advance

"Get in the Van!" - Van B
Le Verdier
13
Years of Service
User Offline
Joined: 10th Jan 2012
Location: In the mosh-pit
Posted: 31st Oct 2013 00:06 Edited at: 31st Oct 2013 00:09
DotProduct(ViewMatrix.Zaxis, Point-CameraPos)
If negative, the point is behind...


Sasuke
19
Years of Service
User Offline
Joined: 2nd Dec 2005
Location: Milton Keynes UK
Posted: 31st Oct 2013 00:31 Edited at: 31st Oct 2013 01:11
I've tried that and it doesn't seem to work. Also I should of said out side of the viewing plane rather than behind it! I just want to work out if it's out of the screen.

EDIT:
When you say ViewMatrix.Zaxis, do you mean forward vector?

I guess I could just do frustum culling now I think of it but that would work out if the point is outside the frustum rather than a projected point

"Get in the Van!" - Van B
Le Verdier
13
Years of Service
User Offline
Joined: 10th Jan 2012
Location: In the mosh-pit
Posted: 31st Oct 2013 09:24
Yes
Get Matrix4 Element(8/9/10)

It should work
!

Sasuke
19
Years of Service
User Offline
Joined: 2nd Dec 2005
Location: Milton Keynes UK
Posted: 31st Oct 2013 10:24
This just doesn't seem to work when the view matrix changes or position to point!

"Get in the Van!" - Van B
Le Verdier
13
Years of Service
User Offline
Joined: 10th Jan 2012
Location: In the mosh-pit
Posted: 31st Oct 2013 10:44
Maybe a problem somewhere in the code...


Sasuke
19
Years of Service
User Offline
Joined: 2nd Dec 2005
Location: Milton Keynes UK
Posted: 31st Oct 2013 11:02
Not that I can see...



"Get in the Van!" - Van B
Rudolpho
19
Years of Service
User Offline
Joined: 28th Dec 2005
Location: Sweden
Posted: 31st Oct 2013 11:25 Edited at: 31st Oct 2013 11:26
What is the application of this?
If you just need to know if a position is outside the camera's view you can transform it to clip space (world * view * projection) and check if any of the coordinates are out of the ranges (x: [-1 .. +1], y: [-1 .. +1], z: [0 .. 1]).

As you said it seems the Z component doesn't go negative when behind the camera but rather wraps around to a large positive value.


Edit: removed unwanted smilies from the coordinate range part...


"Why do programmers get Halloween and Christmas mixed up?"
Sasuke
19
Years of Service
User Offline
Joined: 2nd Dec 2005
Location: Milton Keynes UK
Posted: 31st Oct 2013 11:42
Quote: "What is the application of this?"

I wanted to change parts of my editor, this part being highlighting point. Originally I just used billboards but now I want to use 2d. The projecting is simple but check with it's outside the view isn't.

Quote: "If you just need to know if a position is outside the camera's view you can transform it to clip space (world * view * projection) and check if any of the coordinates are out of the ranges (x: [-1 .. +1], y: [-1 .. +1], z: [0 .. 1])."


Sounds good, I'll have a go

Quote: "removed unwanted smilies from the coordinate range part..."


Now I have nothing to smile at

"Get in the Van!" - Van B
Le Verdier
13
Years of Service
User Offline
Joined: 10th Jan 2012
Location: In the mosh-pit
Posted: 31st Oct 2013 11:54 Edited at: 31st Oct 2013 12:07
Ahhhh..
I found what is wrong
The view matrix4 give inversed camera matrix
Just use inverse matrix4!
..
Also, The normalize shouldnot be usefull

...
Maybe some screenies of your editor!?

Sasuke
19
Years of Service
User Offline
Joined: 2nd Dec 2005
Location: Milton Keynes UK
Posted: 31st Oct 2013 13:40
Quote: "Just use inverse matrix4!"


Cheers that fixed it, awesome

Quote: "Also, The normalize shouldnot be usefull"


The reason is to normalize is the keep the result of the dot product between -1 - +1.

The main reason is now I need to work out the vertical and horizontal bounds based off aspect ration and fov so that I can just check if the result of the dot product becomes lower than the bounds. Not entirely sure how to find these bounds, but I'll give it a good go!

"Get in the Van!" - Van B
Derek Darkly
13
Years of Service
User Offline
Joined: 22nd Sep 2011
Location: Whats Our Vector, Victor?
Posted: 31st Oct 2013 16:46 Edited at: 31st Oct 2013 16:48
Quote: "Also I should of said out side of the viewing plane rather than behind it! I just want to work out if it's out of the screen."


Perhaps I'm oversimplifying what you want, but if I wanted to see if a particular point in space was outside the camera view I would just place a small dummy plain there and use the OBJECT IN SCREEN command.

(This may be completely useless for what you want to do.)

D.D.
Van B
Moderator
22
Years of Service
User Offline
Joined: 8th Oct 2002
Location: Sunnyvale
Posted: 31st Oct 2013 18:14
There is a cheats way, but people will laugh if I bring it up...

Oh well...

Check the distance between the point and the camera.

Move the camera forward by 1 unit.

Check the distance again.

Move the camera backward by 1 unit.

Then if the first distance is greater than the second, the point is in front of the camera, otherwise it's behind.

I am the one who knocks...
Le Verdier
13
Years of Service
User Offline
Joined: 10th Jan 2012
Location: In the mosh-pit
Posted: 31st Oct 2013 19:48 Edited at: 31st Oct 2013 19:51
Nice find, but I fear that this implies a load of
calculations compared to the dotproduct method..

the costliest part is the inverse matrix..

But there is an awesome plugin Extended Rotation by madbit
http://forum.thegamecreators.com/?m=forum_view&t=192386&b=5
There is a DBCAM GET LOOK VECTOR xrotVecResult, CameraID function to directly get the vector..

@Sasuke Any chance to see screenies of your editor ?? I like screenies of 3D editors...

OT Grrr, it seems that the imageserver with my images is down..

Sasuke
19
Years of Service
User Offline
Joined: 2nd Dec 2005
Location: Milton Keynes UK
Posted: 1st Nov 2013 12:54 Edited at: 1st Nov 2013 12:57
Quote: "Perhaps I'm oversimplifying what you want, but if I wanted to see if a particular point in space was outside the camera view I would just place a small dummy plain there and use the OBJECT IN SCREEN command."


Nearly, but basically I want a 3D POINT IN SCREEN command, also object in screen is terrible command, avoid using it and go with frustrum culling which in it's simplest form is easy to implement for bounding meshes like spheres and boxes.

@Van B, I learned that from you a long time ago Thou I usually go with the math (trig) approach!

Though, looking at it now and doing tests, the inverse of the view method is really fast. For instance:

1. Inverse Method is the fastest
2. Trig Method is 1.75 times slower (what I've been using cause I can supply it with any angle from whatever)
3. Van B's cheap method is 3 times slower than the fastest

Quote: "@Sasuke Any chance to see screenies of your editor ?? I like screenies of 3D editors..."


Well it's a new editor. Need to rewrite mine where I could edit the editor inside my editor, confusing yes. But I was thinking the other day, I created an object and my standard editor just didn't show all the info I wanted to see and it hit me, what if I could script how the editor functions inside the editor, so the major rewrite has begun. Gimme a sec to make a pic, I was rewriting something so it doesn't display anything at the mo, I warn you, early stage... but it's pretty!

"Get in the Van!" - Van B

Login to post a reply

Server time is: 2025-05-16 14:54:21
Your offset time is: 2025-05-16 14:54:21