Arbitrary forces
A physics body is affected by forces of gravity and by interactions with other physics bodies. You can also apply arbitrary forces to a physics body.
The applyForce()
method of a PhysicsBody
applies a force to the body. The method takes a Vector
as an argument. The dx
and dy
components of the vector represent the force to apply in the horizontal and vertical directions, respectively. Optionally, it also takes a Point
as a second argument. The x
and y
components of the point represent the position of application of the force. If the position is not specified, the force is applied to the center of the physics body.
The example is a simple slingshot. Drag the pink ball to aim it and adjust the draw length. Release the ball to fire it. The ball will bounce off the edges of the boundary.
- If we draw the slingshot back as far as we can, it will apply a lot of force, and the ball will move at a high speed. To avoid tunneling, the
thickness
of the edge loop was set to1000
. See what happens if you draw the slingshot back as far as you can (to the top of the scene) with thethickness
option removed (which sets it to the default value of50
). Goodbye ball!