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:
Body

Inner Class Summary
 class Universe.CollisionNotification
          Comprises two Body objects that have been 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 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.
 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.
 void removeAll()
          Removes all bodies 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.
 int size()
          Returns the number of bodies in the universe.
 int size(java.lang.Class t)
          Returns the number of bodies of the type t in the universe.
 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.

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()

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()
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.
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.

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:
Body.setOwner(Universe)

removeBody

public void removeBody(Body b)
Removes the body b from the universe and sets its owner to null.
See Also:
Body.setOwner(Universe)

removeAll

public void removeAll()
Removes all bodies from the universe and sets their owner to null.
See Also:
Body.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:
Collections.unmodifiableList(List)

size

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

size

public int size(java.lang.Class t)
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. If a collision occurs, the method realizes an elastic collision between the bodies and notifies the observers.
See Also:
Body.advance(double), Body.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()