public abstract class Body extends Object
Fixture
s in a fixed
configuration. Each body belongs to a particular World
in which it is
simulated. A world typically contains many bodies, which interact with each
other.
The simulation handles bodies bouncing off each other and sliding past each other with friction. Other effects can be obtained by using addCollisionListener() to arrange for application code to be called when this body collides with any other body in the world.
Modifier and Type | Method and Description |
---|---|
void |
addCollisionListener(CollisionListener listener)
Register a listener for collisions with this body.
|
void |
addDestructionListener(DestructionListener listener)
Register a listener for destruction of this body.
|
AttachedImage |
addImage(BodyImage image)
Convenience method to attach an instance of an image.
|
boolean |
contains(Vec2 p)
Is the point inside any of the fixtures of this body?
|
void |
destroy()
Remove this body from the world and notify all registered
DestructionListener s. |
float |
getAngle()
The angle of rotation of this body relative to its original state.
|
float |
getAngleDegrees()
The angle of rotation of this body relative to its original state.
|
float |
getAngularVelocity()
The angular (rotational) velocity of this body about its centre of mass.
|
List<Body> |
getBodiesInContact()
A list of all the other bodies currently in contact with this body.
|
Color |
getFillColor()
Get the colour used to paint the interior of this body's composite shape
when no image is attached.
|
List<Fixture> |
getFixtureList()
Get the constituent fixtures of this body.
|
List<AttachedImage> |
getImages()
The images used to display this body.
|
Vec2 |
getLinearVelocity()
The linear (straight line) velocity of the centre of mass of this body.
|
Color |
getLineColor()
Get the colour used to paint the outline of this body's composite shape
when no image is attached (or if alwaysOutline is true).
|
float |
getMass()
The total mass of this body.
|
String |
getName()
Get the name for this body (used mostly for debugging).
|
Vec2 |
getPosition()
The current position in the world of this body's origin point.
|
World |
getWorld()
The world in which this body exists.
|
boolean |
intersects(Body b)
Do any of the fixtures attached to this body currently overlap any of the
fixtures attached to a given other body.
|
boolean |
intersects(Vec2 centre,
float halfWidth,
float halfHeight)
Do any of the fixtures attached to this body currently overlap a given
axis-aligned rectangle.
|
boolean |
isAlwaysOutline()
Should the outline composite shape of this body always be drawn, even
when an image is attached?
|
boolean |
isClipped()
Whether the images will be clipped to the body's composite shape or
displayed in full (the default).
|
void |
move(Vec2 dp)
Move this body relative to its current position in the world.
|
void |
putOn(Body other)
Put this body on top of another body.
|
void |
putOn(float x,
Body other)
Put this body at the specified x-coordinate on top of another body.
|
void |
removeAllCollisionListeners()
Deregister all collision listeners.
|
void |
removeAllDestructionListeners()
Deregister all destruction listeners.
|
void |
removeAllImages()
Remove all images from this body.
|
void |
removeAttachedImage(AttachedImage image)
Remove an image instance from this body.
|
void |
removeCollisionListener(CollisionListener listener)
Deregister a listener for collisions with this body.
|
void |
removeDestructionListener(DestructionListener listener)
Deregister a listener for destruction of this body.
|
void |
rotate(float dTheta)
Rotate this body from its current orientation around its origin.
|
void |
rotate(Vec2 p,
float dTheta)
Rotate this body from its current orientation around a specified point.
|
void |
rotateDegrees(float dTheta)
Rotate this body from its current orientation around its origin.
|
void |
rotateDegrees(Vec2 p,
float dTheta)
Rotate this body from its current orientation around a specified point.
|
void |
setAlwaysOutline(boolean b)
Set the alwaysOutline attribute.
|
void |
setAngle(float theta)
Set the angle of rotation of this body relative to its original state.
|
void |
setAngleDegrees(float theta)
Set the angle of rotation of this body relative to its original state.
|
void |
setClipped(boolean clipped)
Determine whether the images will be clipped to the body's composite
shape or displayed in full (the default).
|
void |
setFillColor(Color fillColor)
Set the colour used to paint the interior of this body's composite shape
when no image is attached.
|
void |
setLineColor(Color lineColor)
Set the colour used to paint the outline of this body's composite shape
when no image is attached (or if alwaysOutline is true).
|
void |
setName(String name)
Set a name for this body.
|
void |
setPosition(Vec2 position)
Set the position in the world of this body's origin point.
|
String |
toString()
A string describing this body.
|
public void addCollisionListener(CollisionListener listener)
listener
- the listener to addpublic void removeCollisionListener(CollisionListener listener)
listener
- the listener to removepublic void removeAllCollisionListeners()
public AttachedImage addImage(BodyImage image)
b.addImage(image)is equivalent to
new AttachedImage(b, image, 1f, 0f, new Vec2())If a body has more than one image, an image added later will be drawn in front of any added earlier.
image
- the image of which to attach an instanceAttachedImage.AttachedImage(Body, BodyImage, float, float, org.jbox2d.common.Vec2)
public void removeAttachedImage(AttachedImage image)
image
- the image instance to removepublic void removeAllImages()
public List<AttachedImage> getImages()
public void setClipped(boolean clipped)
clipped
- the new value of the clipped attributepublic boolean isClipped()
public void setName(String name)
name
- the new namepublic String getName()
public void addDestructionListener(DestructionListener listener)
listener
- the listener to addpublic void removeDestructionListener(DestructionListener listener)
listener
- the listener to removepublic void removeAllDestructionListeners()
public void destroy()
DestructionListener
s.
Destruction listeners are notified immediately (before any additional
cleanup actions are performed and before the body is removed from the
World
).
This body must not be accessed after being destroyed.public List<Fixture> getFixtureList()
public String toString()
public World getWorld()
public Vec2 getPosition()
public void move(Vec2 dp)
dp
- the vector to add to this body's current positionpublic void setPosition(Vec2 position)
position
- the new positionpublic Vec2 getLinearVelocity()
public float getAngularVelocity()
public float getAngle()
public float getAngleDegrees()
public void setAngle(float theta)
theta
- the new angle (in radians; positive is counter-clockwise)public void setAngleDegrees(float theta)
theta
- the new angle (in degrees; positive is counter-clockwise)public void rotate(Vec2 p, float dTheta)
p
- the point about which to rotatedTheta
- the angle by which to rotate (in radians; positive is
counter-clockwise)public void rotate(float dTheta)
dTheta
- the angle by which to rotate (in radians; positive is
counter-clockwise)public void rotateDegrees(Vec2 p, float dTheta)
p
- the point about which to rotate the bodydTheta
- the angle by which to rotate (in degrees; positive is
counter-clockwise)public void rotateDegrees(float dTheta)
dTheta
- the angle by which to rotate (in degrees; positive is
counter-clockwise)public void putOn(Body other)
other
- the body this one is to be placed onIllegalArgumentException
- if either body is empty (no solid
fixtures), in which case the body is not movedpublic void putOn(float x, Body other)
x
- the x-coordinate of the point of contactother
- the body this one is to be placed onIllegalArgumentException
- if either body is empty (no solid
fixtures), in which case the body is not movedpublic Color getFillColor()
public void setFillColor(Color fillColor)
fillColor
- the fill colourpublic Color getLineColor()
isAlwaysOutline()
public void setLineColor(Color lineColor)
lineColor
- the line coloursetAlwaysOutline(boolean)
public boolean isAlwaysOutline()
public void setAlwaysOutline(boolean b)
b
- the new value for the alwaysOutline attributepublic boolean contains(Vec2 p)
p
- a point in world coordinatespublic boolean intersects(Body b)
b
- the other bodypublic boolean intersects(Vec2 centre, float halfWidth, float halfHeight)
centre
- the centre of the rectangle in world coordinateshalfWidth
- the half width of the rectangle in world unitshalfHeight
- the half height of the rectangle in world unitspublic float getMass()