@asleeprj
You can also use trigonometry to set the forceX, and forceY of the linear impulse. For whatever reason, I ended up using trig instead of vectors for all of my physics calculations. I haven’t compared the two in a while, but my general impression is that vectors (primarily being angle-based) are a faster, cleaner and more efficient for position and motion calculation but trig (being circle-based) has a more nuanced feel that I like for my systems which are more biological rather than vehicular.
I’ve adjusted @roaminggamer’s code for a trig application below.
In the near future you will want to explore linear and angular damping in the physics object properties section
[lua]
local function getAngle ( o, t, inRadians )
local inRadians = inRadians or false
if (inRadians == true) then
local radians = math.atan2(t.y - o.y, t.x - o.x)
return radians – radian - atan2 adjusts or quadrant
elseif (inRadians == false) then
local degrees = math.deg( math.atan2(t.y - o.y, t.x - o.x) )
return degrees – degrees - atan2 adjusts or quadrant
end
end
function balloon.tap( self, event )
local forceFactor = self.mass * 1000 – adjust as needed
local angle = getAngle(self, event, true) – get angle returned in radians
local forceX = math.cos(angle) * forceFactor
local forceY = math.sin(angle) * forceFactor
self:applyLinearImpulse( forceX, forceY, self.x, self.y )
end
balloon:addEventListener(“tap”)
[/lua]