Lua 如何在碰撞后更改对象的角度

Lua 如何在碰撞后更改对象的角度,lua,coronasdk,Lua,Coronasdk,在我的游戏中,两个物体相互碰撞,但我想改变物体的角度 与另一物体碰撞后。我希望物体在碰撞后将其方向改变为180度,我已经使用了碰撞物理学,任何帮助或建议。。谢谢尝试使用以下方法获取身体线速度的x、y分量: vx, vy = myBody:getLinearVelocity() 并将其重置为: myBody:setLinearVelocity(-vx,-vy ) 有关更多信息,请访问 示例: local physics = require( "physics" ) physics.start

在我的游戏中,两个物体相互碰撞,但我想改变物体的角度
与另一物体碰撞后。我希望物体在碰撞后将其方向改变为180度,我已经使用了碰撞物理学,任何帮助或建议。。谢谢

尝试使用以下方法获取身体线速度的x、y分量:

vx, vy = myBody:getLinearVelocity()
并将其重置为:

myBody:setLinearVelocity(-vx,-vy ) 
有关更多信息,请访问

示例:

local physics = require( "physics" )
physics.start()

-- Create ground and bodies ---
local ground = display.newImage( "ground.png" )
ground.x = display.contentWidth / 2
ground.y = 445
ground.myName = "ground"

local crate1 = display.newCircle(0,0,30,30)
crate1.x = 180; crate1.y = 350
crate1.myName = "first crate"

local crate2 = display.newCircle(0,0,30,30)
crate2.x = 220; crate2.y = -50
crate2.myName = "second crate"

-- physics.setDrawMode( "debug" ) -- Uncomment this line to see the physics shapes

-- Adding physics --
physics.addBody( ground, "static", { friction=0.5, bounce=0.3 } )
physics.addBody( crate1, { density=3.0, friction=0.5, bounce=0.3,radius = 30} )
physics.addBody( crate2, { density=3.0, friction=0.5, bounce=0.3,radius = 30} )

crate1:setLinearVelocity( 0, -400 )

-- Collision function --
local function onGlobalCollision( event )
  if ( event.phase == "began" ) then
    print(event.object1.myName .. " & " .. event.object2.myName .. " collision began" )
    vx_1, vy_1 = crate2:getLinearVelocity()     -- get the velocities of crate2
    crate2:setLinearVelocity(-vx_1,-vy_1 )      -- reset the velocities of crate2

    vx_2, vy_2 = crate1:getLinearVelocity()     -- get the velocities of crate1
    crate1:setLinearVelocity(-vx_2,-vy_2 )      -- reset the velocities of crate1
  end   
end
Runtime:addEventListener( "collision", onGlobalCollision )

继续编码………..)

尝试使用以下方法获取身体线速度的x,y分量:

vx, vy = myBody:getLinearVelocity()
并将其重置为:

myBody:setLinearVelocity(-vx,-vy ) 
有关更多信息,请访问

示例:

local physics = require( "physics" )
physics.start()

-- Create ground and bodies ---
local ground = display.newImage( "ground.png" )
ground.x = display.contentWidth / 2
ground.y = 445
ground.myName = "ground"

local crate1 = display.newCircle(0,0,30,30)
crate1.x = 180; crate1.y = 350
crate1.myName = "first crate"

local crate2 = display.newCircle(0,0,30,30)
crate2.x = 220; crate2.y = -50
crate2.myName = "second crate"

-- physics.setDrawMode( "debug" ) -- Uncomment this line to see the physics shapes

-- Adding physics --
physics.addBody( ground, "static", { friction=0.5, bounce=0.3 } )
physics.addBody( crate1, { density=3.0, friction=0.5, bounce=0.3,radius = 30} )
physics.addBody( crate2, { density=3.0, friction=0.5, bounce=0.3,radius = 30} )

crate1:setLinearVelocity( 0, -400 )

-- Collision function --
local function onGlobalCollision( event )
  if ( event.phase == "began" ) then
    print(event.object1.myName .. " & " .. event.object2.myName .. " collision began" )
    vx_1, vy_1 = crate2:getLinearVelocity()     -- get the velocities of crate2
    crate2:setLinearVelocity(-vx_1,-vy_1 )      -- reset the velocities of crate2

    vx_2, vy_2 = crate1:getLinearVelocity()     -- get the velocities of crate1
    crate1:setLinearVelocity(-vx_2,-vy_2 )      -- reset the velocities of crate1
  end   
end
Runtime:addEventListener( "collision", onGlobalCollision )

继续编码………..)

这个办法对我不管用,我试过了。您能告诉我在哪里包含此代码或提供此类解决方案的链接吗。在我的例子中,两个物体都在碰撞,因为它们是物理物体,碰撞后另一个物体向任何方向移动,但我希望它移动到180度,它在哪一部分发生碰撞无关紧要谢谢你的帮助。。上面的代码适合我,但我需要一些修改,在本例中,两个球都会移动,但我希望其中一个静止,而另一个会移动并在碰撞后碰撞。它会改变角度,如果我希望球移动到90度而不是180度的角度,我应该在该代码中做些什么更改…只需注释行:“crate1:setLinearVelocity(0,-400)”将使一个球静止。碰撞后,您可以设置“板条箱1:setLinearVelocity(vx,vy),其中vx和vy分别为xVelocity和yVelocity。板条箱1:setLinearVelocity(-90,0)。这个解决方案对我不起作用,我试过了。您能告诉我在哪里包含此代码或提供此类解决方案的链接吗。在我的例子中,两个物体都在碰撞,因为它们是物理物体,碰撞后另一个物体向任何方向移动,但我希望它移动到180度,它在哪一部分发生碰撞无关紧要谢谢你的帮助。。上面的代码适合我,但我需要一些修改,在本例中,两个球都会移动,但我希望其中一个静止,而另一个会移动并在碰撞后碰撞。它会改变角度,如果我希望球移动到90度而不是180度的角度,我应该在该代码中做些什么更改…只需注释行:“crate1:setLinearVelocity(0,-400)”将使一个球静止。碰撞后,您可以设置“板条箱1:setLinearVelocity(vx,vy),其中vx和vy分别为xVelocity和yVelocity。例如:板条箱1:setLinearVelocity(-90,0)。