Portal Gun Logic

So the current publicly available portal gun is pretty glitchy, and I aim to fix that.

A few questions:

  1. How does Valve make two different parts of the map (seem) attached?
  2. How can I allow a player to walk through a portal with a brush behind it without noclipping them?
  3. Is there a way to create a func_areaportal ingame?
  4. How would bullets be allowed to pass through portals with the same data?
  5. How can a player pass through a little bit at a time rather than all at once?

What I’ve gathered so far:

  1. Portals are inversely symmetrical across their local x-axis.
  2. render.RenderView() can be used with stencils to create the visual effect of a portal, but the image seems to have increased contrast in the current example.
  3. The current transition between portals is far from seemless. Because it uses noclip, velocity transfers are awful.
  4. Props go through nicely, but they are nocollided to the world when they go through the portal and fall through the world.
  5. Roof and floor portals don’t work because when you noclip you stop moving, so as you noclip into it you just float above it.

How would you go about doing this?
Can you answer any of the above questions?

There’s little to no documentation on how the portal gun works in the source engine available on the internet.
I’m sure you’ve all got your ideas on the behind-the-scenes of the portal gun. Please voice them here. I’m just about stumped.

Try to find the sourcecode and all your questions will be awnsered

Do you really think any game companies release source code to their games?

There are leaks dude. Go search and you will find.

Leaks are not game source code releases. This being said, there’s no source code of portal gun.

I’d wager it has something to do with stencils.

The biggest unknown is the physics. How does it move so seamlessly? Does it use cloned entities or does it just move it through? How?

By making a hole in the wall. I have an idea how to achieve this, but it is hacky as fuck. Keep in mind that VALVe had access to the whole engine, including physics engine - we don’t.

Hack away. How would you do it?

Wait, is he implying IMeshes

Making the player not collide with world, but only with specific entities which should be like a frame around the portal. Obviously you’ll have to render the player one more time on the other side so it looks as if he’s moving through the portal.

This actually sounds like a good idea. Make a little floor below the portal (given that it is horizontal) and let entities land on it. Anything above it within the portal’s height becomes nocollide-world.


I worked on some bullet-transmission code, but it crashes when I shoot a portal. Any idea why?
function BulletHook(victim,dmginfo)
if victim and victim:IsValid() and victim:GetClass() == “prop_portal” and dmginfo:GetInflictor() then
local portal = victim:GetNWEntity(“Potal:Other”)

	if portal and portal:IsValid() then
		local offset = victim:WorldToLocal(dmginfo:GetDamagePosition()) --Wayyy better calculation of the offsets
		offset.x = -offset.x;
		offset.y = -offset.y;
		offset = victim:LocalToWorld(offset)
		local newbullet = {}
		newbullet.Num = 1
		newbullet.Src = offset + portal:GetForward() * 55
		--newbullet.Dir = portal:GetAngles():Forward()
		newbullet.Dir = (dmginfo:GetInflictor():GetPos():AngleEx(offset)):Forward() --Get an angle from the position of the gun and the position of the bullet's hit.
		newbullet.Spread = Vector(0.01,0.01,0)
		newbullet.Tracer = 1
		newbullet.TracerName = "Tracer"
		newbullet.Force = dmginfo:GetDamageForce():Length()
		newbullet.Damage = dmginfo:GetDamage()
		newbullet.Attacker = dmginfo:GetAttacker()
		newbullet.AmmoType = dmginfo:GetAmmoType()
		portal:FireBullets(newbullet) --crashes here. I've tested.

hook.Add(“EntityTakeDamage”, “PortalHook”, BulletHook)

I’ve created a phx prop on the bottom of the portal like you said, but now I have no idea how to make the player collide with only it.
I’ve looked at collision groups, movetypes, and solidness, but I can’t think of a way to limit collision to a single entity.

Any ideas?

He got banana’d. What a fool.

I would like to note that Garry said he could have implemented a Portal gun engine to Garry’s Mod, but never had the time for the process. Too bad, but what we have right now isn’t so bad.

A little progress, so you guys see how much I’ve got actually finished:

Very cool. If a speedy thing goes in, will a speedy thing come out? (Aka is momentum kept when you fall through a portal on the ground)

Well for one you should probably be making the firing entity the person who fired the bullet in the first place instead of the portal itself.

Yes, but props just fall through the world after they enter the portal.
Players keep their velocity, though. When I had sloped portals working just a little bit I could jump into one and come flying out the other.

Weapons fire bullets, not players, but I’ll try that thanks.

i will give you a hint, the props not rendering (and skybox) is because of pvs.

Would it be a terrible idea to add “r_novis” to the config menu? If what you say is true then wouldn’t that fix it?

r_novis 1 fixed it, but it dropped my fps by like 10.

I guess it’s not such a good idea… :v:


Uh, the portal code did get leaked with the src code. I have it right now on my hard drive, pm me if you want a link.