math problem with portals (velocity & directions)

User avatar
AiTechEye
Member
 
Posts: 409
Joined: Fri May 29, 2015 21:14

math problem with portals (velocity & directions)

by AiTechEye » Wed Jul 01, 2015 12:15

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 2352 times
Last edited by AiTechEye on Fri Jul 03, 2015 07:34, edited 2 times in total.
Alive AI Mine/Build AI NPC
Gravitygun HL2
Portalgun Portal
Marssurvive Survive on mars
Bows bows + arrows
SoundCloud (Music)
SoundCloud (Classic)
YouTube
 

Hybrid Dog
Member
 
Posts: 2460
Joined: Thu Nov 01, 2012 12:46

Re: math problem with portals (velocity & directions)

by Hybrid Dog » Thu Jul 02, 2015 15:16

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: 409
Joined: Fri May 29, 2015 21:14

Re: math problem with portals (velocity & directions)

by AiTechEye » Fri Jul 03, 2015 07:24

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
Alive AI Mine/Build AI NPC
Gravitygun HL2
Portalgun Portal
Marssurvive Survive on mars
Bows bows + arrows
SoundCloud (Music)
SoundCloud (Classic)
YouTube
 

Hybrid Dog
Member
 
Posts: 2460
Joined: Thu Nov 01, 2012 12:46

Re: math problem with portals (velocity & directions)

by Hybrid Dog » Fri Jul 03, 2015 19:55

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?
 

Hybrid Dog
Member
 
Posts: 2460
Joined: Thu Nov 01, 2012 12:46

Re: math problem with portals (velocity & directions)

by Hybrid Dog » Mon Jul 06, 2015 15:15

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
 

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

Re: math problem with portals (velocity & directions)

by prestidigitator » Tue Jul 07, 2015 05:54

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);
 

Hybrid Dog
Member
 
Posts: 2460
Joined: Thu Nov 01, 2012 12:46

Re: math problem with portals (velocity & directions)

by Hybrid Dog » Tue Jul 07, 2015 13:33

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?
 

User avatar
AiTechEye
Member
 
Posts: 409
Joined: Fri May 29, 2015 21:14

Re: math problem with portals (velocity & directions)

by AiTechEye » Thu Jul 09, 2015 09:08

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
Alive AI Mine/Build AI NPC
Gravitygun HL2
Portalgun Portal
Marssurvive Survive on mars
Bows bows + arrows
SoundCloud (Music)
SoundCloud (Classic)
YouTube
 

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

Re: math problem with portals (velocity & directions)

by prestidigitator » Fri Jul 10, 2015 05:17

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.
 

Hybrid Dog
Member
 
Posts: 2460
Joined: Thu Nov 01, 2012 12:46

Re: math problem with portals (velocity & directions)

by Hybrid Dog » Fri Jul 10, 2015 17:06

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: 632
Joined: Thu Feb 21, 2013 23:54

Re: math problem with portals (velocity & directions)

by prestidigitator » Sat Jul 11, 2015 18:14

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.
 


Return to Modding Discussion

Who is online

Users browsing this forum: Bing [Bot] and 4 guests

cron