View unanswered posts | View active topics It is currently Sat Dec 28, 2024 4:33 pm



Reply to topic  [ 5 posts ] 
 What's the formula for reflecting an object off a surface? 
Author Message
User avatar

Joined: Sun Dec 25, 2011 7:23 am
Posts: 269
Reply with quote
Post 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.

ImageImage
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)?


Sat Feb 04, 2012 9:12 am
Profile
User avatar

Joined: Tue Nov 17, 2009 7:38 pm
Posts: 909
Location: France
Reply with quote
Post 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):
Image
Same thing in 3d.


Attachments:
200px-Reflection_angles.svg.png [7.84 KiB]
Not downloaded yet
Sat Feb 04, 2012 10:46 am
Profile
User avatar

Joined: Sat Feb 26, 2011 10:29 am
Posts: 163
Reply with quote
Post 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.


Sat Feb 04, 2012 11:58 am
Profile
User avatar

Joined: Sun Dec 25, 2011 7:23 am
Posts: 269
Reply with quote
Post 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
Image

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.


Attachments:
vector.PNG [1.48 KiB]
Not downloaded yet
Sat Feb 04, 2012 1:40 pm
Profile
User avatar

Joined: Sat Feb 26, 2011 10:29 am
Posts: 163
Reply with quote
Post Re: What's the formula for reflecting an object off a surface?
Image
or you can use another method, calculate by trigonometric functions (the "Another Method" lines after "--"), but I try both and fold it run slower than the prev one.
My English is not so good, hope it clear enough though.


Attachments:
图示.png
图示.png [ 38.82 KiB | Viewed 2368 times ]
Sat Feb 04, 2012 5:17 pm
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 5 posts ] 

Who is online

Users browsing this forum: No registered users


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by STSoftware for PTF.
[ Time : 0.046s | 15 Queries | GZIP : Off ]