In
this neat technical article, Jeremy Alessi deconstructs Valve's
Portal -- including a demo and example source code -- to give a clear understanding of the physics principles behind the game's compellingly-executed teleportation mechanics.
Though teleport mechanics in video games might seem like nothing new, Portal's deceptively simple implementation of simulated wormholes are still very much worth noting:
"Puzzles from the original Gauntlet were memorable -- and more than likely, that wasn't the first game to use teleportation as a gameplay mechanic. The difference between Portal and all those that came before it is that Portal's teleportation acts as a frictionless tube between point A and point B.
Physics are still hard at work inside the frictionless tube. Instead of simply repositioning an object from point A to point B, the player enters point A with full velocity and exits point B with the same speed, but moving in a new direction."
Before providing a code snippet written in Blitz3D to prototype the game's mechanics, Alessi describes how velocity and surface normals -- rays or unit vectors running perpendicular to a plane -- come into play with the simulated tunnel:
"In order to break this down we will visit our old friend velocity, which is a vector. A vector is a magnitude coupled with a direction. Velocity is a vector whose magnitude is representative of speed. When the player collides with a portal we can extract the speed component of our velocity and redirect it to form a new velocity vector.
... When a portal is applied to a wall, floor, or ramp in the game, the surface normal of the surface is communicated to the portal inhabiting it.
We then take the velocity we entered a portal with, and break it down into components. To do this we multiply by the surface normals of the exit portal.
In the end, Portal uses the traditional teleportation mechanic with a velocity and orientation change."
You can
read the full technical feature on
Portal's mechanics, which includes an associated demo and code sample along with a breakdown on how the code works (no registration required, please feel free to link to this feature from external websites).