## Thrust in Box2D

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Davidobot
Party member
Posts: 1226
Joined: Sat Mar 31, 2012 5:18 am
Location: Oxford, UK
Contact:

### Thrust in Box2D

So, I have a square that I want to apply force to so that the force applied is always perpendicular to the base of the square. How would one accomplish this? See the diagram:
PM me on here or elsewhere if you'd like to discuss porting your game to Nintendo Switch via mazette!
personal page and a raycaster
vrld
Party member
Posts: 917
Joined: Sun Apr 04, 2010 9:14 pm
Location: Germany
Contact:

### Re: Thrust in Box2D

Rotate the force vector according to the rotation of the shape: http://en.wikipedia.org/wiki/Rotation_m ... dimensions. In code:

Code: Select all

function rotate(rot, x,y)
local s,c = math.sin(rot), math.cos(rot)
return c*x - s*y, s*x + c*y
end
[shameless-plug]Or use hump.vector that includes this function.[/shameless-plug]
I have come here to chew bubblegum and kick ass... and I'm all out of bubblegum.

hump | HC | SUIT | moonshine
chezrom
Citizen
Posts: 59
Joined: Tue May 28, 2013 11:03 pm
Location: France

### Re: Thrust in Box2D

Sorry vrld for your hump.vector library, but Box2D has this functions ...

You can use body:getWorldVector(local_x,local_y) => world_x,world_y to transform a vector is the body/local coordinate to the world coordinate.
The local vector is simple to determine because your square shape is always aligned with the axes of the local coordinates.

Also use body:getWorldPoint(local_x,local_y) to determine where you applicate your force (by default it is applied to the center of mass, that is different of the origin of the local coordinates).
Davidobot
Party member
Posts: 1226
Joined: Sat Mar 31, 2012 5:18 am
Location: Oxford, UK
Contact:

### Re: Thrust in Box2D

How would one use this with Body:applyForce?
PM me on here or elsewhere if you'd like to discuss porting your game to Nintendo Switch via mazette!
personal page and a raycaster
chezrom
Citizen
Posts: 59
Joined: Tue May 28, 2013 11:03 pm
Location: France

### Re: Thrust in Box2D

For example :

Code: Select all

body:applyForce(body:getWorldVector(local_fx,local_fy))
The force is applied to the centre of mass, as show in your drawing.
You can have, if your thrust is directed down so the reaction force is up local_fx = 0 and local_fy= - gravity * factor
You must call applyForce at each update when the thrust is here.
Davidobot
Party member
Posts: 1226
Joined: Sat Mar 31, 2012 5:18 am
Location: Oxford, UK
Contact:

### Re: Thrust in Box2D

chezrom wrote:For example :

Code: Select all

body:applyForce(body:getWorldVector(local_fx,local_fy))
The force is applied to the centre of mass, as show in your drawing.
You can have, if your thrust is directed down so the reaction force is up local_fx = 0 and local_fy= - gravity * factor
You must call applyForce at each update when the thrust is here.
Thank you! Works flawlessly.
PM me on here or elsewhere if you'd like to discuss porting your game to Nintendo Switch via mazette!
personal page and a raycaster

### Who is online

Users browsing this forum: Google [Bot] and 61 guests