1. Post #1
    Gold Member
    superadamwo's Avatar
    January 2006
    650 Posts
    two*
    I've been trying to figure this out for ages now, and had to stop working on my gamemode because I couldn't find a way to do it. Is there a hook or some other method I can use to detect collisions between two objects, namely two vehicles? I also need it to report what the two entities were that collided.

  2. Post #2
    We Are No Idiots
    Aide's Avatar
    March 2010
    4,661 Posts
    Reply With Quote Edit / Delete Reply Windows 7 United States Show Events Disagree Disagree x 2 (list)

  3. Post #3
    Always the vGuy.
    vercas's Avatar
    September 2011
    841 Posts
    two*
    I've been trying to figure this out for ages now, and had to stop working on my gamemode because I couldn't find a way to do it. Is there a hook or some other method I can use to detect collisions between two objects, namely two vehicles? I also need it to report what the two entities were that collided.
    Look at the code of the sent_ball (Bouncy Ball).

  4. Post #4
    Gold Member
    superadamwo's Avatar
    January 2006
    650 Posts
    But I can't use SENT hooks to detect collisions between things that aren't SENTs can I?

  5. Post #5
    freemmaann's Avatar
    January 2009
    1,133 Posts
    I would also love to know this, I don't think there is a descent way tho. There's that ShouldCollide hook, but its not reliable.

  6. Post #6
    Gold Member
    superadamwo's Avatar
    January 2006
    650 Posts
    ShouldCollide doesn't work like that, I've tried it. It's called every tick basically, not right before things are colliding.

  7. Post #7
    Always the vGuy.
    vercas's Avatar
    September 2011
    841 Posts
    Entity.PhysicsCollide.

    You can use it on entities that you didn't make by overriding their PhysicsCollide method.

    -- myEnt is my entity.
    local oldPhysicsCollide = myEnt.PhysicsCollide
    function myEnt:PhysicsCollide(data, physobj)
        -- Do shiz.
        oldPhysicsCollide(self, data, physobj)
    end
    

    I told you to look a the bouncy ball's code:

    --[[---------------------------------------------------------
       Name: PhysicsCollide
    -----------------------------------------------------------]]
    function ENT:PhysicsCollide( data, physobj )
    	
    	-- Play sound on bounce
    	if (data.Speed > 80 && data.DeltaTime > 0.2 ) then
    		self:EmitSound( "Rubber.BulletImpact" )
    	end
    	
    	-- Bounce like a crazy bitch
    	local LastSpeed = math.max( data.OurOldVelocity:Length(), data.Speed )
    	local NewVelocity = physobj:GetVelocity()
    	NewVelocity:Normalize()
    	
    	LastSpeed = math.max( NewVelocity:Length(), LastSpeed )
    	
    	local TargetVelocity = NewVelocity * LastSpeed * 0.9
    	
    	physobj:SetVelocity( TargetVelocity )
    	
    end
    
    Reply With Quote Edit / Delete Reply Windows 7 Romania Show Events Disagree Disagree x 1 (list)

  8. Post #8
    Gold Member
    superadamwo's Avatar
    January 2006
    650 Posts
    Yeah I knew about that function but I was confused about the difference between a SENT in gmod and a regular entity. So you are saying that I can use this function with any entity even if it is not Lua based, like a jeep or a regular prop for example?

    I don't actually think you can, so what I was going to do was make an invisible SENT with a bounding box the size of the jeep's which would then have a PhysicsCollide hook in it. The other method I was considering was combining Entity.GetCollisionBounds with ents.FindInBox, but I don't know if that will work at all.

  9. Post #9
    S W
    S W's Avatar
    May 2010
    316 Posts
    I actually asked this exact same question awhile back with no luck:
    http://www.facepunch.com/threads/1059660

  10. Post #10
    Gold Member
    superadamwo's Avatar
    January 2006
    650 Posts
    Yeah I found that thread while searching for an answer. Do you know if FindInBox will find entities that are partially in the box, like if I had a jeep whose front was like 5 inches inside the box would it be counted?

    I've asked this question before as well, like 2 years ago, nobody knew the answer then either. I wish there were more people that browsed this forum looking to answer questions rather than ask them.