Slashdot is powered by your submissions, so send in your scoop


Forgot your password?
Games Entertainment

Examining Portal's Teleportation Code 278

Gamasutra is running a story deconstructing the mechanics of Portal's teleportation programming. They present a snippet of Portal's code and a downloadable demo. They ran another article in this series earlier this year with an analysis Mario Galaxy's unique take on physics. We've discussed the development of Portal in the past. "Teleport mechanics in video games are nothing new. 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." Update: 8/26 at 19:37 by SS: Dan notes that the code was not directly from Portal; it was written to approximate Portal's physics.
This discussion has been archived. No new comments can be posted.

Examining Portal's Teleportation Code

Comments Filter:
  • by Anonymous Coward on Tuesday August 26, 2008 @02:57PM (#24754693)

    Your mom has a frictionless tube.

    First post?

  • Portal (Score:5, Funny)

    by CaptainPatent ( 1087643 ) on Tuesday August 26, 2008 @02:59PM (#24754751) Journal
    Regardless it's a lot better than quantum teleportation. []
    • Re:Portal (Score:5, Informative)

      by Impy the Impiuos Imp ( 442658 ) on Tuesday August 26, 2008 @03:07PM (#24754865) Journal

      Duke Nukem 3D's teleporters would teleport your weapon shots, too.

      I loved shooting the rocket launcher into it only to hear a buddy die on the other side of the map as the rocket appeared and continued on its way.

      I don't recall if you jumped into the teleporter if you'd exit and continue your jump arc, but there is precident for "movement in progress" teleportation.

      • Re:Portal (Score:5, Interesting)

        by pushing-robot ( 1037830 ) on Tuesday August 26, 2008 @03:43PM (#24755345)

        That's not entirely correct, from a programming standpoint.

        In most old games, "physics" were limited to jumping (and, occasionally, explosions knocking players around). Rather than try to simulate ballistic trajectories for every object in the game, rockets and other projectiles were simply moved forward a certain distance for every "tick" of game time.

        So the transporter didn't preserve the rocket's momentum - it just put the rocket at a new location, and the game then resumed moving the rocket forward.

        • by DrYak ( 748999 ) on Tuesday August 26, 2008 @09:55PM (#24759415) Homepage

          So the transporter didn't preserve the rocket's momentum - it just put the rocket at a new location, and the game then resumed moving the rocket forward.

          From a engine coding point of view, i fail to see how there's any difference.

          In both cases, you have a vector attached to an object which describes how an object move :
          - you can call it "trajectory" for shots
          - you can call it "momentum" for portal
          but technically it's just plain stupid "speed" vector. As in "derivative of the position" (= how the position is updated between each turn).

          Eventually, if it's not a rocket, it will also obey to an acceleration (most of the time : gravity. But it can be buoyancy).

          In both situation, all you have to do for any object entering a teleport, is simply change the current coordinate of the object, and eventually perform a transformation on the speed vector if both teleport end point aren't facing the same direction.

          From a coding point of view portal doesn't introduce anything new for the physics that wasn't already done by any of all the multiple game that allow shooting through a portal or *jumping* through a teleport.

          The new thing are the rendering engine (not all engine can easily render see-through portals - due to the way the work, Duke 3D's and Descent [portal based] and Wolfenstein and Doom [raycasting] could do them, but Quake 1 & 2 [BSP polygons] can't)

          and the gameplay (before portal, teleporting thing other than the player was a fun by-product of how teleport work and can enable a couple of giggles. Portal in contrast has all its puzzle based around throwing object through portals)

      • Re:Portal (Score:5, Interesting)

        by SanityInAnarchy ( 655584 ) <> on Tuesday August 26, 2008 @04:37PM (#24756145) Journal

        I don't recall if you jumped into the teleporter if you'd exit and continue your jump arc,

        To some extent, I'd guess. It wouldn't be perfect, but let me put it this way: Duke 3D was a two-and-a-half-D game, not a 3D game.

        This implies, among other things, that the engine didn't actually support rooms on top of one another -- that all had to be faked in some way.

        So how could you swim underwater? The simple answer is, the surface of the water was a silent teleporter -- it might even have to be marked "water" -- and the "underwater" was actually a completely different place in the map.

        Going upstairs was a different trick -- the fact that the game could handle two rooms, or "sectors", occupying the same space, so long as you couldn't see both at once. There was a lot of really creative level design involving staircases and the like to make it seem as though you had a two-story building, while never actually letting you see both stories at once.

        If you want to get a really good idea of what the Duke3D engine was, find one of the secret levels -- the one with a big room in the middle, and a hallway ringing around the outside (kind of a donut shape) -- don't remember what it was called. I do remember that you could turn right three times, and end up in a different room -- there were four separate rooms (or "sectors") set in the same physical space.

    • Re:Portal (Score:5, Funny)

      by ElizabethGreene ( 1185405 ) on Tuesday August 26, 2008 @03:42PM (#24755323)

      First thought: "Hey, if it can work within or near the event horizon of a black hole we could time travel!".

      Second thought: "Wound this violate one of the laws of thermodynamics?"

      Third thought: "I need a life."

      • Fourth thought: "s/wound/would/g"

      • by lgw ( 121541 )

        Whether or not that would work is one of the more interesting questions in physics IMO. I think that Portal-style discontinuities "near" a black hole would not create any such weirdness, but sadly the math physisists used to model this stuff is so complex and abstract that the math simply can't model a discontinuity. It's amusing when someone claims it's impossible on the basis that it doesn't fit the model, though.

        "Within" black hole ... that would be very weird indeed.

        And don't worry, here on /. no one

  • But... (Score:5, Funny)

    by i.r.id10t ( 595143 ) on Tuesday August 26, 2008 @03:00PM (#24754757)

    But... can they telefrag?

    I just love the sound of 2 bodies trying to occupy the same space at the same time in the morning... or afternoon... or evening...

  • Too verbose (Score:5, Funny)

    by Anonymous Coward on Tuesday August 26, 2008 @03:01PM (#24754763)

    >>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."

    Should be "Speedy thing goes in; speedy thing comes out."

    • by elrous0 ( 869638 ) * on Tuesday August 26, 2008 @03:58PM (#24755565)
      My ex-girlfriend used to use that exact phrase all the time.
    • No kidding (Score:3, Insightful)

      by Moraelin ( 679338 )

      No kidding. The whole "frictionless tube" thing is just thinking too hard about it. Yes, instead of simply repositioning an object from point A to point B, you also take the normal of both holes and change the direction of the velocity. That's it. It solves the same problem and produces the exact same result without doing physics for a frictionless tube.

  • by Stanistani ( 808333 ) on Tuesday August 26, 2008 @03:01PM (#24754767) Homepage Journal
    I want to see the nautical version, Porthole.
  • if they'd chosen the Discworld version of teleportation: two objects have to be teleported simultaneously in opposite directions, and the weight difference determines the exit speed of the lighter object.

  • by mikael ( 484 )

    You can do that with 'bzflag' portals. Take a jump through the portal or with the 'wings' flag. Then you appear at the other end like SG-1 stargate. Spins and backward movement work as well.

    • Re: (Score:3, Interesting)

      by Smidge204 ( 605297 )

      Yes no but not really. Vertical velocity is still vertical and horizontal velocity is still horizontal - just pointing in a different direction.

      With Portal (and it's predecessor Narbacular Drop) you can enter a portal on the floor and pop out a portal on a wall - vertical speed seamlessly becomes horizontal speed. And that works at any combination of angles. Add to that collision detection mid-portal and you have something just a little more than what BZFlag offers.

      Regarding the portals in Prey - I don't re

      • by mikael ( 484 )

        'bzflag' portals can be in any direction the level designer wants. They can be vertical (drive into them), horizontal (jump up or down into them), or any angle you like. A 'bzflag' portal is simply a plane equation and a bounding frame. But there is no collision-detection as portal transportation is instantaneous. Having a 'Stargate' tunnel would probably change the dynamics a bit, but maybe it would be just like a secret tunnel.

      • In Prey velocity was maintained just like in portal. Though you're right that all of the portals were pre-created for you. Though I do believe there were one or two that changed position, and quite a few on angular surfaces.

        The biggest difference was that you couldn't create the portals yourself, and there were no situations where you'd leave a portal and enter another one to keep building velocity. I suspect you're right about development shortcuts, else we would have seen more interesting use of Prey'
  • Physics is Phun (Score:4, Interesting)

    by CopaceticOpus ( 965603 ) on Tuesday August 26, 2008 @03:05PM (#24754837)

    A well made 2D Flash version of Portal: []

    • Not really (Score:3, Insightful)

      by Sycraft-fu ( 314770 )

      Started neat but quickly degenerated in to something all to typical with flash games: You have to have really fast reflexes and figure you the logic apparent only to the designer. Neat technically, but the gameplay needs work.

  • Any thoughts on a nice little compare and contrast of the 'teleportation physics' between Prey and Portal?
  • by Zymergy ( 803632 ) * on Tuesday August 26, 2008 @03:07PM (#24754863)
    These two were my favorites: [] (1953)
    and [] (1951)
  • The code is a lie!

    Ok, sorry...
  • Damn! (Score:5, Insightful)

    by khendron ( 225184 ) on Tuesday August 26, 2008 @03:09PM (#24754901) Homepage

    Reading that article makes me want to play Portal through again.

    --sigh-- at least it won't take long.

  • Next step? (Score:4, Funny)

    by Aranwe Haldaloke ( 789555 ) on Tuesday August 26, 2008 @03:13PM (#24754941)

    Porting Portal's portal code?

  • WTF (Score:5, Insightful)

    by 4D6963 ( 933028 ) on Tuesday August 26, 2008 @03:13PM (#24754951)

    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.

    Are you fucking kidding? What's not completely obvious about this algorithm that should be Slashdot-front page worthy? I mean, it's fucking mind blowingly obvious, of course it keeps the velocity and translates it, how else could it do what it does? I can understand why it would be relevant to do a coding tutorial on the subject, but that's about as newsworthy as a Bresenham line drawing algorithm tutorial. TFS looks just like a "hey let's talk about how some super popular game is super awesome and post about it on a high traffic website".

    • Re: (Score:3, Insightful)

      by 77Punker ( 673758 )

      The physics of the teleportation are pretty boring, but the fact that you can see through the portals and have an object go halfway through a portal are unusual.

      I don't care enough about programming games to RTFA, but that could be something worth talking about. Chances are it's not what they're talking about, though.

      • I was wondering the same thing. Sadly, it really didn't go too far into that one, just the physics.

        • by 4D6963 ( 933028 )
          Yeah which is really the easiest and most obvious part. As the GP pointed out the algorithm for seeing through the portal must be less obvious/interesting, same for things standing in the portal, although I suspect there's no big challenge here, I'm ready to be that objects just go on either side of the portal without making any difference.
          • by xhrit ( 915936 )
            >As the GP pointed out the algorithm for seeing through the portal must be less obvious/interesting

            This is easy.

            1) portal_1 & portal_2 are textures that are applied to the walls as decals. (just like every other bullet hole in just about every other non-GeoMod style shooting game)
            2) create a pair ov cameras, one attached to each portal. aim the camera the direction ov the normal with its up vector aligned with the global up vector.
            3) Render target portal_1 with sorurce ov portal_2 and vice
            • by 4D6963 ( 933028 )
              Wait, you don't put your portal camera in the portal, do you? You put them at a distance from the portal (same distance as the player is from the portal) and make sure nothing gets rendered in the way, correct?
      • the fact that you can see through the portals and have an object go halfway through a portal are unusual.

        There's also some potential that's not really unlocked in Portal itself. For example, seeing through the portal could allow you to shoot through it (Portal only allows you to drop things through portals). The game it's based on (Narbacular Drop []) even allowed you to place portals (imagine being able to shoot the portal gun through the portal) while looking through the portals, which made for some interesting strategy.

        So it is an interesting case of teleportation. I'm not entirely sure it even makes sense

    • Re:WTF (Score:5, Funny)

      by kestasjk ( 933987 ) on Tuesday August 26, 2008 @03:26PM (#24755129) Homepage
      It didn't answer the main question for me: How does the code which accelerates players downwards work?

      It must be using some kind of vector mathematics for mapping acceleration onto velocity, and velocity onto position, but I'll be damned if I can figure out how they expressed that in an algorithm.
      • Um... aren't you referring to gravity here?

      • by Goaway ( 82658 )

        Uh... That's game programming 101.

        velocity_z += gravity * delta_t;
        position_x += velocity_x * delta_t;
        position_y += velocity_y * delta_t;
        position_z += velocity_z * delta_t;

        Then again, with the rest of the article being absolutely elementary bullshit, maybe it is a wonder they didn't cover that, too.

      • Re: (Score:3, Insightful)

        by 4D6963 ( 933028 )
        Haha nice one, I didn't think about that ;-). Don't pay attention to the other posters, Slashdot is crowded with people insensitive to even the most blatant sarcasm.
    • by geekoid ( 135745 )

      I lied it, and I like seeing how they did it differently. If comparing how different people implement game teleportation isn't nerdy, then nothing is.
      I mean, you could treat it like a tube, or you could just save the movement information and reimplement it when using a spawn technique of teleportation.

      The fact that you aren't clever enough to see all the variations is hardly Slashdots fault.

      • by 4D6963 ( 933028 )
        What the hell are you drivelling about? It is obvious that the only straightforward and sensible way to do this is to do a line-full circle collision detection between your position (a point) between this frame and the previous frame (a line) and the portal (a flat full circle, well, oval), and that when the line and the circle cross you just translate positions and velocity according to a fairly simple function that relies on the 3D angle between both portals. And wtf is "a spawn technique of teleportation
        • by lgw ( 121541 )

          It made sense to me. People have been doing spawn teleports forever, and Portal was interesting because it used a new technique, though Prey was similar and has been around in development since Quake2. "Zoom" teleports, where your point of view moves very fast from source to destination, are cool as well.

          • by HTH NE1 ( 675604 )

            I think he means whether the teleportation is rendered continuously or discontinuously. A discontinuous teleport would be like Halo where you touch the teleporter and you're suddenly at the other end. A continuous one allows for a smoother transition or even reversal while being transported.

            Compare also the Stargate (SG-1, Atlantis) method where it feels like you walk in on one side and walk out the other, but in fact you're being disintegrated as you step through and aren't reintegrated on the other side

    • Don't you be denigrating Bresenham's line drawing algorithm. Truly a work of algorithmic beauty.

      • by 4D6963 ( 933028 )
        Oh I know, I use it, and I'm not denigrating it. Just saying, if Gamasutra wrote a tutorial about it, I wouldn't want to see that on the front page of Slashdot, no matter how elegant and efficient this algorithm is.
    • by Goaway ( 82658 )

      Well, the Super Mario Galaxies was total bullshit too, so that does seem to be their modus operandi. Take a popular game, bullshit something that sounds plausible to someone who's not experienced with game programming, and get linked on social media sites.

    • Although I appreciate Gamasutra for its laymen-type articles, in reality the portal code is quite simple. Simple matrix-multiplication kids. Take the rate of entry into portal A(incoming), apply this by the normal of portal B(outgoing), then multiply the new vector to the matrix describing the actor (in this case, the player). Don't forget the gravity vector!
      Not terribly difficult, as with the WII Mario gravity. Although, I can really appreciate the creativity.
  • My big question (Score:5, Interesting)

    by Zerth ( 26112 ) on Tuesday August 26, 2008 @03:23PM (#24755085)

    If you could put a portal on a fast falling object, when it lands on you while you are standing still would you have momentum at the other of the portal or would you just poke through since the portal has the initial momentum? (IE nonplayer movement being different than player movement)

    What would that say about your reference frame? Could you use that to distinguish which of two objects had "universal frame" movement? That'd be kinda neat, theoretically, but it'd be way more interesting to be able to put a portal on a crate hanging from a crane, then make the crate fall while standing under it to catapult yourself from a low position.

    • by geekoid ( 135745 )

      You could use to portals to create a geekoid friction bomb.

      Just put one on top of another and but a steel ball so it keeps passing from one portal to another until it gets so hot to ignites the air.

      • Ah but the air would also be falling through at the same speed. If you put a portal on the ceiling and floor of a closed room then soon the small difference in air pressure from the top of the room to the bottom would create a howling vortex.

      • Re: (Score:3, Informative)

        by pla ( 258480 )
        Just put one on top of another and but a steel ball so it keeps passing from one portal to another until it gets so hot to ignites the air.

        The portals don't actually increase the velocity of what passes through them, they just redirect it. So the trick of putting two portals vertically aligned basically simulates "falling" for a much greater distance.

        You would still (if in an atmosphere) reach terminal velocity rather quickly, however, so no vaporized metal (or human) explosions will happen.
    • I think you just helped Valve come up with some concepts for Portal2. That would add a bunch of fun to the game.
    • by Erbo ( 384 )
      Well, in Portal, if an object that has an open portal on it moves, the portal disappears, and the "other end" of the portal is blocked. Hence tricky puzzles like the one in Test Chamber 18 to reach the storage cube. That one was a cast-iron bitch to figure out the first time. (Not going to give it away for the six people who've never played Portal...)
      • by Zerth ( 26112 )

        Well, those were part of the "test". They (or the mechanics of the portal) were coded to dispel on movement. If that weren't the case, I'm curious how portal velocity would affect player velocity.

        They already used the "portal keeps you from being slowly squished" trick, I'd like to see it with faster SMB-Thwomp style traps.

    • Re: (Score:3, Informative)

      by Apache ( 14188 )

      Your velocity relative to the old portal's frame of reference becomes the velocity relative to the new portal's frame of reference.

      For example, if a person falls straight down into a portal on a platform moving 5 m/s, and the exit portal is on a stationary floor, the person will hurl upward out of the stationary portal with a horizontal component of 5 m/s. Which way the person goes depends on the orientations of the portals. Flip either the exit or entrance portal and you'll go the opposite direction.


  • Portalized (Score:3, Informative)

    by TypoNAM ( 695420 ) on Tuesday August 26, 2008 @03:27PM (#24755147)
    Somebody using Ogre3D has been working on a portal like project for nearly a year and has made pretty damn good progress for somebody who was new to game programming: []

    He also has a blog [] which seems quite lacking though.
  • Portal Physics 101 (Score:5, Interesting)

    by xPsi ( 851544 ) * on Tuesday August 26, 2008 @03:34PM (#24755235)
    In the game, GlaDOS says "momentum is conserved through the portal." Assuming our physical system is the character, momentum definitely is not conserved. Neither is energy. The description "the player enters point A with full velocity and exits point B with the same speed, but moving in a new direction" is exactly correct: a textbook example of momentum non-conservation. However, what drives the exciting "flinging" effect, which makes Portal's teleportation so unique, isn't just momentum redirection. It's that you instantly obtain the potential energy of your exit location. This new potential energy can be converted back into kinetic energy, increasing your speed...mix in a little momentum redirection at the portals then wash, rinse, repeat. Although GlaDOS describes the game physics incorrectly, there is a game walkthrough where the programmers do describe it correctly. If you take any physics courses from me, you can expect to see some Portal questions on future quizzes :) Nice article overall.
    • by Tacvek ( 948259 )

      TO be fair, the magnitude of momentum is the same. Most regular people consider velocity to the the magnitude of the physics velocity vector, and momentum as the magnitude of the physics momentum vector.

      The potential energy change is not very unique though, as games do not care about potential energies. Only current kinetic energy is ever important. Thus any sort of portals in a game where the that have different 'z' values would have that attribute.

      What is really unique about Portal is the portal visuals.

    • by rk ( 6314 ) * on Tuesday August 26, 2008 @04:12PM (#24755795) Journal
      I never really thought about that before, but you're right. The portal gun is also a perpetual motion engine. Put a portal above a paddle wheel, and a portal below the paddle wheel. Add enough water to get the paddle going, and poof. Power until enough water evaporates that it can't turn the wheel anymore.
      • by daver00 ( 1336845 ) on Tuesday August 26, 2008 @06:00PM (#24757061)

        So on a slightly off topic note, does that invalidate the theory of 'wormholes' for some hypothetical FTL space travel? Surely, were it possible to create a wormhole in spacetime, you could set up a perpetual motion machine that works exactly like you describe, thus violating the second law. A nice simple thought experiment, I like it.

    • by lgw ( 121541 )

      Nope, momentum *is* conserved, it's just that space is no longer a metric. Portal's portals don't teleport, they join distant portions of space so that you pass through them continuously. You do *not* conserve potential energy - teleporting from higher to lower does not "fling" you: your speed is the same as before.

    • Isn't momentum conserved from the frame of reference of the person traveling through the portal? From a fixed frame you are right, I head into a portal going one way, and come out going another... but from the frame of reference of the person entering the portal you keep moving the same way the entire time.

      • by xPsi ( 851544 ) *
        The difference is that person's frame is non-inertial because it is accelerating (i.e. their velocity vector and speed are changing). Motion is usually best analyzed in an inertial frame (e.g. the fixed frame of the infrastructure).
    • Re: (Score:3, Funny)

      by Atzanteol ( 99067 )
      Sooo... Speedy thing goes in, speedy thing comes out?
    • Re: (Score:3, Insightful)

      by Dr. Cody ( 554864 )

      Actually, there's a "conservation whipping boy" in a hidden room of each level getting pummeled against the wall of his cell to account for inconsistencies arising from use of the gun.

  • by Animats ( 122034 ) on Tuesday August 26, 2008 @03:52PM (#24755471) Homepage

    There's a fair amount of fake physics involved. Properly, the parts of the character on one side of the portal should have the gravity and momentum of that inertial frame, and as the character passes through the portal, the new frame should begin to act on the character. But the sample code in Gamasutra treat the character as a single rigid body.

    It's a neat problem to make the physics correct as the character moves though a portal. It could certainly be done, even for ragdoll characters. From a gameplay perspective, it would drive players nuts. To make the gameplay tolerable, the designers of this game added a pseudo-force that tends to align the character with the local vertical. Otherwise, characters would have execute proper parachute landing falls when moving through a gravity vector change.

    Character physics almost has to be fake. Trying to drive a real car via a game pad is very difficult, and trying to drive a human body via a game pad is worse.

    • by brkello ( 642429 )
      Hate to burst your bubble...but video game characters have no ALL physics in the game would have to be fake ;)
    • Re: (Score:3, Interesting)

      by lgw ( 121541 )

      Technically, the force of gravity should also pass through the portal, providing a smooth transition of forcesm, not a discontinuity. That would be interesting, but I think less fun. The way that the direction of gravity changes abruptly as you move through a portal is part of the charm.

  • by Sockatume ( 732728 ) on Tuesday August 26, 2008 @03:59PM (#24755599)
    Portal exploits portal rendering [] technology - a technique for graphics optimisation which incidentally allows you to take a given map layout and provide a different subjective layout for the player. It's actually a fairly trivial problem to do the portals themselves, as most graphics engines these days should have portalling built in. All of the interesting physics comes in when they start making it work as gameplay, for example by giving portal entrances "push" or "pull" to steer players into and out of them. The article is a good description of how to make a game that behaves a bit like Portal, but it's got nothing to do with that game's actual physics.
  • by p0tat03 ( 985078 ) on Tuesday August 26, 2008 @04:20PM (#24755909)

    Portal's physics go *way* beyond what the article implies.

    Article basically says: it's not a simple teleport, direction of movement and momentum are preserved.

    This is far too much of an oversimplification. Portal was probably a technically difficult game to code for - mostly due to collision physics. The problem is that something does not instantly teleport from one end of the portal to another. You can have an object on BOTH sides of the portal. This makes physics calculations very difficult, since you essentially have a single object of a small finite size, colliding with different objects across the room, affected differently by gravity, etc.

    If I get the right gist from the developer commentary in the game, their solution was the CLONE the two sides of the portal in a mini physics-only environment and run the simulation there.

    Definitely much more complex than the article.

  • Wasn't the first game to have teleportation Zork?

  • by mfnickster ( 182520 ) on Tuesday August 26, 2008 @06:50PM (#24757515)

    Didn't Asteroids have prior art on this..? :)

The world is coming to an end--save your buffers!