fisica
Class FBody

java.lang.Object
  extended by fisica.FBody
Direct Known Subclasses:
FBlob, FBox, FCircle, FLine, FPoly

public abstract class FBody
extends java.lang.Object

Represents a body in the world. A body is an object which may collide and react to forces in the world. The bodies have many properties such as density, velocity, position, etc... with which we can control their behavior. This class cannot be be instantiated, instead use one of the derived classes.


Constructor Summary
FBody()
           
 
Method Summary
 void addForce(float fx, float fy)
          Add a force applied on the center of the body.
 void addForce(float fx, float fy, float px, float py)
          Add a force to applied to a given point of the body.
 void addTorque(float torque)
          Add a rotation force (a torque) to the body.
 void adjustAngularVelocity(float dw)
          Adjust the rotation velocity of the body.
 void adjustPosition(float dx, float dy)
          Adjust the position of the body.
 void adjustRotation(float dw)
          Adjust the rotation of the body.
 void adjustVelocity(float dvx, float dvy)
          Adjust the velocity of the body.
 void attachImage(processing.core.PImage img)
          Attach an image to the object.
 void dettachImage()
          Dettach any image that has been attached to the object.
 void draw(processing.core.PApplet applet)
           
 void draw(processing.core.PGraphics applet)
          This method is called when calling world.draw().
 float getAngularVelocity()
          Returns the rotation velocity of the body.
 java.util.ArrayList getContacts()
          Return a list of contacts currently involving the body.
 int getGroupIndex()
           
 float getImageAlpha()
          Get the opacity with which to draw the attached image.
 float getRotation()
          Returns the rotation of the body.
 java.util.ArrayList getTouching()
          Return a list of bodies currently touching the body.
 float getVelocityX()
          Returns the horizontal velocity of the body.
 float getVelocityY()
          Returns the vertical velocity of the body.
 float getX()
          Returns the horizontal position of the body.
 float getY()
          Returns the vertical position of the body.
 boolean isDrawable()
          Get whether the object must be drawn or not.
 boolean isResting()
          Indicates whether the body is in a resting state.
 boolean isSensor()
          Returns whether the body is a sensor.
 boolean isStatic()
          Returns whether the body is static.
 boolean isTouchingBody(FBody b)
          Return whether the body is currently touching the body passed as argument.
 void resetForces()
          Remove all the forces that are applied to the body.
 void setAngularDamping(float damping)
          Set the damping of the rotation movement of the body.
 void setAngularVelocity(float w)
          Set the rotation velocity of the body.
 void setBullet(boolean value)
          Set whether the body is a bullet.
 void setCategoryBits(int mask)
           
 void setDamping(float damping)
          Set the damping of the translation movement of the body.
 void setDensity(float density)
          Set the density of the body.
 void setDrawable(boolean drawable)
          Set whether the object must be drawn or not.
 void setFill(float g)
          Set the fill color of the object.
 void setFill(float g, float a)
          Set the fill color of the object.
 void setFill(float r, float g, float b)
          Set the fill color of the object.
 void setFill(float r, float g, float b, float a)
          Set the fill color of the object.
 void setFillColor(int col)
          Set the fill color of the object.
 void setFilterBits(int mask)
           
 void setForce(float fx, float fy)
          Set the force applied to the center of the body.
 void setFriction(float friction)
          Set the friction of the body.
 void setGrabbable(boolean value)
          Control if this body can be grabbed by the mouse, when clicked on.
 void setGroupIndex(int index)
          Control the group to which this body belongs.
 void setImageAlpha(float alpha)
          Set the opacity with which to draw the attached image.
 void setNoFill()
          Set that the object must be drawn without fill.
 void setNoStroke()
          Set that the object must be drawn without stroke.
 void setPosition(float x, float y)
          Set the position of the body.
 void setRestitution(float restitution)
          Set the restitution of the body.
 void setRotatable(boolean rotatable)
          Set whether the body can rotate.
 void setRotation(float w)
          Set the rotation of the body.
 void setSensor(boolean value)
          Set whether the body is a sensor.
 void setStatic(boolean value)
          Set whether the body is static.
 void setStaticBody(boolean value)
          Set whether the body is static.
 void setStroke(float g)
          Set the stroke color of the object.
 void setStroke(float g, float a)
          Set the stroke color of the object.
 void setStroke(float r, float g, float b)
          Set the stroke color of the object.
 void setStroke(float r, float g, float b, float a)
          Set the stroke color of the object.
 void setStrokeColor(int col)
          Set the stroke color of the object.
 void setStrokeWeight(float weight)
          Set the stroke weight of the object.
 void setVelocity(float vx, float vy)
          Set the velocity of the body.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

