Understanding how to create beams in raycasting

Goal?

I’m trying to understand how I can make a basic beam in raycasting.

Issue?

I don’t understand the math used in raycasting.

What have I tried?

I’ve looked at various raycasting resources which have shown the basics of raycasting but I want to adapt and grow my skills in raycasting. I’m also watching a video which highlights how to make a beam and I’m trying to understand the math functions.

Main questions:
-How does the create beam function in the video work?(in the raycasting section of the video)
-Is there any math, vector, or magnitude related topics I should know to improve in raycasting?

Specific questions:
-How does line 18 do or "part.CFrame = CFrame.new(midpoint, origin)"work in the video?
-Why is the midpoint = origin + direction/2?

Video regarding specific questions: https://www.youtube.com/watch?v=MQCArb1XiBc&t=959s

Resources I’ve read:
-Resource1
-Resource2

2 Likes

In regards to some of your questions:

  • “How does line 18 do or ‘part.CFrame = CFrame.new(midpoint, origin)’ work in the video?”

This is setting the part’s CFrame (Coordinate-Frame, a 10-parameter representation of position and rotation) to be at the midpoint, facing the origin. CFrame.new can take two parameters, a position CFrame.new(pos: Vector3), or a position and a vector to look at, CFrame.new(pos: Vector3, lookAt: Vector3).

  • “Is there any math, vector, or magnitude related topics I should know to improve in raycasting?”

I would look into learning more about Vectors, such as their properties and what they represent. Learning how to manipulate vectors is really powerful in terms of programming in general, as positions are represented by vectors.

2 Likes

With vectors I recommend starting off with learning vector diagram.

This will help you visualize how these position and direction vectors adds up

For the mid point example you first start up by adding origin which is the position vector of the pistol/gun barrel. In the diagram above it is the blue vector going from world origin (0, 0, 0) towards P1 (variable).

midpoint = origin

Once you reach P1 you add direction which is a direction vector starting from P1 that goes to P2 which is usually the mouse/target position. This is usually calculated via direction = mousePosition - gunPosition (requires a different vector diagram to visualize this). This will end up with the purple vector in the diagram above causing the path to end up at P2 or mouse position.

midpoint = origin + direction

To position the brick in the middle you can divide this direction vector evenly by 2 resulting in a point between point p1 and p2 hence midpoint above

midpoint = origin + direction/2

Did this at 2 am on my phone apologies for any errors.

2 Likes

Does the lookAt make the supposed beam align with the origin of the ray? Is this how the beam is in it’s proper position in the 3D space when clicked?

1 Like

Oh I see now! That’s how the beam is in the middle.

1 Like

Also in another one of B Ricey’s videos he says this method is deprecated, is there another method I should use to align and position the part properly?

https://www.youtube.com/watch?v=m8nse0q1l5w&t=367s

In the video at about exactly minute 6 he says the method is deprecated.

1 Like