Contents
Games Demystified: Portal
 
 
Printer-Friendly VersionPrinter-Friendly Version
 
Latest News
spacer View All spacer
 
November 22, 2009
 
Video Game Watchdog National Institute On Media And The Family Shutting Down [11]
 
Modern Warfare 2 Infinity Ward's 'Most Successful PC Version' Yet [12]
 
New Tech, Design Details Of Project Natal To Emerge At Gamefest In February
spacer
Latest Jobs
spacer View All     Post a Job     RSS spacer
 
November 22, 2009
 
Sucker Punch Productions
Character Artist
 
Sucker Punch Productions
3D Environment Artist
 
Sucker Punch Productions
Network Programmer
 
Sucker Punch Productions
Texture Artist
 
Sony Online Entertainment
Brand Manager
 
Monolith Productions
Sr. Software Engineer, Engine - Monolith Productions - #113767
 
Crystal Dynamics
Sr. Level Designer
 
Gargantuan Studios
Lead World Designer
spacer
Latest Features
spacer View All spacer
 
November 22, 2009
 
arrow Upping The Craft: Susan O'Connor On Games Writing [6]
 
arrow Small Developers: Minimizing Risks in Large Productions - Part II [6]
 
arrow iPhone Piracy: The Inside Story [48]
 
arrow And Yet It Grows: Analyzing the Size and Growth of the European Game Market [5]
 
arrow NPD: Behind the Numbers, October 2009 [13]
 
arrow Reflecting On Uncharted 2: How They Did It [5]
 
arrow Sponsored Feature: Rasterization on Larrabee -- Adaptive Rasterization Helps Boost Efficiency
 
arrow Postmortem: Wadjet Eye's The Blackwell Convergence [2]
spacer
Latest Blogs
spacer View All     Post     RSS spacer
 
November 22, 2009
 
Accepting the Inherent Value of Games
 
Planckogenesis, Part II: Song Structure & Gravy Train [1]
 
Designing Games Is About Matching Personalities [1]
spacer
About
spacer News Director:
Leigh Alexander
Features Director:
Christian Nutt
Editor At Large:
Chris Remo
Advertising:
John 'Malik' Watson
Recruitment/Education:
Gina Gross
 
Features
  Games Demystified: Portal
by Jeremy Alessi
10 comments
Share RSS
 
 
August 26, 2008 Article Start Previous Page 3 of 3
 

The first part of our updatePlayerPhysics() function is an alignToVector() call which slowly re-orients our player back to the upright position. The parameters ( 0, 1, 0, 2, 0.05) dictate that our character will be aligned full upright along the Y-axis at a speed of 0.05, or very slowly. As the player tumbles through portals, the character's alignment gets constantly re-arranged.

Anyone who's played Portal should realize the developers' extra special attention to detail in this regard, as the player and camera are constantly being adjusted to keep the experience as intuitive as possible. In this demo, the player is slowly rotated back to the upright position, which is something to take notice of while tumbling through the puzzles in Portal.

Advertisement

The next part of the code is where the interesting bits lay. There is a check for portal collision. If it returns true, we will set our player collision type to static and search for the other end of our portal.

Afterwards, we reposition our player at the entrance of the opposing portal (or the exit of the portal we had previously collided with).

Then we calculate our player's total velocity and break it down into components relative to the portal we're coming out of  -- by multiplying the player's total velocity by the surface normals of the exit portal.

This is what gives us the "waterslide" effect of entering in one direction and exiting at the same speed but in an entirely new direction.

The rest of the code within the first conditional helps create a smooth transition from one portal to the other. The first part sets the character's orientation with respect to its rotation upon entering the portal.

Afterward, the player is positioned out away from the exit portal an appropriate amount to avoid choppiness or repeated collision, once our player is returned to dynamic collision status.

Finally, a capture, update, and render are completed to avoid any choppiness in the visuals. Portal is especially slick in this regard, as it is virtually seamless when it teleports the player character about.

The final section of the updatePlayerPhysics() function is the flipside of the portal collision conditional. All it does is stop the player upon a static collision or otherwise pull the player down due to gravity.

The most interesting component of this is the collision normal conditional, which only stops the character from falling if they are on flat enough ground. Without that, the player can climb walls.

Portal is a landmark title. It's deceptively simple at a glance, but in truth is the implementation is a highly polished and complex system, which this article only begins to scratch the surface of. As stated above, the story was entertaining and the rendering was nothing short of pure eye candy.

