|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectorg.jbox2d.p5.Physics
public class Physics
A wrapper class to make using JBox2d with Processing dead simple.
The basics...
Required import:
import org.jbox2d.p5.*;Optional imports (if you're doing anything substantial with the physical objects, you'll want some or all of these), ordered by the likelihood of your needing them:
import org.jbox2d.common.*; import org.jbox2d.dynamics.*; import org.jbox2d.dynamics.joints.*; import org.jbox2d.collision.*; import org.jbox2d.dynamics.contacts.*;The above files make up the full JBox2d library. If you're worried about importing too much stuff, you're welcome to figure out exactly what you do and don't need, but there are way too many class files to go through individually here.
Physics physics = new Physics(this, sketchWidth, sketchHeight);You do not need to add anything special to your draw loop to cause the physics to be simulated and/or rendered. This is done automatically.
physics.destroy();To create a static (immobile) body:
physics.setDensity(0.0f); physics.createRect(x0, y0, x1, y1);To create a normal moving body:
physics.setDensity(someNonZeroDensity); physics.createRect(x0, y0, x1, y1);You can also setFriction(float) and setRestitution(float) to change the way your objects collide. This wrapper retains the last set values, much like Processing does with stroke and fill.
Constructor Summary | |
---|---|
Physics(processing.core.PApplet parent,
float screenW,
float screenH)
Set up a default physics world. |
|
Physics(processing.core.PApplet parent,
float screenW,
float screenH,
float gravX,
float gravY,
float screenAABBWidth,
float screenAABBHeight,
float borderBoxWidth,
float borderBoxHeight,
float pixelsPerMeter)
Set up a physics world. |
Method Summary | |
---|---|
void |
applyForce(org.jbox2d.dynamics.Body b,
float fx,
float fy)
Apply a force to the body at the center of mass. |
void |
applyForce(org.jbox2d.dynamics.Body b,
float fx,
float fy,
float pointX,
float pointY)
Apply a force to a body at a point. |
void |
applyForce(org.jbox2d.dynamics.Body b,
org.jbox2d.common.Vec2 f)
Apply a force to the body at the center of mass. |
void |
applyForce(org.jbox2d.dynamics.Body b,
org.jbox2d.common.Vec2 f,
org.jbox2d.common.Vec2 point)
Apply a force to a body at a point |
org.jbox2d.dynamics.Body |
createCircle(float x,
float y,
float r)
Create a circle in screen coordinates |
org.jbox2d.dynamics.joints.DistanceJoint |
createDistanceJoint(org.jbox2d.dynamics.Body a,
org.jbox2d.dynamics.Body b,
float xa,
float ya,
float xb,
float yb)
Create a distance (stick) joint between two bodies that holds the specified points at a constant distance. |
org.jbox2d.dynamics.joints.GearJoint |
createGearJoint(org.jbox2d.dynamics.joints.Joint pj1,
org.jbox2d.dynamics.joints.Joint pj2,
float ratio)
Create a gear joint, which binds together two existing revolute or prismatic joints (any combination will work). |
org.jbox2d.dynamics.Body[] |
createHollowBox(float centerX,
float centerY,
float width,
float height,
float thickness)
Create a hollow box of the given screen dimensions. |
org.jbox2d.dynamics.Body |
createPolygon(float... vertices)
Create a polygon based on vertices. |
org.jbox2d.dynamics.joints.PrismaticJoint |
createPrismaticJoint(org.jbox2d.dynamics.Body a,
org.jbox2d.dynamics.Body b,
float dirX,
float dirY)
Create a prismatic (piston) joint between two bodies that allows movement in the given direction. |
org.jbox2d.dynamics.joints.PulleyJoint |
createPulleyJoint(org.jbox2d.dynamics.Body a,
org.jbox2d.dynamics.Body b,
float groundAnchorAx,
float groundAnchorAy,
float groundAnchorBx,
float groundAnchorBy,
float anchorAx,
float anchorAy,
float anchorBx,
float anchorBy,
float ratio)
Create a pulley joint between the The pulley joint is connected to two bodies and two fixed ground points. |
org.jbox2d.dynamics.Body |
createRect(float x0,
float y0,
float x1,
float y1)
Create a rectangle given by screen coordinates of corners. |
org.jbox2d.dynamics.joints.RevoluteJoint |
createRevoluteJoint(org.jbox2d.dynamics.Body a,
org.jbox2d.dynamics.Body b,
float x,
float y)
Create a revolute (pin) joint between the two bodies at the given position. |
void |
defaultDraw(org.jbox2d.dynamics.World world)
Draws the scene using the default render options. |
void |
destroy()
Destroy this world, unregistering it from the PApplet. |
void |
draw()
Called automatically by Processing. |
float |
getAngle(org.jbox2d.dynamics.Body b)
Get the angle (in radians) |
org.jbox2d.dynamics.Body[] |
getBorder()
Get the border Body[] array, or null if the border has been removed. |
boolean |
getBullet()
Are newly created bodies being created as bullets? |
org.jbox2d.common.Vec2 |
getCMPosition(org.jbox2d.dynamics.Body b)
Get the center of mass position (screen coordinates) |
float |
getDensity()
Get the density being used for newly created shapes. |
float |
getFriction()
Get the friction being used for newly created shapes. |
org.jbox2d.common.Vec2 |
getPosition(org.jbox2d.dynamics.Body b)
Get the location of the body's origin (screen coordinates) - note that this does not usually correspond to the center of mass position, which may be obtained by calling getCMPosition(Body) . |
float |
getRestitution()
Get the restitution being used for newly created shapes. |
boolean |
getSensor()
Are newly created shapes being created as sensors? |
org.jbox2d.testbed.TestSettings |
getSettings()
Get an editable copy of the current TestSettings so that you may change certain aspects of the simulation and display. |
org.jbox2d.dynamics.World |
getWorld()
Get the current physics world. |
void |
removeBody(org.jbox2d.dynamics.Body b)
Remove a body from the world. |
void |
removeBorder()
Remove the solid border if it exists. |
void |
removeJoint(org.jbox2d.dynamics.joints.Joint j)
Remove a joint from the world. |
float |
screenToWorld(float length)
Screen space to world space conversion for length. |
org.jbox2d.common.Vec2 |
screenToWorld(float x,
float y)
Screen space to world space conversion for position. |
org.jbox2d.common.Vec2 |
screenToWorld(org.jbox2d.common.Vec2 v)
Screen space to world space conversion for position. |
org.jbox2d.common.Vec2 |
screenToWorldVector(float sx,
float sy)
|
org.jbox2d.common.Vec2 |
screenToWorldVector(org.jbox2d.common.Vec2 screenV)
|
float |
screenToWorldX(float x,
float y)
Screen space to world space conversion for position. |
float |
screenToWorldY(float x,
float y)
Screen space to world space conversion for position. |
void |
setBullet(boolean bullet)
Set to true to create new bodies as "bullets," which use (slower) continuous collision detection against other moving bodies. |
void |
setCustomRenderingMethod(java.lang.Object object,
java.lang.String methodName)
For advanced users only. |
void |
setDensity(float d)
Set the density used for newly created shapes. |
void |
setFriction(float f)
Set the friction used for newly created shapes. |
void |
setRestitution(float r)
Set the restitution used for newly created shapes. |
void |
setSensor(boolean sensor)
Set to true to create new shapes as sensors. |
void |
unsetCustomRenderingMethod()
Clear any custom rendering method that has been set, and revert to the default Box2d debug renderer. |
float |
worldToScreen(float length)
World space to screen space conversion for length. |
org.jbox2d.common.Vec2 |
worldToScreen(float x,
float y)
World space to screen space conversion for position. |
org.jbox2d.common.Vec2 |
worldToScreen(org.jbox2d.common.Vec2 v)
World space to screen space conversion for position. |
org.jbox2d.common.Vec2 |
worldToScreenVector(float wx,
float wy)
|
org.jbox2d.common.Vec2 |
worldToScreenVector(org.jbox2d.common.Vec2 worldV)
|
float |
worldToScreenX(float x,
float y)
World space to screen space conversion for position. |
float |
worldToScreenY(float x,
float y)
World space to screen space conversion for position. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
---|
public Physics(processing.core.PApplet parent, float screenW, float screenH)
parent
- public Physics(processing.core.PApplet parent, float screenW, float screenH, float gravX, float gravY, float screenAABBWidth, float screenAABBHeight, float borderBoxWidth, float borderBoxHeight, float pixelsPerMeter)
parent
- The PApplet this physics world should usegravX
- The x component of gravity, in meters/sec^2gravY
- The y component of gravity, in meters/sec^2screenAABBWidth
- The world's width, in pixels - should be significantly larger than the area you intend to usescreenAABBHeight
- The world's height, in pixels - should be significantly larger than the area you intend to useborderBoxWidth
- The containing box's width - should be smaller than the world width, so that no object can escapeborderBoxHeight
- The containing box's height - should be smaller than the world height, so that no object can escapepixelsPerMeter
- Pixels per physical meterMethod Detail |
---|
public void draw()
public void setCustomRenderingMethod(java.lang.Object object, java.lang.String methodName)
void myDrawMethod(World world) { // Do a bunch of stuff }then inside either the draw or setup functions you could write:
setCustomRenderingMethod(this, "myDrawMethod");to register that function. Use unsetCustomRenderingMethod() to go back to the default renderer.
object
- The object in which your method is defined ('this' should work if
the object is defined in a .pde file and not within a class)methodName
- The name of the method (without the parenthesis) to callpublic void unsetCustomRenderingMethod()
public void defaultDraw(org.jbox2d.dynamics.World world)
public org.jbox2d.testbed.TestSettings getSettings()
public int hz; // "frame" rate of physics simulation - best to leave at 60 public int iterationCount; // number of constraint iterations - set to 10 normally public boolean enableWarmStarting; // makes constraints work better by reusing last results public boolean enablePositionCorrection; // leave this on...without it, things turn to mush public boolean enableTOI; // enable/disable continuous collision detection public boolean drawShapes; public boolean drawJoints; public boolean drawCoreShapes; public boolean drawOBBs; public boolean drawCOMs; public boolean drawImpulses; public boolean drawAABBs; public boolean drawPairs; public boolean drawContactPoints; public boolean drawContactNormals; public boolean drawContactForces; public boolean drawFrictionForces;Note: the drawing settings only affect the default debug renderer. If you have specified your own renderer, you will have to manually read off and apply these settings if you wish to use them.
public org.jbox2d.dynamics.Body[] createHollowBox(float centerX, float centerY, float width, float height, float thickness)
centerX
- Center of box x coordinate (in screen coordinates)centerY
- Center of box y coordinate (in screen coordinates)width
- Width of box (screen scale)height
- Height of box (screen scale)thickness
- Thickness of box edge (screen scale)
public org.jbox2d.dynamics.Body createRect(float x0, float y0, float x1, float y1)
x0
- y0
- x1
- y1
-
public org.jbox2d.dynamics.Body createCircle(float x, float y, float r)
x
- y
- r
-
public org.jbox2d.dynamics.Body createPolygon(float... vertices)
createPolygon(x0,y0,x1,y1,x2,y2,x3,y3);or
createPolygon(x0,y0,x1,y1,x2,y2,x3,y3,x4,y4,x5,y5);or
float[] xyInterleaved = {x0,y0,x1,y1,x2,y2,x3,y3,x4,y4}; createPolygon(xyInterleaved);are all fine.
vertices
- Any number of pairs of x,y floats, or an array of the same (screen coordinates)
public org.jbox2d.dynamics.joints.DistanceJoint createDistanceJoint(org.jbox2d.dynamics.Body a, org.jbox2d.dynamics.Body b, float xa, float ya, float xb, float yb)
a
- First bodyb
- Second bodyxa
- x component of anchor point on first body (screen coordinates)ya
- y component of anchor point on first body (screen coordinates)xb
- x component of anchor point on second body (screen coordinates)yb
- y component of anchor point on second body (screen coordinates)
public org.jbox2d.dynamics.joints.RevoluteJoint createRevoluteJoint(org.jbox2d.dynamics.Body a, org.jbox2d.dynamics.Body b, float x, float y)
a
- First bodyb
- Second bodyx
- x coordinate of pin joint location (screen coordinates)y
- y coordinate of pin joint location (screen coordinates)
public org.jbox2d.dynamics.joints.PrismaticJoint createPrismaticJoint(org.jbox2d.dynamics.Body a, org.jbox2d.dynamics.Body b, float dirX, float dirY)
a
- First bodyb
- Second bodydirX
- x component of allowed movement directiondirY
- y component of allowed movement direction
public org.jbox2d.dynamics.joints.PulleyJoint createPulleyJoint(org.jbox2d.dynamics.Body a, org.jbox2d.dynamics.Body b, float groundAnchorAx, float groundAnchorAy, float groundAnchorBx, float groundAnchorBy, float anchorAx, float anchorAy, float anchorBx, float anchorBy, float ratio)
a
- First bodyb
- Second bodygroundAnchorAx
- x coordinate of (fixed) ground anchor for body a, in screen coordinatesgroundAnchorAy
- y coordinate of (fixed) ground anchor for body a, in screen coordinatesgroundAnchorBx
- x coordinate of (fixed) ground anchor for body b, in screen coordinatesgroundAnchorBy
- y coordinate of (fixed) ground anchor for body b, in screen coordinatesanchorAx
- x coordinate of body anchor for body a, in screen coordinatesanchorAy
- y coordinate of body anchor for body a, in screen coordinatesanchorBx
- x coordinate of body anchor for body b, in screen coordinatesanchorBy
- y coordinate of body anchor for body b, in screen coordinatesratio
- "Block and tackle" ratio
public org.jbox2d.dynamics.joints.GearJoint createGearJoint(org.jbox2d.dynamics.joints.Joint pj1, org.jbox2d.dynamics.joints.Joint pj2, float ratio)
pj1
- First joint (revolute or prismatic)pj2
- Second joint (revolute or prismatic)ratio
- Gear ratio
public void setDensity(float d)
d
- public float getDensity()
public void setRestitution(float r)
r
- public float getRestitution()
public void setFriction(float f)
f
- public float getFriction()
public void setBullet(boolean bullet)
bullet
- public boolean getBullet()
public void setSensor(boolean sensor)
sensor
- public boolean getSensor()
public void destroy()
public org.jbox2d.dynamics.World getWorld()
public org.jbox2d.dynamics.Body[] getBorder()
public void removeBorder()
public void removeBody(org.jbox2d.dynamics.Body b)
public void removeJoint(org.jbox2d.dynamics.joints.Joint j)
public void applyForce(org.jbox2d.dynamics.Body b, float fx, float fy)
b
- Body you wish to apply force tofx
- x component of force (in pixel units)fy
- y component of force (in pixel units)public void applyForce(org.jbox2d.dynamics.Body b, org.jbox2d.common.Vec2 f)
b
- Body you wish to apply force tof
- force to apply (in pixel units)public void applyForce(org.jbox2d.dynamics.Body b, float fx, float fy, float pointX, float pointY)
b
- Body you wish to apply force tofx
- x component of force (in pixel units)fy
- y component of force (in pixel units)pointX
- x coordinate of application point (in screen/pixel coordinates)pointY
- y coordinate of application point (in screen/pixel coordinates)public void applyForce(org.jbox2d.dynamics.Body b, org.jbox2d.common.Vec2 f, org.jbox2d.common.Vec2 point)
b
- Body you wish to apply force tof
- force to apply (in pixel units)point
- application point (in screen/pixel coordinates)public org.jbox2d.common.Vec2 getPosition(org.jbox2d.dynamics.Body b)
getCMPosition(Body)
.
b
-
public org.jbox2d.common.Vec2 getCMPosition(org.jbox2d.dynamics.Body b)
public float getAngle(org.jbox2d.dynamics.Body b)
public float screenToWorldX(float x, float y)
public float screenToWorldY(float x, float y)
public org.jbox2d.common.Vec2 screenToWorld(float x, float y)
public org.jbox2d.common.Vec2 screenToWorld(org.jbox2d.common.Vec2 v)
public float worldToScreenX(float x, float y)
public float worldToScreenY(float x, float y)
public org.jbox2d.common.Vec2 worldToScreen(float x, float y)
public float worldToScreen(float length)
public org.jbox2d.common.Vec2 worldToScreen(org.jbox2d.common.Vec2 v)
public float screenToWorld(float length)
public org.jbox2d.common.Vec2 screenToWorldVector(org.jbox2d.common.Vec2 screenV)
public org.jbox2d.common.Vec2 screenToWorldVector(float sx, float sy)
public org.jbox2d.common.Vec2 worldToScreenVector(org.jbox2d.common.Vec2 worldV)
public org.jbox2d.common.Vec2 worldToScreenVector(float wx, float wy)
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |