CFrame Math Problem

Howdy developers! I come to you today seeking assistance. I’ll try to keep the description as short as possible.

Monster exists in a labyrinth. He chases a player if he comes within line of sight. While not chasing, he travels node-to-node, turning randomly at each three-way intersection. Each ‘node’ is a part at the center of an intersection. Monster kills you by touching you.

The problem is that Monster can only travel node-to-node, so while chasing a player - when he gets to an intersection - he must choose which node to go to next. Right or left? The player is down one of them, and at the end of each hallway is another node at each intersection.

What methods would I use to determine which node Monster should travel to next, if the player exists somewhere between Monster and the left or right node? (There may be obstacles)

1 Like

Only use the node logic for when the monster is not hunting a player, but when it is, you’ll wanna use PathfindingService.

Here’s a tutorial too:
Character Pathfinding | Roblox Creator Documentation

1 Like

maybe you could use the dot product of the npc’s lookvector and the moster’s lookvector to see which node to go to.

Just know that if you do that it won’t be real-time and will stutter the NPC however you could counter this by having it switch to :MoveTo() if the player sees it. To prevent it from hugging the walls change the agents size

To determine which node the monster should go to, I would say start with pathfinding to the node the player is closest to, if that isn’t fast enough (or just doesn’t work) then use an a-star algorithm to find the closest (primal) path to the player using just the nodes. (I say primal because it should only be used as a guide, not the exact path itself.)

If the monster encounters a drop or staircase it’ll get stuck in a permanent loop (or fall outside the playable area).

I recommend you use SimplePath

It lets you spam a Path:Run method, which re-computes the path but does not cause the NPC to stutter.

Also, what is this title? This question has nothing to do with CFrames, it has to do with pathfinding.

If your paths are straight lines, then choose whichever intersection the player is nearest.

You can use the Dot product to achieve this:

local playerCFrame: CFrame = Player.CFrame

local monsterCFrame: CFrame = Monster.CFrame

local direction: Vector3 = playerCFrame.Position - monsterCFrame.Position

if monsterCFrame.RightVector:Dot(direction) > 0 then
    print("Right")
else
    print("Left")
end

Or else you can use a clever technique:

local playerCFrame: CFrame = Player.CFrame

local monsterCFrame: CFrame = Monster.CFrame

if monsterCFrame:ToObjectSpace(playerCFrame).X > 0 then
    print("Right")
else
    print("Left")
end

Howdy! This solution doesn’t seem to do the trick quite yet, but it’s close I think.

The hallways aren’t always strictly left vs right, some intersections are more like ‘forward vs 90 degrees right’, etc.

I was able to counter the drop or staircase problem by having it jump on chance every periodic loop.

Having it work based on random chance is worse than just getting to re-path without stuttering.