How can I Calculate the "impact force" or "deceleration" of an entity when it touches another entity
2 replies, posted
Ironically, I answered my own question while typing this post, but I thought it would still be helpful for people who wondered the same thing.
I'm making an entity, and this entity will play a sound whenever it makes a significant impact with another solid object.
I started by doing
[CODE]self:EmitSound("sdfsdfzsdfg.wav")[/CODE]
right inside the collision hook, but what happens is. The ball will constantly play that sound every time it touches something new. So it will play the same impact sound while slowing rolling across terrain, which I don't want.
So next I wrapped it in an if statement and made it so the sound only played when the velocity was over a certain amount
[CODE]
if collisionData.Speed > 1000 then
self:EmitSound("sdfsdfzsdfg.wav")
end
[/CODE]
This kinda works, but sometimes it doesn't play a sound when it should, and if you try to tweak the condition so it plays the sound at a lower velocity, then it will play the sound when it shouldn't, like when rolling.
To take it another step forward, I knew that when objects make impact, their acceleration becomes a very 'large' negative number. Meaning that the ball is slowing down very quickly. To get this number, we have to find the change in velocity over a duration of time, which means I would have had to set a Think hook, or a PhysicsSimulate hook, and I would have to make a special variable to hold the previous velocity, which in my opinion is messy and unorganized.
Then I realized, that the Bouncy Ball entity has exactly what I'm looking for. So I look at the source code, and low and behold, you can simply do this.
[CODE]
// FROM sent_ball.lua
if ( data.Speed > 60 && data.DeltaTime > 0.2 ) then
local pitch = 32 + 128 - math.Clamp( self:GetBallSize(), self.MinSize, self.MaxSize )
sound.Play( BounceSound, self:GetPos(), 75, math.random( pitch - 10, pitch + 10 ), math.Clamp( data.Speed / 150, 0, 1 ) )
end
[/CODE]
So there you have it!!!
[QUOTE=Andrew900460;52247675]Ironically, I answered my own question while typing this post, but I thought it would still be helpful for people who wondered the same thing.
I'm making an entity, and this entity will play a sound whenever it makes a significant impact with another solid object.
I started by doing
[CODE]self:EmitSound("sdfsdfzsdfg.wav")[/CODE]
right inside the collision hook, but what happens is. The ball will constantly play that sound every time it touches something new. So it will play the same impact sound while slowing rolling across terrain, which I don't want.
So next I wrapped it in an if statement and made it so the sound only played when the velocity was over a certain amount
[CODE]
if collisionData.Speed > 1000 then
self:EmitSound("sdfsdfzsdfg.wav")
end
[/CODE]
This kinda works, but sometimes it doesn't play a sound when it should, and if you try to tweak the condition so it plays the sound at a lower velocity, then it will play the sound when it shouldn't, like when rolling.
To take it another step forward, I knew that when objects make impact, their acceleration becomes a very 'large' negative number. Meaning that the ball is slowing down very quickly. To get this number, we have to find the change in velocity over a duration of time, which means I would have had to set a Think hook, or a PhysicsSimulate hook, and I would have to make a special variable to hold the previous velocity, which in my opinion is messy and unorganized.
Then I realized, that the Bouncy Ball entity has exactly what I'm looking for. So I look at the source code, and low and behold, you can simply do this.
[CODE]
// FROM sent_ball.lua
if ( data.Speed > 60 && data.DeltaTime > 0.2 ) then
local pitch = 32 + 128 - math.Clamp( self:GetBallSize(), self.MinSize, self.MaxSize )
sound.Play( BounceSound, self:GetPos(), 75, math.random( pitch - 10, pitch + 10 ), math.Clamp( data.Speed / 150, 0, 1 ) )
end
[/CODE]
So there you have it!!![/QUOTE]
But do you understand how it works otherwise it's pretty much of a waste.
I just looked into it, turns out that what's used in the sent_ball entity isn't finding the acceleration exactly, but instead deciding to play the sound if the thing it's colliding with hasn't already collided in the past 0.2 seconds, and is moving at least 60 units per second.
Thanks for pointing this out, I woudn't have realised that if I didn't look into it, I just assumed it was finding the acceleration because there was a velocity and a time
a = (delta)v / t
Sorry, you need to Log In to post a reply to this thread.