Corona SDK自定义物理体
2012-5-13 11:47:23
收藏:0
阅读:125
评论:1
我在 corona 中使用自定义形状遇到了一些问题。
我的代码是将一些球添加到场景中,使它们掉进一个篮子中。这个篮子是我在 newBasket() 函数中定义的自定义形状对象。问题是篮子与地面对象发生碰撞,但它与球不发生碰撞,我不知道为什么,请有经验的人帮帮我,我无法在其他地方找到解决方案,先谢谢了。
_W = display.contentWidth
_H = display.contentHeight
-- 物理引擎
local physics = require("physics")
physics.start()
physics.setDrawMode("debug")
-- iOS
display.setStatusBar(display.HiddenStatusBar)
-- 屏幕边界
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"] = {
{
-- 左臂
density = 10, friction = 10, bounce = 0.15,
shape = {-126, 40, -110, 40, -140, -64, -156, -64}
},
{
-- 底部
density = 10, friction = 1, bounce = 0,
shape = {-121, 60, 125, 60, 128, 40, -126, 40}
},
{
-- 右臂
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)
原文链接 https://stackoverflow.com/questions/10166437
点赞
评论区的留言会收到邮件通知哦~
推荐文章
- 如何在roblox studio中1:1导入真实世界的地形?
- 求解,lua_resume的第二次调用继续执行协程问题。
- 【上海普陀区】内向猫网络招募【Skynet游戏框架Lua后端程序员】
- SF爱好求教:如何用lua实现游戏内调用数据库函数实现账号密码注册?
- Lua实现网站后台开发
- LUA错误显式返回,社区常见的规约是怎么样的
- lua5.3下载库失败
- 请问如何实现文本框内容和某个网页搜索框内容连接,并把网页输出来的结果反馈到另外一个文本框上
- lua lanes多线程使用
- 一个kv数据库
- openresty 有没有比较轻量的 docker 镜像
- 想问一下,有大佬用过luacurl吗
- 在Lua执行过程中使用Load函数出现问题
- 为什么 neovim 里没有显示一些特殊字符?
- Lua比较两个表的值(不考虑键的顺序)
- 有个lua简单的项目,外包,有意者加微信 liuheng600456详谈,最好在成都
- 如何在 Visual Studio 2022 中运行 Lua 代码?
- addEventListener 返回 nil Lua
- Lua中获取用户配置主目录的跨平台方法
- 如何编写 Lua 模式将字符串(嵌套数组)转换为真正的数组?
根据手册:
因此,你有两个问题。首先,形状必须按照顺时针的顺序定义,就像下面的示例一样。其次,所有形状必须是凸的(即使是复杂的物体形状),因此您不能做任何像新月形或篮子那样回旋的操作。
不幸的是,这意味着您必须将篮子分成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()