math problem with portals (velocity & directions)

Post Reply
User avatar
AiTechEye
Member
Posts: 1000
Joined: Fri May 29, 2015 21:14
GitHub: AiTechEye
Location: Sweden

math problem with portals (velocity & directions)

by AiTechEye » Post

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
Attachments
1.png
1.png (13.54 KiB) Viewed 763 times
Last edited by AiTechEye on Fri Jul 03, 2015 07:34, edited 2 times in total.

User avatar
Hybrid Dog
Member
Posts: 2835
Joined: Thu Nov 01, 2012 12:46
GitHub: HybridDog

Re: math problem with portals (velocity & directions)

by Hybrid Dog » Post

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}

‮‪‮
‮‪‮
‮‪‮
‮‪‮
‮‪‮
‮‪‮
‮‪‮
‮‪‮
‮‪

User avatar
AiTechEye
Member
Posts: 1000
Joined: Fri May 29, 2015 21:14
GitHub: AiTechEye
Location: Sweden

Re: math problem with portals (velocity & directions)

by AiTechEye » Post

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

User avatar
Hybrid Dog
Member
Posts: 2835
Joined: Thu Nov 01, 2012 12:46
GitHub: HybridDog

Re: math problem with portals (velocity & directions)

by Hybrid Dog » Post

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?

‮‪‮
‮‪‮
‮‪‮
‮‪‮
‮‪‮
‮‪‮
‮‪‮
‮‪‮
‮‪

User avatar
Hybrid Dog
Member
Posts: 2835
Joined: Thu Nov 01, 2012 12:46
GitHub: HybridDog

Re: math problem with portals (velocity & directions)

by Hybrid Dog » Post

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

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

‮‪‮
‮‪‮
‮‪‮
‮‪‮
‮‪‮
‮‪‮
‮‪‮
‮‪‮
‮‪

prestidigitator
Member
Posts: 647
Joined: Thu Feb 21, 2013 23:54

Re: math problem with portals (velocity & directions)

by prestidigitator » Post

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:

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:

Code: Select all

local newPos, newVel = entryPortal:transformPosVelTo(oldPos, oldVel, exitPortal);

User avatar
Hybrid Dog
Member
Posts: 2835
Joined: Thu Nov 01, 2012 12:46
GitHub: HybridDog

Re: math problem with portals (velocity & directions)

by Hybrid Dog » Post

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:

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:

Code: Select all

local newPos, newVel = entryPortal:transformPosVelTo(oldPos, oldVel, exitPortal);
What are the xDir, yDir and zDir vectors?

‮‪‮
‮‪‮
‮‪‮
‮‪‮
‮‪‮
‮‪‮
‮‪‮
‮‪‮
‮‪

User avatar
AiTechEye
Member
Posts: 1000
Joined: Fri May 29, 2015 21:14
GitHub: AiTechEye
Location: Sweden

Re: math problem with portals (velocity & directions)

by AiTechEye » Post

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

prestidigitator
Member
Posts: 647
Joined: Thu Feb 21, 2013 23:54

Re: math problem with portals (velocity & directions)

by prestidigitator » Post

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.

User avatar
Hybrid Dog
Member
Posts: 2835
Joined: Thu Nov 01, 2012 12:46
GitHub: HybridDog

Re: math problem with portals (velocity & directions)

by Hybrid Dog » Post

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?

‮‪‮
‮‪‮
‮‪‮
‮‪‮
‮‪‮
‮‪‮
‮‪‮
‮‪‮
‮‪

prestidigitator
Member
Posts: 647
Joined: Thu Feb 21, 2013 23:54

Re: math problem with portals (velocity & directions)

by prestidigitator » Post

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.

Post Reply

Who is online

Users browsing this forum: Blockhead and 13 guests