Page 1 of 1

math problem with portals (velocity & directions)

PostPosted: Wed Jul 01, 2015 12:15
by AiTechEye
I making a portalgun mod, almost everyting is done, but still have to calculate the velocity manipulation.
*Remade declaration*

like if
portal 1 yaw = math.pi * 0
portal 2 yaw = math.pi * 1.5
object.velocity.x=-4
after teleporting the object.velocity.x=-4 should be object.velocity.z=4

== or if ==

portal 1 pitch = math.pi * 0
portal 2 yaw = math.pi * 0.5
object.velocity.y=-4
after teleporting the object.velocity.y=-4 should be object.velocity.z=-4

Someone that can mathematical way to calculate it?

Here is an illustation:
Image

Re: math problem with portals (velocity & directions)

PostPosted: Thu Jul 02, 2015 15:16
by Hybrid Dog
it might work
vectors out of the portals
to_exit = {x=1,y=0,z=0}
to_entrance = {x=0,y=-1,z=0}

cross (×): {x = ay*bz - az*by, y = az*bx - ax*bz, z = ax*by - ay*bx}
orthogonal to to_exit and to_entrance → rotate vel vector on this axis
tmp = to_exit × to_entrance = {x=0,y=0,z=-1}

vel = {x=-200, y=33, z=0.1}
orthogonal to current velocity and the axis, 90° rotation
vel × tmp = {x=-33, y=-200, z=0}

Re: math problem with portals (velocity & directions)

PostPosted: Fri Jul 03, 2015 07:24
by AiTechEye
I think i declared on a bad way,
Is it possible to do this, but with the portals-yaw/pitch instead of the portals-positions?
I tried your nice example of different ways, but still not working...

i remade the declaration

Re: math problem with portals (velocity & directions)

PostPosted: Fri Jul 03, 2015 19:55
by Hybrid Dog
Have you tried cosinus yet? https://github.com/HybridDog/realclocks ... it.lua#L64
Why am l and UjEdwin the only ones writing something in this topic?

Re: math problem with portals (velocity & directions)

PostPosted: Mon Jul 06, 2015 15:15
by Hybrid Dog
UjEdwin wrote:portal 1 yaw = math.pi * 0
object.velocity.x = -4

portal 2 yaw = math.pi * 1.5
after teleporting the object.velocity.x=-4 should be object.velocity.z=4

Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
Code: Select all
           0↓vel1
        XXXXXXXXX
1.5pi Z         -Z   
 ←    Z         -Z 0.5pi
vel2  Z         -Z     
       -X-X-X-X-
            pi
       

cos(yaw) = an/hyp
hyp = rt(z*z+x*x)
cos(0) = 1 = x/hyp (= -invel.x/hyp = 4/4)
an = x
if an < 0 then hyp = -hyp end
cos(1.5pi) ~ 0 = x/hyp = 0/hyp = 0

Re: math problem with portals (velocity & directions)

PostPosted: Tue Jul 07, 2015 05:54
by prestidigitator
The most general way to do this is to final position and velocity in the entry portal's local coordinate system, where for a "Portal Gun" type system you'd want one of the colored portal's (red or blue) coordinate system to be rotated 180 degrees around the vertical axis when placed, so that "into" one is, "out of" the other. Then moving through the portal is equivalent to simply moving to the same coordinates in the exit portal's coordinate system. Something like:

Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
Code: Select all
portal = {};

local function vectorDot(va, vb)
   return va.x * vb.x + va.y * vb.y + va.z * vb.z;
end;

--- Constructs a portal from position and unit x, y, and z coordinate vectors.
function portal.new(pos, xDir, yDir, zDir)
   ...
   local portalObj =
      {
         pos  = pos,
         xDir = xDir,
         yDir = yDir,
         zDir = zDir,
         ...
      };
   ...
   return setmetatable(portalObj, { __index = portal });
end;

function portal:worldToLocalVec(v)
   return { x = vectorDot(self.xDir, v),
            y = vectorDot(self.yDir, v),
            z = vectorDot(self.zDir, v) };
end;

function portal:localToWorldVec(v)
   return vector.add(vector.multiply(self.xDir, v.x),
          vector.add(vector.multiply(self.yDir, v.y),
                     vector.multiply(self.zDir, v.z)));
end;

function portal:worldToLocalPos(p)
   return self:worldToLocalVec(vector.subtract(p, self.pos));
end;

function portal:localToWorldPos(p)
   return vector.add(self.pos, self:localToWorldVec(p));
end;

function portal:worldToLocalPosVel(pos, vel)
   return self:worldToLocalPos(pos), self:worldToLocalVec(vel);
