# Volume depending on impact force (physics)

Just started using Construct 2? Post your questions here

### » Sat Apr 09, 2016 10:19 pm

I'm working on physical puzzle, so how do I make impact volume depend on the impact force?
B
11
S
2
Posts: 14
Reputation: 609

### » Sat Apr 09, 2016 11:55 pm

The impulse of a collision is basically the change of velocity from before and after a collision. Using that info you can calculate it. Then you can use lerp or something to map the impulse to a volume.
Here's one possible conversion:
lerp(-1000, 0, min(impulse/1000, 1))
which means when the impulse is 0 the volume will be -1000 dB, and when the impulse is 1000 or higher the volume will be 0 dB. Impulses in between will be between the two volumes. Tweak the numbers for different results.

global number prevVelocityX=0
global number prevVelocityY=0
global number impulse=0

on sprite collides with wall
--- set impulse to distance(0,0, sprite.physics.velocityX-prevVelocityX, sprite.physics.velocityY-prevVelocityY)
--- play sound at volume lerp(-1000, 0, min(impulse/1000, 1))

every tick
--- set prevVelocityX to sprite.physics.velocityX
--- set prevVelocityY to sprite.physics.velocityY
B
100
S
38
G
134
Posts: 5,555
Reputation: 85,323

### » Sun Apr 10, 2016 12:22 am

Oh, thank you, should've think about the impulse before. Not only velocity before impact was important, that's why it was working incorrect.
B
11
S
2
Posts: 14
Reputation: 609