generic image
Processing...
  • Games
  • Catalog
  • Develop
  • Robux
  • Search in Players
  • Search in Games
  • Search in Catalog
  • Search in Groups
  • Search in Library
  • Log In
  • Sign Up
  • Games
  • Catalog
  • Develop
  • Robux
   
ROBLOX Forum » Game Creation and Development » Scripters
Home Search
 

Re: Accurately reflecting a ray?

Previous Thread :: Next Thread 
drahsid5 is not online. drahsid5
Joined: 13 May 2011
Total Posts: 3937
25 Jun 2015 03:41 PM
I can't figure out how to do this,
I don't know how to math.
I'm trying to reflect a ray off of the point another ray hits, accurately.
What's the math equation for this?

local ray = Ray.new(rayPart.Position, (rayPart.Position - Vector3.new(math.rad(0),math.rad(0),math.rad(45))).unit*300)

Defiantly isn't it.
Report Abuse
jode6543 is not online. jode6543
Joined: 16 Jun 2009
Total Posts: 5363
25 Jun 2015 03:56 PM
The angle of incidence is the angle of reflection. In other words, the angle a ray hits an object at (angle of incidence) is the same angle it will reflect at (angle of reflection). In Lua:

local ray = Ray.new([stuff])
local part, position, surfaceNormal = Workspace:FindPartOnRay(ray)
-- this just gets into CFrame; isn't strictly necessary, but makes it easier IMO
local dirRot = CFrame.new(Vector3.new(), ray.Direction.unit)
local surfaceRot = CFrame.new(Vector3.new(), surfaceNormal)
local reflection = surfaceRot:toObjectSpace(dirRot):inverse().lookVector
local reflectedRay = Ray.new(position, reflection)

Try that.
Report Abuse
drahsid5 is not online. drahsid5
Joined: 13 May 2011
Total Posts: 3937
25 Jun 2015 04:00 PM
Wow, thanks!
That worked.
But the ray seems to point at the floor? Maybe it's just the lookvector.
Report Abuse
jode6543 is not online. jode6543
Joined: 16 Jun 2009
Total Posts: 5363
25 Jun 2015 04:04 PM
Can I have a screenshot? I might have written that code incorrectly -- I haven't tested it.
Report Abuse
drahsid5 is not online. drahsid5
Joined: 13 May 2011
Total Posts: 3937
25 Jun 2015 04:08 PM
http://prntscr.com/7lbyct
Report Abuse
Dynerov is not online. Dynerov
Joined: 21 Jul 2014
Total Posts: 14682
25 Jun 2015 04:14 PM
You're shooting a bit upwards, that could be the thing.
Report Abuse
drahsid5 is not online. drahsid5
Joined: 13 May 2011
Total Posts: 3937
25 Jun 2015 04:16 PM
Same thing when shooting lower

http://prntscr.com/7lc2r6
Report Abuse
jode6543 is not online. jode6543
Joined: 16 Jun 2009
Total Posts: 5363
25 Jun 2015 04:51 PM
Seems I was over-complicating things. You don't actually need the surface normal. The reflection is just:

dirRot:inverse().lookVector
Report Abuse
drahsid5 is not online. drahsid5
Joined: 13 May 2011
Total Posts: 3937
25 Jun 2015 05:00 PM
Oh. :p
Report Abuse
drahsid5 is not online. drahsid5
Joined: 13 May 2011
Total Posts: 3937
25 Jun 2015 05:11 PM
Well, this happened:
http://prntscr.com/7lct6u
Report Abuse
MrNicNac is not online. MrNicNac
Joined: 29 Aug 2008
Total Posts: 26567
25 Jun 2015 05:39 PM
"Seems I was over-complicating things. You don't actually need the surface normal."

Yes you do. Not in two dimensions, but in three dimensions you do. Here's the proper way to do this:

http://prntscr.com/7ld536 (sorry, didn't crop it)

My code:

local RayBase = workspace.Model.RayBase

