2016-01-16 84 views
-1

我有一個比編程問題更數學的問題,對不起,如果我不在正確的部分。在我的2D遊戲中,我們可以將相機移動到可以發出聲音的對象的地圖上,並且屏幕中心靠近此對象時,此音量(由0到1的浮點數定義)必須增加。例如,當物體位於屏幕中央時,音量爲1,當我們移開時,音量必須減小。每個對象都有自己的作用域值。 (例如1000個像素)。用距離計算聲音的值

我不知道如何編寫一個可以計算它的方法。 下面是一些我的代碼(這是不正確的計算):

private function setVolumeWithDistance():Void 
{ 
    sound.volume = getDistanceFromScreenCenter()/range; 
    // So the volume is a 0 to 1 float, the range is the scope in pixels and 
    // and the getDistanceFromScreenCenter() is the distance in pixels 
} 

我已經有其計算從中心屏幕中的對象的距離的方法:

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)); 

回答

1

簡單聲學能幫幫我。

Here是從點源聲強的公式。它遵循距離規則的平方反比。將其構建到您的代碼中。

您需要考慮全局座標和屏幕座標之間的映射。您必須將屏幕上的像素位置映射到物理座標並返回。

您的距離代碼有缺陷。沒有人應該使用方形數字pow()。你的問題容易發生錯誤。

此代碼結合距離計算,正確完成,並嘗試解決平方反比計算。注意:反正方形對於零距離是單數的。

package physics; 

/** 
* Simple model for an acoustic point source 
* Created by 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(); 
     for (int i = 0; i < 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 be positive"); 
     if (referenceIntensity <= 0.0) throw new IllegalArgumentException("intensity must be 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 ZERO = 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); 
    } 
} 
+0

感謝您的方法!對於公式,我的問題是我不需要dB,但是一個介於0和1之間的浮點數,當物體位於屏幕中心時爲最大值,物體範圍減小到0這個範圍。 – Karz