Why does Garry do this in the Bouncy Ball Sent?


function ENT:PhysicsCollide( data, physobj )    
    // Play sound on bounce
    if (data.Speed > 80 && data.DeltaTime > 0.2 ) then
        self.Entity: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


This is an excerpt from sent_ball, and controls the “bounce” of the ball. The particular lines in question are these:

local LastSpeed = math.max( data.OurOldVelocity:Length(), data.Speed )
Why does Garry look for the larger of two values? Why not just stick to data.Speed?

Speed appears to be the force of which the two entities collide - it seems fine by itself.

LastSpeed = math.max( NewVelocity:Length(), LastSpeed )
Now it gets even more confusing. Garry is looking for the largest value of physobj:GetVelocity(), data.Speed, and data.OurOldVelocity.Length().

Why?

The answer is in the question.

This is why.

Can you explain why Garry needs to sort between 3 different values to set the speed?

From looking at the CollisionData structure, i’d have to guess it has something to do with intensifying its bounce.

Comparing its velocity before the collision to its speed of impact, and its current velocity compared to the larger of those.

Hes trying to do his math with the largest numbers he can get.

To be honest, i don’t know. Or maybe its that i don’t feel like finding out.

I don’t think Garry ever updates the bouncy ball, so I think it’s there because at that time he didn’t have one function for all that stuff.