Detecting if a non-colliding entity is inside another entity
5 replies, posted
I'm trying to make a "ghost" prop that collides only with the world. I'm accomplishing this by using:
[lua]
self:SetCollisionGroup(COLLISION_GROUP_WORLD)
[/lua]
I also want the prop to turn red if it is inside another entity. However, since the prop doesn't calculate collisions between it and other entities anymore, I can't use the standard ways to detect this. For example, [URL=http://wiki.garrysmod.com/page/PhysObj/IsPenetrating]PhysObj.IsPenetrating[/URL] would work perfectly for me, but it only works if the entities can actually collide. I thought about using the bounds of the prop and using ents.FindInBox, but it would be very limiting while working with angled props, especially large flat ones.
So I'm wondering if anyone can think of any way I can do this. Again, I just want the prop to only collide with the world and turn red if it's inside another entity.
Might not work with all cases but pretty sure nearestpoint uses angled collisions.
[lua]local targetcenter = target:LocalToWorld(target:OBBCenter())
local nearest = self:NearestPoint(targetcenter)
if target:NearestPoint(nearest) == nearest then[/lua]
[QUOTE=JetBoom;41840705]Might not work with all cases but pretty sure nearestpoint uses angled collisions.
[lua]local targetcenter = target:LocalToWorld(target:OBBCenter())
local nearest = self:NearestPoint(targetcenter)
if target:NearestPoint(nearest) == nearest then[/lua][/QUOTE]
Hmm, I can't seem to get it to work how I'd like, but thanks for the help. I've just decided it would be better anyway to have my ghost props collide with each other so that way I can detect if one is penetrating the other.
-snip-
I think this would work:
[url]http://wiki.garrysmod.com/page/util/TraceEntity[/url]
the filter should be a table containing all entities except the one you are tracing and the invisible one.
The hitpos is not null as soon as a tiny bit of the invisible prop is inside the entity you are tracing.
[QUOTE=syl0r;41850822]I think this would work:
[url]http://wiki.garrysmod.com/page/util/TraceEntity[/url]
the filter should be a table containing all entities except the one you are tracing and the invisible one.
The hitpos is not null as soon as a tiny bit of the invisible prop is inside the entity you are tracing.[/QUOTE]
Wow, yeah this works great. The example is all I needed to see. :dance:
[editline]15th August 2013[/editline]
Sorry to mark unsolved again, but this actually doesn't work so great...
[lua]
local center = self:LocalToWorld(self:OBBCenter())
local trace = {start = center, endpos = center, filter = self}
local tr = util.TraceEntity(trace, self)
if tr.Hit then
self:SetColor(Color(255,0,0))
else
self:SetColor(Color(255,255,255))
end
[/lua]
Apparently if the entity is sitting on the ground the trace will sometimes hit the world. This is a problem because then tr.Hit will become true, even though what it's hitting is not a prop and is the world. And I can't just simply use tr.Hit and tr.HitNonWorld, because the world seems to have priority when it comes to traces and I wouldn't be able to tell if it's inside another prop and touching the world at the same time. I tried to find a way to filter/mask the world out of the trace, but I'm pretty sure that's impossible.
Sorry, you need to Log In to post a reply to this thread.