cljbox2d, a 2D physics engine.
A clojure wrapper for JBox2D, which is a close Java port of Erin Catto's excellent C++ Box2D physics engine.
[cljbox2d](https://github.com/floybix/cljbox2d/), a 2D physics engine. A clojure wrapper for [JBox2D](http://www.jbox2d.org/), which is a close Java port of Erin Catto's excellent C++ [Box2D](http://www.box2d.org/) physics engine.
(aabb fixt)
(aabb [x0 y0] [x1 y1])
Axis-Aligned Bounding Box
Axis-Aligned Bounding Box
(all-current-contacts world)
Lazy seq of current contacts in the world. Each contact is a map as
defined by the contact-data
function. Contacts without actual
contact points (i.e. created only due to overlapping bounding
boxes) are excluded.
Lazy seq of current contacts in the world. Each contact is a map as defined by the `contact-data` function. Contacts without actual contact points (i.e. created only due to overlapping bounding boxes) are excluded.
(alljointseq world)
Lazy seq of all joints in the world.
Lazy seq of all joints in the world.
(anchor-a jt)
The anchor point on bodyA in world coordinates
The anchor point on bodyA in world coordinates
(anchor-b jt)
The anchor point on bodyB in world coordinates
The anchor point on bodyB in world coordinates
(angular-velocity body)
Angular velocity of a body in radians/second.
Angular velocity of a body in radians/second.
(apply-force! body force pt)
Apply a force in Newtons to body 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.
Apply a force in Newtons to body 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.
(apply-impulse! body impulse pt)
Apply an impulse in N-seconds or kg-m/s at a point. This immediately modifies the velocity. It also modifies the angular velocity if the point of application is not at the center of mass. This wakes up the body.
Apply an impulse in N-seconds or kg-m/s at a point. This immediately modifies the velocity. It also modifies the angular velocity if the point of application is not at the center of mass. This wakes up the body.
(apply-torque! body torque)
Apply a torque in N-m, i.e. about the z-axis (out of the screen). This affects the angular velocity without affecting the linear velocity of the center of mass. This wakes up the body.
Apply a torque in N-m, i.e. about the z-axis (out of the screen). This affects the angular velocity without affecting the linear velocity of the center of mass. This wakes up the body.
(body! world body-spec & fixture-specs)
Creates a Body together with its Fixtures. The body-spec
map is
passed to the body-def
function. Each of the fixture-specs
are
passed to the fixture-def
function.
Creates a Body together with its Fixtures. The `body-spec` map is passed to the `body-def` function. Each of the `fixture-specs` are passed to the `fixture-def` function.
(body-def {:keys [type position angle bullet fixed-rotation gravity-scale
angular-damping linear-damping angular-velocity
linear-velocity user-data]
:or {angular-velocity 0
linear-velocity [0 0]
linear-damping 0
gravity-scale 1.0
bullet false
type :dynamic
angle 0
angular-damping 0
fixed-rotation false
position [0 0]}})
A BodyDef, which holds properties but not shapes. Do not call this
directly, instead use (body!)
.
A BodyDef, which holds properties but not shapes. Do not call this directly, instead use `(body!)`.
(body-of fixt)
Get the body to which a fixture belongs
Get the body to which a fixture belongs
(body-type body)
The body type as a keyword :dynamic
:static
or :kinematic
.
The body type as a keyword `:dynamic` `:static` or `:kinematic`.
(bodyseq world)
(bodyseq world body)
Lazy seq of all bodies in the world, or a body list
Lazy seq of all bodies in the world, or a body list
(box hx hy)
(box hx hy center)
(box hx hy center angle)
Create a box shape from half-width, half-height, by default centered at [0 0]
Create a box shape from half-width, half-height, by default centered at [0 0]
(circle radius)
(circle radius center)
Create a circle shape, by default centered at [0 0]
Create a circle shape, by default centered at [0 0]
(contact-data contact)
(contact-data contact impulses)
Returns a map with keys :fixture-a :fixture-b :points :normal :normal-impulses :tangent-impulses from a JBox2D Contact object and optional ContactImpulse object. Returns nil if no contact points exist.
Returns a map with keys :fixture-a :fixture-b :points :normal :normal-impulses :tangent-impulses from a JBox2D Contact object and optional ContactImpulse object. Returns nil if no contact points exist.
(contacting bod)
Set of other bodies that the given body is currently contacting.
Set of other bodies that the given body is currently contacting.
(current-contacts bod)
Lazy seq of contacts on this body. Each contact is a map as defined
by the contact-data
function. Contacts without actual contact
points (i.e. created only due to overlapping bounding boxes) are
excluded.
Lazy seq of contacts on this body. Each contact is a map as defined by the `contact-data` function. Contacts without actual contact points (i.e. created only due to overlapping bounding boxes) are excluded.
Abstraction for JBox2D objects which can be destroyed.
Abstraction for JBox2D objects which can be destroyed.
(destroy! this)
Remove object from the World permanantly. Destroying a body automatically deletes all associated shapes and joints.
Remove object from the World permanantly. Destroying a body automatically deletes all associated shapes and joints.
(edge pt1 pt2)
Create an edge shape, a line between two points.
Create an edge shape, a line between two points.
(edge-chain vertices)
Creates a chain of edge shapes, assumed not self-intersecting, left open at the ends.
Creates a chain of edge shapes, assumed not self-intersecting, left open at the ends.
(edge-loop vertices)
Creates a loop of edge shapes, assumed not self-intersecting, where the last point joins back to the first point.
Creates a loop of edge shapes, assumed not self-intersecting, where the last point joins back to the first point.
(fixture! body fixture-spec)
Creates a Fixture on an existing Body. The second argument is a
fixture specification map to be passed to the fixture-def
function.
Creates a Fixture on an existing Body. The second argument is a fixture specification map to be passed to the `fixture-def` function.
(fixture-def {:keys [shape density friction restitution is-sensor group-index
category-bits mask-bits user-data]
:or {density 1
friction 0.3
restitution 0.3
is-sensor false
group-index 0
category-bits 1
mask-bits 65535}})
A FixtureDef: a shape with some physical properties. Do not call
this directly, instead use (body!)
or (fixture!)
.
:group-index
allows a certain group of objects to never
collide (negative) or always collide (positive). Zero means no
collision group.
A FixtureDef: a shape with some physical properties. Do not call this directly, instead use `(body!)` or `(fixture!)`. `:group-index` allows a certain group of objects to never collide (negative) or always collide (positive). Zero means no collision group.
(fixture-of body)
Often a body will only have one fixture. This is a convenience function to pull out the first fixture from a body.
Often a body will only have one fixture. This is a convenience function to pull out the first fixture from a body.
(fixtureseq body)
Lazy seq of fixtures on a body.
Lazy seq of fixtures on a body.
(gravity! world gravity)
Sets the gravity vector.
Sets the gravity vector.
Augments the joint specification map to define body-local anchors,
axes, etc. from given initial world values (e.g. :world-anchor
).
Augments the joint specification map to define body-local anchors, axes, etc. from given initial world values (e.g. `:world-anchor`).
(joint! spec)
Creates a joint from a spec map according to its :type
key. In
most cases the map must contain :body-a
and :body-b
.
:revolute
joint constrains two bodies to share a common point
while they are free to rotate about the point. The relative
rotation about the shared point is the joint angle. You can limit
the relative rotation with a joint limit that specifies a lower
and upper angle. You can use a motor to drive the relative
rotation about the shared point. A maximum motor torque is
provided so that infinite forces are not generated.
:prismatic
joint. This requires defining a line of motion using
an axis and an anchor point. The definition uses local anchor
points and a local axis so that the initial configuration can
violate the constraint slightly. The joint translation is zero
when the local anchor points coincide in world space. Using local
anchors and a local axis helps when saving and loading a game.
:distance
joint. This requires defining an anchor point on both
bodies and the non-zero length of the distance joint. The
definition uses local anchor points so that the initial
configuration can violate the constraint slightly. This helps
when saving and loading a game. Note however that this
initialisation function uses world points. For :damping-ratio
0
= no damping; 1 = critical damping.`
:rope
joint requires two body-local anchor points and a maximum
length.
:constant-volume
joint. Connects a group a bodies together so
they maintain a constant volume within them. Uses Distance joints
internally.
:mouse
joint. By convention body-a
is ground and body-b
is
the selection.
:weld
joint. This requires defining a common anchor point on
both bodies. This initialisation function takes a world point.
Creates a joint from a spec map according to its `:type` key. In most cases the map must contain `:body-a` and `:body-b`. * `:revolute` joint constrains two bodies to share a common point while they are free to rotate about the point. The relative rotation about the shared point is the joint angle. You can limit the relative rotation with a joint limit that specifies a lower and upper angle. You can use a motor to drive the relative rotation about the shared point. A maximum motor torque is provided so that infinite forces are not generated. * `:prismatic` joint. This requires defining a line of motion using an axis and an anchor point. The definition uses local anchor points and a local axis so that the initial configuration can violate the constraint slightly. The joint translation is zero when the local anchor points coincide in world space. Using local anchors and a local axis helps when saving and loading a game. * `:distance` joint. This requires defining an anchor point on both bodies and the non-zero length of the distance joint. The definition uses local anchor points so that the initial configuration can violate the constraint slightly. This helps when saving and loading a game. *Note* however that this initialisation function uses world points. For `:damping-ratio` 0 = no damping; 1 = critical damping.` * `:rope` joint requires two body-local anchor points and a maximum length. * `:constant-volume` joint. Connects a group a bodies together so they maintain a constant volume within them. Uses Distance joints internally. * `:mouse` joint. By convention `body-a` is ground and `body-b` is the selection. * `:weld` joint. This requires defining a common anchor point on both bodies. This initialisation function takes a world point.
(joint-angular-velocity jt)
Relative angular velocity of two attached bodies in radians/second.
Relative angular velocity of two attached bodies in radians/second.
(joint-type joint)
The joint type as a keyword.
The joint type as a keyword.
(jointseq body)
Lazy seq of all joints connected to a body
Lazy seq of all joints connected to a body
Abstraction for JBox2D joints which can have limits
Abstraction for JBox2D joints which can have limits
(enable-limit! this flag)
Set whether limits are in effect.
Set whether limits are in effect.
(limit-enabled? this)
Whether limits are in effect on a joint.
Whether limits are in effect on a joint.
(limits this)
Vector of limits on a joint.
Vector of limits on a joint.
(limits! this limits)
Set [lower upper] limits on a joint.
Set [lower upper] limits on a joint.
(linear-velocity body)
(linear-velocity body loc-pt)
Linear velocity of a point on the body in local coordinates, by default its center of mass. In m/s.
Linear velocity of a point on the body in local coordinates, by default its center of mass. In m/s.
(linear-velocity-world body world-pt)
Linear velocity of a point on the body in world coordinates. In m/s.
Linear velocity of a point on the body in world coordinates. In m/s.
(local-coords fixt)
Local coordinates of polygon vertices. Approximated for circles.
Local coordinates of polygon vertices. Approximated for circles.
Abstraction for JBox2D joints which can have motors
Abstraction for JBox2D joints which can have motors
(max-motor-force! this force)
Set maximum motor force
Set maximum motor force
(max-motor-force this)
Maximum motor force
Maximum motor force
(motor-speed! this speed)
Set motor (target) speed, may or may not be enabled.
Set motor (target) speed, may or may not be enabled.
(enable-motor! this flag)
Set whether a motor is enabled.
Set whether a motor is enabled.
(motor-torque this inv-dt)
Current motor torque.
Current motor torque.
(max-motor-torque! this torque)
Set maximum motor torque
Set maximum motor torque
(motor-speed this)
Motor (target) speed, may or may not be enabled.
Motor (target) speed, may or may not be enabled.
(motor-enabled? this)
Whether a motor is enabled on a joint.
Whether a motor is enabled on a joint.
(joint-speed this)
Current joint speed, may be linear or angular.
Current joint speed, may be linear or angular.
(motor-force this inv-dt)
Current motor force.
Current motor force.
(max-motor-torque this)
Maximum motor torque
Maximum motor torque
(new-world)
(new-world gravity)
Returns a new Box2D world. Gravity defaults to [0 -10] m/s^2.
Returns a new Box2D world. Gravity defaults to [0 -10] m/s^2.
(polygon vertices)
Create a polygon shape. Must be convex! It is assumed that the exterior is the right of each edge. i.e. vertices go counter-clockwise.
Create a polygon shape. Must be convex! It is assumed that the exterior is the right of each edge. i.e. vertices go counter-clockwise.
(power-watts jt)
Instantaneous rate of work done by a revolute joint in Watts. Multiply by the time step to get work in Joules.
Instantaneous rate of work done by a revolute joint in Watts. Multiply by the time step to get work in Joules.
(query-aabb world bb)
(query-aabb world bb max-take)
Return a vector of (up to a given number of) fixtures overlapping an Axis-Aligned Bounding Box
Return a vector of (up to a given number of) fixtures overlapping an Axis-Aligned Bounding Box
(query-at-point world pt)
(query-at-point world [x y] max-take)
Return a vector of fixtures overlapping the given point. The point is tested to be inside each shape, not just within its bounding box.
Return a vector of fixtures overlapping the given point. The point is tested to be inside each shape, not just within its bounding box.
(raycast world
start-pt
end-pt
mode
&
{:keys [ignore] :or {ignore (constantly false)}})
Raycast from start-pt to end-pt, returning a sequence of fixture
intersections, each a map (record) with keys :fixture
:point
:normal
:fraction
. If mode
is :all
, all intersections are
returned in no particular order. If mode is :closest
, only the
closest intersection is returned in a single-element list, or an
empty list if none exist. If an :ignore
function is given,
fixtures returning logical true from it are ignored.
Raycast from start-pt to end-pt, returning a sequence of fixture intersections, each a map (record) with keys `:fixture` `:point` `:normal` `:fraction`. If `mode` is `:all`, all intersections are returned in no particular order. If mode is `:closest`, only the closest intersection is returned in a single-element list, or an empty list if none exist. If an `:ignore` function is given, fixtures returning logical true from it are ignored.
(reaction-force jt inv-dt)
The reaction force on bodyB at the joint anchor in Newtons. Give the inverse of the timestep.
The reaction force on bodyB at the joint anchor in Newtons. Give the inverse of the timestep.
(reaction-torque jt inv-dt)
The reaction torque on bodyB in N*m. Give the inverse of the timestep.
The reaction torque on bodyB in N*m. Give the inverse of the timestep.
(rod from-pt angle length width)
Create a long thin (box) shape extending from a point to a given length and with a given angle.
Create a long thin (box) shape extending from a point to a given length and with a given angle.
(set-buffering-contact-listener! world)
Sets a ContactListener on the world which stores contacts. Returns
an atom which will be populated with a sequence of contact-data
records. Consumer is responsible for emptying it.
Sets a ContactListener on the world which stores contacts. Returns an atom which will be populated with a sequence of `contact-data` records. Consumer is responsible for emptying it.
(shape-type fixt)
The shape type of a Fixture as a keyword e.g. :circle
or :polygon
.
The shape type of a Fixture as a keyword e.g. `:circle` or `:polygon`.
(snapshot-scene world prev-scene well-behaved?)
(snapshot-scene world prev-scene well-behaved? identify)
Returns an immutable value representation of the world for drawing,
with keys :bodies and :joints. Body snapshots and joint snapshots
are stored in two-level nested maps, allowing bodies to be grouped
together. identify
returns a 2-tuple giving the group
and (nested) body keys. The default case stores all bodies under a
single key :all
and keys them by object hash value.
If a non-nil prev-scene
is given then it is used as a basis: only
the differences are applied to form a new value, improving memory
use via structural sharing.
Argument well-behaved?
asserts that Fixtures will not change, and
that static bodies will not move: they can then be ignored for
efficiency.
Returns an immutable value representation of the world for drawing, with keys :bodies and :joints. Body snapshots and joint snapshots are stored in two-level nested maps, allowing bodies to be grouped together. `identify` returns a 2-tuple giving the group and (nested) body keys. The default case stores all bodies under a single key `:all` and keys them by object hash value. If a non-nil `prev-scene` is given then it is used as a basis: only the differences are applied to form a new value, improving memory use via structural sharing. Argument `well-behaved?` asserts that Fixtures will not change, and that static bodies will not move: they can then be ignored for efficiency.
(step! world dt)
(step! world dt velocity-iterations position-iterations)
Simulate the world for a time step given in seconds. Note that Box2D objects are locked during simulation. Returns the world (although it remains a mutable object).
Simulate the world for a time step given in seconds. Note that Box2D objects are locked during simulation. Returns the world (although it remains a mutable object).
General methods for 2D physical objects which may be aggregated
General methods for 2D physical objects which may be aggregated
(center this)
Center of mass in world coordinates.
Center of mass in world coordinates.
(loc-center this)
Center of mass in local coordinates.
Center of mass in local coordinates.
(mass this)
Total mass in kg.
Total mass in kg.
(position this)
(position this loc-pt)
World coordinates of a local point, default [0 0].
World coordinates of a local point, default [0 0].
(to-local this pt)
Local coordinates of a world point.
Local coordinates of a world point.
(to-local-vect this vect)
Local vector of a world vector.
Local vector of a world vector.
(v2xy v)
Makes a vector [x y] from a Vec2
Makes a vector [x y] from a Vec2
(vary-user-data this f & args)
Alters the userdata attached to this
by applying f
to the
existing value (or the empty map) with given args. Returns the
object.
Alters the userdata attached to `this` by applying `f` to the existing value (or the empty map) with given args. Returns the object.
(vec2 v)
(vec2 x y)
Make a JBox2D Vec2
object from given x
, y
.
Make a JBox2D `Vec2` object from given `x`, `y`.
(world-coords fixt)
World coordinates of polygon vertices. Approximated for circles.
World coordinates of polygon vertices. Approximated for circles.
cljdoc is a website building & hosting documentation for Clojure/Script libraries
× close