2012-12-02 123 views
0

我在舞臺中央放置了一個大球「centerBall」。然後我在 中添加了一些較小的,給它們隨機的大小和速度。這些將隨基本運動代碼一起移動並從牆壁反彈。在每個框架上,在每個移動的球和中心球之間進行基於距離的碰撞檢查。如果碰撞,我已經根據兩個球之間的角度和最小距離計算了一個偏置彈簧目標。 還有一個問題:一些較小的球繞過「中心球」的邊界,然後反彈。你可以在附圖中看到。爲什麼發生這種情況? 下面是代碼:基於碰撞的彈跳

import flash.display.Sprite; 
import flash.events.Event; 

public class Bubbles extends Sprite 
{ 
    private var balls:Array; 
    private var numBalls:Number = 10; 
    private var centerBall:Ball; 
    private var bounce:Number = -1; 
    private var spring:Number = 0.2; 

    public function Bubbles() 
    { 
     init(); 
    } 

    private function init():void 
    { 
     balls = new Array(); 
     centerBall = new Ball(100, 0xcccccc); 
     addChild(centerBall); 
     centerBall.x = stage.stageWidth/2; 
     centerBall.y = stage.stageHeight/2; 

     for(var i:uint = 0; i < numBalls; i++) 
     { 
      var ball:Ball = new Ball(Math.random() * 40 + 5, Math.random() * 0xffffff); 
      ball.x = Math.random() * stage.stageWidth; 
      ball.y = Math.random() * stage.stageHeight; 
      ball.vx = Math.random() * 6 - 3; 
      ball.vy = Math.random() * 6 - 3; 
      addChild(ball); 
      balls.push(ball); 
     } 

     addEventListener(Event.ENTER_FRAME, onEnterFrame);   
    } 

    private function onEnterFrame(event:Event):void 
    { 
     for(var i:uint = 0; i < numBalls; i++) 
     { 
      var ball:Ball = balls[i]; 
      move(ball); 
      var dx:Number = ball.x - centerBall.x; 
      var dy:Number = ball.y - centerBall.y; 
      var dist:Number = Math.sqrt(dx * dx + dy * dy); 
      var minDist:Number = ball.radius + centerBall.radius; 
      if(dist < minDist) 
      { 
       var angle:Number = Math.atan2(dy, dx); 
       var targetX:Number = centerBall.x + Math.cos(angle) * minDist; 
       var targetY:Number = centerBall.y + Math.sin(angle) * minDist; 
       ball.vx += (targetX - ball.x) * spring; 
       ball.vy += (targetY - ball.y) * spring; 
      } 
     } 
    } 

    private function move(ball:Ball):void 
    { 
     ball.x += ball.vx; 
     ball.y += ball.vy; 
     if(ball.x + ball.radius > stage.stageWidth) 
     { 
      ball.x = stage.stageWidth - ball.radius; 
      ball.vx *= bounce; 
     } 
     else if(ball.x - ball.radius < 0) 
     { 
      ball.x = ball.radius; 
      ball.vx *= bounce; 
     } 
     if(ball.y + ball.radius > stage.stageHeight) 
     { 
      ball.y = stage.stageHeight - ball.radius; 
      ball.vy *= bounce; 
     } 
     else if(ball.y - ball.radius < 0) 
     { 
      ball.y = ball.radius; 
      ball.vy *= bounce; 
     } 
    } 
} 

Click here to see the pic

回答

0

你的問題是,你正在做基於這些幀,而不是位置的碰撞檢測。

您需要檢查它現在在哪裏以及它是上一幀的位置,以便您可以跟蹤它的移動。這就是爲什麼它通過你的中心球,因爲你檢查當前幀是否有碰撞。

這是一個指向基於時間的圓圈碰撞檢測的鏈接。

Timed based collision

希望這有助於; )

+0

是的,你說得對。我做了一些研究 – LuciM

+0

是的,你說得對。問題是我在當前幀中進行碰撞測試,而不是之前的測試。屏幕更新,我看到進入大球的小球。我已經做了一些研究,並提出了一個簡單的公式:Verlet集成不計算對象的當前位置。相反,它是根據與當前幀和最後一幀的差異來計算對象的速度。關鍵是測試屏幕之間的碰撞檢測更新位置。 – LuciM

+0

很高興幫助:) –