math problem with portals (velocity & directions)
math problem with portals (velocity & directions)
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:
*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:
- Attachments
-
- 1.png (13.54 KiB) Viewed 763 times
Last edited by AiTechEye on Fri Jul 03, 2015 07:34, edited 2 times in total.
- Hybrid Dog
- Member
- Posts: 2835
- Joined: Thu Nov 01, 2012 12:46
- GitHub: HybridDog
Re: math problem with portals (velocity & directions)
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)
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
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
- Hybrid Dog
- Member
- Posts: 2835
- Joined: Thu Nov 01, 2012 12:46
- GitHub: HybridDog
Re: math problem with portals (velocity & directions)
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?
Why am l and UjEdwin the only ones writing something in this topic?
- Hybrid Dog
- Member
- Posts: 2835
- Joined: Thu Nov 01, 2012 12:46
- GitHub: HybridDog
Re: math problem with portals (velocity & directions)
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
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
-
- Member
- Posts: 647
- Joined: Thu Feb 21, 2013 23:54
Re: math problem with portals (velocity & directions)
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:
Then it's as simple as:
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;
Code: Select all
local newPos, newVel = entryPortal:transformPosVelTo(oldPos, oldVel, exitPortal);
- Hybrid Dog
- Member
- Posts: 2835
- Joined: Thu Nov 01, 2012 12:46
- GitHub: HybridDog
Re: math problem with portals (velocity & directions)
What are the xDir, yDir and zDir vectors?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:
Then it's as simple as: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;
Code: Select all
local newPos, newVel = entryPortal:transformPosVelTo(oldPos, oldVel, exitPortal);
Re: math problem with portals (velocity & directions)
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
viewtopic.php?f=9&t=12772
-
- Member
- Posts: 647
- Joined: Thu Feb 21, 2013 23:54
Re: math problem with portals (velocity & directions)
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 wrote:What are the xDir, yDir and zDir vectors?
- Hybrid Dog
- Member
- Posts: 2835
- Joined: Thu Nov 01, 2012 12:46
- GitHub: HybridDog
Re: math problem with portals (velocity & directions)
Wouldn't the vector out of the portal (n⁰) and the up direction be enough information?prestidigitator wrote: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 wrote:What are the xDir, yDir and zDir vectors?
-
- Member
- Posts: 647
- Joined: Thu Feb 21, 2013 23:54
Re: math problem with portals (velocity & directions)
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.Hybrid Dog wrote:Wouldn't the vector out of the portal (n⁰) and the up direction be enough information?prestidigitator wrote: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 wrote:What are the xDir, yDir and zDir vectors?
Who is online
Users browsing this forum: Blockhead and 13 guests