2014-09-03 70 views
2

我有一個屏幕(480x800),M(mx,my)是一個靜態點,N(nx,ny)是屏幕上的一個動態點。 N(nx,ny)的位置取決於觸摸的位置。我想確定P(?,?)和Q(?,?)的位置以繪製行1行2第2行反光第1行如何確定在AndEngine屏幕上畫線的位置

enter image description here

這是我的代碼:

private Line l2; 

    @Override 
    public boolean onSceneTouchEvent(final Scene pScene, 
      final TouchEvent pSceneTouchEvent) { 
     if (this.mPhysicsWorld != null) { 
      switch (pSceneTouchEvent.getAction()) { 
      case TouchEvent.ACTION_DOWN: 
       // Get position 
       p1x = pSceneTouchEvent.getX(); 
       p1y = pSceneTouchEvent.getY(); 
       return true; 

      case TouchEvent.ACTION_MOVE: 
       // Remove instance of the old line 
       mScene.detachChild(l2); 

       p3x = pSceneTouchEvent.getX(); 
       p3y = pSceneTouchEvent.getY(); 

       Rectangle testR = new Rectangle(CAMERA_WIDTH/2, 
         CAMERA_HEIGHT/2, 20, 20, 
         getVertexBufferObjectManager()); 

       l2 = new Line(CAMERA_WIDTH/2, CAMERA_HEIGHT/2, p3x, p3y, 
         getVertexBufferObjectManager()); 
       l2.setColor(new Color(223f/255f, 118f/255f, 43f/255f)); 
       l2.setLineWidth(5); 
       mScene.attachChild(l2); 
       return true; 
      } 
      return false; 
     } 
     return false; 
    } 

如果您有其他的方式來解決我的問題。請與我分享。謝謝。

回答

1

我沒有用AndEngine編程一段時間,但這不是關於AndEngine的,所以我可以給你一個psedu代碼來解決你的問題。

1)檢查NX < MX

2)計算線1的斜率: line1slope =(MY-NY)/(MX-NX)

3)求P座標使用等式:Y-Y1 = M(X-X1)

,其中M = line1slope

Y1 =我

X1 = MX

Y = 480(P的y座標)(如果line1slope> 0,則Y = 0)

然後你可以找到你的X(其爲P的x座標)

line2slope = -1 * line1slope因爲他們是反射

現在再次,你需要找到Q(你知道= 0,所以你需要找到只有Y座標的X) 使用方程:Y-Y1 = M(X-X1)

其中m = line2slope

Y1 = PY

X1 = PX

X = 0

然後你可以找到你的y座標(這是Q的y座標)

希望它能幫助。