FBody

public FBody()
Method Detail

getGroupIndex

public int getGroupIndex()

setGroupIndex

public void setGroupIndex(int index)
Control the group to which this body belongs. Groups allow to select the bodies that may collide together or with others. If the group index is negative then they will not collide with each other but they will collide with all the bodies of the other groups.

Parameters:
index - the index of the group

setFilterBits

public void setFilterBits(int mask)

setCategoryBits

public void setCategoryBits(int mask)

setGrabbable

public void setGrabbable(boolean value)
Control if this body can be grabbed by the mouse, when clicked on. This property only has effect if the world is grabbable. If a body is grabbable, then it can be dragged around by the mouse.

Parameters:
value - if true and the world it belongs to is grabbable, then the body is grabbable by the mouse
See Also:
FWorld.setGrabbable(boolean)

setForce

public void setForce(float fx,
                     float fy)
Set the force applied to the center of the body.

Parameters:
fx - the x coordinate of the force
fy - the y coordinate of the force
See Also:
addForce(float,float)

addTorque

public void addTorque(float torque)
Add a rotation force (a torque) to the body.

Parameters:
torque - the value of the torque
See Also:
addForce(float,float), addForce(float,float,float,float)

addForce

public void addForce(float fx,
                     float fy)
Add a force applied on the center of the body.

Parameters:
fx - the x coordinate of the force
fy - the y coordinate of the force
See Also:
addTorque(float), addForce(float,float,float,float)

addForce

public void addForce(float fx,
                     float fy,
                     float px,
                     float py)
Add a force to applied to a given point of the body. If the force is not applied on the center of the body this force might induce a rotation change. It would be as applying a force on the center of the body and a torque.

Parameters:
fx - the x coordinate of the force
fy - the y coordinate of the force
px - the x position relative to the body's center, where to apply the force
py - the y position relative to the body's center, where to apply the force
See Also:
addTorque(float), addForce(float,float,float,float)

resetForces

public void resetForces()
Remove all the forces that are applied to the body.


getVelocityX

public float getVelocityX()
Returns the horizontal velocity of the body.

Returns:
the horizontal velocity of the body in pixels per second

getVelocityY

public float getVelocityY()
Returns the vertical velocity of the body.

Returns:
the vertical velocity of the body in pixels per second

setVelocity

public void setVelocity(float vx,
                        float vy)
Set the velocity of the body.

Parameters:
vx - the horizontal velocity of the body in pixels per second
vy - the vertical velocity of the body in pixels per second

adjustVelocity

public void adjustVelocity(float dvx,
                           float dvy)
Adjust the velocity of the body.

Parameters:
dvx - the horizontal velocity to be added to the body in pixels per second
dvy - the vertical velocity to be added to the body in pixels per second

getX

public float getX()
Returns the horizontal position of the body.

Returns:
the horizontal position of the body in pixels
See Also:
getY(), setPosition(float,float)

getY

public float getY()
Returns the vertical position of the body.

Returns:
the vertical position of the body in pixels
See Also:
getX(), setPosition(float,float)

setPosition

public void setPosition(float x,
                        float y)
Set the position of the body.

Parameters:
x - the horizontal position of the body in pixels
y - the vertical position of the body in pixels

adjustPosition

public void adjustPosition(float dx,
                           float dy)
Adjust the position of the body.

Parameters:
dx - the horizontal position to be added to the body in pixels
dy - the vertical position to be added to the body in pixels

getRotation

public float getRotation()
Returns the rotation of the body.

Returns:
the rotation of the body in radians
See Also:
setRotation(float)

setRotation

public void setRotation(float w)
Set the rotation of the body.

Parameters:
w - the rotation of the body in radians
See Also:
getRotation()

adjustRotation

public void adjustRotation(float dw)
Adjust the rotation of the body.

Parameters:
dw - the rotation to be added to the body in radians
See Also:
getRotation(), setRotation(float)

isResting

public boolean isResting()
Indicates whether the body is in a resting state. The resting state of a body is reached when it has not moved or has not received any forces nor collisions for some time.

Returns:
true if the body is resting

getAngularVelocity

public float getAngularVelocity()
Returns the rotation velocity of the body.

Returns:
the rotation velocity of the body in radians per second
See Also:
setAngularVelocity(float), adjustAngularVelocity(float)

setAngularVelocity

public void setAngularVelocity(float w)
Set the rotation velocity of the body.

Parameters:
w - the rotation velocity of the body in radians per second

adjustAngularVelocity

