Advertisement
StefanBashkir

Ray Reflection

Jun 25th, 2015
272
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.27 KB | None | 0 0
  1. local RayBase = workspace.Model.RayBase
  2.  
  3. function RenderRay(R)
  4.     local Part = Instance.new("Part")
  5.     local RayLength = ( (R.Origin + R.Direction) - R.Origin ).magnitude
  6.     Part.Size = Vector3.new(0,0,RayLength)
  7.     Part.CFrame = CFrame.new(R.Origin, R.Origin + R.Direction) * CFrame.new(0,0,RayLength/-2)
  8.     Part.BrickColor = BrickColor.Yellow()
  9.     Part.Anchored = true
  10.     Part.Name = "Ray"
  11.     Part.Parent = workspace
  12. end
  13.  
  14. do
  15.     -- Cast ray in direction of RayBase's front surface
  16.     local R = Ray.new(RayBase.Position, RayBase.CFrame.lookVector * 500)
  17.     local HitPart, HitPoint, Normal = workspace:FindPartOnRay(R, RayBase)
  18.     -- Override normal
  19.     Normal = Vector3.new(Normal.Y, Normal.Z, Normal.X)
  20.     --Normal = Vector3.new(0,1,0)
  21.     -- Draw the initial ray
  22.     RenderRay( Ray.new(RayBase.Position, RayBase.CFrame.lookVector * (HitPoint - RayBase.Position).magnitude) )
  23.    
  24.     -- Calculate the direction of the reflected ray
  25.     local ClosestPointOnNormalLine = Ray.new(HitPoint, Normal):ClosestPoint(RayBase.Position)
  26.     local Alpha = (ClosestPointOnNormalLine - RayBase.Position).magnitude
  27.     local ReflectionDirection = (((2 *  Normal) * (R.Unit.Direction:Dot(Normal))) - (1 * R.Unit.Direction)).unit
  28.     local ReflectedRay = Ray.new(HitPoint, ReflectionDirection * 100, HitPart)
  29.     RenderRay(ReflectedRay)
  30. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement