Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Can someone answer these raycasting questions? [closed]

Asked by
Perci1 4988 Trusted Moderation Voter Community Moderator
9 years ago

I'm confused about some things in raycasting.

local ray = Ray.new(tool.Handle.CFrame.p, (mouse.Hit.p -  tool.Handle.CFrame.p).unit*300)

The first argument is start, and the second is offset. The start is where the ray is coming from and the offset is how far to check off of the start. Here are my questions:

  1. Why subtract tool.Handle.CFrame.pfrom mouse.Hit.p? Isn't the goal to create a ray between the handle and the mouse? If so, why are we subtracting things from the mouse?

  2. What does .unit do what why multiply it by 300?

local hit, position = game.Workspace:FindPartOnRay(ray, user)
--sutff here
local distance = (position - tool.Handle.CFrame.p).magnitude
--stuff here
rayPart.CFrame        = CFrame.new(position, tool.Handle.CFrame.p) * CFrame.new(0, 0, -distance/2)

So, this is CFrame.new(startPosition, lookAt). We're making the ray start at the position the hit was at, and point towards the tool. So,

  1. Why multiply it by -distance/2? I know the wiki says "Then we offset it by negative the distance between the points divided by two, so we end up in the middle." I know that's what it does, but why does it work like that? It does not make sense to me.

Locked by Perci1, Shawnyg, and BlueTaslem

This question has been locked to preserve its current state and prevent spam and unwanted comments and answers.

Why was this question closed?

1 answer

Log in to vote
3
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
9 years ago

1. When we move from one point to another, we move a certain distance in a certain direction. That quantity is a vector (a distance in a particular direction). The vector meaning the path from one point to another is called the displacement from the start point to the end point.

The displacement from point A to point B is B - A.

This makes sense since if we start at A, we move the displacement, and we should get at B. That is, A + D = B, so D = B - A.

So to find the direction and distance from from to to, we do to - from.

2. A unit vector is a Vector of length one (the unit). .unit turns the displacement into a direction by ignoring how long it is. We change it into something we know is length 1. By multiplying it by 300, we get something that we now know is length 300 but still in the direction from the tool to the mouse.

3. The math works out that way because of the matrix definition of CFrames.

A clearer way to write it would be:

rayPart.CFrame = CFrame.new(   (position + tool.Handle.CFrame.p) / 2 ,  tool.Handle.CFrame.p)

Remember,startPosition is the center of the part, not one of its ends. We want the part to span from position to the tool (tool.Handle.CFrame.p). So the center is actually halfway,length / 2, from either position or tool.Handle.CFrame.p!

The * CFrame.new(0,0, -length/2) essentially moves the CFrame forward length/2 studs, which puts one end of the part at position.


Hopefully this is all clear enough, I can edit this to improve anything if you want something better explained

0
I understand 2 and 3 but I'm still confused with number 1. What do you mean by 'displacement'? I know you said it's the direction * the distance, but I don't understand that either. Perci1 4988 — 9y
0
I edited the explanation to be more thorough BlueTaslem 18071 — 9y
0
Thank you! Perci1 4988 — 9y
Ad