hermes.physics
Class MassedBeing

java.lang.Object
  extended by hermes.HObject
      extended by hermes.Being
          extended by hermes.physics.MassedBeing
All Implemented Interfaces:
KeySubscriber, MouseSubscriber, MouseWheelSubscriber, OscSubscriber

public abstract class MassedBeing
extends Being

An extension of Being representing a body with mass and elasticity.


Field Summary
protected  float _mass
           
 
Fields inherited from class hermes.Being
_position, _shape, _time, _velocity
 
Constructor Summary
MassedBeing(HShape shape, PVector velocity, float mass, float elasticity)
          Instantiates a new MassedBeing with given mass and elasticity.
MassedBeing(HShape shape, PVector velocity, float mass, float elasticity, float sampleLength)
           Instantiates a new MassedBeing with given mass and elasticity, and multisampling with a default maximum samples value of 10.
MassedBeing(HShape shape, PVector velocity, float mass, float elasticity, float sampleLength, int maxSamples)
           Instantiates a new MassedBeing with given mass and elasticity, and multisampling.
 
Method Summary
 void addDisplacement(PVector displacement)
          Adds a displacement to the being, which will be applied at the next step.
 void addForce(PVector force)
          Adds a force to the being, which will be applied at the next step.
 void addImpulse(PVector impulse)
          Adds an impulse to the being, which will be applied at the next step.
protected  void addImpulseCollision(ImpulseCollision collision)
          Adds an impulse collision to the being's collision list.
static ImpulseCollision addImpulseCollision(MassedBeing being1, MassedBeing being2, PVector projection)
          Sets up a collision between two beings, with elasticity equal to the average of the being's elasticity.
static ImpulseCollision addImpulseCollision(MassedBeing being1, MassedBeing being2, PVector projection, float elasticity)
          Sets up a collision between two beings, with custom elasticity.
 void addMass(float mass)
          adds mass to the being
 void addMassSafe(float mass)
          Will add a mass to the being, unless this would cause the beings mass to become zero or negative, in which case it has no effect.
protected  void applyDisplacement()
          Applies the accumulated displacement and clears it.
protected  void applyImpulse()
          Applies the currently acccumulated impulse and clears it.
protected  void clearCollisions()
          clears the collision list
protected  void clearForce()
          clears all forces
protected  void EulerIntegrateVelocity(double dt)
          Integrates velocity on acceleration using Euler-Cromer.
 PVector getDisplacement()
          Gets the current displacement on the being (note that this will be non-zero only after displacements have been applied, before updates).
 float getElasticity()
          the being's elasticity
 PVector getForce()
          gets the current force on the being (note that this will be non-zero only after forces have been applied, before updates) this is a reference, if you manipulate the returned vector it will change the force
 PVector getImpulse()
          Gets the current impulse on the being (note that this will be non-zero only after impulses have been applied, before updates).
 ImpulseCollision getImpulseCollisionWith(MassedBeing other)
          Returns the collision between this being and another, if such a collision has been added.
 float getMass()
          gets the being's mass
 boolean needsMoreSamples()
          Used for multisampling -- if true the object needs to be sampled more on the current update.
 void setElasticity(float elasticity)
          Sets the being's elasticity (bounciness).
 void setMass(float mass)
          sets the being's mass
 void step()
          Updates the being's position and velocity based on the forces applied since the last step, using Euler-Cromer integration.
 java.lang.String toString()
           
 
Methods inherited from class hermes.Being
draw, EulerIntegratePosition, getBoundingBox, getPosition, getShape, getVelocity, getVelocityX, getVelocityY, getX, getY, processUpdate, setDone, setPosition, setPosition, setPosition, setVelocity, setVelocityX, setVelocityY, setX, setY, update, updateTime
 
Methods inherited from class hermes.HObject
addToGroup, delete, getGroups, receive, receive, receive, receive, removeFromGroup
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

_mass

protected float _mass
Constructor Detail

MassedBeing

public MassedBeing(HShape shape,
                   PVector velocity,
                   float mass,
                   float elasticity)
Instantiates a new MassedBeing with given mass and elasticity. Elasticity determies bounciness of collisions, a collision between beings of elasticity 1 will be perfectly elastic, and a collision between beings of mass 0 will be perfectly inelastic, ie they will lose all velocity parallel to the collision axis. If elasticity is greater than 1, they will gain speed from collisions, which may produce unrealistic results

Parameters:
shape - Shape used to determine Being's position and collision detection
velocity - The Being's initial velocity. Velocity gets updated automatically every timestep.
mass - the Being's mass
elasticity - the Being's elasticity

MassedBeing

public MassedBeing(HShape shape,
                   PVector velocity,
                   float mass,
                   float elasticity,
                   float sampleLength,
                   int maxSamples)

Instantiates a new MassedBeing with given mass and elasticity, and multisampling. Elasticity determines bounciness of collisions, a collision between beings of elasticity 1 will be perfectly elastic, and a collision between beings of mass 0 will be perfectly inelastic, ie they will lose all velocity parallel to the collision axis. If elasticity is greater than 1, they will gain speed from collisions, which may produce unrealistic results.

Multisampling causes a Being to be updated and checked for interactions multiple times per update, if it is moving above a certain speed. This is used to prevent collision detection and other interactions from failing due to sampling. Multisampling is determined by sampleLength, the distance the being must travel before multisampling is applied, and maxSample, the number of samples allowed to prevent very long loops if a Being moves too fast.

