Making a CFrame that is orientated to look at a position but is also offset from the center so that orientation is only done around a position?

So as you guys know doing CFrame.new(pos,look) will create a CFrame at pos oriented to look.

I was wondering how you could make a CFrame that is offset from another point that can only rotate around said point but still have it’s lookVector face the look position.

If I do,

while wait() do
	part1.CFrame = CFrame.new((part0.CFrame * CFrame.new(0,part0.Size.Y/2,0)).p,target.CFrame.p)
end

I get the problem which is the part is centered on the offset point(which happens to be the top of another part).
That looks like this:

However, I can do

while wait() do
	part1.CFrame = CFrame.new((part0.CFrame * CFrame.new(0,part0.Size.Y/2,0)).p,target.CFrame.p) * CFrame.new(0,part1.Size.Y/2,0)
end

This results in something that looks okay:

The problem is because I moved it up the angle is actually off by a little bit:
https://gyazo.com/dac0c8b01ce83b2ce1bb4e2299426f54
It’s very little but I was wondering if there is a better method for getting an accurate CFrame?

11 Likes

I am literally trying to solve this exact problem right now for myself.

1 Like

You get any further than me?

1 Like

Multiplying lookvector will keep the rotation, while “moving” the CFrame towards (or backwards if negative)

1 Like

Can you explain this some more I don’t quite get what you mean. What are you supposed to do with lookVectors?

Edit: I know what you mean now but the problem isn’t the rotation is being changed it’s that because the part is being moved up, it no longer points directly at where I intended it too

The system from what I can tell based off what you described is over constrained. Hard to tell without pictures though. They are broken for me.

Rotating around a point locks in all 3 axis of rotation

To demonstrate:

The yellow part here is the center, green is the block you are moving, and red is the target.

image

If you were to move the green part around the center it would rotate like so:

image

In order for it look at the target, it would have do something like his:

image

Which at that point it is no longer rotating around the center part.

You have to first set position, and then make it look towards other part

After like 2 hours of geometry (much longer than this should have taken, but it’s 2:30AM at the time of posting this), I got it working. If you have any questions I’ll answer them tomorrow when the rest of my brain is functional

local part = script.Parent
local base = workspace.Base
local target = workspace.Target
local radius = part.Size.Y/2
local basePos = base.Position + Vector3.new(0,base.Size.Y/2,0)

while wait() do
    local a = basePos
    local b = target.Position
    local d = (a-b).Magnitude
    
    local c = a.Y - b.Y
    local B1 = math.acos(c/d)
    local B2 = math.acos(radius/d)
    
    local angle = math.pi - B1 - B2
    
    local x = radius * math.sin(angle)
    local y = radius * math.cos(angle)
    
    local dir = Vector2.new(b.x-a.x,b.z-a.z).Unit
    
    local position = basePos + Vector3.new(x*dir.X,y,x*dir.Y)
    
    part.CFrame = CFrame.new(position,b)
end

Here is the result: https://gyazo.com/b447904efa8f81c18d970238e798d8ca

24 Likes

Thanks man.

1 Like