object:applyForce()

Type Function
Object Body
Library physics.*
Return value none
Revision Release 2024.3703
Keywords body, applyForce
See also object:applyLinearImpulse()
object:applyTorque()
object:applyAngularImpulse()

Overview

A function that accepts x and y components of a linear force, applied at a given point with x and y world coordinates. If the target point is the body’s center of mass, it will tend to push the body in a straight line; if the target is offset from the body’s center of mass, the body will spin about its center of mass.

For symmetrical objects, the center of mass and the center of the object will have the same position (object.x and object.y).

Note that the amount of force required to move heavy objects may need to be fairly high.

Syntax

object:applyForce( xForce, yForce, bodyX, bodyY )
xForce, yForce (required)

Numbers. Amount of force to apply in the x and y directions respectively.

bodyX, bodyY (required)

Numbers. Point on the object to apply the force to.

Example

-- Create a rectangle
local myRect = display.newRect( 0, 0, 100, 100 )

-- Add a body to the rectangle
physics.addBody( myRect, "dynamic" )

-- Apply force
myRect:applyForce( 50, 200, myRect.x, myRect.y )