Corona SDK 物理 - 在特定方向施加力?
我是 Corona 新手,作为学习练习,我正在使用 Corona SDK 物理引擎创建一个简单的“keepie uppie”游戏 (http://en.wikipedia.org/wiki/Keepie_uppie)。
除了“踢”时的球运动外,我的所有方面都工作正常。我通过 applyLenearImpulse 方法使用触摸事件“开始”阶段来应用踢。
我的问题是,球的行为通常就像从上方施加力一样,导致它射向游戏环境的地板。尽管在球的接触点处施加了力,但仍然如此。
我想出了以下解决方法:
function ball:touch( event )
-- only allow taps in bottom half of ball
if ( event.y > ball.y and event.phase == "began" ) then
-- temporarily move floor to just below ball
floor.y = ball.y + ballSize
local flipx = 0
if(event.x > ball.x) then
flipx = event.x - ballSize
elseif(event.x < ball.x) then
flipx = event.x + ballSize
else
flipx = event.x
end
ball:applyLinearImpulse( 0, kickForce, flipx, event.y)
end
end
上述方法的工作原理是在施加力之前暂时将地板位置移动到球下方(然后通过 EnterFrame 事件侦听器将地板移回到正确的位置)。
我还发现,使用此解决方案时,我必须翻转事件触摸的 x 位置,否则它会与预期方向相反地水平弹跳。
上述情况显然并不理想。我应该在施加力之前停止球的运动吗?如果是,该怎么做?或者我完全采取了错误的方法?
I'm new to Corona and as a learning exercise I'm creating a simple game of "keepie uppie" (http://en.wikipedia.org/wiki/Keepie_uppie) using the Corona SDK physics engine.
I have all aspects working correctly, except the ball motion on "kick". I'm using the touch event "began" phase to apply a kick, via the applyLenearImpulse method.
My issue is that the ball is generally behaving as if force is being applied from above, causing it to shoot towards the game environment's floor. This is despite the force being applied at the point of contact on the ball.
I have come up with the following workaround:
function ball:touch( event )
-- only allow taps in bottom half of ball
if ( event.y > ball.y and event.phase == "began" ) then
-- temporarily move floor to just below ball
floor.y = ball.y + ballSize
local flipx = 0
if(event.x > ball.x) then
flipx = event.x - ballSize
elseif(event.x < ball.x) then
flipx = event.x + ballSize
else
flipx = event.x
end
ball:applyLinearImpulse( 0, kickForce, flipx, event.y)
end
end
The above works by temporarily moving the floor position to just under the ball before the force is applied (the floor is then moved back to the correct position via an enterFrame event listener).
I also found that with this solution I had to flip the x position of the event touch, or it would otherwise bounce horizontally opposite to the expected direction.
The above is clearly not ideal. Should I be stopping the ball motion before applying the force and, if so, how? Or am I taking the wrong approach completely?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
applyLinearImpulse 是您想要用于此目的的命令,但在所有解决方法中,您没有提到我会尝试的第一个调整:反转所施加的力的方向。您发布的代码没有告诉我们“kickForce”的值;你尝试过否定这一点吗?
applyLinearImpulse is the command you want to use for this, but in all your workarounds you didn't mention the first adjustment I would try: reversing the direction of the force applied. The code you posted doesn't tell us the value of "kickForce"; have you tried negating that?
是一种点击时“弹跳”球的简单方法。
它的作用:
如果点击事件已结束,它将获取当前球的线速度并将其反转。
我不知道你的代码在做什么(看不到如何运行它。)所以我在一分钟之内就做到了:)
热爱电晕不到一周<3
is a simple way to "bounce" a ball on click.
what it does:
if the click event has ended, it takes the current balls linear velocity and inverses it.
I dunno what your code was doing (couldn't see how to run it.) so I made that in a minute :)
been loving corona for less than a week <3