fi.utu.cs.physics
Class Body

java.lang.Object
  |
  +--fi.utu.cs.physics.Particle
        |
        +--fi.utu.cs.physics.Body
Direct Known Subclasses:
FrictionalBody, MomentaryBody, OrientedBody

public abstract class Body
extends Particle

A moving physical body with mass and dimensionality. The outline of the body is approximated with a bounding circle of a given radius, which is used in detecting overlaps. The body can be owned by an instance of the class Universe, which may impose limits to the position.

See Also:
Universe

Constructor Summary
protected Body(double m, double r)
          Constructs a body with the mass m and the radius r.
protected Body(double m, double r, Point2 p)
          Constructs a body with the mass m, the radius r and the position p.
protected Body(double m, double r, Point2 p, Vector2 v)
          Constructs a body with the mass m, the radius r, the position p and the velocity v.
protected Body(double m, double r, Point2 p, Vector2 v, Vector2 a)
          Constructs a body with the mass m, the radius r, the position p, the velocity v and the acceleration a.
 
Method Summary
 void advance(double dt)
          Advances the time dt seconds and updates the position and velocity of the body accordingly.
 void elasticCollision(Body other)
          Realizes an elastic collision for both the body and the body other.
 Point2 futurePosition(double dt)
          Returns the position of the body after seconds.
 Vector2 futureVelocity(double dt)
          Returns the velocity of the body after seconds.
 Vector2 getAcceleration()
          Returns the acceleration of the body.
 double getMass()
          Returns the mass of the body.
 Point2 getPosition()
          Returns the position of the body.
 double getRadius()
          Returns the radius of the bounding circle.
 Vector2 getVelocity()
          Returns the velocity of the body.
 void inelasticCollision(Body other)
          Realizes an inelastic collision for both the body and the body other.
 boolean isColliding(Body other)
          Returns whether the body collides with the body other.
 boolean isOverlapping(Body other)
          Checks whether the body overlaps the body other.
 void setAcceleration(double ax, double ay)
          Sets the acceleration of the body.
 void setAcceleration(Vector2 a)
          Sets the acceleration of the body to a.
 void setMass(double m)
          Sets the mass of the body to m.
 void setPosition(double x, double y)
          Sets the position of the body.
 void setPosition(Point2 p)
          Sets the position of the body to p.
 void setRadius(double r)
          Sets the radius of the bounding circle to r.
 void setVelocity(double vx, double vy)
          Sets the velocity of the body.
 void setVelocity(Vector2 v)
          Sets the velocity of the body to v.
 java.lang.String toString()
          Returns a string that contains the values of the attributes.
 
Methods inherited from class fi.utu.cs.physics.Particle
addCollisionNotification, clearCollisionNotifications, getOwner, isCollisionNotified, isCollisionNotified, isCorporeal, removeCollisionNotification, setCorporeal, setEthereal, setOwner
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

Body

protected Body(double m,
               double r)
Constructs a body with the mass m and the radius r. The body resides in the origin and has a zero velocity and a zero acceleration. The body is corporeal and has no owner.


Body

protected Body(double m,
               double r,
               Point2 p)
Constructs a body with the mass m, the radius r and the position p. The body has a zero velocity and a zero acceleration. The body is corporeal and has no owner.


Body

protected Body(double m,
               double r,
               Point2 p,
               Vector2 v)
Constructs a body with the mass m, the radius r, the position p and the velocity v. The body has a zero acceleration. The body is corporeal and has no owner.


Body

protected Body(double m,
               double r,
               Point2 p,
               Vector2 v,
               Vector2 a)
Constructs a body with the mass m, the radius r, the position p, the velocity v and the acceleration a. The body is corporeal and has no owner.

Method Detail

getMass

public double getMass()
Returns the mass of the body.


getRadius

public double getRadius()
Returns the radius of the bounding circle.


getPosition

public Point2 getPosition()
Returns the position of the body.


getVelocity

public Vector2 getVelocity()
Returns the velocity of the body.


getAcceleration

public Vector2 getAcceleration()
Returns the acceleration of the body.


setMass

public void setMass(double m)
Sets the mass of the body to m.


setRadius

public void setRadius(double r)
Sets the radius of the bounding circle to r.


setPosition

public void setPosition(double x,
                        double y)
Sets the position of the body.

Parameters:
x - The x coordinate of the position.
y - The y coordinate of the position.
See Also:
setPosition(Point2)

setPosition

public void setPosition(Point2 p)
Sets the position of the body to p.


setVelocity

public void setVelocity(double vx,
                        double vy)
Sets the velocity of the body.

Parameters:
vx - The horizontal velocity.
vy - The vertical velocity.
See Also:
setVelocity(Vector2)

setVelocity

public void setVelocity(Vector2 v)
Sets the velocity of the body to v.


setAcceleration

public void setAcceleration(double ax,
                            double ay)
Sets the acceleration of the body.

Parameters:
ax - The horizontal acceleration.
ay - The vertical acceleration.
See Also:
setAcceleration(Vector2)

setAcceleration

public void setAcceleration(Vector2 a)
Sets the acceleration of the body to a.


advance

public void advance(double dt)
Advances the time dt seconds and updates the position and velocity of the body accordingly.

See Also:
futurePosition(double), futureVelocity(double)

futurePosition

public Point2 futurePosition(double dt)
Returns the position of the body after seconds. The new horizontal position x' is calculated from the equation x' = ½axt² + vxt + x, and the vertical position is calculated accordingly.

Since:
1.1
See Also:
advance(double), futureVelocity(double)

futureVelocity

public Vector2 futureVelocity(double dt)
Returns the velocity of the body after seconds. The new horizontal velocity vx' is calculated from the equation vx' = axt + vx, and the vertical velocity is calculated accordingly.

Since:
1.1
See Also:
advance(double), futurePosition(double)

isOverlapping

public boolean isOverlapping(Body other)
Checks whether the body overlaps the body other. An overlap occurs if the distance between the centers of the bodies is smaller than the sum of the respective radii.

See Also:
isColliding(Body)

isColliding

public boolean isColliding(Body other)
Returns whether the body collides with the body other. A collision occurs when the bodies overlap each other for the first time (i.e., at the moment of impact) and both bodies are corporeal.

See Also:
isOverlapping(Body), Particle.isCorporeal()

elasticCollision

public void elasticCollision(Body other)
Realizes an elastic collision for both the body and the body other. The linear momentum and kinetic energy of the bodies is conserved in the collision. The new velocity v1' is calculated from the equation v1' = v1 (m1 - m2) / (m1 + m2) + v2 2m2 / (m1 + m2) and v2' from the equation v2' = v1 2m1 / (m1 + m2) + v2 (m2 - m1) / (m2 + m1). The new acceleration a1' is calculated from from the equation a1' = m2a2 / m1, and the new acceleration a2' is calculated accordingly. The collision is realized regardless whether the bounding circles ovelap or not.

Specified by:
elasticCollision in class Particle
See Also:
inelasticCollision(Body), isOverlapping(Body)

inelasticCollision

public void inelasticCollision(Body other)
Realizes an inelastic collision for both the body and the body other. The linear momentum of the bodies is conserved but kinetic energy is lost.

Specified by:
inelasticCollision in class Particle
Since:
1.1
See Also:
elasticCollision(Body), isOverlapping(Body)

toString

public java.lang.String toString()
Returns a string that contains the values of the attributes.

Overrides:
toString in class Particle