math - Calculate sound value with distance -


i have more mathematical programming question, sorry if i'm not in right section. in 2d game, can move camera on map there objects can emit sound, , sound volume (defined float 0 1) must increase when screen center near object. example, when object @ screen center, sound volume 1, , when move away, volume must decrease. each object has own scope value. (for example 1000 pixels).

i don't know how write method can calculate it. here of code (which not right calculation) :

private function setvolumewithdistance():void {     sound.volume = getdistancefromscreencenter() / range;     // volume 0 1 float, range scope in pixels ,      // , getdistancefromscreencenter() distance in pixels }   

i have method calculates distance of object center screen :

public function getdistancefromscreencenter():float {     return math.sqrt(math.pow((cameraman.getinstance().getfocusposition().x - position.x), 2) +                      math.pow((cameraman.getinstance().getfocusposition().y - position.y), 2)); 

simple acoustics can help.

here formula sound intensity point source. follows inverse square of distance rule. build code.

you need consider mapping between global , screen coordinates. have map pixel location on screen physical coordinates , back.

your distance code flawed. no 1 should use pow() square numbers. yours susceptible round off errors.

this code combines distance calculation, done properly, , attempts solve inverse square intensity calculation. note: inverse square singular 0 distance.

package physics;  /**  * simple model acoustic point source  * created michael  * creation date 1/16/2016.  * @link https://stackoverflow.com/questions/34827629/calculate-sound-value-with-distance/34828300?noredirect=1#comment57399595_34828300  */ public class acousticpointsource {      // units matter here....     private static final double default_reference_intensity = 0.01;     private static final double default_reference_distance = 1.0;      // units matter here...     private double referencedistance;     private double referenceintensity;      public static void main(string[] args) {         int numpoints = 20;         double x = 0.0;         double dx = 0.05;         acousticpointsource source = new acousticpointsource();         (int = 0; < numpoints; ++i) {             x += dx;             point p = new point(x);             system.out.println(string.format("point %s intensity %-10.6f", p, source.intensity(p)));         }     }      public acousticpointsource() {         this(default_reference_distance, default_reference_intensity);     }      public acousticpointsource(double referencedistance, double referenceintensity) {         if (referencedistance <= 0.0) throw new illegalargumentexception("distance must positive");         if (referenceintensity <= 0.0) throw new illegalargumentexception("intensity must positive");         this.referencedistance = referencedistance;         this.referenceintensity = referenceintensity;     }      public double distance2d(point p1) {         return distance2d(p1, point.zero);     }      public double distance2d(point p1, point p2) {         double distance = 0.0;         if ((p1 != null) && (p2 != null)) {             double dx = math.abs(p1.x - p2.x);             double dy = math.abs(p1.y - p2.y);             double ratio;             if (dx > dy) {                 ratio = dy/dx;                 distance = dx;             } else {                 ratio = dx/dy;                 distance = dy;             }             distance *= math.sqrt(1.0 + ratio*ratio);             if (double.isnan(distance)) {                 distance = 0.0;             }         }         return distance;     }      public double intensity(point p) {         double intensity = 0.0;         if (p != null) {             double distance = distance2d(p);             if (distance != 0.0) {                 double ratio = this.referencedistance/distance;                 intensity = this.referenceintensity*ratio*ratio;             }         }         return intensity;     } }  class point {      public static final point 0 = new point(0.0, 0.0, 0.0);      public final double x;     public final double y;     public final double z;      public point(double x) {         this(x, 0.0, 0.0);     }      public point(double x, double y) {         this(x, y, 0.0);     }     public point(double x, double y, double z) {         this.x = x;         this.y = y;         this.z = z;     }      @override     public string tostring() {         return string.format("(%-10.4f,%-10.4f,%-10.4f)", x, y, z);     } } 

Comments

Popular posts from this blog

c++ - llvm function pass ReplaceInstWithInst malloc -

java.lang.NoClassDefFoundError When Creating New Android Project -

Decoding a Python 2 `tempfile` with python-future -