2012-02-02 9 views
1

私たちは物理エンジンを必ずしも利用する必要はないオブジェクトを使用していますが、依然として衝突を検出する必要があります。Corona SDK - 非物理的な物体の衝突を検出する方法は?

ソリティアでは、カードはドラッグ可能なオブジェクトです。それらが別のカードのスタック(衝突)の上にリリースされると、それらはそのデッキに「つい」ます。ターゲットホットスポット(カードスタック)は、常に前もって知られているとは限らず、動的です。

コロナSDKでこの問題を解決するにはどうすればよいでしょうか?あなたのカードを動かす機能で

答えて

4

あなたは物理エンジンを使用したくない場合は、コードができ重複しているカードを確認するためにタッチイベントリスナーを起動します。私はカードの積み重ねや単一のカードに違いはないと仮定します。

local cards={} --a list of display objects 
cards[1]=display.newRect(100,100,100,100) 
cards[2]=display.newRect(100,210,100,100) 
cards[3]=display.newRect(100,320,100,100) 

local tolerance=20 --20px radius 
local cardAtPointer=0 --the index of the card stuck to user hand 

local function onOverlap(self,event) 
    if event.phase == "began" then 
     cardAtPointer=self.index --hold one card only  
    elseif event.phase == "moved" then 
     if cardAtPointer > 0 and self.index == cardAtPointer then 
      self.x,self.y = event.x,event.y  --drag card around 
     end 
    elseif event.phase == "ended" or event.phase == "cancelled" then 
     local s=self 

     for i=1,#cards do 
      local t=cards[i] 
      if s.index ~= t.index then --dont compare to self 
       if math.pow(tolerance,2) >= math.pow(t.x-s.x,2)+math.pow(t.y-s.y,2) then 
        print(s.index.." overlap with "..t.index) 
        break --compare only 2 overlapping cards, not 3,4,5... 
       end 
      end 
     end 
     cardAtPointer=0  --not holding any cards 
    end 
end 

for i=1,#cards do 
    cards[i].index=i 
    cards[i].touch=onOverlap 
    cards[i]:addEventListener("touch",cards[i]) 
end 
+0

このコードはこれまでのところ素晴らしいものです。 「動いた」段階のx/yの動きはちょっとばっちりですが、ここから管理できると思います。これにより、非物理的な身体問題の衝突検出が解決されました。ありがとう! – Alex

1

CGRectIntersect (card, cardStack) 

を使用して基礎となるカードのスタックを交差点のチェックを追加して、イベントを発生。 (カードが長方形であると仮定して)。

私もちょうどコロナを使用し始めているが、役に立つかもしれませんあなたの質問のトピックに関するこのスレッドが見つかりました:

http://developer.anscamobile.com/forum/2010/10/29/collision-detection-without-physics

+0

CGRectIntersectはObjective C関数ではありませんか?しかし、あなたのリンクに役立つコードがたくさんあるようです...私はそれを試してみましょう! – Alex

関連する問題