function RenderRay(R)
local Part = Instance.new("Part")
local RayLength = ( (R.Origin + R.Direction) - R.Origin ).magnitude
Part.Size = Vector3.new(0,0,RayLength)
Part.CFrame = CFrame.new(R.Origin, R.Origin + R.Direction) * CFrame.new(0,0,RayLength/-2)
Part.BrickColor = BrickColor.Yellow()
Part.Anchored = true
Part.Name = "Ray"
Part.Parent = workspace
end

do
-- Cast ray in direction of RayBase's front surface
local R = Ray.new(RayBase.Position, RayBase.CFrame.lookVector * 500)
local HitPart, HitPoint, Normal = workspace:FindPartOnRay(R, RayBase)
-- Override normal
Normal = Vector3.new(-1,0,0)
-- Draw the initial ray
RenderRay( Ray.new(RayBase.Position, RayBase.CFrame.lookVector * (HitPoint - RayBase.Position).magnitude) )

-- Calculate the direction of the reflected ray
local ClosestPointOnNormalLine = Ray.new(HitPoint, Normal):ClosestPoint(RayBase.Position)
local Alpha = (ClosestPointOnNormalLine - RayBase.Position).magnitude
local ReflectionDirection = (((2 * Normal) * (R.Unit.Direction:Dot(Normal))) - (1 * R.Unit.Direction)).unit
local ReflectedRay = Ray.new(HitPoint, ReflectionDirection * 100, HitPart)
RenderRay(ReflectedRay)
end
Report Abuse
MrNicNac is not online. MrNicNac
Joined: 29 Aug 2008
Total Posts: 26567
25 Jun 2015 05:43 PM
My bad, the code under "--Override normal" should be:

Normal = Vector3.new(Normal.Y, Normal.Z, Normal.X)

The normal needs to be shifted, not struct as a constant
Report Abuse
jode6543 is not online. jode6543
Joined: 16 Jun 2009
Total Posts: 5363
25 Jun 2015 06:00 PM
@MrNicNac
What's all that ClosestPoint and Alpha code for? You never use it. Also, in your reflected ray, you call Ray.new with three arguments. Regardless, your code is probably more correct than mine.
Report Abuse
MrNicNac is not online. MrNicNac
Joined: 29 Aug 2008
Total Posts: 26567
25 Jun 2015 06:09 PM
@jode6543

ClosestPoint and Alpha variables weren't used because I was going in order from the article in that screenshot. It turned out they were eventually cancelled out in the equation (the alpha variable).

The Ray.new() being called with three variables is because I mixed it up with FindPartOnRay. Notice the third argument is a part, meant to be the ignored part.
Report Abuse
drahsid5 is not online. drahsid5
Joined: 13 May 2011
Total Posts: 3937
25 Jun 2015 06:17 PM
Wow, thank you for that, MrNicNac.
What's the link to the webpage for that calculation?
Report Abuse
MrNicNac is not online. MrNicNac
Joined: 29 Aug 2008
Total Posts: 26567
25 Jun 2015 06:22 PM
Just zoom in on the left monitor from the screenshot. It's in the address bar.
Report Abuse
drahsid5 is not online. drahsid5
Joined: 13 May 2011
Total Posts: 3937
25 Jun 2015 06:24 PM
Alright, thanks!
Report Abuse
Previous Thread :: Next Thread 
Page 1 of 1
 
 
ROBLOX Forum » Game Creation and Development » Scripters
   
 
   
  • About Us
  • Jobs
  • Blog
  • Parents
  • Help
  • Terms
  • Privacy

©2017 Roblox Corporation. Roblox, the Roblox logo, Robux, Bloxy, and Powering Imagination are among our registered and unregistered trademarks in the U.S. and other countries.



Progress
Starting Roblox...
Connecting to Players...
R R

Roblox is now loading. Get ready to play!

R R

You're moments away from getting into the game!

Click here for help

Check Remember my choice and click Launch Application in the dialog box above to join games faster in the future!

Gameplay sponsored by:
Loading 0% - Starting game...
Get more with Builders Club! Join Builders Club
Choose Your Avatar
I have an account
generic image