fi.utu.cs.physics
Class Universe

java.lang.Object
  |
  +--java.util.Observable
        |
        +--fi.utu.cs.physics.Universe

public class Universe
extends java.util.Observable

A universe comprising a collection of Body objects. The type of the universe can be infinite, finite or toroid. Classes implementing the Observer interface can observe the universe for detecting collisions between the corporeal bodies; an instance of the CollisionPair inner class is submitted to the observers.

See Also:
Particle, Body, Boundary

Nested Class Summary
 class Universe.CollisionNotification
          Comprises a Body object and a Particle object that have been the counterparts in a collision.
 
Field Summary
static int FINITE
          A finite universe which limits the position of the bodies.
static int INFINITE
          An infinite universe which does not impose any limits to the position of the bodies.
static int TOROID
          An infinite universe of finite size where the opposite vertical and horizontal edges are connected to form a torus.
 
Constructor Summary
Universe()
          Creates an infinite universe.
Universe(int t, Point2 min, Point2 max)
          Creates a universe.
 
Method Summary
 void addBody(Body b)
          Adds the body b to the universe, which is also set to be the owner of the body.
 void addBoundary(Boundary b)
          Adds the boundary b to the universe, which is also set to be the owner of the body.
 void advance(double dt)
          Advances the time dt seconds and updates its bodies accordingly.
 java.util.List bodies()
          Returns an unmodifiable (i.e., "read-only") list of the bodies in the universe.
 int bodySize()
          Returns the number of bodies in the universe.
 int bodySize(java.lang.Class t)
          Returns the number of bodies of the type t in the universe.
 java.util.List boundaries()
          Returns an unmodifiable (i.e., "read-only") list of the boundaries in the universe.
 int boundarySize()
          Returns the number of boundaries in the universe.
 int boundarySize(java.lang.Class t)
          Returns the number of boundaries of the type t in the universe.
 void clearInelasticCollision(java.lang.Class bc, java.lang.Class pc)
          Sets the collisions between instances of the classes bc and pc to be elastic.
 double getAccelerationOfGravity()
          Returns the acceleration of gravity of this universe.
 Point2 getCenter()
          Returns the center of the universe or null if the universe is infinite.
 Point2 getMaximumCorner()
          Returns the maximum corner of the universe or null if the universe is infinite.
 Point2 getMinimumCorner()
          Returns the minimum corner of the universe or null if the universe is infinite.
 int getType()
          Returns the type of the universe.
 boolean isInelasticCollision(Body b, Particle p)
          Returns true if a collision between the body b and the particle p is inelastic; otherwise, returns false.
 boolean isInelasticCollision(java.lang.Class bc, java.lang.Class pc)
          Returns true if collisions between instances of the classes bc and pc are inelastic; otherwise, returns false.
 void removeAll()
          Removes all bodies and boundaries from the universe and sets their owner to null.
 void removeBody(Body b)
          Removes the body b from the universe and sets its owner to null.
 void removeBoundary(Boundary b)
          Removes the boundary b from the universe and sets its owner to null.
 void setAccelerationOfGravity(double g)
          Sets the acceleration of gravity of this universe to g.
 void setInelasticCollision(java.lang.Class bc, java.lang.Class pc)
          Sets the collisions between instances of the classes bc and pc to be inelastic.
 int size()
          Deprecated. As of version 1.1, replaced by bodySize().
 int size(java.lang.Class t)
          Deprecated. As of version 1.1, replaced by bodySize(Class).
 Point2 transpose(Point2 p)
          Transposes the point p according to the type of the universe.
 
Methods inherited from class java.util.Observable
addObserver, clearChanged, countObservers, deleteObserver, deleteObservers, hasChanged, notifyObservers, notifyObservers, setChanged
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

INFINITE

public static final int INFINITE
An infinite universe which does not impose any limits to the position of the bodies.

See Also:
Constant Field Values

FINITE

public static final int FINITE
A finite universe which limits the position of the bodies. The shape of the universe is the rectangle (getMinimumCorner(), getMaximumCorner()).

See Also:
getMinimumCorner(), getMaximumCorner(), Constant Field Values

TOROID

public static final int TOROID
An infinite universe of finite size where the opposite vertical and horizontal edges are connected to form a torus. The shape of the universe is the rectangle (getMinimumCorner(), getMaximumCorner()).

See Also:
getMinimumCorner(), getMaximumCorner(), Constant Field Values
Constructor Detail

Universe

public Universe()
Creates an infinite universe. After the creation, the universe is devoid of bodies.


Universe

public Universe(int t,
                Point2 min,
                Point2 max)
Creates a universe. After the creation, the universe is devoid of bodies and boundaries. The collisions are set fully elastic.

Parameters:
t - The type of the universe (infinite, finite or toroid).
min - The minimum corner of the universe.
max - The maximum corner of the universe.
See Also:
INFINITE, FINITE, TOROID
Method Detail

getType

public int getType()
Returns the type of the universe.

See Also:
INFINITE, FINITE, TOROID

getMinimumCorner

