Emini Physics Engine 1.3.5a API Documentation

at.emini.physics2D
Class Body

java.lang.Object
  extended by at.emini.physics2D.Body

public class Body
extends java.lang.Object

The Body class represents the physical state of a body.
This comprises:

  • Position (2D)
  • Angle (1D)
  • Velocity (2D)
  • Angular Velocity (1D)
  • Shape used as a body template, containing friction, elasticity and density (->mass)
  • Each object can be static or dynamic. Static objects do not move and act like having a mass of infinity.

    Author:
    Alexander Adensamer

    Field Summary
    protected  float mAngularVelocity2FX
              Current angular velocity (2FX) of the body.
    protected  int mId
              Body id.
    protected  FXVector mPositionFX
              Position vector of the body.
    protected  float mRotation2FX
              Current angle (2FX) of the body.
    protected  Shape mShape
              The shape of the body.
    protected  UserData mUserData
              User data.
    protected  FXVector mVelocityFX
              Current velocity of the body.
    protected  float mVirtualAngularVelocity2FX
              Current Virtual angular velocity of the body.
    protected  FXVector mVirtualVelocityFX
              Current virtual velocity of the body.
     
    Constructor Summary
    Body(Body other)
              Copy constructor.
    Body(FXVector pos, Shape shape, boolean dynamic)
              Constructor.
    Body(int x, int y, Shape shape, boolean dynamic)
              Constructor.
     
    Method Summary
     void addCollisionLayer(int layer)
              Add a collision layer to the body.
    protected  void addContact(Contact c)
              Adds a new contact.
     float angularVelocity2FX()
              Gets the current angular velocity.
     void angularVelocity2FX(float angularVelocity2FX)
              Sets the angular velocity.
     void applyAcceleration(FXVector acceleration, float timestepFX)
              Applies acceleration to the body.
     void applyForce(FXVector force, float timestepFX)
              Applies forces to the body.
     void applyMomentum(FXVector impulse)
              Applies an impulse to the body center.
     void applyMomentumAt(FXVector impulse, FXVector position)
              Applies an impulse at a position.
     void applyTorque(float torqueFX)
              Applies torque to the body.
     void applyVirtualMomentumAt(FXVector impulse, FXVector position)
              Applies a virtual impulse at a position.
    protected  void calculateAABB(float timestepFX)
              Recalculates the AABB.
     boolean canRotate()
              Checks if the body can rotate.
     Body copy()
              Copy method.
     boolean equals(Body other)
              Compares two bodies.
     void forceUpdate(float timestepFX)
              Forces an update of body internals.
     float getAABBMaxXFX()
              Gets the right side of the AABB.
     float getAABBMaxYFX()
              Gets the bottom of the AABB.
     float getAABBMinXFX()
              Gets the left side of the AABB.
     float getAABBMinYFX()
              Gets the top of the AABB.
     FXVector getAbsoluePoint(FXVector relativePoint)
              Converts a relative position to absolute coordinates.
     void getAbsoluePoint(FXVector relativePoint, FXVector target)
              Converts a relative position to absolute coordinates.
     FXVector[] getAxes()
              Calculates the projection axes in absolute coordinates.
     int getColissionBitFlag()
              Gets the complete collision flag.
    protected  Contact getContact(Body other)
              Find a contact with another body.
    protected  Contact getContact(int idx1, Body other, int idx2)
              Find a contact with another body.
     Contact[] getContacts()
              Gets the current contacts.
     int getId()
              Gets the id.
     float getInvInertia2FX()
              Gets the inverted inertia of the body.
     float getInvMass2FX()
              Gets the inverted mass of the body.
     FXVector getRelativePoint(FXVector absolutePoint)
              Converts an absolute position to relative coordinates.
     FXMatrix getRotationMatrix()
              Gets the rotation matrix.
     UserData getUserData()
              Get user data.
     FXVector getVelocity(FXVector relativePositionFX)
              Calculates the velocity of a point of the body.
     void getVelocity(FXVector relativePositionFX, FXVector result)
              Calculates the velocity of a point (reuse object).
     FXVector[] getVertices()
              Calculates the current vertices in absolute coordinates.
    protected  FXVector getVirtualVelocity(FXVector relativePositionFX)
              Calculates the virtual velocity of a point of the body.
    protected  void getVirtualVelocity(FXVector relativePositionFX, FXVector result)
              Calculates the virtual velocity of a point (reuse object).
    protected  void initShapeInternals()
              Caches collision info from the shape.
     void integrateVelocity(float dtFX)
              Integrates the velocity.
     void integrateVirtualVelocity(float dtFX, FXVector gravity)
               
     boolean isAffectedByGravity()
              Checks if the body is affected by gravity.
     boolean isDynamic()
              Checks if the body is dynamic.
     boolean isInteracting()
              Checks if the body can interact with other bodies.
     boolean isResting()
              Checks if the body is resting.
    static Body loadBody(PhysicsFileReader reader, java.util.Vector shapes, UserData userData)
              Loads a body from stream.
     FXVector positionFX()
              Gets the body position
     void removeCollisionLayer(int layer)
              Remove a collision layer from the body.
    protected  void resetContacts()
              Clear all contacts from previous step.
     float rotation2FX()
              Gets the current angle.
     float rotationVelocity2FX()
              Gets the angulare velocity.
     void setDynamic(boolean dynamic)
              Sets dynamic property of the body.
     void setGravityAffected(boolean affected)
              Sets if the body is affected by gravity.
     void setInteracting(boolean interacting)
              Sets if the body is allowed to interact with other bodies.
     void setPositionFX(FXVector positionFX)
              Sets the position.
     void setRotatable(boolean rotatable)
              Sets if the body is allowed to rotate.
     void setRotation2FX(float rotation2FX)
              Sets the current angle (2FX).
     void setRotationDeg(int rot)
              Sets the current angle.
     void setShape(Shape newShape)
              Sets the shape.
     void setUserData(UserData userData)
              Set User data
     Shape shape()
              Gets the shape of the body.
     void translate(FXVector translation, float timestepFX)
              Moves the body directly.
    protected  void updateVelocity(float dampingLinearFX, float dampingRotationalFX)
              Postprocesses the velocity.
     FXVector velocityFX()
              Gets the current velocity.
     
    Methods inherited from class java.lang.Object
    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
     

    Field Detail

    mPositionFX

    protected FXVector mPositionFX
    Position vector of the body. The position of the center of gravity.


    mVelocityFX

    protected FXVector mVelocityFX
    Current velocity of the body.


    mVirtualVelocityFX

    protected FXVector mVirtualVelocityFX
    Current virtual velocity of the body. Virtual velocity is used for resolving body penetration. This is the implementation of split impulses.


    mRotation2FX

    protected float mRotation2FX
    Current angle (2FX) of the body. An angle of zero points in positive x direction.


    mAngularVelocity2FX

    protected float mAngularVelocity2FX
    Current angular velocity (2FX) of the body.


    mVirtualAngularVelocity2FX

    protected float mVirtualAngularVelocity2FX
    Current Virtual angular velocity of the body. Virtual angular velocity is used for resolving body penetration. This is the implementation of split impulses.


    mShape

    protected Shape mShape
    The shape of the body. The shape and physical properties of the body.


    mId

    protected int mId
    Body id.


    mUserData

    protected UserData mUserData
    User data.

    Constructor Detail

    Body

    public Body(int x,
                int y,
                Shape shape,
                boolean dynamic)
    Constructor. The body has to be added to the World to act there.

    Parameters:
    x - x position
    y - y position
    shape - Shape of the body
    dynamic - whether the body can move or not

    Body

    public Body(FXVector pos,
                Shape shape,
                boolean dynamic)
    Constructor. The body has to be added to the World to act there.

    Parameters:
    pos - position of the body center
    shape - Shape of the body
    dynamic - whether the body can move or not

    Body

    public Body(Body other)
    Copy constructor. Creates a deep copy of the source body. Only the shape, which is considered a prototype and is shared among bodies anyway is not copied. Has same id as the copied body.

    Parameters:
    other - the source to copy
    Method Detail

    copy

    public Body copy()
    Copy method.

    Returns:
    a deep copy of the body.

    loadBody

    public static Body loadBody(PhysicsFileReader reader,
                                java.util.Vector shapes,
                                UserData userData)
    Loads a body from stream. The body is read from the input stream.

    Parameters:
    reader - the file reader representing the data stream
    shapes - a vector containing the shapes in correct order, (the order they are saved in the stored file)
    Returns:
    the loaded body

    equals

    public boolean equals(Body other)
    Compares two bodies. The comparison checks the id

    Parameters:
    other - the other body to compare
    Returns:
    whether the bodies are equal

    initShapeInternals

    protected void initShapeInternals()
    Caches collision info from the shape. Should be called when the shape internals have changed.


    getId

    public int getId()
    Gets the id.

    Returns:
    the body id.

    isDynamic

    public boolean isDynamic()
    Checks if the body is dynamic.

    Returns:
    whether the body can move

    isResting

    public boolean isResting()
    Checks if the body is resting.

    Returns:
    whether the body is at rest

    canRotate

    public boolean canRotate()
    Checks if the body can rotate.

    Returns:
    whether the body can rotate.

    setRotatable

    public void setRotatable(boolean rotatable)
    Sets if the body is allowed to rotate.

    Parameters:
    rotatable - whether the body is allowed to rotate.

    isInteracting

    public boolean isInteracting()
    Checks if the body can interact with other bodies.

    Returns:
    whether the body interacts.

    setInteracting

    public void setInteracting(boolean interacting)
    Sets if the body is allowed to interact with other bodies.

    Parameters:
    interacting - whether the body is allowed to interact.

    isAffectedByGravity

    public boolean isAffectedByGravity()
    Checks if the body is affected by gravity.

    Returns:
    whether gravity affects the body.

    setGravityAffected

    public void setGravityAffected(boolean affected)
    Sets if the body is affected by gravity.

    Parameters:
    affected - whether the body is affecetd by gravity.

    addCollisionLayer

    public void addCollisionLayer(int layer)
    Add a collision layer to the body. Further details for collision layers see mColissionBitFlag.

    Parameters:
    layer - the layer for the interaction, must be smaller than 32 as the bitflag is stored in an int

    removeCollisionLayer

    public void removeCollisionLayer(int layer)
    Remove a collision layer from the body. Further details for collision layers see mColissionBitFlag.

    Parameters:
    layer - the layer for the interaction, must be smaller than 32 as the bitflag is stored in an int

    getInvMass2FX

    public final float getInvMass2FX()
    Gets the inverted mass of the body. For static bodies the inverted mass is always zero.

    Returns:
    the mass defined in the shape.

    getInvInertia2FX

    public final float getInvInertia2FX()
    Gets the inverted inertia of the body. For static bodies the inverted inertia is always zero.

    Returns:
    the inertia defined in the shape.

    translate

    public void translate(FXVector translation,
                          float timestepFX)
    Moves the body directly.

    Parameters:
    translation - vector for translation
    timestepFX - timestep of the simulation

    forceUpdate

    public void forceUpdate(float timestepFX)
    Forces an update of body internals.

    Parameters:
    timestepFX - timestep of the simulation

    getAbsoluePoint

    public final FXVector getAbsoluePoint(FXVector relativePoint)
    Converts a relative position to absolute coordinates.

    Parameters:
    relativePoint - position relative to the body center,
    Returns:
    the position in absolute coordinates.

    getAbsoluePoint

    public final void getAbsoluePoint(FXVector relativePoint,
                                      FXVector target)
    Converts a relative position to absolute coordinates.

    Parameters:
    relativePoint - position relative to the body center,
    target - the vector to store the result into

    getRelativePoint

    public final FXVector getRelativePoint(FXVector absolutePoint)
    Converts an absolute position to relative coordinates. Note: This method is very expensive! (create and invert rotation matrix)

    Parameters:
    absolutePoint -
    Returns:
    a new vector of the relative position.

    getVelocity

    public final FXVector getVelocity(FXVector relativePositionFX)
    Calculates the velocity of a point of the body. The point is given in relative (wrt. the body) coordinates.

    Parameters:
    relativePositionFX - coordinates of the point relative to the body center
    Returns:
    the velocity at this point.

    getVirtualVelocity

    protected final FXVector getVirtualVelocity(FXVector relativePositionFX)
    Calculates the virtual velocity of a point of the body. The point is given in relative (wrt. the body) coordinates.

    Parameters:
    relativePositionFX - coordinates of the point relative to the body center
    Returns:
    the virtual velocity at this point.

    getVelocity

    public final void getVelocity(FXVector relativePositionFX,
                                  FXVector result)
    Calculates the velocity of a point (reuse object). The point is given in relative (wrt. the body) coordinates. Writes the resulting velocity into the supplied vector.

    Parameters:
    relativePositionFX - coordinates of the point relative to the center.
    result - the vector for storing the resulting velocity.

    getVirtualVelocity

    protected final void getVirtualVelocity(FXVector relativePositionFX,
                                            FXVector result)
    Calculates the virtual velocity of a point (reuse object). The point is given in relative (wrt. the body) coordinates. Writes the resulting velocity into the supplied vector.

    Parameters:
    relativePositionFX - coordinates of the point relative to the center.
    result - the vector, where the result is written to

    shape

    public Shape shape()
    Gets the shape of the body.

    Returns:
    the shape of the body.

    setShape

    public void setShape(Shape newShape)
    Sets the shape.

    Parameters:
    newShape - the shape of the body.

    getVertices

    public final FXVector[] getVertices()
    Calculates the current vertices in absolute coordinates.

    Returns:
    the vertices in absolute coordinates.

    getAxes

    public final FXVector[] getAxes()
    Calculates the projection axes in absolute coordinates.

    Returns:
    array containing the projection axes in absolute coordinates.

    rotation2FX

    public float rotation2FX()
    Gets the current angle.

    Returns:
    the current rotation (2FX) of the body.

    angularVelocity2FX

    public float angularVelocity2FX()
    Gets the current angular velocity.

    Returns:
    the current angular velocity (2FX) of the body.

    getRotationMatrix

    public FXMatrix getRotationMatrix()
    Gets the rotation matrix.

    Returns:
    the current rotation matrix.

    positionFX

    public FXVector positionFX()
    Gets the body position

    Returns:
    the current position of the center of gravity.

    velocityFX

    public FXVector velocityFX()
    Gets the current velocity.

    Returns:
    the current velocity.

    rotationVelocity2FX

    public float rotationVelocity2FX()
    Gets the angulare velocity.

    Returns:
    the angular velocity(2FX)

    setRotation2FX

    public void setRotation2FX(float rotation2FX)
    Sets the current angle (2FX). Updates the rotation matrix and invalidates the vertices and axes.

    Parameters:
    rotation2FX - new angle (2FX)

    setRotationDeg

    public void setRotationDeg(int rot)
    Sets the current angle. Updates the rotation matrix and invalidates the vertices and axes.

    Parameters:
    rot - new angle in degrees (0-360)

    setPositionFX

    public void setPositionFX(FXVector positionFX)
    Sets the position. Use this method with care. Directly manipulating body positions can cause non-physical behavior.

    Parameters:
    positionFX - new position for the center of gravity.

    angularVelocity2FX

    public void angularVelocity2FX(float angularVelocity2FX)
    Sets the angular velocity. Directly modifies the angular velocity of the body.

    Parameters:
    angularVelocity2FX - the new velocity (2FX)

    applyAcceleration

    public void applyAcceleration(FXVector acceleration,
                                  float timestepFX)
    Applies acceleration to the body. This can be used for forces like gravity.

    Parameters:
    acceleration - the acceleration
    timestepFX - timestep of the simulation (= time that the force is acting). This should not be larger than the simulation timestep.

    applyForce

    public void applyForce(FXVector force,
                           float timestepFX)
    Applies forces to the body. This can be used for forces like gravity.

    Parameters:
    force - the applied force
    timestepFX - timestep of the simulation (= time that the force is acting). This should not be larger than the simulation timestep.

    applyMomentumAt

    public final void applyMomentumAt(FXVector impulse,
                                      FXVector position)
    Applies an impulse at a position. Applying an impulse represents an acting force over the period of the timestep. The force causes a change in velocity and angular velocity.

    Parameters:
    impulse - the impulse vector.
    position - position in the body (relative to the center).

    applyVirtualMomentumAt

    public final void applyVirtualMomentumAt(FXVector impulse,
                                             FXVector position)
    Applies a virtual impulse at a position. Implements split impulses. The force causes a change in the virtual velocity and virtual angular velocity.

    Parameters:
    impulse - the virtual impulse vector.
    position - position in the body (relative to the center).

    applyMomentum

    public final void applyMomentum(FXVector impulse)
    Applies an impulse to the body center.

    Parameters:
    impulse - the impulse vector to apply

    applyTorque

    public final void applyTorque(float torqueFX)
    Applies torque to the body.

    Parameters:
    torqueFX - the torque to apply (FX).

    integrateVelocity

    public final void integrateVelocity(float dtFX)
    Integrates the velocity. This moves the body one tick forward. Euler integration scheme is used.

    Parameters:
    dtFX - timestep (FX)

    integrateVirtualVelocity

    public final void integrateVirtualVelocity(float dtFX,
                                               FXVector gravity)

    calculateAABB

    protected final void calculateAABB(float timestepFX)
    Recalculates the AABB. The AABB (axis aligned bounding boxes) are recomputed. This is typically done by the simulation, but can be relevant when a body is manually moved.

    Parameters:
    timestepFX - the timestep for the lookahead. Use the current timestep of the world.

    updateVelocity

    protected final void updateVelocity(float dampingLinearFX,
                                        float dampingRotationalFX)
    Postprocesses the velocity. This is used for damping.

    Parameters:
    dampingLinearFX - linear damping factor
    dampingRotationalFX - rotational damping factor

    resetContacts

    protected void resetContacts()
    Clear all contacts from previous step.


    addContact

    protected void addContact(Contact c)
    Adds a new contact. This is used by the collision detection.

    Parameters:
    c - the new Contact

    getContacts

    public Contact[] getContacts()
    Gets the current contacts.

    Returns:
    all current contacts

    getContact

    protected Contact getContact(Body other)
    Find a contact with another body.

    Parameters:
    other - other body.
    Returns:
    the contact if exists, null otherwise.

    getContact

    protected Contact getContact(int idx1,
                                 Body other,
                                 int idx2)
    Find a contact with another body.

    Parameters:
    other - other body.
    Returns:
    the contact if exists, null otherwise.

    setDynamic

    public void setDynamic(boolean dynamic)
    Sets dynamic property of the body.

    Parameters:
    dynamic - if the body can move.

    getAABBMinXFX

    public float getAABBMinXFX()
    Gets the left side of the AABB.

    Returns:
    the Left side of the AABB.

    getAABBMaxXFX

    public float getAABBMaxXFX()
    Gets the right side of the AABB.

    Returns:
    the Left side of the AABB.

    getAABBMinYFX

    public float getAABBMinYFX()
    Gets the top of the AABB.

    Returns:
    the Left side of the AABB.

    getAABBMaxYFX

    public float getAABBMaxYFX()
    Gets the bottom of the AABB.

    Returns:
    the Left side of the AABB.

    getColissionBitFlag

    public int getColissionBitFlag()
    Gets the complete collision flag.

    Returns:
    the complete bit flag containing information about all collision layers.

    getUserData

    public UserData getUserData()
    Get user data.

    Returns:
    the user data.

    setUserData

    public void setUserData(UserData userData)
    Set User data

    Parameters:
    userData - the user data

    Emini Physics Engine 1.3.5a API Documentation

    Emini Physics Engine 1.3.5a API Documentation - Copyright 2014 Alexander Adensamer