Data Realms Fan Forums http://45.55.195.193/ |
|
What's the formula for reflecting an object off a surface? http://45.55.195.193/viewtopic.php?f=1&t=29848 |
Page 1 of 1 |
Author: | ryry1237 [ Sat Feb 04, 2012 9:12 am ] |
Post subject: | What's the formula for reflecting an object off a surface? |
I decided to put this in mod making since it's a very useful question when it comes to modding shields and force fields that have realistic reflections and ricochets, and because it's a question I've been thinking over for awhile but haven't gotten anywhere. A trig question for the math pros out there. Given the angle of the surface (in radians) and the vector of the incoming ray (in x/y coordinates), what formula should I use to get the coordinates of the resulting ray? This is assuming that the bounce is perfect and the speed of the ray remains constant. bonus: What would the formula be if this were to happen in 3-D space (x, y, z coordinates)? |
Author: | Mehman [ Sat Feb 04, 2012 10:46 am ] | ||
Post subject: | Re: What's the formula for reflecting an object off a surface? | ||
The outcoming object should have the same angle relative the the surface normal as the incoming object (same as geometrical optics reflection): Same thing in 3d.
|
Author: | Xery [ Sat Feb 04, 2012 11:58 am ] |
Post subject: | Re: What's the formula for reflecting an object off a surface? |
I'd given out this many many times... Code: for target in MovableMan.Particles(Items) do local pardist = SceneMan:ShortestDistance(self.Pos,target.Pos,self.mapwrapx); local PX = target.Pos.X - self.Pos.X; local PY = target.Pos.Y - self.Pos.Y; local VX = target.Vel.X - self.Vel.X; local VY = target.Vel.Y - self.Vel.Y; local judgeX = PX * VX; --to judge if it's moving toward "self" at X.axis local judgeY = PY * VY; local UV = -((PX*VX) + (PY*VY))/(PX^2 + PY^2); local NX = 2*PX*UV + VX; ---new Vel.X of the target local NY = 2*PY*UV + VY; ---new Vel.Y of the target local paVel = math.sqrt(VX^2 + VY^2); ------------Another Method --local angP = math.atan(PY/PX); --local angV = math.atan(VY/VX); --local paVel = math.sqrt(VY^2 + VX^2); --local angCA = angP - (angV - angP); --local flip = (-1 * spX) / math.abs(spX); if target.HitsMOs == true and pardist.Magnitude < (58 + (paVel / 12)) then if judgeX < 0 or judgeY < 0 then ---whether it's moving toward "self" if target.Vel.Magnitude > 5 then if target.ClassName == "MOPixel" then self.counterpar = CreateMOPixel(target.PresetName); elseif target.ClassName == "MOSParticle" then self.counterpar = CreateMOSParticle(target.PresetName); elseif target.ClassName == "MOSRotating" then self.counterpar = CreateMOSRotating(target.PresetName); elseif target.ClassName == "AEmitter" then self.counterpar = CreateAEmitter(target.PresetName); end self.counterpar.Vel.X = 0.8*NX; self.counterpar.Vel.Y = 0.8*NY; -- self.counterpar.Vel.X = ((math.cos(angCA) * paVel) + self.Vel.X) * flip; -- self.counterpar.Vel.Y = ((math.sin(angCA) * paVel) + self.Vel.Y) * flip; self.counterpar.Pos = target.Pos; self.counterpar:SetWhichMOToNotHit(self,-1); MovableMan:AddMO(self.counterpar); end self.gloweffect = CreateMOPixel("X-CutTool Shield LittleGlow","X-Region.rte"); self.gloweffect.Pos = target.Pos; target.ToDelete = true; MovableMan:AddMO(self.gloweffect); end end end So anything moving toward you that can hit you will be reflect to a realistic direction and can hit the original shooter. It might not be the best solution, but works quite well. |
Author: | ryry1237 [ Sat Feb 04, 2012 1:40 pm ] | ||
Post subject: | Re: What's the formula for reflecting an object off a surface? | ||
Xery wrote: Xery's post I'm looking at the code and this is what I understand so far pardist = Distance of object away from self PX = When positive, target is right of self and vice versa PY = When positive, target is below self and vice versa VX = When positive, target is traveling right relative to self and vv VY = When positive, target is traveling down relative to self and vv judgeX = When positive, target is traveling away from self based on X value judgeY = When positive, target is traveling away from self based on Y value UV = Hmm... don't really get what this is supposed to do. Code: local UV = -((PX*VX) + (PY*VY))/(PX^2 + PY^2); If velocity relative to both self and target = 0, then UV = 0 If UV = 0, then new velocity of X and Y are also 0 If position of y and velocity of y are not considered (1-D perspective) When PX*VX = positive, UV goes into negatives, target traveling away from self, When PX*VX = negative, UV becomes positive, and target is traveling towards self (ready to be reflected) As VX increases, UV also increases proportionally and vice versa Given that VX remains constant (constant X-coordinate movement), as PX gets closer to 0 (target gets closer to self X-wise), UV decreases. Purpose of UV for X coordinate appears to be determining velocity X wise Same goes for Y coordinate. Position of both self and target cannot be same otherwise game will divide by 0 Gonna think about this later since there are too many fireworks and screaming car alarms outside.
|
Page 1 of 1 | All times are UTC [ DST ] |
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group http://www.phpbb.com/ |