/* * Simple Mechanics Simulator (SiMS) * copyright (c) 2009 Jakob Odersky * made available under the MIT License */ package sims.dynamics import sims.geometry._ import sims.collision._ import sims.dynamics.joints._ import scala.collection.mutable.ArrayBuffer /**A world contains and simulates a system of rigid bodies and joints.*/ class World { /**Time intervals in which this world simulates.*/ var timeStep: Double = 1.0 / 60 /**Number of constraint corrections per time step.*/ var iterations: Int = 10 /**Gravity in this world.*/ var gravity = Vector2D(0, -9.81) /**Bodies contained in this world.*/ val bodies = new ArrayBuffer[Body] /**Joints contained in this world.*/ val joints = new ArrayBuffer[Joint] /**Monitoring methods for bodies. *
* The first element of the tuple is the method's title and the second the method.
* Example usage: monitors += ("Y-Position", _.pos.y.toString)
* This will calculate all bodies - whose monitor
field is set to
* true
- second position components.*/
val monitors = new ArrayBuffer[(String, Body => Any)]
/**Collsion detector who manages collision detection in this world.*/
val detector: Detector = new GridDetector(this)
/**Warning if a body's velocity exceeds the speed of light.*/
var overCWarning = false
/**Flag to enable collision detection.*/
var enableCollisionDetection = true
/**Flag to enable position correction for constraints.*/
var enablePositionCorrection = true
/**Minimal, non-zero linear velocity.*/
var minLinearVelocity: Double = 0.0001
/**Minimal, non-zero angular velocity.*/
var minAngularVelocity: Double = 0.0001
/**Returns all shapes of all bodies in this world.*/
def shapes = for (b <- bodies; s <- b.shapes) yield s
/**Adds the given body to this world.*/
def +=(body: Body) = bodies += body
/**Adds the given joint to this world.*/
def +=(joint: Joint): Unit = joints += joint
/**Adds the given prefabricated system of bodies and joints to this world.*/
def +=(p: sims.prefabs.Prefab): Unit = {
for (b <- p.bodies) this += b
for (j <- p.joints) this += j
}
/**Adds the given sequence of bodies to this world.*/
def ++=(bs: Seq[Body]): Unit = for(b <- bs) this += b
/**Removes the given body from this world.*/
def -=(body: Body): Unit = bodies -= body
/**Removes the given joint from this world.*/
def -=(joint: Joint): Unit = joints -= joint
/**Removes the given prefabricated system of bodies and joints from this world.*/
def -=(p: sims.prefabs.Prefab): Unit = {
for (b <- p.bodies) this -= b
for (j <- p.joints) this -= j
}
/**Removes the given sequence of bodies from this world.*/
def --=(bs: Seq[Body]) = for(b <- bs) this -= b
/**Removes all bodies, joints and monitoring methods from this world.*/
def clear() = {joints.clear(); bodies.clear(); monitors.clear()}
/**Current time in this world.*/
var time: Double = 0.0
/**Simulates a time step of the duration timeStep
.
*
* The time step is simulated in the following phases: *
postStep()
is executed.