如何在corona sdk中的碰撞后更改对象的角度

在我的游戏中,两个物体正在发生碰撞,但我想在碰撞后改变物体的角度。我希望物体在碰撞后改变方向为180度,我已经使用了物理碰撞。请问有什么帮助或建议吗?谢谢。

点赞
用户1979583
用户1979583

尝试使用以下代码获取物体的线性速度的x,y分量:

vx, vy = myBody:getLinearVelocity()

并重置为:

myBody:setLinearVelocity(-vx,-vy )

了解更多信息,请访问Corona - 物理体

示例:

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

-- 创建地面和物体 ---
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" ) 

-- 添加物理 --
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 )

-- 碰撞函数 --
local function onGlobalCollision( event )
  if ( event.phase == "began" ) then
    print(event.object1.myName .. " & " .. event.object2.myName .. " collision began" )
    vx_1, vy_1 = crate2:getLinearVelocity()     -- 获取crate2的速度
    crate2:setLinearVelocity(-vx_1,-vy_1 )      -- 重置crate2的速度

    vx_2, vy_2 = crate1:getLinearVelocity()     -- 获取crate1的速度
    crate1:setLinearVelocity(-vx_2,-vy_2 )      -- 重置crate1的速度
  end
end
Runtime:addEventListener( "collision", onGlobalCollision )

保持编码 ............. :)

2013-08-05 19:34:18