Constrain CFrame so it does not cross a certain point?

How do I forcefully constrain the green orbs CFrame so it cannot cross behind the red barrier which is at (CFrame.new(rootPart.CFrame.p)-camera.CFrame.LookVector*2)*(camera.CFrame-camera.CFrame.p)?

You can use plane-point intersection

If you can give us the normal of the plane of the part facing your character and a point on the plane you are using to constrain the point you can do:

local n = normal
local p = pointOnPlane
local x = pointToConstrain

local signedDist = n:Dot(x-p)
if signedDist > 0 then
   x=x-n*signedDist -- move x onto plane
else
   -- point is on other side of/on plane

(Typed from phone so that’s why it’s pseudo-code)

Normal and point on plane would change every time part updates

To get normal and point on plane for upFace you can do:
local normal = partCf.upVector
local point = part.Cf*(0,part.Size.Y/2,0)

3 Likes

What if I wanted the barrier to have an infinite size? It is so small just as an example so you could see it more clearly.

So I’ve been playing around with your code and I don’t understand what it’s doing at all? I assume it has something to do with putting the orb on a plane so it cannot cross it, but it’s not doing that at all :confused:

What I’m trying to do is to constrain the orbs CFrame so it can only move around freely behind the character, but have the constraint zone rotate with the camera. It’s kinda hard to explain, if you don’t understand I can show you more examples.

This assumes the plane has an infinite size

do you agree that the normal of a plane is perpendicular to the surface of the plane?
So if a point is on the surface of the plane, it’s dot product with the normal is 0

But If the dot product is > 0 then that means it’s angle with the normal <90 (definition of dot product, idk proof of dot product) and it means the point is outward of the plane in the normal direction

Also by trig u can see that the dot product is the magnitude of the projection of the point on the normal (since dot product = cos angle * mag(point) *mag(normal) and u know mag(normal)=1 since it’s a unit vector)

If dot is negative then u know it’s in opposite direction of normal, and u can just add the normal*mag (since it’s signed) to the original point to get the point projected on the plane

But a key thing to remember is that you need to localize the point into the plane by using subtracting a sample plane point from it (to make that sample plane point be the origin)

It’s super annoying to type code on phone so if no one answers I’ll type out a coded solution during lunch (1.6 hrs)

2 Likes

Thanks for the great explanation :hugs: I still didn’t understand a couple things, but I’ll play around with it and let you know if it worked :+1:

Thanks a lot! It worked exactly how I wanted it to

The reason I was confused at first was because I used upVector and messed up some of the values, but after you explained it I realised I have to use lookVector. Thanks :relaxed:

1 Like

If you want to ask about what you don’t understand I’ll try my best to explain it