在我的游戏中,两个物体相互碰撞,但我想改变物体的角度 与另一个物体碰撞后。我希望物体在碰撞后将其方向改为180度,我使用了物理碰撞,任何帮助或建议..谢谢
答案 0 :(得分:1)
尝试使用以下方法获取身体线速度的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 )
保持编码..............:)