2012-04-15 35 views
2

我對corona中的自定義形狀有些麻煩。Corona SDK定製物理機構

這裏是我的代碼,它的功能是將一些球體添加到場景中,以便它們落入籃子內,這個籃子是我在newBasket()函數中定義的自定義形狀對象,問題是籃子確實與地面物體發生碰撞,但它不會與球體發生碰撞,我不知道爲什麼,請別人幫助我,我在其他地方找不到解決方案,在此先感謝。

_W = display.contentWidth 
_H = display.contentHeight 

--Physics 
local physics = require("physics") 
physics.start() 
physics.setDrawMode("debug") 

-- iOS 
display.setStatusBar(display.HiddenStatusBar) 

-- screen boundaries 
local ground = display.newRect(0, _H, _W, 5) 
local leftWall = display.newRect(0,0,1,_H) 
local rightWall = display.newRect(_W,0,1,_H) 
physics.addBody(ground, "static", {friction = .2, bounce = .1}) 
physics.addBody(leftWall, "static", {friction = .2, bounce = .1}) 
physics.addBody(rightWall, "static", {friction = .2, bounce = .1}) 

local function newBasket() 
    local body = display.newImage("assets/basket.png") 
    body.x = 0 body.y = 0 

    local physics_body = {} 
    physics_body["basket"] = { 
     { 
      --LeftArm 
      density = 10, friction = 10, bounce = 0.15, 
      shape = {-126, 40, -110, 40, -140, -64, -156, -64} 

     }, 

     { 
      --Bottom 
      density = 10, friction = 1, bounce = 0, 
      shape = {-121, 60, 125, 60, 128, 40, -126, 40} 

     }, 
     { 
      --RightArm 
      density = 10, friction = 10, bounce = 0.15, 
      shape = {113, 40, 129, 40, 158, -64, 143, -64} 

     } 

    } 
    physics.addBody(body, unpack(physics_body["basket"])) 

    return body 
end 

local basket = newBasket() 
basket.x = _W * .5 basket.y = _H - 100 

local function newPlanet() 

    local planets = { 
     {img = "bigBall.png", radius = 45}, 
     {img = "mediumBall.png", radius = 30}, 
     {img = "smallBall.png", radius = 20} 
    } 

    local n = math.random(1,3) 

    local img = "assets/" .. planets[n].img 
    local ball = display.newImage(img) 
    ball.x = math.random((_W * 0.5) -100, (_W * 0.5) + 100) ball.y = 0 

    physics.addBody(ball, {bounce = 0.3, radius = planets[n].radius}) 
end 

local function spawnPlanets(number) 

    local function spawn(e) 
     newPlanet() 

     if(e.count == number) then 
      timer.cancel(tmr) 
      tmr = nil 
     end 
    end 

    tmr = timer.performWithDelay(500, spawn, number) 
end 

spawnPlanets(20) 

回答

3

按照manual

如果指定形狀,然後身體邊界將遵循由形狀所提供的多邊形。請注意,每個形狀的最大邊數爲8,所有角度必須爲凸形。 [...]形狀座標必須按順時針順序定義,並且生成的形狀必須僅爲凸形。

所以你有兩個問題對你有用。首先,形狀必須按順時針順序定義,正如我在下面的示例中所做的那樣。其次,所有的形狀必須是凸面的(甚至是複雜的身體形狀),所以你不能做任何能夠像月牙或籃子那樣進入自身的東西。

不幸的是,這意味着你必須讓你的籃子成爲3個獨立的形狀。而且,由於它們現在是獨立的形狀,它們在墜落時不會停留在一起(除非使用joints)。我剛纔提出的籃3靜態物體,並把它在正確的地方入手:

local function newBasket() 
    local physics_body = {} 
    physics_body["basket"] = { 
     { 
      --LeftArm 
      density = 10, friction = 10, bounce = 0.15, 
      shape = {-126, 40, -156, -64, -140, -64, -110, 40 } 

     }, 

     { 
      --Bottom 
      density = 10, friction = 1, bounce = 0, 
      shape = {-121, 60,-126, 40, 128, 40, 125, 60 } 

     }, 
     { 
      --RightArm 
      density = 10, friction = 10, bounce = 0.15, 
      shape = {113, 40, 143, -64, 158, -64, 129, 40 } 

     } 

    } 
    for k,shape in pairs(physics_body["basket"]) do 
     local body = display.newRect(0, 0, 200, 100) 
     body.x = display.contentCenterX 
     body.y = display.contentHeight - 60 

     physics.addBody(body, 'static', shape) 
    end 
end 

newBasket() 
+0

你先生是真棒!是的,我在下面講複雜的物理對象的舊的輔導和小夥子定義形狀在counterClokwise順序中,我閱讀文檔,但不明白爲什麼這個人工作正常,我甚至嘗試使用順時針順序的形狀,但沒有工作......我仍然需要非常瞭解發生在那個「對於「你在那裏做的句子,你宣佈了一個newRect?...嗯,並且contentCenter和Height是我猜想的物理體的本地中心,那麼爲什麼你要聲明一個newRect? – ershin69 2012-04-16 18:48:04

+0

我使用了矩形,因爲我沒有圖像,而在Corona中,您的物理機構必須附加某種顯示對象。您可以將newRect替換爲您選擇的圖像。 display.contentCenterX是屏幕的中心。 – six8 2012-04-16 20:49:38