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:
Thrust in Box2D
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
Thrust in Box2D
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
personal page and a raycaster
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:
[shameless-plug]Or use hump.vector that includes this function.[/shameless-plug]
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
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).
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).
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
personal page and a raycaster
Re: Thrust in Box2D
For example :
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.
Code: Select all
body:applyForce(body:getWorldVector(local_fx,local_fy))
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.
Re: Thrust in Box2D
Thank you! Works flawlessly.chezrom wrote:For example :The force is applied to the centre of mass, as show in your drawing.Code: Select all
body:applyForce(body:getWorldVector(local_fx,local_fy))
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.
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
personal page and a raycaster
Who is online
Users browsing this forum: No registered users and 23 guests