ApplyForce isn't working as I expect. Is somethong wrong?

Hello I'm new to Box2D.

I was playing with the Hello world project and noticed some unusual behavior that didn't sit well with what I know about physics.

The code bellow is the HelloWorld file that comes with Box2D with two modifications:
1. I disabled gravity
Code:
   b2Vec2 gravity(0.0f, 0.0f);


2. I'm applying a torque to this floating object
Code:
   // This is our little game loop.
   for (int32 i = 0; i < 60; ++i)
   {
      b2Vec2 force(0,40);
      b2Vec2 point(1,4);
      body->ApplyForce(force, point, true);
      
      // Instruct the world to perform a single step of simulation.
      // It is generally best to keep the time step and iterations fixed.
      world.Step(timeStep, velocityIterations, positionIterations);


Code:
/*
* Copyright (c) 2006-2007 Erin Catto http://www.box2d.org
*
* This software is provided 'as-is', without any express or implied
* warranty.  In no event will the authors be held liable for any damages
* arising from the use of this software.
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
*/

#include <Box2D/Box2D.h>

#include <stdio.h>

// This is a simple example of building and running a simulation
// using Box2D. Here we create a large ground box and a small dynamic
// box.
// There are no graphics for this example. Box2D is meant to be used
// with your rendering engine in your game engine.
int main(int argc, char** argv)
{
   B2_NOT_USED(argc);
   B2_NOT_USED(argv);

   // Define the gravity vector.
   b2Vec2 gravity(0.0f, 0.0f);

   // Construct a world object, which will hold and simulate the rigid bodies.
   b2World world(gravity);

   // Define the ground body.
   b2BodyDef groundBodyDef;
   groundBodyDef.position.Set(0.0f, -10.0f);

   // Call the body factory which allocates memory for the ground body
   // from a pool and creates the ground box shape (also from a pool).
   // The body is also added to the world.
   b2Body* groundBody = world.CreateBody(&groundBodyDef);

   // Define the ground box shape.
   b2PolygonShape groundBox;

   // The extents are the half-widths of the box.
   groundBox.SetAsBox(50.0f, 10.0f);

   // Add the ground fixture to the ground body.
   groundBody->CreateFixture(&groundBox, 0.0f);

   // Define the dynamic body. We set its position and call the body factory.
   b2BodyDef bodyDef;
   bodyDef.type = b2_dynamicBody;
   bodyDef.position.Set(0.0f, 4.0f);
   b2Body* body = world.CreateBody(&bodyDef);

   // Define another box shape for our dynamic body.
   b2PolygonShape dynamicBox;
   dynamicBox.SetAsBox(1.0f, 1.0f);

   // Define the dynamic body fixture.
   b2FixtureDef fixtureDef;
   fixtureDef.shape = &dynamicBox;

   // Set the box density to be non-zero, so it will be dynamic.
   fixtureDef.density = 1.0f;

   // Override the default friction.
   fixtureDef.friction = 0.3f;

   // Add the shape to the body.
   body->CreateFixture(&fixtureDef);

   // Prepare for simulation. Typically we use a time step of 1/60 of a
   // second (60Hz) and 10 iterations. This provides a high quality simulation
   // in most game scenarios.
   float32 timeStep = 1.0f / 60.0f;
   int32 velocityIterations = 6;
   int32 positionIterations = 2;

   // This is our little game loop.
   for (int32 i = 0; i < 60; ++i)
   {
      b2Vec2 force(0,40);
      b2Vec2 point(1,4);
      body->ApplyForce(force, point, true);
      
      // Instruct the world to perform a single step of simulation.
      // It is generally best to keep the time step and iterations fixed.
      world.Step(timeStep, velocityIterations, positionIterations);

      // Now print the position and angle of the body.
      b2Vec2 position = body->GetPosition();
      float32 angle = body->GetAngle();

      printf("%4.2f %4.2f %4.2f\n", position.x, position.y, angle);
   }

   // When the world destructor is called, all bodies and joints are freed. This can
   // create orphaned pointers, so be careful about your world management.

   return 0;
}




The output of running this code is (the columns are x, y, and angle:
Code:
0.00 4.00 0.00
0.00 4.01 0.01
0.00 4.02 0.03
0.00 4.03 0.04
0.00 4.04 0.06
0.00 4.06 0.09
0.00 4.08 0.12
0.00 4.10 0.15
0.00 4.12 0.19
0.00 4.15 0.23
0.00 4.18 0.28
0.00 4.22 0.33
0.00 4.25 0.38
0.00 4.29 0.44
0.00 4.33 0.50
0.00 4.38 0.57
0.00 4.42 0.64
0.00 4.47 0.71
0.00 4.53 0.79
0.00 4.58 0.88
0.00 4.64 0.96
0.00 4.70 1.05
0.00 4.77 1.15
0.00 4.83 1.25
0.00 4.90 1.35
0.00 4.97 1.46
0.00 5.05 1.58
0.00 5.13 1.69
0.00 5.21 1.81
0.00 5.29 1.94
0.00 5.38 2.07
0.00 5.47 2.20
0.00 5.56 2.34
0.00 5.65 2.48
0.00 5.75 2.63
0.00 5.85 2.78
0.00 5.95 2.93
0.00 6.06 3.09
0.00 6.17 3.25
0.00 6.28 3.42
0.00 6.39 3.59
0.00 6.51 3.76
0.00 6.63 3.94
0.00 6.75 4.13
0.00 6.87 4.31
0.00 7.00 4.50
0.00 7.13 4.70
0.00 7.27 4.90
0.00 7.40 5.10
0.00 7.54 5.31
0.00 7.68 5.53
0.00 7.83 5.74
0.00 7.97 5.96
0.00 8.12 6.19
0.00 8.28 6.42
0.00 8.43 6.65
0.00 8.59 6.89
0.00 8.75 7.13
0.00 8.92 7.38
0.00 9.08 7.63



If you notice the body is created in position 0,4 and I apply the force from the point 1,4 and upwards towards positive y.
The direction from the center of the body (the fulcrum) to the point where the force originates is (1,4)-(0,4)=(1,0).
The force direction is (0,40).
This means that there should be a torque of 1*40 on the object.
and there should be no change in acceleration of the center point of the body since the force being applied is perpendicular to the vector between the fulcrum and the force's origin.

However look at how the y coordinate of the body keeps increasing. Shouldn't the body just be spinning in place?
I think there is a flag on b2World for resetting forces at the end of a Step.
Thats not how torque from a point works.
Image
I meant to say rotation, not horizontal
http://www.dummies.com/how-to/content/how-to-convert-linear-force-to-angular-torque.html

What you want to do is apply a velocity tangent to your offset
Another way of putting it is: the angle from the center to your forcepoint +/- 90 degrees or PI/2 (I'm going to do this in degrees since I don't know which you like better and few people besides me like radians)

Since your offset is (1,4), the angle between the center and the offset is
atan(4/1) approximately 1.325 or around 75.96 degrees

it seems as though your desire is to rotate counter clockwise, as such, you should take that angle and add 90 degrees or PI/2
from there you'll want to create a new vector with it by
Vec2(cos(ourangle+90), sin(ourangle+90))

then multiply it by the force that you want to apply .mul(4)

and you'll have it properly rotating, keep in mind, the further away you're applying the force, the less effect it will have on a body. If you apply a big force at a close distance, it will move quickly, if you have a big force far away, it will move gradually.

simple terms
Code:
float desired_force = 4;
float angle = atan(offset.y/offset.x);
Vec2 ourforce = new Vec2(cos(angle+90), sin(angle+90)).mul(desired_force);



Keep in mind, depending on how your fixtures are structured, its important to apply it tangent to the bodies center of mass not just its world center. Thats where things like getLocalCenter becomes useful
Oh now I understand, the "point" parameter in the "ApplyForce(force,point,wake)" function is relative to the body center of mass?

so I guess doing this:
Code:
   ...
   for (int32 i = 0; i < 60; ++i)
   {
      b2Vec2 force(0,40);
      b2Vec2 point(1,0);
      body->ApplyForce(force, point, true);
      ...


would now make it just spin because the offset (1,0) is perpendicular to the force direction (0,40)
looks good to me
Actually I tried it out but it still doesn't cause the behavior I would expect

Code:
0.00 4.00 0.00
0.00 4.01 0.01
0.00 4.02 0.03
0.00 4.03 0.04
0.00 4.04 0.06
0.00 4.06 0.09
0.00 4.08 0.12
0.00 4.10 0.15
0.00 4.12 0.19
0.00 4.15 0.23
0.00 4.18 0.28
0.00 4.22 0.33
0.00 4.25 0.38
0.00 4.29 0.44
0.00 4.33 0.50
0.00 4.38 0.57
0.00 4.42 0.64
0.00 4.47 0.71
0.00 4.53 0.79
0.00 4.58 0.88
0.00 4.64 0.96
0.00 4.70 1.05
0.00 4.77 1.15
0.00 4.83 1.25
0.00 4.90 1.35
0.00 4.97 1.46
0.00 5.05 1.58
0.00 5.13 1.69
0.00 5.21 1.81
0.00 5.29 1.94
0.00 5.38 2.07
0.00 5.47 2.20
0.00 5.56 2.34
0.00 5.65 2.48
0.00 5.75 2.63
0.00 5.85 2.78
0.00 5.95 2.93
0.00 6.06 3.09
0.00 6.17 3.25
0.00 6.28 3.42
0.00 6.39 3.59
0.00 6.51 3.76
0.00 6.63 3.94
0.00 6.75 4.13
0.00 6.87 4.31
0.00 7.00 4.50
0.00 7.13 4.70
0.00 7.27 4.90
0.00 7.40 5.10
0.00 7.54 5.31
0.00 7.68 5.53
0.00 7.83 5.74
0.00 7.97 5.96
0.00 8.12 6.19
0.00 8.28 6.42
0.00 8.43 6.65
0.00 8.59 6.89
0.00 8.75 7.13
0.00 8.92 7.38
0.00 9.08 7.63


Even tho the force (0,40) is tangent to the offset (1,0)
Try Circle Shape for me, I'm curious if its because it has points

btw you are aware there is applyAngularImpulse as well right?
I created a circle with

Code:
   b2CircleShape circle;
   circle.m_radius = 1;


replaced it with the box and ran the same scenario with zero gravity:

Code:
   // Define the gravity vector.
   b2Vec2 gravity(0.0f, 0.0f);

   // Construct a world object, which will hold and simulate the rigid bodies.
   b2World world(gravity);


and applying the same force with an offset:
Code:
   b2Vec2 force(0,40);
   b2Vec2 point(1,0);
   body->ApplyForce(force, point, true);


but the result is:
Code:
0.00 4.00 0.01
0.00 4.01 0.02
0.00 4.02 0.04
0.00 4.04 0.07
0.00 4.05 0.11
0.00 4.07 0.15
0.00 4.10 0.20
0.00 4.13 0.25
0.00 4.16 0.32
0.00 4.19 0.39
0.00 4.23 0.47
0.00 4.28 0.55
0.00 4.32 0.64
0.00 4.37 0.74
0.00 4.42 0.85
0.00 4.48 0.96
0.00 4.54 1.08
0.00 4.60 1.21
0.00 4.67 1.34
0.00 4.74 1.49
0.00 4.82 1.63
0.00 4.89 1.79
0.00 4.98 1.95
0.00 5.06 2.12
0.00 5.15 2.30
0.00 5.24 2.48
0.00 5.34 2.67
0.00 5.44 2.87
0.00 5.54 3.08
0.00 5.64 3.29
0.00 5.75 3.51
0.00 5.87 3.73
0.00 5.98 3.97
0.00 6.10 4.21
0.00 6.23 4.46
0.00 6.36 4.71
0.00 6.49 4.97
0.00 6.62 5.24
0.00 6.76 5.52
0.00 6.90 5.80
0.00 7.05 6.09
0.00 7.19 6.39
0.00 7.35 6.69
0.00 7.50 7.00
0.00 7.66 7.32
0.00 7.82 7.65
0.00 7.99 7.98
0.00 8.16 8.32
0.00 8.33 8.67
0.00 8.51 9.02
0.00 8.69 9.38
0.00 8.87 9.75
0.00 9.06 10.12
0.00 9.25 10.50
0.00 9.45 10.89
0.00 9.64 11.29
0.00 9.85 11.69
0.00 10.05 12.10
0.00 10.26 12.52
0.00 10.47 12.94


the body is still moving up when it should just be spinning in place.

Yea I know I could use other functions but I just want to figure out how ApplyForce(force,point,wake) works and why it's not working the way I thought it would
If I don't understand it I won't even have the option to use it in the future
Quote:
the body is still moving up when it should just be spinning in place.
????
Suppose you have a toy boat in a pool, and there is a rope tied to the front of the boat. If you pull the rope sideways, would you expect the boat to spin in place and not move?
irresistible force wrote:
Quote:
the body is still moving up when it should just be spinning in place.
????
Suppose you have a toy boat in a pool, and there is a rope tied to the front of the boat. If you pull the rope sideways, would you expect the boat to spin in place and not move?


yes, exactly

Original thread: https://box2d.org/forum/viewtopic.php?t=9708