1
我遇到的問題很簡單。我在屏幕中心有一個物體,然後是一個繞着該物體的圓圈。我想要做的是讓對象圍繞對象軌道運動,當使用觸摸屏幕時,它將在對象周圍繞行並停止面向用戶點擊屏幕的位置。Corona SDK物理與線,爲什麼線變成物理引擎中的矩形?
我有很大的難度試圖獲得這項工作。但我想我會做的是試圖從對象的中心畫一條線到使用點擊的位置,然後繞行星繞行的球擊中它將停止的線(足夠簡單)。我遇到的問題是線對象(當在混合模式下查看時)更像是一個矩形,然後它是一條線(在物理引擎中,所以這會混淆我的預期結果。是否有人知道我做錯了什麼?這裏?電暈或如何處理呢?以及如何可能解決呢?或者任何其他解決方案將工作相當好。
physics = require("physics") ; physics.start() ; physics.setGravity(0,0) ;
physics.setDrawMode("hybrid")
display.setStatusBar(display.HiddenStatusBar)
math.randomseed(os.time())
--set up some references and other variables
local ox, oy = math.abs(display.screenOriginX), math.abs(display.screenOriginY)
local cw, ch = display.contentWidth, display.contentHeight
--set up collision filters
local screenFilter = { categoryBits=2, maskBits=1 }
local objFilter = { categoryBits=1, maskBits=14 }
local fieldFilter = { categoryBits=4, maskBits=1 }
local magnetFilter = { categoryBits=8, maskBits=1 }
--set initial magnet pull
local magnetPull = 0.85
--set up world and background
local screenBounds = display.newRect(-ox, -oy, display.contentWidth+ox+ox, display.contentHeight+oy+oy)
screenBounds.name = "screenBounds"
screenBounds.isVisible = false ; physics.addBody(screenBounds, "static", { isSensor=true, filter=screenFilter })
local function newPositionVelocity(object)
local math_random = math.random
local side = math_random(1,4) ; local posX ; local posY ; local velX ; local velY
if (side == 1 or side == 3) then
posX = math_random(0,display.pixelHeight)
velX = math_random(-10,10) * 5
if (side == 1) then posY = -oy-40 ; velY = math_random(8,18) * 16
else posY = display.contentHeight+oy+40 ; velY = math_random(8,16) * -16
end
else
posY = math_random(0,display.pixelWidth)
velY = math_random(-10,10) * 5
if (side == 4) then posX = -ox-40 ; velX = math_random(8,16) * 16
else posX = display.contentWidth+ox+40 ; velX = math_random(8,16) * -16
end
end
object.x = posX ; object.y = posY
object:setLinearVelocity(velX, velY)
object.angularVelocity = math_random(-3,3) * 40
object.alpha = 1
end
require("repeller");
local rect = createRepeller(15, "rect",cw/2, cw/2);
local function objectCollide(self, event)
local otherName = event.other.name;
local function onDelay(event)
local action = ""
if (event.source) then action = event.source.action; timer.cancel(event.source) end
if (action == "makeJoint") then
self.hasJoint = true
self.touchJoint = physics.newJoint("touch", self, self.x, self.y)
self.touchJoint.frequency = magnetPull
self.touchJoint.dampingRatio = 0.0
self.touchJoint:setTarget(512, 384)
elseif (action == "leftField") then
self.hasJoint = false ; self.touchJoint:removeSelf() ; self.touchJoint = nil
else
if (self.hasJoint == true) then self.hasJoint = false ; self.touchJoint:removeSelf() ; self.touchJoint = nil end
newPositionVelocity(self)
end
end
if (event.phase == "ended" and otherName == "screenBounds") then
local tr = timer.performWithDelay(10, onDelay) ; tr.action = "leftScreen"
elseif (event.phase == "began" and otherName == "rect") then
transition.to(self, { time=400, alpha=0, onComplete=onDelay })
elseif (event.phase == "began" and otherName == "field" and self.hasJoint == false) then
local tr = timer.performWithDelay(10, onDelay) ; tr.action = "makeJoint"
elseif (event.phase == "ended" and otherName == "field" and self.hasJoint == true) then
local tr = timer.performWithDelay(10, onDelay) ; tr.action = "leftField"
end
end
local function setupWorld()
for i=1, 0 do
local obj = display.newCircle(0,0, 12)
physics.addBody(obj, "dynamic", { bounce=.3, radius=12, density = 0.0 --[[,filter=objFilter]] })
obj. isBullet = true;
newPositionVelocity(obj)
obj.hasJoint = false;
obj.name = "ball";
obj.collision = objectCollide ; obj:addEventListener("collision", obj)
end
local field = display.newCircle(cw/2, ch/2, 320);
field.alpha = 0.2;
field.name = "field";
field.x = display.contentCenterX ; field.y = display.contentCenterY;
physics.addBody(field, "static", { isSensor=true, radius=320, filter=fieldFilter });
magnet = display.newCircle(cw/2, ch/2, 40)
magnet.name = "magnet"
magnet.x = display.contentCenterX ; magnet.y = display.contentCenterY
physics.addBody(magnet, "static", { bounce=0, radius=40, filter=magnetFilter })
end
setupWorld()
deg = 0.0;
local prevPosY = 0
local line = display.newLine(cw/2,ch/2,cw/2,ch/2);
local function onTouch(event)
if line ~= nil then
line:removeSelf();
end
if event.phase == "began" then
prevPosY = event.y
end
if(event.y >= prevPosY) then
myJoint.motorSpeed = -(1.5 * prevPosY - event.y);
elseif(event.y <= prevPosY) then
myJoint.motorSpeed = 1.5 * prevPosY - event.y;
end
if(prevPosY - event.y == 0) then
--myJoint.motorSpeed = 0;
end
prevPosY = event.y;
line = display.newLine(cw/2,ch/2,event.x, event.y);
line.width= 1;
line.name = "line";
physics.addBody(line, { isSensor = true })
local function lineCol(self, event)
if (event.other.name=="rect") then
myJoint.motorSpeed = 0;
print(event.other.name, event.name);
--rect:removeSelf();
end
end
line.collision = lineCol;
line:addEventListener("collision", line);
end
line = display.newLine(cw/2,ch/2, 200 , 250);
line.width= 1;
line.name = "line";
physics.addBody(line, { isSensor = true, shape=line })
--Runtime:addEventListener("collision", lineCol)
physics.addBody(rect,"dynamic", {bounce = 0, density = 0})
myJoint = physics.newJoint("pivot", rect, magnet, cw/2, ch/2);
myJoint.isMotorEnabled = true;
myJoint.motorSpeed = 0;
myJoint.maxMotorTorque = 100;
Runtime:addEventListener("touch", onTouch);
和我的「repeller.lua」
function createRepeller(size, name, locX, locY)
local rep = display.newCircle(locX,locY, size);
rep.name = name;
return rep;
end
請留下唯一的問題相關的代碼 – Andrew
林不知道我明白了。我的代碼有問題。我當然希望你不是指語法錯誤?我的問題的問題是,我不完全明白我的問題在哪裏,甚至從哪裏開始解決。 –
你真的需要使用物理?因爲我不認爲物理是必要的。用簡單的數學,對象的位置和對象的旋轉值可以實現你想要的。我建議你以這種方式改變你的算法。而且你也可以使用math.atan函數來處理這樣的事情。 –