Portals and Teleporting the Player

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
Selenaut
Prole
Posts: 6
Joined: Wed Jul 24, 2013 10:41 pm

Portals and Teleporting the Player

Post by Selenaut »

Hello, all.

This is my first post here, and it's mostly due to the fact that I haven't needed to post a question before.

I am working on a game that I have mostly figured out in theory; however, I have run into a problem in implementing these portals. Basically, I want them to work like the ones from Portal: The Flash Version, but I can't figure out a good way of triggering the portals so that when an object is moved into Portal A, it comes out of A's linked Portal B. Obviously it should conserve velocity and make sure it is pointing in the right direction, but I can figure the math out on my own. My question is on how to create a portal that can act as a trigger - I have looked through the wiki, and it seems that Shapes' ability to act as a trigger was removed in 0.8.0. I can provide some code, if it is needed, but I think the code I have at the moment won't help to answer this much.

tl,dr: How do I make a shape/body using love.physics that can act as a trigger when a body touches it?

Thanks in advance,

Selenaut
Making a game in Lua without knowing Lua.
User avatar
seanmd
Prole
Posts: 35
Joined: Sat Jun 22, 2013 7:47 am

Re: Portals and Teleporting the Player

Post by seanmd »

I think what you want is a sensor.
http://www.love2d.org/wiki/Fixture:setSensor
Selenaut
Prole
Posts: 6
Joined: Wed Jul 24, 2013 10:41 pm

Re: Portals and Teleporting the Player

Post by Selenaut »

Hey seanmd, thanks for the reply.

That sounds like it should work. I'll continue posting my progress on getting these portals to work here. Hopefully soon I'll have a quick demo set up so people can check it out.

Thanks again,

Selenaut
Making a game in Lua without knowing Lua.
Selenaut
Prole
Posts: 6
Joined: Wed Jul 24, 2013 10:41 pm

Re: Portals and Teleporting the Player

Post by Selenaut »

Alright, I have the portal class setup so that its fixture is a sensor. In the callback functions, how do I know which portal it's coming from?

At the moment, I have a table of portals, and I don't know how to tell which one was collided with, nor what collided with it. How do I get those?

Thanks,

Selenaut
Making a game in Lua without knowing Lua.
User avatar
seanmd
Prole
Posts: 35
Joined: Sat Jun 22, 2013 7:47 am

Re: Portals and Teleporting the Player

Post by seanmd »

They're supposed to represent the same point in space, right? You may be overthinking the problem.
If you need to attach any arbitrary data to the fixture you can do so via fixture:setUserData(aTable)
Selenaut
Prole
Posts: 6
Joined: Wed Jul 24, 2013 10:41 pm

Re: Portals and Teleporting the Player

Post by Selenaut »

So I've set up a table for my Portal "class" called usrData. However, when I write:

Code: Select all

function beginCallback(fixture1, fixture2, contact)
	if fixture1:getUserData().obj == "portal" then
		--stuff
	end
	if fixture2:getUserData().obj == "portal" then
		--other stuff
	end
end

function endCallback(fixture1, fixture2, contact)
	if fixture1:getUserData().obj == "portal" then
		--more stuff
	end
	if fixture2:getUserData().obj == "portal" then
		--still more stuff
	end
end
and two objects collide, I get an error stating "attempt to index a nil value," by which it means that the one fixture's getUserData() didn't return a table. How can I avoid this?

Thanks,

Selenaut
Making a game in Lua without knowing Lua.
User avatar
seanmd
Prole
Posts: 35
Joined: Sat Jun 22, 2013 7:47 am

Re: Portals and Teleporting the Player

Post by seanmd »

well, there are probably going to be objects without userdata unless you're explicitly setting them yourself. you'll need to do something like:

Code: Select all

if fixture1:getUserData() and fixture1:getUserData().obj == "portal" then
  ...
if fixture2 ...
you could do something else to determine it's a table first, or you could just set the userData to "portal" rather than a table if you don't need anything else stored in it.

caveat: make sure you nil out the userdata when/if you finally destroy the object. I've found that they tend to stick around in memory even after destroying the fixture.

Keep on chuggin'
Post Reply

Who is online

Users browsing this forum: No registered users and 170 guests