How do I use applyLinearImpulse based on rotation in Corona / Lua

家住魔仙堡 提交于 2019-12-04 13:06:46

Ok .... about 5 min after I posted this I figured it out. Here is the answer

speedX = 0.5 * (math.sin(ship.rotation*(math.pi/180)))
speedY = -0.5 * (math.cos(ship.rotation*(math.pi/180)))

if(event.phase =="began") then
  ship:applyLinearImpulse(speedX, speedY, player.x, player.y)
end

There are several things that you can improve in your code.

The first one is the way you calculate the angle. Instead of

ship.rotation*(math.pi/180)

You could do this:

local inverseRotation = ship.rotation + math.pi

An addition is faster than a division and a multiplication. Also, storing it on a variable will save you from calculating it twice. Then you can do:

local inverseRotation = ship.rotation + math.pi
local speedX, speedY = 0.5 * math.sin(inverseRotation), -0.5 * math.cos(inverseRotation)

Regards!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!