Corona/Lua 中的简单物理对象
在 Corona 论坛上没有任何运气,所以我想我应该在这里尝试一下。
我只是想创建一个带有枢轴关节的对象。看起来很简单,但就是行不通。
如果这些物体是在单独的文件(类)中创建的对象的一部分,我只是不知道如何将它们添加到物理系统中。希望有人能提供帮助 - 已经为此奋斗了数周。
这是我的代码:
main.lua
:
local JointedObj = require("JointedObj")
local physics = require("physics")
physics.start()
local o = JointedObj.new()
o.x = 50
o.y = 200
local wall = display.newRect( 350, 10, 50, 300 )
physics.addBody ( wall, "static", {density=1, friction=1, bounce=.5})
local floor = display.newRect( 0, 300, 400, 10 )
physics.addBody ( floor, "static", {density=1, friction=1, bounce=.5})
--toss the object against the wall
o:toss(120, -160, o.x, o.y)
JointedObj.lua
:
module(..., package.seeall)
--constructor
function new()
local obj = display.newGroup()
local red = display.newImageRect( "images/red.png", 27, 18 )
local blue = display.newImageRect( "images/blue.png", 11, 9 )
blue.x = -16
obj:insert(red)
obj:insert(blue)
function obj:toss(xForce, yForce, xPos, yPos )
--THIS WORKS, BUT OBVIOUSLY THE OBJECT HAS NO JOINTS
--physics.addBody( obj, "dynamic", {density=1, friction=1, bounce=0.3} )
--obj:applyForce( xForce, yForce, xPos, yPos )
--THIS IS WHAT I WANT TO DO. AS-IS, THE OBJECT JUST FALLS THROUGH EVERYTHING
physics.addBody( red, {density=1, friction=1, bounce=0.3} )
physics.addBody( blue, {density=1, friction=1, bounce=0.3} )
myJoint = physics.newJoint( "pivot", red, blue, 0,0 )
myJoint.isLimitEnabled = true
myJoint:setRotationLimits( -30, 30 )
--obj:applyForce( xForce, yForce, xPos, yPos ) --THIS THROWS A NIL ERROR IF UNCOMMENTED
end
return obj;
end
Not having any luck in the Corona forums, so I thought I'd try here.
I'm simply trying to create an object with a pivot joint. Seems simple but it's just not working.
I just can't figure out how to add bodies to the physics system if those bodies are part of an object that is created in a separate file (class). Hope someone can help - been struggling for weeks on this.
Here is my code:
main.lua
:
local JointedObj = require("JointedObj")
local physics = require("physics")
physics.start()
local o = JointedObj.new()
o.x = 50
o.y = 200
local wall = display.newRect( 350, 10, 50, 300 )
physics.addBody ( wall, "static", {density=1, friction=1, bounce=.5})
local floor = display.newRect( 0, 300, 400, 10 )
physics.addBody ( floor, "static", {density=1, friction=1, bounce=.5})
--toss the object against the wall
o:toss(120, -160, o.x, o.y)
JointedObj.lua
:
module(..., package.seeall)
--constructor
function new()
local obj = display.newGroup()
local red = display.newImageRect( "images/red.png", 27, 18 )
local blue = display.newImageRect( "images/blue.png", 11, 9 )
blue.x = -16
obj:insert(red)
obj:insert(blue)
function obj:toss(xForce, yForce, xPos, yPos )
--THIS WORKS, BUT OBVIOUSLY THE OBJECT HAS NO JOINTS
--physics.addBody( obj, "dynamic", {density=1, friction=1, bounce=0.3} )
--obj:applyForce( xForce, yForce, xPos, yPos )
--THIS IS WHAT I WANT TO DO. AS-IS, THE OBJECT JUST FALLS THROUGH EVERYTHING
physics.addBody( red, {density=1, friction=1, bounce=0.3} )
physics.addBody( blue, {density=1, friction=1, bounce=0.3} )
myJoint = physics.newJoint( "pivot", red, blue, 0,0 )
myJoint.isLimitEnabled = true
myJoint:setRotationLimits( -30, 30 )
--obj:applyForce( xForce, yForce, xPos, yPos ) --THIS THROWS A NIL ERROR IF UNCOMMENTED
end
return obj;
end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
物理学在群体之间不起作用,只在群体内部起作用。这样做的目的是为了允许使用组来移动摄像机。请参阅 Egg Breaker 演示来了解我的意思。整个场景位于一个四处移动的组中,但组内的对象不会对组的移动做出反应。
顺便说一句,最后一行抛出错误的原因是你只能在物理体上使用 applyForce 并且你没有在“obj”上设置物理体,只能在“红色”和“蓝色”上设置物理体。
Physics don't work between groups, only within a group. That's done on purpose to allow for moving cameras done using groups. Refer to the Egg Breaker demo to see what I mean. The entire scene is in a group that moves around, but objects within the group don't react to the group moving.
Incidentally, the reason that last line throws an error is because you can only use applyForce on a physics body and you haven't set a physics body on "obj", only on "red" and "blue".