Parameters:
shape - Shape used to determine Being's position and collision detection.
velocity - The Being's initial velocity. Velocity gets updated automatically every timestep.
mass - the Being's mass
elasticity - the Being's elasticity
sampleLength - The length of the motion sample, ie how for the being has to travel before more samples are needed. The being's shortest spanning length is a reasonable value
maxSamples - The maximum number of samples allowed. If the being can reach a very high speed, this must be very high for motion sampling to work, but this reduces performance. Increasing the sample length allows lower maxSamples values, but decreases accuracy.

MassedBeing

public MassedBeing(HShape shape,
                   PVector velocity,
                   float mass,
                   float elasticity,
                   float sampleLength)

Instantiates a new MassedBeing with given mass and elasticity, and multisampling with a default maximum samples value of 10. Elasticity determines bounciness of collisions, a collision between beings of elasticity 1 will be perfectly elastic, and a collision between beings of mass 0 will be perfectly inelastic, ie they will lose all velocity parallel to the collision axis. If elasticity is greater than 1, they will gain speed from collisions, which may produce unrealistic results.

Multisampling causes a Being to be updated and checked for interactions multiple times per update, if it is moving above a certain speed. This is used to prevent collision detection and other interactions from failing due to sampling. Multisampling is determined by sampleLength, the distance the being must travel before multisampling is applied, and maxSample, the number of samples allowed to prevent very long loops if a Being moves too fast.

Parameters:
shape - Shape used to determine Being's position and collision detection.
velocity - The Being's initial velocity. Velocity gets updated automatically every timestep.
mass - the Being's mass
elasticity - the Being's elasticity
sampleLength - The length of the motion sample, ie how for the being has to travel before more samples are needed. The being's shortest spanning length is a reasonable value
Method Detail

getMass

public float getMass()
gets the being's mass

Returns:
the being's mass

setMass

public void setMass(float mass)
sets the being's mass

Parameters:
mass - the mass to set (must be positive)

addMass

public void addMass(float mass)
adds mass to the being

Parameters:
mass - the mass of the being

addMassSafe

public void addMassSafe(float mass)
Will add a mass to the being, unless this would cause the beings mass to become zero or negative, in which case it has no effect. While convenient because you don't have to catch an exception, be careful as this may make it harder to find bugs in your code that would have cause a non-positive mass.

Parameters:
mass - the mass to add

getElasticity

public float getElasticity()
the being's elasticity

Returns:
the being's elasticity

getImpulse

public PVector getImpulse()
Gets the current impulse on the being (note that this will be non-zero only after impulses have been applied, before updates). This is a reference, if you manipulate the returned vector it will change the impulse.

Returns:
the impulse

getDisplacement

public PVector getDisplacement()
Gets the current displacement on the being (note that this will be non-zero only after displacements have been applied, before updates). This is a reference, if you manipulate the returned vector it will change the displacement.

Returns:
the displacement

getForce

public PVector getForce()
gets the current force on the being (note that this will be non-zero only after forces have been applied, before updates) this is a reference, if you manipulate the returned vector it will change the force

Returns:
the force

setElasticity

public void setElasticity(float elasticity)
Sets the being's elasticity (bounciness). Elasticity cannot be negative, and in general it should be no higher than 1. Collision between beings with elasticity higher than one will result beings gaining velocity when they collide, violating conservation of momentum.

Parameters:
elasticity - the elasticity, should be in the range [0,1]

addForce

public void addForce(PVector force)
Adds a force to the being, which will be applied at the next step.

Parameters:
force - the force the add

addImpulse

public void addImpulse(PVector impulse)
Adds an impulse to the being, which will be applied at the next step.

Parameters:
impulse - the impulse to add

addDisplacement

public void addDisplacement(PVector displacement)
Adds a displacement to the being, which will be applied at the next step.

Parameters:
displacement - the displacement to add

step

public void step()
Updates the being's position and velocity based on the forces applied since the last step, using Euler-Cromer integration.

Overrides:
step in class Being

needsMoreSamples

public boolean needsMoreSamples()
Description copied from class: HObject
Used for multisampling -- if true the object needs to be sampled more on the current update.

Overrides:
needsMoreSamples in class Being
Returns:
whether the object needs more samples this update

applyImpulse

protected void applyImpulse()
Applies the currently acccumulated impulse and clears it.


applyDisplacement

protected void applyDisplacement()
Applies the accumulated displacement and clears it.


EulerIntegrateVelocity

protected void EulerIntegrateVelocity(double dt)
Integrates velocity on acceleration using Euler-Cromer.

Parameters:
dt - the time step

clearCollisions

protected void clearCollisions()
clears the collision list


clearForce

protected void clearForce()
clears all forces


addImpulseCollision

public static ImpulseCollision addImpulseCollision(MassedBeing being1,
                                                   MassedBeing being2,
                                                   PVector projection)
Sets up a collision between two beings, with elasticity equal to the average of the being's elasticity.

Parameters:
being1 - the first being
being2 - the second being
projection - the projection vector from being1 to being2

addImpulseCollision

public static ImpulseCollision addImpulseCollision(MassedBeing being1,
                                                   MassedBeing being2,
                                                   PVector projection,
                                                   float elasticity)
Sets up a collision between two beings, with custom elasticity.

Parameters:
being1 - the first being
being2 - the second being
projection - the projection vector from being1 to being2

getImpulseCollisionWith

public ImpulseCollision getImpulseCollisionWith(MassedBeing other)
Returns the collision between this being and another, if such a collision has been added.

Parameters:
other - the being to check for collision with
Returns:
the collision

addImpulseCollision

protected void addImpulseCollision(ImpulseCollision collision)
Adds an impulse collision to the being's collision list.

Parameters:
collision - the collision

toString

public java.lang.String toString()
Overrides:
toString in class Being
Returns:
String with position and velocity of Being, use for debugging