使用线的Corona SDK物理,为什么线在物理引擎中变成矩形?

时间:2013-05-16 02:20:18

标签: sdk box2d physics corona game-physics

我遇到的问题很简单。我在屏幕中央有一个物体,然后围绕该物体旋转一个圆圈。我想要做的是让物体围绕物体运行,当使用触摸屏幕时,它将围绕物体运行并停止面向用户点击屏幕的位置。

我在努力完成这项工作时遇到了巨大的困难。但我想我要做的是尝试从物体中心画一条线到使用点击的地方,然后围绕行星的球击中它将停止的线(足够简单)。我遇到的问题是线对象(在混合模式下查看时)更像是一个矩形然后它是一条线(在物理引擎中所以这与我的预期结果混淆。有没有人知道我做错了什么在这里?或者电晕是如何处理的?以及如何解决它?或者任何其他解决方案都可以很好地工作。

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 

1 个答案:

答案 0 :(得分:0)

在Corona中,线条并不适用于物理实体......就像在,你可以应用一个,但它可能不是你期望的形状。我建议的是创建一个非常薄的矩形(newRect())对象,然后将该行旋转/定位到您需要的位置。或者,只需使用其他用户建议的数学公式。

祝你好运, 布伦特