2016-04-25 38 views
0

我意識到這個問題已經以不同的方式回答了,而我目前的解決方案是基於以前的答案的,但它對我來說並不完全適用,我無法弄清楚爲什麼。讓子彈從頭開始遊戲到目的地

  float targetX = bullet.getTarget().x; 
      float targetY = bullet.getTarget().y; 
      float bulletX = bullet.getLocation().x; 
      float bulletY = bullet.getLocation().y; 

      // Version of setX(getX() + (speed * Math.cos(direction))); 
      float deltaX = targetX - bulletX; 
      float deltaY = targetY - bulletY; 
      float direction = (float) Math.atan2(deltaY, deltaX); 
      float x = bulletX + BULLET_SPEED * (float) Math.cos(direction); 
      float y = bulletY + BULLET_SPEED * (float) Math.sin(direction); 
      bullet.setLocation(x, y); 

這是效果在行動:

Animated Gif Of The Bullet Going The (Almost Right) Direction

有沒有用我的數學問題?

回答

0

它最終成爲畫布邊界偏移目標點的邊界,所以我對它進行了補償並獲得了接近準確行爲的東西。