gpt4 book ai didi

lua - Corona SDK 自定义物理体

转载 作者:行者123 更新时间:2023-12-04 05:50:09 25 4
gpt4 key购买 nike

我对电晕中的自定义形状有一些麻烦。

这是我的代码,它的作用是我在场景中添加了一些球体,以便它们落入篮子内,这个篮子是我在 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)

最佳答案

根据manual :

If a shape is specified, then the body boundaries will follow the polygon provided by the shape. Note that the maximum number of sides per shape is eight, and all angles must be convex. [...] The shape coordinates must be defined in clockwise order, and the resulting shape must be convex-only.



所以你有两个问题对你不利。首先,必须按顺时针顺序定义形状,就像我在下面的示例中所做的那样。其次,所有的形状都必须是凸面的(即使是复杂的体型),所以你不能做任何像新月或篮子一样自行转动的东西。

不幸的是,这意味着您必须将篮子做成 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()

关于lua - Corona SDK 自定义物理体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10166437/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com