# Strange behavior when applying force (SOLVED)

**URL:** <https://forum.defold.com/t/strange-behavior-when-applying-force-solved/64061>\
**Category:** Questions\
**Created:** [December 8, 2019, 9:34pm UTC](https://forum.defold.com/t/strange-behavior-when-applying-force-solved/64061 "2019-12-08T21:34:30Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![Pawel](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/pawel/32/24623_2.png) [@Pawel](https://forum.defold.com/u/Pawel)\
**Post date:** [December 8, 2019, 9:34pm UTC](https://forum.defold.com/t/strange-behavior-when-applying-force-solved/64061/1 "2019-12-08T21:34:30Z")

</div>

Why is applying force behave normally in that case:  
 ![com-gif-maker%20(3)](https://forum-defold.b-cdn.net/uploads/default/original/3X/d/9/d9dc10489583fcd466225e7932292f3e0c5a08e8.gif)

But strange in the vertical shot?  
 ![5112ae92fbf1a0d9312ba25bb3dc983c](https://forum-defold.b-cdn.net/uploads/default/original/3X/5/d/5da7163654587c73013e95ac2960c3acbc2ac054.gif)

I mean this should end like the first shot (on the height of the factory) but goes further down 😕  
The only different between above is:

```
msg.post("#collisionobject", hash("apply_force"),
	{force = vmath.vector3(0,12000,0), position = go.get_world_position()})

```

and in the first one there is a 4000 as x value of the force vector.

Below is the repro:

[Empty Project.zip](https://forum.defold.com/uploads/default/original/3X/b/1/b1e4a2c4d345fe99dfb217df3a34505e6ecff922.zip) (21.0 KB)

---

<div class="post-metadata">

**Author:** ![Pkeod](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/pkeod/32/606_2.png) [@Pkeod](https://forum.defold.com/u/Pkeod)\
**Post date:** [December 9, 2019, 3:09am UTC](https://forum.defold.com/t/strange-behavior-when-applying-force-solved/64061/2 "2019-12-09T03:09:03Z")

</div>

How are you calculating the force vector? Are you using a radian / angle and converting it to a unit vector and then multiplying a force magnitude value?

[edit.zip](https://forum.defold.com/uploads/default/original/3X/0/0/00cc446da66a8400386bff5adfb1bf9d9896bd73.zip) (3.1 KB)

```lua
it = it or 0

function init(self)
	it = it + 1
	print("I'm here", msg.url())
	--local dir = math.pi / math.random(1,10)
	dir = it / 10
	local heading = vmath.vector3(math.cos(dir), math.sin(dir), 0)
	local power = 75000
	local force = heading * power
	
	msg.post("#collisionobject", hash("apply_force"),
	{force = force, position = go.get_world_position()})
	
	timer.delay(3, false, function() go.delete() end)
end

```

[https://forum.defold.com/uploads/default/original/3X/0/6/060141cdd3b985ad2dab5a91051081badbed452a.mp4](https://forum.defold.com/uploads/default/original/3X/0/6/060141cdd3b985ad2dab5a91051081badbed452a.mp4)

---

<div class="post-metadata">

**Author:** ![Pawel](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/pawel/32/24623_2.png) [@Pawel](https://forum.defold.com/u/Pawel)\
**Post date:** [December 9, 2019, 9:30pm UTC](https://forum.defold.com/t/strange-behavior-when-applying-force-solved/64061/3 "2019-12-09T21:30:01Z")

</div>

Ok, but does it even matter? I used just vmath.vector3(0,12000,0) - why is it bad if the result of calculating angles and multiplying is also a vector3? 😕 I don’t know why it would be non deterministic? Does the moment of applying forces depends on the frame or something? I have no idea 😕

---

<div class="post-metadata">

**Author:** ![Pkeod](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/pkeod/32/606_2.png) [@Pkeod](https://forum.defold.com/u/Pkeod)\
**Post date:** [December 10, 2019, 12:32am UTC](https://forum.defold.com/t/strange-behavior-when-applying-force-solved/64061/4 "2019-12-10T00:32:49Z")

</div>

I’m trying to understand how you are figuring out the force vector to use.

If I understand you then you have two vectors

```lua
vmath.vector3(0,12000,0)

```

and

```lua
vmath.vector3(4000,12000,0)

```

or

```lua
vmath.vector3(4000,0,0)

```

?

In any case, these have different lengths, which means the final force applied will be different.

```lua
	print(vmath.length(vmath.vector3(0,12000,0))) --> 12000
	print(vmath.length(vmath.vector3(4000,12000,0))) --> 12649.110351563
	print(vmath.length(vmath.vector3(4000,0,0))) --> 4000

```

That’s why I say you should first get a unit vector of length 1 and then multiply it by however much force you want to apply in that direction.

But maybe there’s something I’m not understanding?

Don’t forget that besides force there is gravity too.

Can you explain what you are trying to do?

---

<div class="post-metadata">

**Author:** ![roccosaienz](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/roccosaienz/32/19991_2.png) [@roccosaienz](https://forum.defold.com/u/roccosaienz)\
**Post date:** [December 10, 2019, 7:21am UTC](https://forum.defold.com/t/strange-behavior-when-applying-force-solved/64061/5 "2019-12-10T07:21:58Z")

</div>

Hi there!

I have tried the repro and, in my opinion, there Is something strange.

The equation of motion of a free body under Gravity Is Linear. This implies that the movements along different axis are Independent.

If I apply the force (0, 12000, 0) or the force (4000, 12000, 0) I must get the same behaviour along the Y axis. And this Is not what I see in the repro.

Maybe init Is not the right Place to apply forces? And, more important, for how long the msg apply\_force applies the given force? This must be guaranteed to be constant along the simulation.

Indeed, what change velocity Is the product F\*DT, where F Is the force and DT Is the interval of time the force Is applied.

I apologize for the length of this post about elementary physics…

---

<div class="post-metadata">

**Author:** ![Mathias\_Westerdahl](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/mathias_westerdahl/32/228_2.png) [@Mathias\_Westerdahl](https://forum.defold.com/u/Mathias_Westerdahl)\
**Post date:** [December 10, 2019, 7:48am UTC](https://forum.defold.com/t/strange-behavior-when-applying-force-solved/64061/6 "2019-12-10T07:48:08Z")

</div>

> [@roccosaienz](#):
>
> (0, 12000, 0) or the force (4000, 12000, 0)

They have different _magnitudes_. Compare e.g. with a “softer” or “harder” push.

> for how long the msg apply\_force applies the given force?

It is applied once, for that frame.

---

<div class="post-metadata">

**Author:** ![roccosaienz](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/roccosaienz/32/19991_2.png) [@roccosaienz](https://forum.defold.com/u/roccosaienz)\
**Post date:** [December 10, 2019, 8:17am UTC](https://forum.defold.com/t/strange-behavior-when-applying-force-solved/64061/7 "2019-12-10T08:17:51Z")

</div>

The two forces have the same Y components, so the mass should move in the same way along the Y axis (this is the superposition principle). And, if I understand, @Pawel is reporting that it is not.

The X components are different, hence the movements along X are different. So the total push is different as @Mathias_Westerdahl has said.

---

<div class="post-metadata">

**Author:** ![Mathias\_Westerdahl](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/mathias_westerdahl/32/228_2.png) [@Mathias\_Westerdahl](https://forum.defold.com/u/Mathias_Westerdahl)\
**Post date:** [December 10, 2019, 9:56am UTC](https://forum.defold.com/t/strange-behavior-when-applying-force-solved/64061/8 "2019-12-10T09:56:39Z")

</div>

Is your example project really the same as the one in your gif’s?  
In both cases, the particle dies after 2 seconds, since you have the timer set to 2 seconds.  
When running it, it does not look like your second gif for me.

---

<div class="post-metadata">

**Author:** ![roccosaienz](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/roccosaienz/32/19991_2.png) [@roccosaienz](https://forum.defold.com/u/roccosaienz)\
**Post date:** [December 10, 2019, 12:19pm UTC](https://forum.defold.com/t/strange-behavior-when-applying-force-solved/64061/9 "2019-12-10T12:19:53Z")

</div>

I have added the lines

print("y = " .. go.get\_position().y)

before go.delete() in ball.script in order to print the last value of Y before the deletion. This SHOULD be independent of the X component of the force applied to the body.

When the force (4000, 12000, 0) is applied I see “y = 129.16…”  
When the force (10000, 12000, 0) is applied I see “y = 86.15…”

I hope this may help to explain the “strange behavior” reported by @Pawel

Ciao!

---

<div class="post-metadata">

**Author:** ![Pawel](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/pawel/32/24623_2.png) [@Pawel](https://forum.defold.com/u/Pawel)\
**Post date:** [December 10, 2019, 2:37pm UTC](https://forum.defold.com/t/strange-behavior-when-applying-force-solved/64061/10 "2019-12-10T14:37:51Z")

</div>

Ok, so I think I get misunderstood. It’s not about a magnitude - it’s constant in both cases (doesn’t matter, that it has a different magnitude). The strange behavior is that in the vertical shot presented in the second gif in my first post, normally the ball is deleted before falling below the starting point, but **sometimes** , the ball is falling below it: 😕

 ![image](https://forum-defold.b-cdn.net/uploads/default/original/3X/1/3/13facd254a52a78655848589194a0d71c987e043.png)

---

<div class="post-metadata">

**Author:** ![Pkeod](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/pkeod/32/606_2.png) [@Pkeod](https://forum.defold.com/u/Pkeod)\
**Post date:** [December 10, 2019, 9:58pm UTC](https://forum.defold.com/t/strange-behavior-when-applying-force-solved/64061/11 "2019-12-10T21:58:58Z")

</div>

That sounds more like an issue related to the way you are deleting the object somehow. Could you modify your test to have a timer of dt increment inside the script’s update instead of using timer.delay() and see if you still see the odd behavior?

Worth noting in your uploaded example the group and mask are the same value. So since you create and destroy at the same interval, it’s possible that sometimes the creation happens before deletion where there are 2 objects in a single frame which pushes a ball downward on spawn to create an illusion? Try alternating the orb color too.

---

<div class="post-metadata">

**Author:** ![Pawel](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/pawel/32/24623_2.png) [@Pawel](https://forum.defold.com/u/Pawel)\
**Post date:** [December 11, 2019, 2:34pm UTC](https://forum.defold.com/t/strange-behavior-when-applying-force-solved/64061/12 "2019-12-11T14:34:39Z")

</div>

You are right!

I didn’t notice I wasn’t changing collisionobject groups nor masks 😕 It explains the issue, ball was simply colliding with other and was bumped up, and it was noticed only in a vertical shot, because in the leaned shot there were no collisions 😕 Moreover the timing in the first gif I uploaded was so bad, that the created ball was bumped down by a ball falling just before it was deleted.  
 ![6fc9e0d26b2b279974bd9fa9b6173cec](https://forum-defold.b-cdn.net/uploads/default/original/3X/c/7/c7b42043865ea1d8b68b78b9bfcb0fa6354eb3b7.gif) ![ef104d708ce11ee38fa362d86c059373](https://forum-defold.b-cdn.net/uploads/default/original/3X/6/8/681c8e1c626530b613382c6cd0e7a6762d49ebce.gif)  
Different collision groups **vs** overlapped collision groups.

What a dumb mistake 🤦‍♂️ Thank you! ❤

---

<div class="post-metadata">

**Author:** ![roccosaienz](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/roccosaienz/32/19991_2.png) [@roccosaienz](https://forum.defold.com/u/roccosaienz)\
**Post date:** [December 11, 2019, 6:42pm UTC](https://forum.defold.com/t/strange-behavior-when-applying-force-solved/64061/13 "2019-12-11T18:42:00Z")

</div>

Great @Pawel @Pkeod.

However it seems to me that the problem I mentioned in my last post about the Y final position that should be independent of the X component of the force is still there. And there is no collision involved.

I have to say that I do NOT use physics, so my interest in that problem is limited but if someone want to investigate further…

Ciao!

---

<div class="post-metadata">

**Author:** ![Pkeod](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/pkeod/32/606_2.png) [@Pkeod](https://forum.defold.com/u/Pkeod)\
**Post date:** [December 11, 2019, 8:35pm UTC](https://forum.defold.com/t/strange-behavior-when-applying-force-solved/64061/14 "2019-12-11T20:35:24Z")

</div>

Defold uses Box2D for its 2D physics. May be worth reaching out to the Box2D users/devs and asking about it.

---

<div class="post-metadata">

**Author:** ![roccosaienz](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/roccosaienz/32/19991_2.png) [@roccosaienz](https://forum.defold.com/u/roccosaienz)\
**Post date:** [December 11, 2019, 8:39pm UTC](https://forum.defold.com/t/strange-behavior-when-applying-force-solved/64061/15 "2019-12-11T20:39:15Z")

</div>

I am quite sure Box2D simulate the physics in the right way 🙂

Maybe there is something I am overlooking on the Defold side but I have no doubt about the physical content of the remark above.

---

<div class="post-metadata">

**Author:** ![Pkeod](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/pkeod/32/606_2.png) [@Pkeod](https://forum.defold.com/u/Pkeod)\
**Post date:** [December 11, 2019, 8:43pm UTC](https://forum.defold.com/t/strange-behavior-when-applying-force-solved/64061/16 "2019-12-11T20:43:40Z")

</div>

Can you make a new topic about the issue you are seeing? I am still confused about it. And I’m no physics expert either.

---

<div class="post-metadata">

**Author:** ![roccosaienz](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/roccosaienz/32/19991_2.png) [@roccosaienz](https://forum.defold.com/u/roccosaienz)\
**Post date:** [December 11, 2019, 8:49pm UTC](https://forum.defold.com/t/strange-behavior-when-applying-force-solved/64061/17 "2019-12-11T20:49:12Z")

</div>

Ok! I will try to adapt the small repro of @Pawel and start a new topic. However I am not an expert about physics in Defold…
