Jump to content

Getting a list of entities in a collision trigger


Einlander
 Share

Recommended Posts

Can a mod move this to the programming section?

 

 

Is there a way to progamatically get a list of entities that are currently colliding with an object? I would like to be able to get a a list of all the unique entities in a collision trigger then compare it later. For example, a person walks into a room, then leaves. I want to be able to detect when a person leaves the area.

 

I have been partially able to solve some of my needs.

 

--[[
This script will make any entity act as a collision trigger. It works best when you set the
entity's collision type to "Trigger". This will continuously detect collisions without causing
any physical reaction.
--This Has been modified to behave more like the Source Engine/Unity Collision Triggers
]]--
function Script:Start()
--Create a table to keep track of all entitys that are interacting with the collision trigger
self.CollisionEntites = {}
end
function Script:Collision(entity, position, normal, speed)
--[[
We are going to keep track of 3 things:
[Done] When an entity ENTERS the collision trigger
[Done] When an entity is IN the collision trigger
[] When an entity EXITS the collision trigger
]]--
--When an entity ENTERS the collision trigger
if (self.CollisionEntites[entity] )== nil then -- Check table, is the entity already in it?
-- No?
--check if its in the list
local matchfound = false
if matchfound == false then
for itemnum,value in ipairs(self.CollisionEntites) do
 if entity == value then
 matchfound=true
 end
end
table.insert(self.CollisionEntites,entity) --add entity to list of objects currenty in the collision trigger
if matchfound == false then
 self:CollisionOnEnter(entity, position, normal, speed) -- Raise CollisionOnEnter event
end
end
-- yes? Call the CollisionOnStay Event
self:CollisionOnStay(entity, position, normal, speed)	
end
end
function Script:CollisionOnEnter(entity, position, normal, speed)
self.component:CallOutputs("CollisionOnEnter")
end
function Script:CollisionOnStay(entity, position, normal, speed)
self.component:CallOutputs("CollisionOnStay")
end

 

I can tell when an entity enters, and stays inside. But not when it leaves.

Link to comment
Share on other sites

True, but that one indiscriminately report when itself has been entered and exited. I need the script to report which entity has exited. I would like the on exit to be able to report the same information the collision event does. Which entity left, and optionally, position and speed

Link to comment
Share on other sites

It was a rough night last night so maybe it's me, but what do you mean it indiscriminately reports when itself has been entered/exited? You would put this on a csg brush and paint the invisible material on it and it becomes your volume trigger reporting things that have entered and exited it.

Link to comment
Share on other sites

The script you have, when a collision enters,exits and stays it reports an event happens. By indiscriminately I mean that it doesn't take account of which entity entered, or which entity exited. With that script, it doesn't say which entity entered, or which exited. I want the collision script to be able to affect other entities. For example, When an entity enters, set its gravity state off, then when it leaves turn it back on. That script will only know its holding something, not what or how many.

 

Don't worry, I wrote this post after spending an all nighter. I'm writing this post while at work as fast as possible, so I dont know if I'm communicating my idea properly.

Link to comment
Share on other sites

This script, as is, works for 1 entity, but you should easily be able to extend that to work with any number of entities. There are 3 variables (entered, exited, hasCollision). Make a table that stores objects of: entity, entered, exited, hasCollision).

 

OnEnter you would add them to this list. Then you loop through the list and set variables for that element like the script does for the one.

 

Also, it does tell you which entity entered. The collision function has an entity parameter which is the entity that entered. What is doesn't do, but you can modify it to, is tell you which entity exited, because it doesn't care, but again you can make it care by modifying it :)

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...