Slashdot Mirror


Examining Portal's Teleportation Code

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.

61 of 278 comments (clear)

  1. Frictionless tube? by Anonymous Coward · · Score: 5, Funny

    Your mom has a frictionless tube.

    First post?

    1. Re:Frictionless tube? by Chris+Burke · · Score: 5, Funny

      For friction to be present, the object being inserted into the tube must be large enough to touch the sides. That must be your problem.

      --

      The enemies of Democracy are
    2. Re:Frictionless tube? by jollyreaper · · Score: 4, Funny

      For friction to be present, the object being inserted into the tube must be large enough to touch the sides. That must be your problem.

      THAT'S WHAT SHE SAID! -er, shit. *self pwn*

      --
      Kwisatz Haderach
      Sell the spice to CHOAM
      This Mahdi took Shaddam's Throne
    3. Re:Frictionless tube? by noundi · · Score: 2, Funny

      Your internet has a series of frictionless tubes. *high fives Ted Stevens*

      --
      I am the lawn!
  2. Portal by CaptainPatent · · Score: 4, Funny

    Regardless it's a lot better than quantum teleportation.

    --
    Well, back to rejecting software patent applications.
    1. Re:Portal by Impy+the+Impiuos+Imp · · Score: 5, Informative

      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.

      --
      (-1: Post disagrees with my already-settled worldview) is not a valid mod option.
    2. Re:Portal by ElizabethGreene · · Score: 4, Funny

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

    3. Re:Portal by pushing-robot · · Score: 4, Interesting

      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.

      --
      How can I believe you when you tell me what I don't want to hear?
    4. Re:Portal by SanityInAnarchy · · Score: 4, Interesting

      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.

      --
      Don't thank God, thank a doctor!
    5. Re:Portal by beav007 · · Score: 2, Funny

      It's not the execution, it's the cake.

      Note that, in Portal, execution and cake seem to be linked...

    6. Re:Portal by grahamd0 · · Score: 2, Funny

      It's not the execution, it's the cake.

      I didn't want him to think of me as a liar.

    7. Re:Portal by lysergic.acid · · Score: 2, Interesting

      well, i used to spend a lot of time creating custom maps in DN3D's build program, so i know how much of a pain in the ass it is, and the wide variety of hacks that are often employed--but isn't this true with every game engine? unless you can actually model the natural laws of physics, you're going to have to use hacks to fake most of it.

      i know that Quake was a huge improvement over DN3D--for one it didn't use 2d sprites--but i still think it's a much bigger jump from Doom to Duke Nukem 3D than it is from Duke Nukem 3D to Quake. i just remember the first time i played Duke Nukem 3D, dropping down from that vent shaft in Hollywood Holocaust at the beginning of Episode 1: L.A. Meltdown, it totally blew my mind how "realistic" this game was. having only played games like Wolfenstein and Doom before this, it was completely amazing to me that i could actually look around in any direction i wanted to using the mouse (including up and down). and when i discovered that you could actually climb onto the buildings, walls, tables, etc. and interact with the environment so freely (especially with the jet pack) rather than being restricted to set paths or hallways, it was like a door to a whole new world of virtual reality was opened to me.

      there are a lot of games that would be better classified as 2.5D than Duke Nukem 3D--for instance Crash Bandicoot, which although fully 3D graphically, still limited player movement to a fixed 2D "track." i mean, in Duke 3D you could actually have aerial deathmatch battles with the jet pack and literally fly over or under the other player. so the vertical layering issue is strictly limited to the map engine, and inside every "room" you had a fully 3d environment.

    8. Re:Portal by SanityInAnarchy · · Score: 2, Insightful

      unless you can actually model the natural laws of physics, you're going to have to use hacks to fake most of it.

      True.

      My point here is that Quake was a milestone in avoiding such hacks -- most levels built in most modern FPSes could have been built in Quake, it's just that the performance would suck. These levels really couldn't have been built in Duke3D.

      dropping down from that vent shaft in Hollywood Holocaust at the beginning of Episode 1: L.A. Meltdown, it totally blew my mind how "realistic" this game was.

      Yeah, I remember that -- it was my first FPS. The explosions surrounding that were pretty cool, too -- camera shaking and everything.

      Of course, it was also my first abuse of cheats. DNKROZ.

      so the vertical layering issue is strictly limited to the map engine, and inside every "room" you had a fully 3d environment.

      Well, you have a fully 3D column of air. I still find it quite a lot more interesting to explore a fully 3D environment -- Half-Life was probably the next game that blew my mind. (Though, to be honest, it was probably Counter-Strike first.)

      --
      Don't thank God, thank a doctor!
    9. Re:Portal by TheThiefMaster · · Score: 2, Insightful

      The point is that you actually walk INTO the portal, you don't teleport on contact. The physics in portal has been written to allow you to essentially walk into the wall the portal is on.

      Perhaps a better way of putting it would be that there is continuity of collision detection across the portal.

      No attempts to imitate it have done this, they've just done the traditional "teleport on contact", which means that there is an obvious jump as you touch the portal. Including in TFA's attempt.

  3. But... by i.r.id10t · · Score: 5, Funny

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

    --
    Don't blame me, I voted for Kodos
  4. Too verbose by Anonymous Coward · · Score: 5, Funny

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

    1. Re:Too verbose by elrous0 · · Score: 5, Funny

      My ex-girlfriend used to use that exact phrase all the time.

      --
      SJW: Someone who has run out of real oppression, and has to fake it.
  5. Ahoy, GlaDOS! by Stanistani · · Score: 4, Funny

    I want to see the nautical version, Porthole.

    1. Re:Ahoy, GlaDOS! by Darth_brooks · · Score: 4, Funny

      I'm waiting on the hot grits version, Portman

      --
      There are some people that if they don't know, you can't tell 'em.
    2. Re:Ahoy, GlaDOS! by Underfoot · · Score: 3, Funny

      I want to see the white water version, Portage.

      --
      I mentioned tinker-toys once in a post - now I'm modded down for life.
    3. Re:Ahoy, GlaDOS! by Captain+Splendid · · Score: 4, Funny

      I'm waiting on the drunken lawyer version, Port

      -Rowley Birkin, QC

      --
      Linux, you magnificent bastard, I read the fucking manual!
    4. Re:Ahoy, GlaDOS! by Anonymous Coward · · Score: 5, Funny

      The grits are a lie :(

    5. Re:Ahoy, GlaDOS! by MobileTatsu-NJG · · Score: 4, Funny

      I'm waiting on the drunken lawyer version, Port

      I'm waiting for the Wii version, P.

      --

      "I like to lick butts!" by MobileTatsu-NJG (#32700246) (Score:5, Informative)

    6. Re:Ahoy, GlaDOS! by shadwstalkr · · Score: 2, Funny

      I'm waiting for the Wii version, P.

      I'm waiting for the Scrabble version, Portmanteau.

  6. Physics is Phun by CopaceticOpus · · Score: 3, Interesting

    A well made 2D Flash version of Portal:

    http://portal.wecreatestuff.com/portal.php

  7. M.C.Escher has good OLD examples of the concept: by Zymergy · · Score: 3, Interesting
  8. The code by anomnomnomymous · · Score: 2, Funny

    The code is a lie!


    Ok, sorry...

    --
    When you shoot a mime, do you use a silencer?
  9. Damn! by khendron · · Score: 4, Insightful

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

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

    --
    Life is like a web application. Sometime you need cookies just to get by.
    1. Re:Damn! by Eli+Gottlieb · · Score: 2, Informative

      Try this. They claim it's 3.5 hours of game time, but the puzzles are *hard*, so it takes more like a day.

  10. Next step? by Aranwe+Haldaloke · · Score: 4, Funny

    Porting Portal's portal code?

    1. Re:Next step? by saboola · · Score: 5, Funny

      I'm not a portal porter
      I'm a portal porter's son
      And i'm only porting portal
      Till the portal porter comes

  11. WTF by 4D6963 · · Score: 5, Insightful

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

    --
    You just got troll'd!
    1. Re:WTF by 77Punker · · Score: 2, Insightful

      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.

    2. Re:WTF by kestasjk · · Score: 5, Funny

      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.

      --
      // MD_Update(&m,buf,j);
    3. Re:WTF by 4D6963 · · Score: 2, Insightful

      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.

      --
      You just got troll'd!
    4. Re:WTF by 4D6963 · · Score: 2, Insightful

      You're pretty much missing the point, or at least your comparison is quite flawed. The Mona Lisa isn't obvious, not anyone who wanted to a woman's portrait came up with the Mona Lisa. And neither is Portal as a whole. But the algorithm behind the teleportation in Portal is flat out obvious. Like, really, anyone can come up with it when they're thinking of teleportation. The idea behind the game is what's admirable and ingenuous, not the little necessary and obvious algorithm.

      --
      You just got troll'd!
    5. Re:WTF by xhrit · · Score: 2

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

      I am using this method for the intersteller transit gates in my Zero Point War game series. I have not figured out how to preserve momentum using ODE yet tho - my simulation breaks.

    6. Re:WTF by billsnow · · Score: 3, Funny

      velocity is the correct term. even though the moving object changes direction within the 3 dimensional space, the vector value must be retained in relation to the plane of the original portal and translated to the plane of the new portal. don't feel bad; as an engineer, i'm accustomed to correcting physics majors on such topics.

  12. Re:bzflag? by Smidge204 · · Score: 2, Interesting

    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 recall the portals being multi-directional but that game had a lot of funky physics in it so maybe I just don't remember. Also, the portals were all scripted in-place so they might have taken 'shortcuts' in the code to make them work.
    =Smidge=

  13. My big question by Zerth · · Score: 4, Interesting

    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.

    1. Re:My big question by pla · · Score: 2, Informative

      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.

    2. Re:My big question by Apache · · Score: 2, Informative

      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.

      Sounds like a fantastic idea for portal 2. :D

  14. Portalized by TypoNAM · · Score: 2, Informative

    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:
    http://www.ogre3d.org/phpBB2/viewtopic.php?t=37376

    He also has a blog which seems quite lacking though.

    --
    This space is not for rent.
  15. Portal Physics 101 by xPsi · · Score: 5, Interesting

    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.

    --
    i\hbar\dot{\psi}=\hat{H}\psi
    1. Re:Portal Physics 101 by rk · · Score: 3, Interesting

      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.

    2. Re:Portal Physics 101 by Atzanteol · · Score: 2, Funny

      Sooo... Speedy thing goes in, speedy thing comes out?

      --
      "Ignorance more frequently begets confidence than does knowledge"

      - Charles Darwin
    3. Re:Portal Physics 101 by daver00 · · Score: 4, Insightful

      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.

    4. Re:Portal Physics 101 by Dr.+Cody · · Score: 2, Insightful

      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.

  16. Re:It would have been more fun by Punko · · Score: 2, Funny

    Dwarves on Discworld don't fight golems. They fight trolls. This is why the internet needs more dwarves.

    --
    If only we could fall into a woman's arms without falling into her hands
  17. The physics is actually fake. by Animats · · Score: 3, Interesting

    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.

    1. Re:The physics is actually fake. by lgw · · Score: 2, Interesting

      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.

      --
      Socialism: a lie told by totalitarians and believed by fools.
  18. Re:It would have been more fun by xouumalperxe · · Score: 4, Funny

    multi-hundred pound canon

    Omnianism has loads of holy books, but I don't think even those collected amount to several hundred pounds.

  19. They omitted what Portal actually does. by Sockatume · · Score: 3, Interesting

    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.

    --
    No kidding!!! What do you say at this point?
  20. Re:M.C.Escher has good OLD examples of the concept by Chris+Burke · · Score: 4, Funny

    Speaking of, whens he gonna put a new album out?

    Are you talking about M.C. Escher who designed the crazy staircase house? Or David Bowie, who lives there?

    --

    The enemies of Democracy are
  21. Re:am i by FiloEleven · · Score: 4, Insightful

    When it was ugly, buggy, and short?

    Narbuncular Drop was a great student project showing off a new idea, and I'm glad it's still available to play with. Neglecting the gameplay polish, puzzle depth, and environmental detail improvements that went into Portal is, IMO, a gross error in judgment.

    You forgot to end with "get off my lawn" =)

  22. No kidding by Moraelin · · Score: 2, Insightful

    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.

    --
    A polar bear is a cartesian bear after a coordinate transform.
  23. Grossly Oversimplified by p0tat03 · · Score: 5, Insightful

    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.

  24. Not really by Sycraft-fu · · Score: 2, Insightful

    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.

  25. Asteroids anyone? by mfnickster · · Score: 3, Funny

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

    --
    "Slow down, Cowboy! It has been 3 years, 7 months and 26 days since you last successfully posted a comment."
  26. It's exactly the same. by DrYak · · Score: 4, Insightful

    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)

    --
    "Sufficiently advanced satire is indistinguishable from reality." - [Tips: 1DrYakQDKCQ6y52z6QbnkxHXAocMZJE61o ]
  27. Re:Ahhh by yanyan · · Score: 2, Funny

    +1, Touche.