Apply Force to Game Object Instance? [SOLVED]

Hey, folks-- I’m testing the ‘apply_force’ message, and can’t seem to get it to work. See my code below; the asteroid collision object is dynamic:

local asteroid = factory.create("controller#asteroidFactory", vmath.vector3(math.random(40, 1240),720,0.3), nil, {}, math.random() + .20)
_asteroid_count = _asteroid_count + 1
msg.post(msg.url(nil, asteroid, "asteroidCollision"), "apply_force", {force = vmath.vector3(1, 0, 0), position = go.get_world_position(asteroid)})

…Gravity is pulling the asteroids down, as expected, but there is no force being applied to the collider in the x direction…

Thanks!
Bryan

1 Like

I think you have to apply a much large force for it to be noticeable.

1 Like

Well, I also tried 100 with no results… :slight_smile:

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()})

Bryan

Study this: https://github.com/britzl/publicexamples/blob/master/examples/rotate_collision_object/rotate_collision_object.script

Did you go through the platformer tutorial already? It uses different methods that are generally better afaik. http://www.defold.com/tutorials/platformer.html

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…

Bryan

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…]

Bryan

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.

1 Like

If you are still only applying a force of vector3(1, 0, 0) then it is way too small. Did you try with a much larger value?

1 Like

@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

3 Likes

Ah-- I upped the value I was testing with during init() up to 500 and I’m now seeing what I would expect.

Thanks, all!
Bryan

2 Likes