如何改变物体在电晕中碰撞后的角度



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

尝试使用获取身体线速度的x,y分量

vx, vy = myBody:getLinearVelocity()

并将其重置为:

myBody:setLinearVelocity(-vx,-vy ) 

欲了解更多信息,请访问Corona-Physics Bodies。

样本:

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 )

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

最新更新