corona lua代码 - 为什么这个对象不旋转? (即质心相关测试代码)

时间:2012-08-21 03:26:19

标签: lua corona

有人可以解释为什么矩形不会在这里旋转吗?这是Lua代码,我正在使用Corona SDK。

也就是说,我试图设置一个矩形,它的质心偏离中心,然后在中间施加一个力,期望它旋转,因为它的质量中心是关闭的....

display.setStatusBar( display.HiddenStatusBar )

-- Setup
local screenCenterX, screenCenterY =  display.contentWidth/2, display.contentHeight/2

-- Create Object
local myRect = display.newRect(0, 0, 30, 90)
myRect.strokeWidth = 2
myRect:setFillColor(140, 140, 140, 0)
myRect:setStrokeColor(180, 180, 180)
myRect:setReferencePoint(display.CenterReferencePoint)
myRect.x, myRect.y = screenCenterX, screenCenterY

-- Apply Physics
local physics = require("physics")
physics.start()
physics.setGravity(0,0)
physics.addBody( myRect, "kinimatic", { friction=0.5, bounce=0.1, radius = 45 } )

-- Redefine Centre of Mass (what I'm trying to get right)
myRect.xOrigin, myRect.yOrigin = screenCenterX, screenCenterY - 100

-- Replace myRect to the center as setting the xOrigin/yOrigin seems to have moved it
myRect.x, myRect.y = screenCenterX, screenCenterY

-- Apply Force
timer.performWithDelay(3000,
        function(event)
                myRect:applyForce(5,0,  screenCenterX, screenCenterY)
                -- WHY DOES THIS NOT SPIN THE OBJECT???
                -- Centre of gravity has been change so shouldn't it rotate now?   
                -- That is, trying to simulate applying a force to an object who's centre of mass is NOT in 
                -- the center, and then see it spin.
        end
)

1 个答案:

答案 0 :(得分:1)

变量xOrigin和yOrigin仅用于移动对象,这些变量根本不会影响物理,它们实际上不是“原点”(它们是父对象中对象的x / y位置) x / yOrigin“变量”。

要做一个不平衡的物理对象,你需要用形状做,所以你必须创建两个形状(因此你不能使用自动形状,你将不得不使用多边形),其中一个比另一个是模拟偏心质量。

例如,一个长度为100个单位的矩形,你制作50个单位的权重1,另外50个单位的权重为2,结果将是x或多或少的中心(我的数学可能在这里错了)。