public Point2 getMinimumCorner()
Returns the minimum corner of the universe or null if the universe is infinite.


getMaximumCorner

public Point2 getMaximumCorner()
Returns the maximum corner of the universe or null if the universe is infinite.


getCenter

public Point2 getCenter()
Returns the center of the universe or null if the universe is infinite.


isInelasticCollision

public boolean isInelasticCollision(java.lang.Class bc,
                                    java.lang.Class pc)
Returns true if collisions between instances of the classes bc and pc are inelastic; otherwise, returns false.

Since:
1.1
See Also:
setInelasticCollision(Class,Class), clearInelasticCollision(Class,Class)

isInelasticCollision

public boolean isInelasticCollision(Body b,
                                    Particle p)
Returns true if a collision between the body b and the particle p is inelastic; otherwise, returns false.

Since:
1.1

setInelasticCollision

public void setInelasticCollision(java.lang.Class bc,
                                  java.lang.Class pc)
Sets the collisions between instances of the classes bc and pc to be inelastic.

Since:
1.1
See Also:
isInelasticCollision(Class,Class), clearInelasticCollision(Class,Class)

clearInelasticCollision

public void clearInelasticCollision(java.lang.Class bc,
                                    java.lang.Class pc)
Sets the collisions between instances of the classes bc and pc to be elastic.

Since:
1.1
See Also:
isInelasticCollision(Class,Class), setInelasticCollision(Class,Class)

addBody

public void addBody(Body b)
Adds the body b to the universe, which is also set to be the owner of the body.

See Also:
Particle.setOwner(Universe)

removeBody

public void removeBody(Body b)
Removes the body b from the universe and sets its owner to null.

See Also:
Particle.setOwner(Universe)

addBoundary

public void addBoundary(Boundary b)
Adds the boundary b to the universe, which is also set to be the owner of the body.

Since:
1.1
See Also:
Particle.setOwner(Universe)

removeBoundary

public void removeBoundary(Boundary b)
Removes the boundary b from the universe and sets its owner to null.

Since:
1.1
See Also:
Particle.setOwner(Universe)

removeAll

public void removeAll()
Removes all bodies and boundaries from the universe and sets their owner to null.

See Also:
Particle.setOwner(Universe), Particle.setOwner(Universe)

bodies

public java.util.List bodies()
Returns an unmodifiable (i.e., "read-only") list of the bodies in the universe. Attempts to modify the returned list, whether direct or via its iterator, result in an UnsupportedOperationException.

See Also:
boundaries(), Collections.unmodifiableList(List)

bodySize

public int bodySize()
Returns the number of bodies in the universe.

Since:
1.1

bodySize

public int bodySize(java.lang.Class t)
Returns the number of bodies of the type t in the universe.

Since:
1.1

boundaries

public java.util.List boundaries()
Returns an unmodifiable (i.e., "read-only") list of the boundaries in the universe. Attempts to modify the returned list, whether direct or via its iterator, result in an UnsupportedOperationException.

Since:
1.1
See Also:
bodies(), Collections.unmodifiableList(List)

boundarySize

public int boundarySize()
Returns the number of boundaries in the universe.

Since:
1.1

boundarySize

public int boundarySize(java.lang.Class t)
Returns the number of boundaries of the type t in the universe.

Since:
1.1

size

public int size()
Deprecated. As of version 1.1, replaced by bodySize().

Returns the number of bodies in the universe.


size

public int size(java.lang.Class t)
Deprecated. As of version 1.1, replaced by bodySize(Class).

Returns the number of bodies of the type t in the universe.


advance

public void advance(double dt)
Advances the time dt seconds and updates its bodies accordingly. The methods advances the bodies iteratively. After each advancement, it examines whether the body in question is corporeal and does it collide with any of the remaining (i.e., not yet advanced) corporeal bodies or boundaries. If a collision occurs, the method realizes an elastic collision between the bodies. If either particle have requested a collision notification on the other particle, all observers will be notified.

See Also:
Body.advance(double), Particle.isCorporeal(), Body.isColliding(Body), Body.elasticCollision(Body), Universe.CollisionNotification

transpose

public Point2 transpose(Point2 p)
Transposes the point p according to the type of the universe. If the universe is infinite, the method returns p. Otherwise, the method returns a new point where getMinimumCorner.getX() <= result.getX() <= getMaximumCorner.getX() and getMinimumCorner.getY() <= result.getY() <= getMaximumCorner.getY(). If the universe is finite, an exceeding point is moved back inside the rectangle. If the universe is toroid, an exceeding point is transposed back inside the rectangle by regarding it as a torus surface.

See Also:
getType(), getMinimumCorner(), getMaximumCorner()

getAccelerationOfGravity

public double getAccelerationOfGravity()
Returns the acceleration of gravity of this universe. By default g = 9.80665 m/sē.

Since:
1.1
See Also:
setAccelerationOfGravity(double), OrientedBody

setAccelerationOfGravity

public void setAccelerationOfGravity(double g)
Sets the acceleration of gravity of this universe to g.

Since:
1.1
See Also:
getAccelerationOfGravity(), OrientedBody