end;

function portal:localToWorldPosVel(pos, vel)
   return self:localToWorldPos(pos), self:localToWorldVec(vel);
end;

function portal:transformPosVelTo(pos, vel, exitPortal)
   local localPos, localVel = self:worldToLocalPosVel(pos, vel);
   return exitPortal:localToWorldPosVel(localPos, localVel);
end;


Then it's as simple as:

Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
Code: Select all
local newPos, newVel = entryPortal:transformPosVelTo(oldPos, oldVel, exitPortal);

Re: math problem with portals (velocity & directions)

PostPosted: Tue Jul 07, 2015 13:33
by Hybrid Dog
prestidigitator wrote:The most general way to do this is to final position and velocity in the entry portal's local coordinate system, where for a "Portal Gun" type system you'd want one of the colored portal's (red or blue) coordinate system to be rotated 180 degrees around the vertical axis when placed, so that "into" one is, "out of" the other. Then moving through the portal is equivalent to simply moving to the same coordinates in the exit portal's coordinate system. Something like:

Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
Code: Select all
portal = {};

local function vectorDot(va, vb)
   return va.x * vb.x + va.y * vb.y + va.z * vb.z;
end;

--- Constructs a portal from position and unit x, y, and z coordinate vectors.
function portal.new(pos, xDir, yDir, zDir)
   ...
   local portalObj =
      {
         pos  = pos,
         xDir = xDir,
         yDir = yDir,
         zDir = zDir,
         ...
      };
   ...
   return setmetatable(portalObj, { __index = portal });
end;

function portal:worldToLocalVec(v)
   return { x = vectorDot(self.xDir, v),
            y = vectorDot(self.yDir, v),
            z = vectorDot(self.zDir, v) };
end;

function portal:localToWorldVec(v)
   return vector.add(vector.multiply(self.xDir, v.x),
          vector.add(vector.multiply(self.yDir, v.y),
                     vector.multiply(self.zDir, v.z)));
end;

function portal:worldToLocalPos(p)
   return self:worldToLocalVec(vector.subtract(p, self.pos));
end;

function portal:localToWorldPos(p)
   return vector.add(self.pos, self:localToWorldVec(p));
end;

function portal:worldToLocalPosVel(pos, vel)
   return self:worldToLocalPos(pos), self:worldToLocalVec(vel);
end;

function portal:localToWorldPosVel(pos, vel)
   return self:localToWorldPos(pos), self:localToWorldVec(vel);
end;

function portal:transformPosVelTo(pos, vel, exitPortal)
   local localPos, localVel = self:worldToLocalPosVel(pos, vel);
   return exitPortal:localToWorldPosVel(localPos, localVel);
end;


Then it's as simple as:

Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
Code: Select all
local newPos, newVel = entryPortal:transformPosVelTo(oldPos, oldVel, exitPortal);

What are the xDir, yDir and zDir vectors?

Re: math problem with portals (velocity & directions)

PostPosted: Thu Jul 09, 2015 09:08
by AiTechEye
After a few days to making this mod I feel too bored, but I released the mod, feel free to remake it to a better version :)

viewtopic.php?f=9&t=12772

Re: math problem with portals (velocity & directions)

PostPosted: Fri Jul 10, 2015 05:17
by prestidigitator
Hybrid Dog wrote:What are the xDir, yDir and zDir vectors?

They are perpendicular unit vectors that define the portal's local coordinate frame. One points to the portal's side, one points into/out of the portal, and one points in the portal's "up" direction.

Re: math problem with portals (velocity & directions)

PostPosted: Fri Jul 10, 2015 17:06
by Hybrid Dog
prestidigitator wrote:
Hybrid Dog wrote:What are the xDir, yDir and zDir vectors?

They are perpendicular unit vectors that define the portal's local coordinate frame. One points to the portal's side, one points into/out of the portal, and one points in the portal's "up" direction.

Wouldn't the vector out of the portal (n⁰) and the up direction be enough information?

Re: math problem with portals (velocity & directions)

PostPosted: Sat Jul 11, 2015 18:14
by prestidigitator
Hybrid Dog wrote:
prestidigitator wrote:
Hybrid Dog wrote:What are the xDir, yDir and zDir vectors?

They are perpendicular unit vectors that define the portal's local coordinate frame. One points to the portal's side, one points into/out of the portal, and one points in the portal's "up" direction.

Wouldn't the vector out of the portal (n⁰) and the up direction be enough information?

Technically yes, but usually you keep the entire basis around for simplicity and performance. Otherwise there are places you have to do a cross product every time you need the third vector.