public void adjustAngularVelocity(float dw)
Adjust the rotation velocity of the body.

Parameters:
dw - the rotation velocity to be added to the body in radians per second
See Also:
getAngularVelocity(), setAngularVelocity(float)

setAngularDamping

public void setAngularDamping(float damping)
Set the damping of the rotation movement of the body. The damping constantly reduces the rotation velocity of the body.

Parameters:
damping - the damping of the rotation movement of the body
See Also:
setDamping(float)

setDamping

public void setDamping(float damping)
Set the damping of the translation movement of the body. The damping constantly reduces the translation velocity of the body.

Parameters:
damping - the damping of the translation movement of the body
See Also:
setAngularDamping(float)

setDensity

public void setDensity(float density)
Set the density of the body. The density will determine the total mass of the body and thus it's behavior with respect to collisions, bounces, inertia, joints,... When the density is set, the mass of the body is recalculated automatically given it's area. Note that a density of 0.0 corresponds to a mass of 0.0 independently of the area and the body will be considered static.

Parameters:
density - the density of the body

setSensor

public void setSensor(boolean value)
Set whether the body is a sensor. Sensor bodies act as normal bodies in the sense that they notify about contacts, however they do not collide with other bodies (they act like ghost bodies).

Parameters:
value - if true the body will be a sensor. It will not collide when enters contact with other bodies

isSensor

public boolean isSensor()
Returns whether the body is a sensor. Sensor bodies act as normal bodies in the sense that they notify about contacts, however they do not collide with other bodies (they act like ghost bodies).

Returns:
if true the body is a sensor. It will not collide when enters contact with other bodies

setStaticBody

public void setStaticBody(boolean value)
Set whether the body is static. Static bodies do not move or rotate, unless done manually using setPosition or setRotation.

Parameters:
value - if true the body will be static

setStatic

public void setStatic(boolean value)
Set whether the body is static. Static bodies do not move or rotate, unless done manually using setPosition or setRotation.

Parameters:
value - if true the body will be static

isStatic

public boolean isStatic()
Returns whether the body is static. Static bodies do not move or rotate, unless done manually using setPosition or setRotation.

Returns:
if true the body is static

setBullet

public void setBullet(boolean value)
Set whether the body is a bullet. Bullet bodies are computationally more expensive but more accurate in their movement. Use this only with fast objects.

Parameters:
value - if true the body will be a bullet

setRestitution

public void setRestitution(float restitution)
Set the restitution of the body. The restitution determines the ratio of the reaction force normal to a contact, when the body collides with another body. Basically it can be seen as a coefficient that will control the strength with which the body bounces back from a collision. The resititution of a contact of two bodies in a collision is calculated as the maximum of the restitution values of the 2 bodies involved.

Parameters:
restitution - a positive value. A value of 0 means no bounce after a collision, and a value of 1 means bounce with it's full speed from a collision

setFriction

public void setFriction(float friction)
Set the friction of the body. The friction determines the ratio of the reaction force tangent to a contact, when the body collides with another body. Basically it can be seen as a coefficient that will control how the body gets slown down when the body slides against another body. The friction of a contact of two bodies in a collision is calculated from the friction values of the 2 bodies involved.

Parameters:
friction - a positive value. A value of 0 means no friction and thus the body will not be slown down if no other forces are applied

setRotatable

public void setRotatable(boolean rotatable)
Set whether the body can rotate.

Parameters:
rotatable - if true the body will not rotate

getTouching

public java.util.ArrayList getTouching()
Return a list of bodies currently touching the body.

Returns:
list of bodies (ArrayList of FBody) touching the body

getContacts

public java.util.ArrayList getContacts()
Return a list of contacts currently involving the body.

Returns:
list of contacts (ArrayList of FContact) touching the body

isTouchingBody

public boolean isTouchingBody(FBody b)
Return whether the body is currently touching the body passed as argument.

Parameters:
b - the body for which we want to know if there is contact
Returns:
if true the body is touching b

draw

public void draw(processing.core.PGraphics applet)
This method is called when calling world.draw(). This method may be overriden to allow custom drawing of the object.

Parameters:
applet - the applet onto which the object must be drawn.

draw

public void draw(processing.core.PApplet applet)

attachImage

public void attachImage(processing.core.PImage img)
Attach an image to the object. This method allows to draw an image onto the screen instead of calling the draw(PApplet) method.

Parameters:
img - the PImage to attach to the object.

dettachImage

public void dettachImage()
Dettach any image that has been attached to the object.

See Also:
attachImage(PImage)

getImageAlpha

