My code pretty much matches the example in the API documentation:
-- apply a force of 1 Newton towards world-x at the center of the game object instance
msg.post("#co", "apply_force", {force = vmath.vector3(1, 0, 0), position = go.get_world_position()})
Yeah, none of the tutorials address applying forces to dynamic objects; I want to use physics to send spawned asteroids in semi-random directions by applying forces to them in the init() function for each object.
As a test I’ve also tried creating a (dynamic) spaceship and having its left and right movement controlled by applying forces rather than explicitly modifying its x and y positions (as a kinematic object), but nothing has worked.
I’m following the documentation I posted above, so not sure why nothing is working so far…
OK, problem solved-- I was only applying the force once in the ‘init()’ function of my objects, rather than in update()…
[That being said, if an object has very low friction and is only ‘pushed’ once, shouldn’t the physics engine calculate its path automatically without applying a force every frame? Applying a force each frame is not much different than me updating an object’s position every frame…]
It’s likely if you modify the values more you’ll be able to get the effect you want without constantly applying force, but I have not experimented enough with physics yet so can’t say for sure.
@Bryan_Green That “push” you mention is usually called “Impulse”, and since you only apply it once, you’ll need a much higher force to apply, than you would if constantly updating. Here is one link but there are many more out there.
Also, yes, changing the velocity of an object is very much related to applying forces, as you’ll see when reading these formulas. Different games need different approaches to solve their movement in a satisfying manner