diff options
Diffstat (limited to 'src/sims/dynamics')
-rw-r--r-- | src/sims/dynamics/Body.scala | 104 | ||||
-rw-r--r-- | src/sims/dynamics/Circle.scala | 11 | ||||
-rw-r--r-- | src/sims/dynamics/Constraint.scala | 12 | ||||
-rw-r--r-- | src/sims/dynamics/Rectangle.scala | 22 | ||||
-rw-r--r-- | src/sims/dynamics/RegularPolygon.scala | 18 | ||||
-rw-r--r-- | src/sims/dynamics/Shape.scala | 70 | ||||
-rw-r--r-- | src/sims/dynamics/World.scala | 84 | ||||
-rw-r--r-- | src/sims/dynamics/joints/DistanceJoint.scala | 36 | ||||
-rw-r--r-- | src/sims/dynamics/joints/ForceJoint.scala | 4 | ||||
-rw-r--r-- | src/sims/dynamics/joints/Joint.scala | 12 | ||||
-rw-r--r-- | src/sims/dynamics/joints/RevoluteJoint.scala | 4 | ||||
-rw-r--r-- | src/sims/dynamics/joints/SpringJoint.scala | 44 | ||||
-rw-r--r-- | src/sims/dynamics/joints/test/PrismaticJoint.scala | 26 |
13 files changed, 225 insertions, 222 deletions
diff --git a/src/sims/dynamics/Body.scala b/src/sims/dynamics/Body.scala index d5b2a0e..8c0e2ee 100644 --- a/src/sims/dynamics/Body.scala +++ b/src/sims/dynamics/Body.scala @@ -1,3 +1,4 @@ + /* * Simple Mechanics Simulator (SiMS) * copyright (c) 2009 Jakob Odersky @@ -9,17 +10,17 @@ package sims.dynamics import sims.geometry._ import sims.dynamics.joints._ -/**Ein 2-Dimensionaler Koerper besteht aus mehreren Formen. Im gegensatz zu letzteren, enthaelt ein Koerper dynamische Informationen (v, F, etc...). - * @param shps zu dem Koerper gehoerende Formen.*/ +/**A two dimensional rigid body is made out of shapes. + * @param shps shapes that belong to this body.*/ class Body(shps: Shape*){ - /**Einzigartige Identifikationsnummer dieses Koerpers.*/ + /**Unique identification number.*/ val uid = Body.nextUid - /**Formen aus denen dieser Koerper besteht.*/ + /**Shapes that belong to this body.*/ val shapes: List[Shape] = shps.toList - //Formen werden bei Initialisierung eingefuegt + //Shapes are added during initialisation. for (s <- shapes) { s.body = this s.refLocalPos = s.pos - pos @@ -28,41 +29,40 @@ class Body(shps: Shape*){ private var isFixed: Boolean = false - /**Gibt an ob dieser Koerper fixiert ist.*/ + /**Returns whether this body is fixed or not.*/ def fixed = isFixed - /**Fixiert oder unfixiert diesen Koerper.*/ + /**Fixes or frees this body. By fixing, linear and angular velocities are set to zero.*/ def fixed_=(value: Boolean) = { if (value) {linearVelocity = Vector2D.Null; angularVelocity = 0.0} isFixed = value } - /**Gibt an ob die Eigenschaften dieses Koerpers ueberwacht werden sollen. + /**Flag for a world to monitor the properties of this body. * @see World#monitors*/ var monitor: Boolean = false - /**Ermittelt die Position dieses Koerpers. Die Position entspricht dem Schwerpunkt. - * @return Position dieses Koerpers*/ - def pos: Vector2D = // Shwerpunkt = sum(pos*mass)/M + /**Returns the position of this body. The position is equivalent to the center of mass. + * @return position of this body*/ + def pos: Vector2D = // COM = sum(pos*mass)/M (Vector2D.Null /: shapes)((v: Vector2D, s: Shape) => v + s.pos * s.mass) / (0.0 /: shapes)((i: Double, s: Shape) => i + s.mass) - /**Setzt die Position dieses Koerpers und verschiebt dadurch die Positionen seiner Formen. - * @param newPos neue Position*/ + /**Sets the position of this body. By doing so all its shapes are translated. + * @param newPos new position*/ def pos_=(newPos: Vector2D) = { val stepPos = pos shapes.foreach((s: Shape) => s.pos = s.pos - stepPos + newPos) } - /**Enthaelt die aktuelle Rotation dieses Koerpers.*/ - private var _rotation: Double = 0.0 //shapes(0).rotation + /**Contains the current rotation of this body.*/ + private var _rotation: Double = 0.0 - /**Ergibt die aktuelle Rotation dieses Koerpers. - * @return aktuelle Rotation dieses Koerpers*/ + /**Returns the current rotation of this body.*/ def rotation: Double = _rotation - /**Setzt die Rotation dieses Koerpers. Dazu werden auch die Positionen und Rotationen seiner Formen entsprechend veraendert. - * @param r neue Rotation*/ + /**Sets the rotation of this body. Position and rotation of shapes are modified accordingly. + * @param r new rotation*/ def rotation_=(newRotation: Double) = { _rotation = newRotation val stepPos = pos @@ -72,69 +72,67 @@ class Body(shps: Shape*){ } } - /**Lineargeschwindigkeit dieses Koerpers.*/ + /**Linear velocity of this body.*/ var linearVelocity: Vector2D = Vector2D.Null - /**Winkelgeschwindigkeit dieses Koerpers.*/ + /**Angular velocity of this body.*/ var angularVelocity: Double = 0 - /**Lineargeschwindigkeit des gegebenen Punktes auf diesem Koerper. In Weltkoordinaten.*/ + /**Linear velocity of the given point on this body (in world coordinates).*/ def velocityOfPoint(point: Vector2D) = linearVelocity + ((point - pos).leftNormal * angularVelocity) - /**Resultierende Kraft auf den Schwerpunkt dieses Koerpers.*/ + /**Resulting force on the COM of this body.*/ var force: Vector2D = Vector2D.Null - /**Resultierender Drehmoment zu dem Schwerpunkt dieses Koerpers.*/ + /**Resulting torque on this body.*/ var torque: Double = 0 - /**Ergibt die Masse dieses Koerpers. Die Masse ist gleich die Summe aller Massen seiner Formen. - * @return Masse des Koerpers*/ + /**Returns the mass of this body. If the body is free, its mass is the sum of the masses of its shapes. + * If the body is fixed, its mass is infinite (<code>Double.PositiveInfinity</code>). + * @return this body's mass*/ def mass: Double = if (fixed) Double.PositiveInfinity else (0.0 /: shapes)((i: Double, s: Shape) => i + s.mass) - /**Ergibt den Traegheitsmoment zu dem Schwerpunkt dieses Koerpers. Der Traegheitsmoment wird mit Hilfe des Steinerschen Satzes errechnet. - * @return Traegheitsmoment relativ zu dem Schwerpunkt dieses Koerpers*/ + /**Returns the moment of inertia for rotations about the COM of this body. + * It is calculated using the moments of inertia of this body's shapes and the parallel axis theorem. + * If the body is fixed, its moment of inertia is infinite (<code>Double.PositiveInfinity</code>). + * @return moment of inertia for rotations about the COM of this body*/ def I: Double = if (fixed) Double.PositiveInfinity else (0.0 /: (for (s <- shapes) yield (s.I + s.mass * ((s.pos - pos) dot (s.pos - pos)))))(_+_) - /**Wendet eine Kraft auf den Schwerpunkt dieses Koerpers an. - * @param force anzuwendender Kraftvektor*/ + /**Applies a force to the COM of this body. + * @param force applied force*/ def applyForce(force: Vector2D) = if (!fixed) this.force += force - /**Wendet eine Kraft auf einen Punkt dieses Koerpers an. Achtung: der gegebene Punkt wird nicht auf angehoerigkeit dieses - * Koerpers ueberprueft. - * @param force anzuwendender Kraftvektor - * @param point Ortsvektor des Punktes auf den die Kraft wirken soll (gegeben in Weltkoordinaten).*/ + /**Applies a force to a point on this body. Warning: the point is considered to be contained within this body. + * @param force applied force + * @param point position vector of the point (in world coordinates)*/ def applyForce(force: Vector2D, point: Vector2D) = if (!fixed) {this.force += force; torque += (point - pos) cross force} - /**Wendet einen Impuls auf den Schwerpunkt dieses Koerpers an. - * @param impulse anzuwendender Impulsvektor*/ + /**Applies an impulse to the COM of this body. + * @param impulse applied impulse*/ def applyImpulse(impulse: Vector2D) = if (!fixed) linearVelocity += impulse / mass - /**Wendet einen Impuls auf einen Punkt dieses Koerpers an. Achtung: der gegebene Punkt wird nicht auf angehoerigkeit dieses - * Koerpers ueberprueft. - * @param impulse anzuwendender Impulsvektor - * @param point Ortsvektor des Punktes auf den der Impuls wirken soll (gegeben in Weltkoordinaten).*/ + /**Applies an impulse to a point on this body. Warning: the point is considered to be contained within this body. + * @param impulse applied impulse + * @param point position vector of the point (in world coordinates)*/ def applyImpulse(impulse: Vector2D, point: Vector2D) = if (!fixed) {linearVelocity += impulse / mass; angularVelocity += ((point - pos) cross impulse) / I} - /**Ueberprueft ob der gegebene Punkt <code>point</code> sich in diesem Koerper befindet.*/ + /**Checks if the point <code>point</code> is contained in this body.*/ def contains(point: Vector2D) = shapes.exists(_.contains(point)) override def toString: String = { "Body" + uid + " " + shapes + " fixed=" + fixed + " m=" + mass + " I=" + I + " pos=" + pos + " rot=" + rotation + " v=" + linearVelocity + " w=" + angularVelocity + " F=" + force + " tau=" + torque } - /**Erstellt einen neuen Koerper der zusaetzlich die Form <code>s</code> enthaelt. - * @param s zusaetzliche Form - * @return neuer Koerper*/ - def ^(s: Shape) = new Body((s :: shapes): _*) - - /**Erstellt einen neuen Koerper der zusaetzlich die Formen von dem Koerper <code>b</code> enthaelt. - * @param b Koerper mit zusaetzlichen Formen - * @return neuer Koerper*/ - def ^(b: Body) = { - val shapes = this.shapes ::: b.shapes - new Body(shapes: _*) - } + /**Creates a new body containing this body's shapes and the shape <code>s</code>. + * @param s new shape + * @return new body*/ + def ~(s: Shape) = new Body((s :: shapes): _*) + + /**Creates a new body containing this body's shapes and the shapes of another body <code>b</code>. + * @param b body with extra shapes + * @return new body*/ + def ~(b: Body) = new Body((this.shapes ::: b.shapes): _*) } object Body { diff --git a/src/sims/dynamics/Circle.scala b/src/sims/dynamics/Circle.scala index 26f3ad4..b1d3703 100644 --- a/src/sims/dynamics/Circle.scala +++ b/src/sims/dynamics/Circle.scala @@ -10,18 +10,16 @@ import sims.geometry._ import sims.collision._ /** - * Circle ist die Definition eines Kreises. - * @param radius Radius dieses Kreises - * @param density Dichte dieses Kreises + * A circle. + * @param radius radius of this circle + * @param density density of this circle */ -case class Circle(radius: Double, // Radius - density: Double) extends Shape{ // Dichte +case class Circle(radius: Double, density: Double) extends Shape{ val volume = Math.Pi * radius * radius val I = mass * radius * radius / 2 - // AABB(Zentrum - Radius, Zentrum + Radius) def AABB = new AABB(pos - Vector2D(radius,radius), pos + Vector2D(radius,radius)) @@ -32,6 +30,5 @@ case class Circle(radius: Double, // Radius (pos.project(axis).y / axis.y) - radius, (pos.project(axis).y / axis.y) + radius) - //Ist der gegebene punkt im Radius dieses kreises? def contains(point: Vector2D) = (point - pos).length <= radius } diff --git a/src/sims/dynamics/Constraint.scala b/src/sims/dynamics/Constraint.scala index 74c2af3..eaa6952 100644 --- a/src/sims/dynamics/Constraint.scala +++ b/src/sims/dynamics/Constraint.scala @@ -6,14 +6,16 @@ package sims.dynamics -/**Randbedingungen erben von dem Trait <code>Constraint</code>. - * Fuer jeden Constraint koennen Position und Geschwindigkeit korrigiert werden. - * Ihre Implementierung wurde von Erin Catto's box2d inspiriert.*/ +/**All constraints in SiMS implement this trait. + * Position and velocity can be corrected for each constraint. + * The implementation of constraints was inspired by Erin Catto's box2d.*/ trait Constraint { - /**Korrigiert die Geschwindigkeit der Koerper damit diese den Randbedingungen entsprechen.*/ + /**Corrects the velocities of bodies according to this constraint. + * @param h a time interval, used for converting forces and impulses*/ def correctVelocity(h: Double): Unit - /**Korrigiert die Position der Koerper damit diese den Randbedingungen entsprechen.*/ + /**Corrects the positions of bodies according to this constraint. + * @param h a time interval, used for converting forces and impulses*/ def correctPosition(h: Double): Unit } diff --git a/src/sims/dynamics/Rectangle.scala b/src/sims/dynamics/Rectangle.scala index adaa634..89ab4c0 100644 --- a/src/sims/dynamics/Rectangle.scala +++ b/src/sims/dynamics/Rectangle.scala @@ -9,10 +9,10 @@ package sims.dynamics import sims.geometry._ import sims.collision._ -/**Rechteck ist eine Art Polygon. - * @param halfWidth halbe Breite dieses Rechtecks - * @param halfHeight halbe Hoehe dieses Rechtecks - * @param density dichte dieses Rechtecks +/**A rectangle is a polygon. + * @param halfWidth this rectangle's half width + * @param halfHeight this rectangle's half height + * @param density density of this rectangle */ case class Rectangle(halfWidth: Double, halfHeight : Double, @@ -22,19 +22,17 @@ case class Rectangle(halfWidth: Double, val I = 1.0 / 12.0 * mass * ((2 * halfWidth) * (2 * halfWidth) + (2 * halfHeight) * (2 * halfHeight)) - /**Ergibt Vektoren vom Zentrum dieses Rectecks bis zu den Ecken. - * Erste Ecke entspricht der Ecke oben rechts bei einer Rotation von 0. - * Folgende Ecken sind gegen den Uhrzeigersinn geordnet. - * @return Vektoren vom Zentrum dieses Rectecks bis zu den Ecken*/ + /**Returns the vectors from the center to the vertices of this rectangle. + * The first vertex is the upper-right vertex at a rotation of 0. + * Vertices are ordered counter-clockwise.*/ def halfDiags: Array[Vector2D] = Array(Vector2D(halfWidth, halfHeight), Vector2D(-halfWidth, halfHeight), Vector2D(-halfWidth, -halfHeight), Vector2D(halfWidth, -halfHeight)) map (_ rotate rotation) - /**Ergibt die Ortsvektoren der Ecken dieses Rechtecks. - * Erste Ecke entspricht der Ecke oben rechts bei einer Rotation von 0. - * Folgende Ecken sind gegen den Uhrzeigersinn geordnet. - * @return Ortsvektoren der Ecken dieses Rechtecks*/ + /**Returns the position vectors of this rectangle's vertices. + * The first vertex is the upper-right vertex at a rotation of 0. + * Vertices are ordered counter-clockwise.*/ def vertices = for (h <- halfDiags) yield pos + h }
\ No newline at end of file diff --git a/src/sims/dynamics/RegularPolygon.scala b/src/sims/dynamics/RegularPolygon.scala index c5b8a13..b49d100 100644 --- a/src/sims/dynamics/RegularPolygon.scala +++ b/src/sims/dynamics/RegularPolygon.scala @@ -9,17 +9,17 @@ package sims.dynamics import Math._ import sims.geometry._ -/**Ein regelmaessiges Polygon mit <code>n</code> Seiten, dass der Kreis mit radius <code>radius</code> umschreibt. - * @param n Anzahl der Seiten. - * @param radius Radius des umschreibenden Kreises. - * @param density Dichte. - */ +/**A regular polygon with <code>n</code> sides whose excircle has a radius <code>radius</code>. + * @param n nmber of sides. + * @param radius radius of the excircle + * @param density density of this regular polygon + * @throws IllegalArgumentException if <code>n</code> is smaller than 3 */ case class RegularPolygon(n: Int, radius: Double, density: Double) extends Shape with ConvexPolygon{ - require(n >= 3, "Polygon must have at least 3 sides.") + require(n >= 3, "A polygon must have at least 3 sides.") - /**Hoehe eines der konstituierneden Dreiecke des Polygons.*/ + /**Height of one of the constituting triangles.*/ private val h: Double = radius * cos(Pi / n) - /**Halbe Breite eines der konstituierneden Dreiecke des Polygons.*/ + /**Half width of one of the constituting triangles.*/ private val b: Double = radius * sin(Pi / n) def halfDiags = (for (i: Int <- (0 until n).toArray) yield (Vector2D(0, radius) rotate (2 * Pi * i / n))) map (_ rotate rotation) @@ -28,7 +28,7 @@ case class RegularPolygon(n: Int, radius: Double, density: Double) extends Shape val volume = n * h * b - /**Traegheitsmoment eines der konstituierneden Dreiecke im Zentrum des Polygons.*/ + /**Moment of inertia of one of the constituting triangles about the center of this polygon.*/ private val Ic: Double = density * b * (3 * b + 16) * h * h * h * h / 54 val I = n * Ic diff --git a/src/sims/dynamics/Shape.scala b/src/sims/dynamics/Shape.scala index f57bbc6..47a4199 100644 --- a/src/sims/dynamics/Shape.scala +++ b/src/sims/dynamics/Shape.scala @@ -10,78 +10,82 @@ import sims.geometry._ import sims.collision._ /** -* Eine abstrakte Form. +* An abstract shape. */ abstract class Shape{ - /**Einzigartige Identifikationsnummer.*/ + /**Unique identification number.*/ val uid: Int = Shape.nextUid - /**Kollisionsfaehigkeit.*/ + /**Flag determining this shapes ability to collide with other shapes.*/ var collidable: Boolean = true - /**Teil der Stosszahl bei einer Kollision zwischen dieser Form und einer anderen. - * Die Stosszahl wird aus dem Produkt der beiden Teile der Formen errechnet.*/ + /**Part of the coefficient of restitution for a collision between this shape and another. + * The coefficient of restitution is calculated out of the product of this part and the other shape's part.*/ var restitution: Double = 0.7 - /**Teil des Reibungskoeffizienten bei einer Kollision zwischen dieser Form und einer anderen. - * Der Reibungskoeffizient wird aus dem Produkt der beiden Teile der Formen errechnet.*/ + /**Part of the coefficient of friction for a collision between this shape and another. + * The coefficient of friction is calculated out of the product of this part and the other shape's part.*/ var friction: Double = 0.707 - /**Position des Schwerpunktes in Welt.*/ + /**Position of this shape's COM (in world coordinates).*/ var pos: Vector2D = Vector2D.Null - /**Rotation. Entspricht Laenge des Rotationsvektors.*/ + /**Rotation of this shape about its COM.*/ var rotation: Double = 0 - /**Initiale Rotation. (Rotation ohne Koerper)*/ + /**Initial rotation. Rotation of this shape before it was added to a body.*/ var rotation0 = 0.0 - /**Referenzposition in Koerper. Wird zur Rotation von Formen in Koerpern verwendet.*/ + /**Local position of this shape's body COM to its COM at a body rotation of zero.*/ var refLocalPos: Vector2D = Vector2D.Null - /**Dichte. (Masse pro Flaeche)*/ + /**Density. (Mass per area)*/ val density: Double - /**Volumen. Entspricht eigentlich der Flaeche dieser Form (in 2D) wird aber zum Errechnen der Masse verwendet.*/ + /**Volume. The volume is actually equivalent to this shape's area (SiMS is in 2D) + * and is used with this shape's density to calculate its mass.*/ val volume: Double - /**Errechnet die Masse dieser Form. Masse ist gleich Volumen mal Dichte. - @return Masse der Form*/ + /**Returns the mass of this shape. The mass is given by volume times density. + @return mass of this shape*/ def mass = volume * density - /**Errechnet Traegheitsmoment zum Schwerpunkt dieser Form. - @return Traegheitsmoment zum Schwerpunkt*/ + /**Moment of inertia for a rotation about this shape's COM.*/ val I: Double - /**Beinhaltender Koerper. Sollte nicht selbst bei Initialisierung definiert werden.*/ - var body: Body = _ + /**Containing body.*/ + private var _body: Body = _ - /**Gibt das umfassende AABB dieser Form zurueck. - @return umfassendes AABB*/ + /**Returns this shape's containing body.*/ + def body = _body + + /**Sets this shape's containing body.*/ + private[dynamics] def body_=(b: Body) = _body = b + + /**Returns this shape's axis aligned bounding box.*/ def AABB: AABB - /**Ergibt die Projektion dieser Form auf eine Gerade gegeben durch den - * Richtungsvektor <code>axis</code>. - * @param axis Richtungsvektor der Geraden - * @return Projektion dieser Form*/ + /**Returns the projection of this shape onto the line given by the directional vector <code>axis</code>. + * @param axis directional vector of the line + * @return projection of this shape*/ def project(axis: Vector2D): Projection - /**Ermittelt ob der gebene Punkt <code>point</code> in dieser Form enthalten ist.*/ + /**Checks if the point <code>point</code> is contained in this shape.*/ def contains(point: Vector2D): Boolean - /**Baut einen Koerper aus dieser Form. - @return ein Koerper bestehend aus dieser Form. */ + /**Creates a new body made out of tis shape. + @return a body made out of tis shape*/ def asBody = new Body(this) - /**Formen mit denen diese Form nicht Kollidiert.*/ + /**Shapes with which this shape cannot collide.*/ val transientShapes: collection.mutable.Set[Shape] = collection.mutable.Set() - /**Erstellt einen Koerper aus dieser Form und der Form <code>s</code>.*/ - def ^(s: Shape) = new Body(this, s) + /**Creates a new body out of this shape and the shape <code>s</code>.*/ + def ~(s: Shape) = new Body(this, s) - /**Erstellt einen Koerper aus dieser Form und den Formen des Koerpers <code>b</code>.*/ - def ^(b: Body) = { + /**Creates a new body out of this shape and the shapes of body <code>b</code>.*/ + def ~(b: Body) = { val shapes = this :: b.shapes new Body(shapes: _*) } diff --git a/src/sims/dynamics/World.scala b/src/sims/dynamics/World.scala index 7b165f5..0230a50 100644 --- a/src/sims/dynamics/World.scala +++ b/src/sims/dynamics/World.scala @@ -11,104 +11,107 @@ import sims.collision._ import sims.dynamics.joints._ import scala.collection.mutable._ -/**Eine Welt enthaelt und Simuliert ein System aus Koerpern und Verbindungen.*/ +/**A world contains and simulates a system of rigid bodies and joints.*/ class World { - /**Zeitschritt in dem diese Welt die Simulation vorranschreiten laesst.*/ + /**Time intervals in which this world simulates.*/ var timeStep: Double = 1.0 / 60 - /**Anzahl der Constraint-Korrekturen pro Zeitschritt.*/ + /**Number of constraint corrections per time step.*/ var iterations: Int = 10 - /**Schwerkraft die in dieser Welt herrscht.*/ + /**Gravity in this world.*/ var gravity = Vector2D(0, -9.81) - /**Alle Koerper die diese Welt simuliert.*/ + /**Bodies contained in this world.*/ val bodies = new ArrayBuffer[Body] - /**Alle Verbindungen die diese Welt simuliert.*/ + /**Joints contained in this world.*/ val joints = new ArrayBuffer[Joint] - /**Ueberwachungsfunktionen fuer Koerper. + /**Monitoring methods for bodies. * <p> - * Das erste Element des Tuples ist die Ueberschrift und das zweite Element, der Wert.*/ + * 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 <code>monitor</code> field is set to + * <code>true</code> - second position components.*/ val monitors = new ArrayBuffer[(String, Body => String)] - /**Kollisionsdetektor dieser Welt.*/ + /**Collsion detector who manages collision detection in this world.*/ val detector: Detector = new GridDetector(this) - /**Warnung wenn Koerper schneller als Lichtgeschwindigkeit.*/ + /**Warning if a body's velocity exceeds the speed of light.*/ var overCWarning = false - /**Kollisionerkennung.*/ + /**Flag to enable collision detection.*/ var enableCollisionDetection = true - /**Positionskorrekturen.*/ + /**Flag to enable position correction for constraints.*/ var enablePositionCorrection = true - /**Die minimale, nicht als null geltende Geschwindigkeit.*/ + /**Minimal, non-zero linear velocity.*/ var minLinearVelocity: Double = 0.0001 - /**Die minimale, nicht als null geltende Winkelgeschwindigkeit.*/ + /**Minimal, non-zero angular velocity.*/ var minAngularVelocity: Double = 0.0001 - /**Ergibt alle Formen aus allen Koerpern in dieser Welt.*/ + /**Returns all shapes of all bodies in this world.*/ def shapes = for (b <- bodies; s <- b.shapes) yield s - /**Fuegt dieser Welt einen Koerper hinzu.*/ + /**Adds the given body to this world.*/ def +=(body: Body) = bodies += body - /**Fuegt dieser Welt eine Verbindung hinzu.*/ + /**Adds the given joint to this world.*/ def +=(joint: Joint): Unit = joints += joint - /**Fuegt dieser Welt ein vorangefertigtes System vaus Koerpern und Verbindungen hinzu.*/ + /**Adds the given prefabricated system of bodies and joints to this world.*/ def +=(p: 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 - /**Entfernt den gegebenen Koerper aus dieser Welt.*/ + /**Removes the given body from this world.*/ def -=(body: Body): Unit = bodies -= body - /**Entfernt die gegebene Verbindung aus dieser Welt.*/ + /**Removes the given joint from this world.*/ def -=(joint: Joint): Unit = joints -= joint - /**Entfernt das gegebene System aus Koerpern und Verbindungen aus dieser Welt.*/ + /**Removes the given prefabricated system of bodies and joints from this world.*/ def -=(p: 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 - /**Entfernt alle Koerper, Verbindungen und Ueberwachungsfunktionen dieser Welt.*/ + /**Removes all bodies, joints and monitoring methods from this world.*/ def clear() = {joints.clear(); bodies.clear(); monitors.clear()} - /**Aktuelle Zeit in Sekunden dieser Welt. Nach jedem Zeitschritt wird die Zeit erhoeht.*/ + /**Current time in this world.*/ var time: Double = 0.0 - /**Simuliert einen von <code>timeStep</code> angegebenen Zeitschritt. - * Ihre Aufgabe ist es die Koerper dieser Welt so zu simulieren wie diese sich in einer Welt mit den gegebenen - * Bedingungen verhalten wuerden. + /**Simulates a time step of the duration <code>timeStep</code>. * <p> - * Der Zeitschritt wird in folgenden Phasen ausgefuehrt: + * The time step is simulated in the following phases: * <ol> - * <li>Kraefte wirken auf die Koerper (z.B Schwerkraft, andere Kraftfaehige Objekte).</li> - * <li>Beschleunigungen werden integriert.</li> - * <li>Geschwindigkeiten werden korrigiert.</li> - * <li>Geschwindigkeiten werden integriert.</li> - * <li>Positionen werden korrigiert.</li> - * <li>Die Methode <code>postStep()</code> wird ausgefuehrt.</li> + * <li>Forces are applied to bodies.</li> + * <li>Accelerations are integrated.</li> + * <li>Velocities are corrected.</li> + * <li>Velocities are integrated.</li> + * <li>Postions are corrected.</li> + * <li>The method <code>postStep()</code> is executed.</li> * </ol>*/ def step() = { time += timeStep - //Kraftobjekte + //force applying objects for (j <- joints) j match {case f: ForceJoint => f.applyForce; case _ => ()} - //integriert v + //integration of acclerations, yields velocities for (b <- bodies) { val m = b.mass b.applyForce(gravity * b.mass) @@ -118,13 +121,13 @@ class World { b.angularVelocity = b.angularVelocity + alpha * timeStep } - //korrigiert v + //correction of velocities for (i <- 0 until iterations){ for(c <- joints) c.correctVelocity(timeStep) if (enableCollisionDetection) for (c <- detector.collisions) c.correctVelocity(timeStep) } - //integriert pos + //integration of velocities, yields positions for (b <- bodies) { //warning when body gets faster than speed of light if (b.linearVelocity.length >= 300000000) overCWarning = true @@ -136,7 +139,7 @@ class World { b.torque = 0.0 } - //korrigiert pos + //correction of positions if (enablePositionCorrection) for (i <- 0 until iterations){ for (c <- joints) c.correctPosition(timeStep) if (enableCollisionDetection) for (c <- detector.collisions) c.correctPosition(timeStep) @@ -145,10 +148,11 @@ class World { postStep() } - /**Wird nach jedem Zeitschritt ausgefuehrt.*/ + /**Initially empty method that is executed after each time step. This method + * may be overriden to create custom behaviour in a world.*/ def postStep() = {} - /**Ergibt Informationen ueber diese Welt.*/ + /**Returns information about this world.*/ def info = { "Bodies = " + bodies.length + "\n" + "Shapes = " + shapes.length + "\n" + diff --git a/src/sims/dynamics/joints/DistanceJoint.scala b/src/sims/dynamics/joints/DistanceJoint.scala index 2d5633f..efb49e5 100644 --- a/src/sims/dynamics/joints/DistanceJoint.scala +++ b/src/sims/dynamics/joints/DistanceJoint.scala @@ -8,15 +8,15 @@ package sims.dynamics.joints import sims.geometry._ -/** DistanceJoints halten die Bindungspunkte auf ihren Bindungskoerpern bei einem konstanten Abstand. - * @param node1 erster Koerper der Verbindung - * @param anchor1 Bindungspunkt auf Koerper eins - * @param node2 zweiter Koerper der Verbindung - * @param anchor2 Bindungspunkt auf Koerper zwei*/ +/** DistanceJoints keep their connection points at a constant distance. + * @param node1 first associated body + * @param anchor1 first connection point + * @param node2 second associated body + * @param anchor2 second connection point*/ case class DistanceJoint(node1: Body, anchor1: Vector2D, node2: Body, anchor2: Vector2D) extends Joint{ def this(node1: Body, node2: Body) = this(node1, node1.pos, node2, node2.pos) - /**Abstand der beiden Bindungspunkte bei initialisierung (der gewollte Abstand).*/ + /**Distance between the two connection points at initialisation (the desired distance).*/ val distance = (anchor2 - anchor1).length private val a1 = anchor1 - node1.pos @@ -24,16 +24,16 @@ case class DistanceJoint(node1: Body, anchor1: Vector2D, node2: Body, anchor2: V private val initRotation1 = node1.rotation private val initRotation2 = node2.rotation - /**Ergibt den Bindungspunkt auf Koerper eins.*/ + /**Returns the connection point on body one (in world coordinates).*/ def connection1 = (a1 rotate (node1.rotation - initRotation1)) + node1.pos - /**Ergibt den Bindungspunkt auf Koerper zwei.*/ + /**Returns the connection point on body two (in world coordinates).*/ def connection2 = (a2 rotate (node2.rotation - initRotation2)) + node2.pos - /**Relative Position der Bindungspunkte.*/ + /**Relative position of the connection points.*/ def x = connection2 - connection1 - /**Relative Geschwindigkeit der Bindungspunkte.*/ + /**Relative velocity of the connection points.*/ def v = node2.velocityOfPoint(connection2) - node1.velocityOfPoint(connection1) /* x = connection2 - connection1 @@ -45,15 +45,15 @@ case class DistanceJoint(node1: Body, anchor1: Vector2D, node2: Body, anchor2: V * 1/m = J * M^-1 * JT * = 1/m1 * u * u + 1/m2 * u * u + 1/I1 * (r1 cross u)^2 + 1/I2 * (r2 cross u)^2*/ override def correctVelocity(h: Double) = { - val x = this.x //relativer Abstand - val v = this.v //relative Geschwindigkeit - val r1 = (connection1 - node1.pos) //Abstand Punkt-Schwerpunkt, Koerper 1 - val r2 = (connection2 - node2.pos) //Abstand Punkt-Schwerpunkt, Koerper 2 - val cr1 = r1 cross x.unit //Kreuzprodukt - val cr2 = r2 cross x.unit //Kreuzprodukt - val Cdot = x.unit dot v //Velocity-Constraint + val x = this.x //relative position + val v = this.v //relative velocity + val r1 = (connection1 - node1.pos) + val r2 = (connection2 - node2.pos) + val cr1 = r1 cross x.unit + val cr2 = r2 cross x.unit + val Cdot = x.unit dot v //velocity constraint val invMass = 1/node1.mass + 1/node1.I * cr1 * cr1 + 1/node2.mass + 1/node2.I * cr2 * cr2 //=J M^-1 JT - val m = if (invMass == 0.0) 0.0 else 1/invMass //Test um Nulldivision zu vermeiden + val m = if (invMass == 0.0) 0.0 else 1/invMass //avoid division by zero val lambda = -m * Cdot //=-JV/JM^-1JT val impulse = x.unit * lambda //P=J lambda node1.applyImpulse(-impulse, connection1) diff --git a/src/sims/dynamics/joints/ForceJoint.scala b/src/sims/dynamics/joints/ForceJoint.scala index fa17eac..1eed850 100644 --- a/src/sims/dynamics/joints/ForceJoint.scala +++ b/src/sims/dynamics/joints/ForceJoint.scala @@ -6,9 +6,9 @@ package sims.dynamics.joints -/**Eine Verbindung die Kraft auf ihre Bindungskoerper ausueben kann.*/ +/**A joint which can apply a force to its anchor bodies, thus adding or removing energy to the system.*/ trait ForceJoint { - /**Uebt eine Kraft auf die Bindungskoerper aus.*/ + /**Applies a force on the achor bodies.*/ def applyForce(): Unit } diff --git a/src/sims/dynamics/joints/Joint.scala b/src/sims/dynamics/joints/Joint.scala index 9690af2..652df97 100644 --- a/src/sims/dynamics/joints/Joint.scala +++ b/src/sims/dynamics/joints/Joint.scala @@ -9,19 +9,19 @@ package sims.dynamics.joints import sims.geometry._ import sims.dynamics._ -/**Joints sind Verbindungen die die Bewegung zwischen zwei Koerpern einschraenken. - * Ihre Implementierung wurde von Erin Catto's box2d inspiriert.*/ +/**Joints constrain the movement of two bodies. + * Their implementation was inspired by Erin Catto's box2d.*/ abstract class Joint extends Constraint{ - /**Erster Koerper der Verbindung.*/ + /**First body of the joint.*/ val node1: Body - /**Zweiter Koerper der Verbindung.*/ + /**Second body of the joint.*/ val node2: Body - /**Korrigiert die Geschwindigkeit der Koerper damit diese den Randbedingungen der Verbindung entsprechen.*/ + /**Corrects the velocities of this joint's associated bodies.*/ def correctVelocity(h: Double): Unit - /**Korrigiert die Position der Koerper damit diese den Randbedingungen der Verbindung entsprechen.*/ + /**Corrects the positions of this joint's associated bodies.*/ def correctPosition(h: Double): Unit }
\ No newline at end of file diff --git a/src/sims/dynamics/joints/RevoluteJoint.scala b/src/sims/dynamics/joints/RevoluteJoint.scala index 7a7ae1c..66a4c06 100644 --- a/src/sims/dynamics/joints/RevoluteJoint.scala +++ b/src/sims/dynamics/joints/RevoluteJoint.scala @@ -11,7 +11,9 @@ import sims.math._ import sims.dynamics._ import Math._ -/**Ein Gelenk, dass zwei Koerper an einem Punkt verbindet. Inspiriert von JBox2D.*/ +/**A revolute joint that connects two bodies at a singe point. Inspired from JBox2D. + * <b>Warning:</b> there are still several bugs with revolute joints, if they are between two free + * bodies and not connected at their respective COMs.*/ case class RevoluteJoint(node1: Body, node2: Body, anchor: Vector2D) extends Joint{ private val a1 = anchor - node1.pos private val a2 = anchor - node2.pos diff --git a/src/sims/dynamics/joints/SpringJoint.scala b/src/sims/dynamics/joints/SpringJoint.scala index 67ea57f..1267ccf 100644 --- a/src/sims/dynamics/joints/SpringJoint.scala +++ b/src/sims/dynamics/joints/SpringJoint.scala @@ -8,13 +8,13 @@ package sims.dynamics.joints import sims.geometry._ -/**Eine Hooksche Feder. - * @param node1 erster Koerper der Verbindung - * @param anchor1 Bindungspunkt auf Koerper eins - * @param node2 zweiter Koerper der Verbindung - * @param anchor2 Bindungspunkt auf Koerper zwei - * @param springConstant Federkonstante - * @param initialLength Initiallaenge +/**A spring obeying Hooke's law. + * @param node1 first associated body + * @param anchor1 first connection point + * @param node2 second associated body + * @param anchor2 second connection point + * @param springConstant spring constant + * @param initialLength initial length */ case class SpringJoint(node1: Body, anchor1: Vector2D, node2: Body, anchor2: Vector2D, springConstant: Double, initialLength: Double) extends Joint with ForceJoint{ @@ -34,25 +34,25 @@ case class SpringJoint(node1: Body, anchor1: Vector2D, node2: Body, anchor2: Vec private val initRotation1 = node1.rotation private val initRotation2 = node2.rotation - /**Ergibt den Bindungspunkt auf Koerper eins.*/ + /**Returns the connection point on body one (in world coordinates).*/ def connection1 = (a1 rotate (node1.rotation - initRotation1)) + node1.pos - /**Ergibt den Bindungspunkt auf Koerper zwei.*/ + /**Returns the connection point on body two (in world coordinates).*/ def connection2 = (a2 rotate (node2.rotation - initRotation2)) + node2.pos - /**Daempfung.*/ + /**Damping.*/ var damping = 0.0 - /**Relative Position der Bindungspunkte.*/ + /**Relative position of the connection points.*/ def x = connection2 - connection1 - /**Relative Geschwindigkeit der Bindungspunkte.*/ + /**Relative velocity of the connection points.*/ def v = node2.velocityOfPoint(connection2) - node1.velocityOfPoint(connection1) - /**Ergibt die Federkraft nach dem Hookschen Gesetz.*/ + /**Returns the spring force.*/ def force = (x.length - initialLength) * springConstant - /**Uebt die Federkraft auf die Bindungspunkte aus.*/ + /**Applies the spring force to the connection points.*/ def applyForce() = { node1.applyForce(x.unit * force - (v * damping) project x, connection1) node2.applyForce(-x.unit * force - (v * damping) project x, connection2) @@ -61,15 +61,15 @@ case class SpringJoint(node1: Body, anchor1: Vector2D, node2: Body, anchor2: Vec def correctVelocity(h: Double) = { /* - val x = this.x //relativer Abstand - val v = this.v //relative Geschwindigkeit - val r1 = (connection1 - node1.pos) //Abstand Punkt-Schwerpunkt, Koerper 1 - val r2 = (connection2 - node2.pos) //Abstand Punkt-Schwerpunkt, Koerper 2 - val cr1 = r1 cross x.unit //Kreuzprodukt - val cr2 = r2 cross x.unit //Kreuzprodukt - val Cdot = x.unit dot v //Velocity-Constraint + val x = this.x + val v = this.v + val r1 = (connection1 - node1.pos) + val r2 = (connection2 - node2.pos) + val cr1 = r1 cross x.unit + val cr2 = r2 cross x.unit + val Cdot = x.unit dot v val invMass = 1/node1.mass + 1/node1.I * cr1 * cr1 + 1/node2.mass + 1/node2.I * cr2 * cr2 //=J M^-1 JT - val m = if (invMass == 0.0) 0.0 else 1/invMass //Test um Nulldivision zu vermeiden + val m = if (invMass == 0.0) 0.0 else 1/invMass val lambda = Math.min(Math.max(-this.force * h, (-m * Cdot)), this.force * h) println (force * h, -m * Cdot) val impulse = x.unit * lambda diff --git a/src/sims/dynamics/joints/test/PrismaticJoint.scala b/src/sims/dynamics/joints/test/PrismaticJoint.scala index f163261..d4b43b2 100644 --- a/src/sims/dynamics/joints/test/PrismaticJoint.scala +++ b/src/sims/dynamics/joints/test/PrismaticJoint.scala @@ -15,10 +15,8 @@ case class PrismaticJoint(node1: Body, anchor1: Vector2D, node2: Body, anchor2: def connection1 = (a1 rotate (node1.rotation - initRotation1)) + node1.pos def connection2 = (a2 rotate (node2.rotation - initRotation2)) + node2.pos - /**Relative Position der Bindungspunkte.*/ def x = connection2 - connection1 - /**Relative Geschwindigkeit der Bindungspunkte.*/ def v = node2.velocityOfPoint(connection2) - node1.velocityOfPoint(connection1) @@ -28,13 +26,13 @@ case class PrismaticJoint(node1: Body, anchor1: Vector2D, node2: Body, anchor2: } def correctLinear(h: Double) = { - val x = this.x.unit //relativer Abstand + val x = this.x.unit val n0 = x.leftNormal - val v = this.v //relative Geschwindigkeit - val r1 = (connection1 - node1.pos) //Abstand Punkt-Schwerpunkt, Koerper 1 - val r2 = (connection2 - node2.pos) //Abstand Punkt-Schwerpunkt, Koerper 2 - val cr1 = r1 cross n0 //Kreuzprodukt - val cr2 = r2 cross n0 //Kreuzprodukt + val v = this.v + val r1 = (connection1 - node1.pos) + val r2 = (connection2 - node2.pos) + val cr1 = r1 cross n0 + val cr2 = r2 cross n0 val Cdot = n0 dot v val invMass = 1/node1.mass + 1/node1.I * cr1 * cr1 + 1/node2.mass + 1/node2.I * cr2 * cr2 val m = if (invMass == 0.0) 0.0 else 1/invMass @@ -57,13 +55,13 @@ case class PrismaticJoint(node1: Body, anchor1: Vector2D, node2: Body, anchor2: def correctPosition(h: Double) = { /* - val x = this.x.unit //relativer Abstand + val x = this.x.unit val n0 = x.leftNormal - val v = this.v //relative Geschwindigkeit - val r1 = (connection1 - node1.pos) //Abstand Punkt-Schwerpunkt, Koerper 1 - val r2 = (connection2 - node2.pos) //Abstand Punkt-Schwerpunkt, Koerper 2 - val cr1 = r1 cross n0 //Kreuzprodukt - val cr2 = r2 cross n0 //Kreuzprodukt + val v = this.v + val r1 = (connection1 - node1.pos) + val r2 = (connection2 - node2.pos) + val cr1 = r1 cross n0 + val cr2 = r2 cross n0 val C = n0 dot x val invMass = 1/node1.mass + 1/node1.I * cr1 * cr1 + 1/node2.mass + 1/node2.I * cr2 * cr2 val m = if (invMass == 0.0) 0.0 else 1/invMass |