public float getImageAlpha()
Get the opacity with which to draw the attached image.

Returns:
the opacity, a value from 0.0 to 1.0 with which to draw the attached image
See Also:
attachImage(PImage), setImageAlpha(float)

setImageAlpha

public void setImageAlpha(float alpha)
Set the opacity with which to draw the attached image.

Parameters:
alpha - the opacity, a value from 0.0 to 1.0 with which to draw the attached image
See Also:
attachImage(PImage), getImageAlpha()

setDrawable

public void setDrawable(boolean drawable)
Set whether the object must be drawn or not.

Parameters:
drawable - if true the object will be drawn, else it will not
See Also:
isDrawable()

isDrawable

public boolean isDrawable()
Get whether the object must be drawn or not.

Returns:
drawable if true the object will be drawn, else it will not
See Also:
setDrawable(boolean)

setFillColor

public void setFillColor(int col)
Set the fill color of the object. This method must be used in conjunction with Processing's color(). In most cases users will find it more convenient to use the versions of setFill(float), setFill(float,float), setFill(float,float,float) or setFill(float,float,float,float)

Parameters:
col - a Processing color type. e.g. myBody.setFillColor(color(20,100,30,90));
See Also:
setNoFill(), setFill(float), setFill(float,float), setFill(float,float,float), setFill(float,float,float,float)

setNoFill

public void setNoFill()
Set that the object must be drawn without fill.

See Also:
setFill(float), setFill(float,float), setFill(float,float,float), setFill(float,float,float,float)

setFill

public void setFill(float g)
Set the fill color of the object.

Parameters:
g - gray value
See Also:
setFill(float), setFill(float,float), setFill(float,float,float), setFill(float,float,float,float)

setFill

public void setFill(float g,
                    float a)
Set the fill color of the object.

Parameters:
g - gray value
a - alpha (opacity) value
See Also:
setFill(float), setFill(float,float), setFill(float,float,float), setFill(float,float,float,float)

setFill

public void setFill(float r,
                    float g,
                    float b)
Set the fill color of the object.

Parameters:
r - red value
g - green value
b - blue value
See Also:
setFill(float), setFill(float,float), setFill(float,float,float), setFill(float,float,float,float)

setFill

public void setFill(float r,
                    float g,
                    float b,
                    float a)
Set the fill color of the object.

Parameters:
r - red value
g - green value
b - blue value
a - alpha (opacity) value
See Also:
setFill(float), setFill(float,float), setFill(float,float,float), setFill(float,float,float,float)

setStrokeColor

public void setStrokeColor(int col)
Set the stroke color of the object. This method must be used in conjunction with Processing's color(). In most cases users will find it more convenient to use the versions of setStroke(float), setStroke(float,float), setStroke(float,float,float) or setStroke(float,float,float,float)

Parameters:
col - a Processing color type. e.g. myBody.setStrokeColor(color(20,100,30,90));
See Also:
setNoStroke(), setStroke(float), setStroke(float,float), setStroke(float,float,float), setStroke(float,float,float,float)

setNoStroke

public void setNoStroke()
Set that the object must be drawn without stroke.

See Also:
setStroke(float), setStroke(float,float), setStroke(float,float,float), setStroke(float,float,float,float)

setStroke

public void setStroke(float g)
Set the stroke color of the object.

Parameters:
g - gray value
See Also:
setStroke(float), setStroke(float,float), setStroke(float,float,float), setStroke(float,float,float,float)

setStroke

public void setStroke(float g,
                      float a)
Set the stroke color of the object.

Parameters:
g - gray value
a - alpha (opacity) value
See Also:
setStroke(float), setStroke(float,float), setStroke(float,float,float), setStroke(float,float,float,float)

setStroke

public void setStroke(float r,
                      float g,
                      float b)
Set the stroke color of the object.

Parameters:
r - red value
g - green value
b - blue value
See Also:
setStroke(float), setStroke(float,float), setStroke(float,float,float), setStroke(float,float,float,float)

setStroke

public void setStroke(float r,
                      float g,
                      float b,
                      float a)
Set the stroke color of the object.

Parameters:
r - red value
g - green value
b - blue value
a - alpha (opacity) value
See Also:
setStroke(float), setStroke(float,float), setStroke(float,float,float), setStroke(float,float,float,float)

setStrokeWeight

public void setStrokeWeight(float weight)
Set the stroke weight of the object.

Parameters:
weight - weight value in pixels
See Also:
setStroke(float), setStroke(float,float), setStroke(float,float,float), setStroke(float,float,float,float)


processing library fisica by Ricard Marxer. (c) 2009