However, without the mechanics Portal would not have been nearly as compelling. The title really conveys the feeling of partaking in the empirical process of groundbreaking science.

Just reading about something like this in a Michael Crichton novel or watching a sci-fi flick with great CG effects would not have had the same impact. Portal's mechanics distinguished it from other games this year, and more importantly, they distinguished games as a whole from the rest of contemporary media.

And now I'm going to grab a piece of cake from my rehearsal dinner and I'm not lying! Until next time... do not step into the... ... ... ... it may cause permanent malfunctions such as death.

[To download the associated demo and code sample for Portal Demystified, please click here.]

 
Article Start Previous Page 3 of 3
 
Comments

Haig James Toutikian
profile image
Coming from a science background, I can really appreciate articles like this. Very refreshing!


Eric Diepeveen
profile image
Awesome article. You keep picking the most innovative & jawdropping gameplay features from games and turn them into understandable lines of code. Your fascination with physics makes for some awesome reads. Keep up the good work.

Milosz Derezynski
profile image
Haig: Not coming from a science background (well, CS..) i appreciate this article too ;)

Bart Stewart
profile image
Love it. It's like going back to the heady days when Byte magazine still printed code listings.

Knowing how stuff really works is glorious.

Thanks for choosing to publish this, Gamasutra editors!

Stone Bytes
profile image
If portals are nothing really new, Portal itself has been a gem for genuinely focusing on a main, say single positive gimmick which is key to the gameplay. Just proof that you don't need games that do all, just be sure that the one core idea or function that supports the whole product is solid and rich enough.
I also honestly hope that people won't start to say that anyone could have made Portal, notably because it's not that hard to "find" the code behind the rule. This is something recurrent you hear from jaleous mouths, but the point is that one guy did it, you others didn't, and too bad for you.

Dario Hardmeier
profile image
Another great article with very inspiring insights.

I suggest one slight modification to the transformation of the player velocity : instead of changing the velocity direction to the normal of the exit portal, thus changing any movement at a right angle to the portal normal into forward motion, i propose projecting the velocity vector onto the basis vectors of the entrance portal. This gives back the players velocity relative to the entrance portal's orientation. This new velocity can then be multiplied with the basis vectors of the exit portal, resulting in the "correct" transition.

Might be a few more lines of code (and a bit more of a hassle with setting up the basis vectors), but allows far more possibilities when playing around with portals.

Things could even be taken further when using different sizes of portals (ie differing, non-normalized basis vectors for the entrance and exit portals), in combination with scaling of objects passing through a portal. For a very nice example of this, see Peter Molyneux's GDC 2005 "Room" demo ( http://www.youtube.com/watch?v=vGiPUx9Zgi0 ).

PS: Very looking forward to you next article!

Anonymous
profile image
I would've liked to hear about the rendering of the portals as well, which it seems to me is the more technically challenging part. There was a commentary node about it in the game: as I recall, they iterated through several methods, each of which had its own performance issues, and eventually arrived at rendering inside a stencil buffer using a different camera. I would expect this to put particular pressure on the vertex unit of the GPU, which may explain why they chose very clean-looking environments that are low on vertices. It's also only done to two levels of recursion, after which further portals are cheaply faked by only rendering the fiery oval instead of the actual scene. Very clever to use a stencil buffer for such an unusual trick.

Anonymous
profile image
You're absolutely right. The rendering part is so much more interesting. I think everybody with a bit of understanding on vectors knows how to do this. Probably anybody who can code in Blitz3D. :)

The Room from GDC 2005 is really interesting. I did not get whether they included this mechanic in BW2???

Shay Pierce
profile image
I'm with everyone else on wanting to see some practical implementations of the portal rendering. But since the author was apparently writing this on the eve of his wedding, I certainly forgive him for only analyzing ONE fascinating piece of code. :)

Maybe the next installment of the column could come back to Portal and analyze that part as well.

If not, I'd like to see the next one be about Braid's time rewinding mechanics! Seems like Jon Blow has described how he did it enough times that it would be easy to reverse-engineer. But even when you know you could do it yourself if you had to, there's something about seeing someone else actually implement it and share the code - even if it's a non-refined implementation - that gets the creative juices flowing. Thanks for the articles Jeremy!

Anonymous
profile image
The rendering of the portals is not quite right. The portals are obviously rendered with a set camera position, but should be rendered using the direction of the player to give a correct perspective on the 2D plane. For instance if you walk up to one of the portal entrances and look diagonally in it, it does not look correct.


none
 
Comment:
 


Submit Comment