Sdk 在物理引擎中,为什么线会变成矩形?

Sdk 在物理引擎中,为什么线会变成矩形?,sdk,box2d,physics,coronasdk,game-physics,Sdk,Box2d,Physics,Coronasdk,Game Physics,我遇到的问题很简单。我在屏幕的中心有一个物体,然后有一个圆圈围绕着这个物体旋转。我想做的是让对象围绕对象旋转,当用户触摸屏幕时,它将围绕对象旋转,并停止面向用户点击屏幕的位置 我很难得到这份工作。但我想我会做的是试着从物体的中心画一条线,到使用者点击的地方,然后围绕行星旋转的球击中它将停止的线(足够简单)。我遇到的问题是,线对象(在混合模式下查看时)更像是一个矩形,而不是一条线(在物理引擎中,因此这会干扰我的预期结果。有人知道我在这里做错了什么吗?或者corona是如何处理这个问题的?以及如何可

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

我很难得到这份工作。但我想我会做的是试着从物体的中心画一条线,到使用者点击的地方,然后围绕行星旋转的球击中它将停止的线(足够简单)。我遇到的问题是,线对象(在混合模式下查看时)更像是一个矩形,而不是一条线(在物理引擎中,因此这会干扰我的预期结果。有人知道我在这里做错了什么吗?或者corona是如何处理这个问题的?以及如何可能修复它?或者任何其他解决方案都会很好

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);

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

祝你好运,
Brent

请只留下与问题相关的代码我不确定我是否理解。我的代码有问题。我当然希望你不是指语法错误?我的问题是,我不完全理解我的问题在哪里,甚至不知道从哪里开始解决。你真的需要使用物理吗?因为我认为物理在这里是不必要的简单的数学,物体的位置和旋转值你可以实现你想要的。我建议你用这种方式改变你的算法。你也可以用math.atan函数来做这样的事情。顺便说一句,我用简单的数学,物体的位置和旋转值创建了类似的东西。有一些大炮,瞄准敌人没有物理,一切都很顺利。
function createRepeller(size, name, locX, locY)
   local rep = display.newCircle(locX,locY, size);
   rep.name = name;
   return rep;
end