How can I use the object:applyForce command to apply a force in the direction the object is already traveling, not just in the x and y direction?
Do some 2D Math on the result of getLinearVelocity()
-
Activate this plugin: https://store.coronalabs.com/plugin/math2d
-
Put it in your build.settings file: http://roaminggamer.com/math2d/#usage
-
Do this (tested in head, may have typos):
local function applyDirectionalForce( obj, magnitude ) local math2d = require “plugin.math2d” local vx,vy = obj:getLinearVelocity() vx,vy = math2d.normalize( vx, vy ) vx,vy = math2d.scale( vx, vy, magnitude * obj.mass ) obj:applyForce( vx, vy, obj.x, obj.y ) end applyDirectionalForce( bob, 10 )
I wouldn’t be able to use this on a collision though would I? I want the force to be applied once has collided with a physics sensor
timer.performWithDelay(1,function() applyDirectionalForce( bob, 10 ) end )
i.e. wait one frame. Tip: A timer delay of 1ms is enough to force it to wait till the next frame.
Cheers. Is it supposed to be “local function applyDirectionalForce” rather than “local applyDirectionalForce” And would I just have it as timer.performWithDelay(1, function(applyDirectionalForce)(bob, 10) end) ?
Also, sorry but what does bob mean, is it just an example??
-
Was missing keyword function - Fixed.
-
The timer example was to answer your question about handling physics updates in a collision handler. Just use a timer to delay the update.
-
Bob, Sue, Bill, Buddy, … gotta give example objects some kind of name…

Do some 2D Math on the result of getLinearVelocity()
-
Activate this plugin: https://store.coronalabs.com/plugin/math2d
-
Put it in your build.settings file: http://roaminggamer.com/math2d/#usage
-
Do this (tested in head, may have typos):
local function applyDirectionalForce( obj, magnitude ) local math2d = require “plugin.math2d” local vx,vy = obj:getLinearVelocity() vx,vy = math2d.normalize( vx, vy ) vx,vy = math2d.scale( vx, vy, magnitude * obj.mass ) obj:applyForce( vx, vy, obj.x, obj.y ) end applyDirectionalForce( bob, 10 )
I wouldn’t be able to use this on a collision though would I? I want the force to be applied once has collided with a physics sensor
timer.performWithDelay(1,function() applyDirectionalForce( bob, 10 ) end )
i.e. wait one frame. Tip: A timer delay of 1ms is enough to force it to wait till the next frame.
Cheers. Is it supposed to be “local function applyDirectionalForce” rather than “local applyDirectionalForce” And would I just have it as timer.performWithDelay(1, function(applyDirectionalForce)(bob, 10) end) ?
Also, sorry but what does bob mean, is it just an example??
-
Was missing keyword function - Fixed.
-
The timer example was to answer your question about handling physics updates in a collision handler. Just use a timer to delay the update.
-
Bob, Sue, Bill, Buddy, … gotta give example objects some kind of name…
