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

Are you serious... the only way it would spin in place is if there was something holding it there, like a joint to rotate around.

Here is the explanation of ApplyForce:
Code:
        /// Apply a force at a world point. If the force is not
        /// applied at the center of mass, it will generate a torque and
        /// affect the angular velocity. This wakes up the body.
        /// @param force the world force vector, usually in Newtons (N).
        /// @param point the world position of the point of application.
        /// @param wake also wake up the body
        void ApplyForce(const b2Vec2& force, const b2Vec2& point, bool wake);

Since the force is applied at the same location all the time, you get this result:
Attachment:
applyforce.png
applyforce.png [ 11.85 KiB | Viewed 223 times ]

Actually my example with the toy boat is not quite right, it would be more like having a hose spraying water sideways at the front of the boat.
Oh yea, I see what you mean.
Initially the force applied won't move the body off its center very much but as soon as it moves off the force will not be perpendicular to the offset where its being applied. So it will move the body further off the (0,4) point
That makes sense.
I'll try moving the force along with the body's center point instead of keeping it fixed at (1,4)
Well actually I ran two experiments

Both with gravity turned off:
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);


Both using a circle:
using a circle:
Code:
   // Define another box shape for our dynamic body.
   b2CircleShape circle;
   circle.m_radius = 1;

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


The first experiment had this force:
Applying a 40N force to the center of the body:
Code:
   b2Vec2 force(0,40);
   b2Vec2 point(1,4);
   body->ApplyForce(force, point, true);


And the second experiment had this force:
Code:
   b2Vec2 force(0,40);
   b2Vec2 center = body->GetWorldCenter();
   b2Vec2 offset = b2Vec2(1,0);
   b2Vec2 point = center+offset;
   body->ApplyForce(force, point, true);


Note how on the first experiment the force is being applied always at (1,4) while on the second the force is being applied always at an offset (1,0) to the center of the body as the body is moving up in y.

I would expect that in the second experiment the object would have moved less and would have spun more because the force being applied is always perpendicular to the offset from the body.

But the result for both experiments is exactly the same:
Code:
x    y    angle
0.00 4.00 0.01
0.00 4.01 0.02
0.00 4.02 0.04
...
0.00 10.05 12.10
0.00 10.26 12.52
0.00 10.47 12.94


That seems kind of strange.
It's as if the code is ignoring the fact that the offset of the force to the body is increasing in the first experiment while it's constant in the second experiment.

EDIT:
I did some physics by hand and apparently physics agrees with this result.
That seems counter intuitive to me.
But if physics says it's right then it's definitely right

EDIT2:
OOOh of course!
Now I understand why.
It's because the force becoming less and less tangent to the offset is perfectly countered by the increasing radius where the torque is applied.
The two cases you have there are not really much different. In my picture above, it would be just like having the red dot moving vertically upward along beside the body. So yes, the results should be exactly the same.

If you want to spin the body more than move it, you would also need to have the force rotating around as the body rotates. You can use the functions:
b2Body::GetWorldPoint
b2Body::GetWorldVector
... to convert a point/vector from local coordinates of the body into world coordinates for use with ApplyForce.

For example in your case, the starting direction for your force is 0,1 and the location to apply it is 1,0. These are both relative to the body, so we would call them 'local' coordinates.
Code:
b2Vec2 forcePoint_local = b2Vec2(1,0);
b2Vec2 forceDir_local = b2Vec2(0,1);
As the body moves/rotates, we can use the GetWorldxxx functions to find the current world coordinate/direction for these:
Code:
b2Vec2 forcePoint_world = body->GetWorldPoint( forcePoint_local );
b2Vec2 forceDir_world = body->GetWorldVector( forceDir_local );

body->ApplyForce( 40 * forceDir_world, forcePoint_world, true );
I hate to disappoint you but this will still not cause it to spin in place 😀 The effect would be like this, which only stays in place because it is nailed there:
https://www.youtube.com/watch?v=FEG_XDcqUdo
To keep it spinning in place without any joint to 'nail it down' you could use a second force of equal and opposite strength on the other side of the body to cancel the movement and leave only rotation as a result. I have a feeling it would slowly drift away from the starting location though over time, since the precision of the calculation is limited.
Wow I had it all wrong.

When applying a force in a direction orthogonal to the offset between the body's center and the force.
I thought that kind of force would only produce tangential acceleration and no linear acceleration.
Now I'm starting to remember my physics classes more clearly.
Your help and the recent reading I did on the subject cleared everything up.

The reason I was trying to figure all this out is because I'm making a 2D spaceship game with similar spaceship mechanics as Asteroids.
I will take your advice and use an opposing force (spaceship thruster) to counteract any linear acceleration caused by the ship's turning thrusters.

Thanks for the help.
It might be easier to just use ApplyTorque, which will turn the body about its center of mass without the need for multiple forces and the calculations above.
You may want to apply an opposite equal on the other side of the object as well.

I wasn't thinking about it before but it makes sense in terms of spaceships. Where if you just turn on one thruster your going to be piviting around a point that is outside the ship itself. If you turn oon one opposite and one forward, it should work properly
I won't be using ApplyTorque because I wanted the ship to thrust realistically and it will display particles from wherever it's thrusting.
When only applying torque I don't see how to clearly show what's causing that torque.

So the best solution in my case is applying an equal but opposite force on the other side of the object.
Or applying a force to the center of mass to negate the linear acceleration. But this would be less fuel efficient.

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