# How to clamp a position to the FrustumFar... left and right?

Hi, I hope somebody knows maths more than I do. I’ve been pulling my hair out for a day now trying to position a node to the “FrustumFar” even when the camera doesn’t look straight at it. If I position my node to (0,0,1000) and aim at it, it’s perfectly on the edge of the FrustumFar. The problem is if I do not aim at it, like if I aim at the left of the node, then it goes out of the Frustum. Of course, because the Frustum is not a cube shape, thus the left/right sides are shorter if you will. Here’s a graphic to show you what I mean:

As you can see, Figure 1. shows the camera at (0,0,0) aiming straight at a red shape node that is positioned at (0,0,1000) which is the FrustumFar. All is well if the camera aims at it. Now on Figure 2. you can see that the camera is aiming on the left of the red shape node, let’s say it is rotated at about (0,65,0) and the red shape node is still at (0,0,1000) but the problem is of course that it is now out of the Frustum and so it disappears. I tried to change the values of cam.setFrustumLeft() (or right) but it distorts the viewport of course, because those settings are a proportion and not actual size, therefore, it wouldn’t make sense to change them if I understand correctly. Now how the heck would one go and position the red shape node where the green dot is on Figure 2. meaning clamped on the FrustumRight (or FrustumLeft IF the camera was looking on the right side of the red shape node) therefore how does one go and clamp a node on the Frustum or whatever side it would otherwise go out of bounds?

This looks like serious math to me! Am I mistaken to think there must a function implemented in the SDK that does this clamping for us?

Thank you so much for reading!

Can I ask what this is needed for? I can’t imagine a use-case for this (which doesn’t mean there isn’t one) so I start to wonder if there is maybe a different way to solve the problem.

Clamping to the frustum is possible with some math but I can think of one or two different ways to do it depending on what you are actually trying to accomplish.

Hi Paul. First thank you so much for replying that fast. Your input is very much appreciated.

Well, I (think I) need this to position a lamp node in front another node that is drawn in the sky bucket, therefore that is in the “falsely far away” fixed background where there are no real coordinates I could copy from, else I’d just have translated my node to it and z-=1’ed it.

Since the sky bucket stuff is not really far away that much (it’s just a trick in the render queue that makes it look far away, it looks as if it never moves about, it’s “pinned” like a static background if you will), then I realized I’d have to compute a direction vector from the camera to that node in the sky bucket which would give me a small vector that I would use to rotate the node I wanted to place in front of it, then I multiply it by the FrustumFar scalar, so that it is perfectly aligned with the other node in the sky bucket, but is “clamped” to the FrustumFar (updating it every update frame) so when I move the camera about anywhere, it always is “pinned” on top of the other node in the sky bucket, which works only if I aim at it, because otherwise if the camera is not looking directly at it, it falls offscreen since the FrustumFar=1000 is only valid for the FrustumFar, not the FrustumLeft or right you know?

There must be an easier way to go about this, I guess, I’m just not much of a math guy, which I’m not too proud to tell!

I’m wondering how would one go and compute it with the new camera angle. Something like: The more you are off from the origin (angleY=0) the more the node has to be brought near the camera, thus z=800,700,600… you know? Maybe like a rule of three or something, altough it’s probably a very sloppy way to “make it work” but since FrustumLeft and right are at the exact same distance from the center, I guess it’s symetrical so it would be like this:

http://fooplot.com/#W3sidHlwZSI6MCwiZXEiOiIoLSh4LzEwKV4yKSsxMDAwIiwiY29sb3IiOiIjRTM2NzI0In0seyJ0eXBlIjoxMDAwLCJ3aW5kb3ciOlsiLTExNjAuNzYxODAzMzQ3NTI5MiIsIjExNDguNTAyMDg3ODcyNzcyOCIsIi0zMDMuODkxNDg1NDk4NzYwNjMiLCIxMTE3LjE5Mzk4NjAyMTQyNTYiXSwic2l6ZSI6WzExNTAsNTAwXX1d

That’s about as far as I’m going to go right now, I need some sleep, but let me think what you think!

Thank you so much.

I’m still trying to understand what needs to happen to the node. What is it in the sky? Maybe that will help.

Because it could be that you just need to place it relative to the camera.

Well, yes, it’s already relative to the camera, but the question on every frame update is how far from the camera until it goes out of the Frustum bounds? Like I said, I already have the rotation vector that makes it align perfectly with the node in the sky bucket, so only thing left is to compute somehow the distance until it goes out of the Frustum bounds.

Since it’s a light, it has to be far away enough that it is behind every other nodes, except for that target node in the sky bucket (on my graphic in the top post, it’s the red shape node)

did you try to put it in the sky bucket?

1 Like

wait… WHAT?! All this air pulling for a solution THAT simple?! Well, I’ll be F**** damned Rémy It couldn’t have been more obvious! Thank you so much for opening my eyes. I guess I was too tired and went in the wrong direction right from the start. I’m shocked.

Thank you so much guys, I still can’t believe it >.<