簡體   English   中英

Corona / Lua中的簡單物理對象

[英]Simple physics objects in 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

物理在小組之間不起作用,僅在小組內起作用。 這樣做是為了允許使用組移動攝像機。 請參閱Egg Breaker演示以了解我的意思。 整個場景位於一個可移動的組中,但是組內的對象對組的移動沒有​​反應。

順便說一句,最后一行引發錯誤的原因是,您只能在物理實體上使用applyForce,而沒有在“ obj”上設置物理實體,而僅在“ red”和“ blue”上設置了物理實體。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM