From be2ee4dea92c5a4fc2329959d6e7272c50b221a6 Mon Sep 17 00:00:00 2001 From: Jakob Odersky Date: Sun, 29 Nov 2009 16:15:03 +0000 Subject: Translated comments to english. --- src/graphyx/tests/Friction2.scala | 2 +- src/graphyx/tests/General1.scala | 2 +- src/graphyx/tests/Spring.scala | 4 +- src/sims/collision/AABB.scala | 13 ++- src/sims/collision/CircleCollision.scala | 2 +- src/sims/collision/Collision.scala | 12 +-- src/sims/collision/Detector.scala | 6 +- src/sims/collision/GridDetector.scala | 38 ++++---- src/sims/collision/Pair.scala | 2 +- src/sims/collision/PolyCircleCollision.scala | 4 +- src/sims/collision/PolyCollision.scala | 2 +- src/sims/dynamics/Body.scala | 104 ++++++++++----------- src/sims/dynamics/Circle.scala | 11 +-- src/sims/dynamics/Constraint.scala | 12 ++- src/sims/dynamics/Rectangle.scala | 22 ++--- src/sims/dynamics/RegularPolygon.scala | 18 ++-- src/sims/dynamics/Shape.scala | 70 +++++++------- src/sims/dynamics/World.scala | 84 +++++++++-------- src/sims/dynamics/joints/DistanceJoint.scala | 36 +++---- src/sims/dynamics/joints/ForceJoint.scala | 4 +- src/sims/dynamics/joints/Joint.scala | 12 +-- src/sims/dynamics/joints/RevoluteJoint.scala | 4 +- src/sims/dynamics/joints/SpringJoint.scala | 44 ++++----- src/sims/dynamics/joints/test/PrismaticJoint.scala | 26 +++--- src/sims/geometry/ConvexPolygon.scala | 33 +++---- src/sims/geometry/Projection.scala | 19 ++-- src/sims/geometry/Ray.scala | 20 ++-- src/sims/geometry/Segment.scala | 28 +++--- src/sims/geometry/Vector2D.scala | 60 +++++------- src/sims/materials/Steel.scala | 2 +- src/sims/math/Matrix22.scala | 26 +++--- src/sims/util/Polar.scala | 4 +- src/sims/util/Positioning.scala | 2 +- 33 files changed, 352 insertions(+), 376 deletions(-) (limited to 'src') diff --git a/src/graphyx/tests/Friction2.scala b/src/graphyx/tests/Friction2.scala index 6eb445b..f6731e2 100644 --- a/src/graphyx/tests/Friction2.scala +++ b/src/graphyx/tests/Friction2.scala @@ -26,7 +26,7 @@ object Friction2 extends Test{ ground.rotation = -0.2 world += ground - val b: Body = (new Circle(0.1,10)) ^ (new Circle(0.1,10) {pos = Vector2D(0.2,0)}) ^ (new Circle(0.1,10) {pos = Vector2D(0.4,0)}) + val b: Body = (new Circle(0.1,10)) ~ (new Circle(0.1,10) {pos = Vector2D(0.2,0)}) ~ (new Circle(0.1,10) {pos = Vector2D(0.4,0)}) b.pos = Vector2D(0.1,0.1) world += b } diff --git a/src/graphyx/tests/General1.scala b/src/graphyx/tests/General1.scala index 1a71c24..7d8703b 100644 --- a/src/graphyx/tests/General1.scala +++ b/src/graphyx/tests/General1.scala @@ -103,7 +103,7 @@ object General1 extends Test{ ground.fixed = true world += ground - world += (new Circle(0.1,1) {pos = Vector2D(2,2)}) ^ (new Circle(0.1,1) {pos = Vector2D(2,2.2)}) + world += (new Circle(0.1,1) {pos = Vector2D(2,2)}) ~ (new Circle(0.1,1) {pos = Vector2D(2,2.2)}) } override def fireEvent() = blastBomb diff --git a/src/graphyx/tests/Spring.scala b/src/graphyx/tests/Spring.scala index 4eec8de..439259f 100644 --- a/src/graphyx/tests/Spring.scala +++ b/src/graphyx/tests/Spring.scala @@ -7,8 +7,8 @@ import java.io._ object Spring extends Test{ val title = "Spring" - val fout = new java.io.FileOutputStream("out.csv") - val sout = new java.io.PrintStream(fout) + //val fout = new java.io.FileOutputStream("out.csv") + //val sout = new java.io.PrintStream(fout) val world = new World { override def postStep = { //for (b <- bodies; if (b.monitor)) sout.println(monitors(0)._2(b)) diff --git a/src/sims/collision/AABB.scala b/src/sims/collision/AABB.scala index 51b3e12..ea696f2 100644 --- a/src/sims/collision/AABB.scala +++ b/src/sims/collision/AABB.scala @@ -9,18 +9,17 @@ package sims.collision import geometry._ /** - * Axis Aligned Bounding Boxes, kurz AABBs, sind Rechtecke die eine bestimmte Form umhuellen. - * Da AABBs nach den X- und Y-Achsen orientiert sind, ermoeglichen sie eine schnelle - * und einfache Feststellung ob zwei AABBs sich ueberschneiden. - * @param minVertex Ortsvektor der minimalen Ecke des AABBs - * @param maxVertex Ortsvektor der maximalen Ecke des AABBs + * Axis Aligned Bounding Boxes (AABBs) are rectangles that frame a shape. + * Their X-Axis and Y-Axis orientation makes it easy to test two AABBs for overlap. + * @param minVertex Position vector of the bottom-left vertex + * @param maxVertex Position vector of the upper-right vertex */ case class AABB(val minVertex: Vector2D, val maxVertex: Vector2D) { /** - * Ueberprueft ob dieses AABB sich mit dem AABB box ueberschneidet. - * @param box das mit diesem auf Ueberschneidung zu ueberpruefende AABB*/ + * Checks this AABB with box for overlap. + * @param box AABB with which to check for overlap*/ def overlaps(box: AABB): Boolean = { val d1 = box.minVertex - maxVertex val d2 = minVertex - box.maxVertex diff --git a/src/sims/collision/CircleCollision.scala b/src/sims/collision/CircleCollision.scala index baf401a..e77c8e2 100644 --- a/src/sims/collision/CircleCollision.scala +++ b/src/sims/collision/CircleCollision.scala @@ -9,7 +9,7 @@ package sims.collision import geometry._ import dynamics._ -/**Kollision zwischen zwei Kreisen.*/ +/**Collision between two circles.*/ case class CircleCollision(c1: Circle, c2: Circle) extends Collision { val shape1 = c1 val shape2 = c2 diff --git a/src/sims/collision/Collision.scala b/src/sims/collision/Collision.scala index d674b30..ce09ac2 100644 --- a/src/sims/collision/Collision.scala +++ b/src/sims/collision/Collision.scala @@ -9,19 +9,19 @@ package sims.collision import dynamics._ import geometry._ -/**Kollisionen zwischen zwei Formen enthalten Methoden zur Berrechnen der Kollisionsreaktion.*/ +/**Collision between two shapes. Contains methods to compute the collision response.*/ abstract class Collision extends Constraint { - /**Erste Kollisionsform (Referenz).*/ + /**First colliding shape (reference shape).*/ val shape1: Shape - /**Zweite Kollisionsform (eindringend).*/ + /**Second colliding shape (incident shape).*/ val shape2: Shape - /**Kollisionspunkte.*/ + /**Collision points.*/ val points: Iterable[Vector2D] - /**Normalenvektor zu der Kollisionsebene.*/ + /**Normal vector to the collision face.*/ val normal: Vector2D /* C = delta @@ -103,6 +103,6 @@ abstract class Collision extends Constraint { object Collision { - /**Erlaubte Ueberlappung.*/ + /**Tolerated overlap. Collision response will only be applied if the overlap of two shapes exceeds the tolerated overlap.*/ val ToleratedOverlap: Double = 0.01 } diff --git a/src/sims/collision/Detector.scala b/src/sims/collision/Detector.scala index e847235..0a3ad5b 100644 --- a/src/sims/collision/Detector.scala +++ b/src/sims/collision/Detector.scala @@ -12,12 +12,12 @@ import sims.dynamics._ import scala.collection._ import scala.collection.mutable._ -/**Eine Welt ermittelt ihre Kollisionen durch konkrete Implementierungen dieser Klasse.*/ +/**A world detects its collisions through concrete implementations of this class.*/ abstract class Detector { - /**Die Welt dessen Formen auf Kollisionen ueberprueft werden sollen.*/ + /**The world whose shapes are to be checked for collisions.*/ val world: World - /**Ergibt alle Kollisionen zwischen Formen der Welt world.*/ + /**Returns all collisions between shapes in the world world.*/ def collisions: Seq[Collision] } \ No newline at end of file diff --git a/src/sims/collision/GridDetector.scala b/src/sims/collision/GridDetector.scala index 2c027a3..f5ed99a 100644 --- a/src/sims/collision/GridDetector.scala +++ b/src/sims/collision/GridDetector.scala @@ -11,11 +11,11 @@ import sims.geometry._ import scala.collection._ import scala.collection.mutable._ -/**Eine konkrete Implementierung von Detector. GridDetector ermittelt - * alle Kollisionen mit einem Gittersystem.*/ +/**A conrete implementation of Detector. GridDetector divides the world into a grid + * for faster collision detection.*/ class GridDetector(override val world: World) extends Detector { - /**Array von Kollisionserkennungsmethoden fuer Formenpaare.*/ + /**Array of collision detection methods. These methods return true if two shapes are colliding.*/ val detectionMethods = new ArrayBuffer[PartialFunction[(Shape, Shape), Boolean]] detectionMethods += { case (c1: Circle, c2: Circle) => { //Kollision wenn Distanz <= Summe der Radien @@ -41,7 +41,7 @@ class GridDetector(override val world: World) extends Detector { } } - /**Array von Kollisionsmethoden fuer Formenpaare.*/ + /**Array of methods returning collisions. It is assumed that both shapes are colliding.*/ val collisionMethods = new ArrayBuffer[PartialFunction[(Shape, Shape), Collision]] collisionMethods += { case (c1: Circle, c2: Circle) => CircleCollision(c1, c2) @@ -50,34 +50,34 @@ class GridDetector(override val world: World) extends Detector { case (c: Circle, p: ConvexPolygon) => PolyCircleCollision(p, c) } - /**Gibt an, ob das Formenpaar p kollidiert. - * @param p Formenpaar.*/ + /**Checks the pair of shapes p for collision. + * @param p Pair of shapes.*/ def colliding(p: Pair) = { if (detectionMethods.exists(_.isDefinedAt(p))) detectionMethods.find(_.isDefinedAt(p)).get.apply(p) else throw new IllegalArgumentException("No collision method for colliding pair!") } - /**Gibt die Kollision des Formenpaares p zurueck. - * @param p Formenpaar.*/ + /**Returns the collision between both shapes of the pair p. + * @param p Pair of shapes.*/ def collision(p: Pair): Collision = { if (collisionMethods.exists(_.isDefinedAt(p))) collisionMethods.find(_.isDefinedAt(p)).get.apply(p) else throw new IllegalArgumentException("No collision found in colliding pair!") } - /**Breite und Hoehe einer Gitterzelle.*/ + /**Width and height of a grid cell.*/ var gridSide: Double = 2 - /**Ergibt potenzielle Kollisionspaare der Welt world. + /**Returns potential colliding pairs of shapes of the world world. *

- * Ein Kollisionspaar ist ein Paar aus zwei verschiedenen Formen, das folgenden Bedingungen unterliegt: + * A potential colliding pair is a pair of two shapes that comply with the following criteria: *

*/ def getPairs = { val grid = new HashMap[(Int, Int), List[Shape]] @@ -110,13 +110,13 @@ class GridDetector(override val world: World) extends Detector { private var cache = (world.time, getPairs) - /**Alle potentiellen Kollisionspaare der Welt. + /**All potential colliding pairs of the world. * @see getPairs*/ def pairs = {if (world.time != cache._1) cache = (world.time, getPairs); cache._2} - /**Ergibt alle kollidierenden Paare.*/ + /**Returns all colliding pairs.*/ def collidingPairs: Seq[Pair] = for(p <- pairs; if (colliding(p))) yield p - /**Ergibt alle Kollisionen.*/ + /**Returns all collisions.*/ def collisions: Seq[Collision] = for(p <- pairs; if (colliding(p))) yield collision(p) } diff --git a/src/sims/collision/Pair.scala b/src/sims/collision/Pair.scala index 048748d..e4ae947 100644 --- a/src/sims/collision/Pair.scala +++ b/src/sims/collision/Pair.scala @@ -8,7 +8,7 @@ package sims.collision import sims.dynamics._ -/**Formenpaar.*/ +/**Pair of shapes.*/ case class Pair(s1: Shape, s2: Shape) extends Tuple2(s1, s2){ def this(t: Tuple2[Shape, Shape]) = this(t._1, t._2) diff --git a/src/sims/collision/PolyCircleCollision.scala b/src/sims/collision/PolyCircleCollision.scala index 1bf982e..20f1d49 100644 --- a/src/sims/collision/PolyCircleCollision.scala +++ b/src/sims/collision/PolyCircleCollision.scala @@ -9,7 +9,7 @@ package sims.collision import sims.dynamics._ import sims.geometry._ -/**Kollision zwischen einem konvexen Polygon und einem Kreis.*/ +/**Collision between a convex polygon and a circle.*/ case class PolyCircleCollision(p: ConvexPolygon, c: Circle) extends Collision { require(p.isInstanceOf[Shape]) val shape1 = p.asInstanceOf[Shape] @@ -33,6 +33,4 @@ case class PolyCircleCollision(p: ConvexPolygon, c: Circle) extends Collision { val points = List( c.pos - normal * c.radius ) - - } diff --git a/src/sims/collision/PolyCollision.scala b/src/sims/collision/PolyCollision.scala index 3eeb7ca..b4fa917 100644 --- a/src/sims/collision/PolyCollision.scala +++ b/src/sims/collision/PolyCollision.scala @@ -11,7 +11,7 @@ import sims.dynamics._ import scala.collection.mutable.Map import scala.collection.mutable._ -/**Kollision zwischen zwei konvexen Polygonen.*/ +/**Collision between two convex polygons.*/ case class PolyCollision(p1: ConvexPolygon, p2: ConvexPolygon) extends Collision { require(p1.isInstanceOf[Shape]) require(p2.isInstanceOf[Shape]) 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 (Double.PositiveInfinity). + * @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 (Double.PositiveInfinity). + * @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 point sich in diesem Koerper befindet.*/ + /**Checks if the point point 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 s 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 b 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 s. + * @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 b. + * @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 Constraint. - * 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 n Seiten, dass der Kreis mit radius radius umschreibt. - * @param n Anzahl der Seiten. - * @param radius Radius des umschreibenden Kreises. - * @param density Dichte. - */ +/**A regular polygon with n sides whose excircle has a radius radius. + * @param n nmber of sides. + * @param radius radius of the excircle + * @param density density of this regular polygon + * @throws IllegalArgumentException if n 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 axis. - * @param axis Richtungsvektor der Geraden - * @return Projektion dieser Form*/ + /**Returns the projection of this shape onto the line given by the directional vector axis. + * @param axis directional vector of the line + * @return projection of this shape*/ def project(axis: Vector2D): Projection - /**Ermittelt ob der gebene Punkt point in dieser Form enthalten ist.*/ + /**Checks if the point point 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 s.*/ - def ^(s: Shape) = new Body(this, s) + /**Creates a new body out of this shape and the shape s.*/ + def ~(s: Shape) = new Body(this, s) - /**Erstellt einen Koerper aus dieser Form und den Formen des Koerpers b.*/ - def ^(b: Body) = { + /**Creates a new body out of this shape and the shapes of body b.*/ + 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. *

- * 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 monitor field is set to + * true - 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 timeStep 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 timeStep. *

- * Der Zeitschritt wird in folgenden Phasen ausgefuehrt: + * The time step is simulated in the following phases: *

    - *
  1. Kraefte wirken auf die Koerper (z.B Schwerkraft, andere Kraftfaehige Objekte).
  2. - *
  3. Beschleunigungen werden integriert.
  4. - *
  5. Geschwindigkeiten werden korrigiert.
  6. - *
  7. Geschwindigkeiten werden integriert.
  8. - *
  9. Positionen werden korrigiert.
  10. - *
  11. Die Methode postStep() wird ausgefuehrt.
  12. + *
  13. Forces are applied to bodies.
  14. + *
  15. Accelerations are integrated.
  16. + *
  17. Velocities are corrected.
  18. + *
  19. Velocities are integrated.
  20. + *
  21. Postions are corrected.
  22. + *
  23. The method postStep() is executed.
  24. *
*/ 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. + * Warning: 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 diff --git a/src/sims/geometry/ConvexPolygon.scala b/src/sims/geometry/ConvexPolygon.scala index 7bf881c..cb4a429 100644 --- a/src/sims/geometry/ConvexPolygon.scala +++ b/src/sims/geometry/ConvexPolygon.scala @@ -9,21 +9,20 @@ package sims.geometry import sims.collision._ import sims.geometry._ -/**Gemeinsame Eigenschaften aller konvexen Polygone.*/ +/**Common properties of all convex polygons.*/ trait ConvexPolygon { - /**Ergibt Position aller Ecken dieses Polygons. Die Ecken sind gegen den Uhrzeigersinn folgend. - * @return Ortsvektoren der Ecken*/ + /**Returns positions of all vertices of this Polygon. Vertices are ordered counter-clockwise. + * @return position vectors of the vertices*/ def vertices: Seq[Vector2D] - /**Ergibt alle Seiten dieses Polygons. - * @return Seiten dieses Polygons*/ + /**Returns all sides of this polygon. The sides are ordered counter-clockwise, the first vertex of the side + * giving the side index.*/ def sides = (for (i <- 0 until vertices.length) yield (new Segment(vertices(i), vertices((i + 1) % vertices.length)))).toArray - /**Ergibt die Projektion dieses Polygons auf eine Gerade gegeben durch den - * Richtungsvektor axis - * @param axis Richtungsvektor der Geraden - * @return Projektion dieses Polygons*/ + /**Returns the projection of this polygon onto the line given by the directional vector axis. + * @param axis directional vector of the line + * @return projection of this polygon*/ def project(axis: Vector2D) = { val points = for (v <- vertices) yield {v project axis} val bounds = for (p <- points) yield {if (axis.x != 0) p.x / axis.x else p.y / axis.y} @@ -32,8 +31,7 @@ trait ConvexPolygon { (bounds(0) /: bounds)(Math.max(_,_))) } - /**Errechnet das AABB dieses Polygons - * @return umfassendes AABB + /**Returns this polygon's axis aligned bounding box. * @see collision.AABB*/ def AABB = { val xs = vertices map (_.x) @@ -42,15 +40,12 @@ trait ConvexPolygon { Vector2D(Iterable.max(xs), Iterable.max(ys))) } - /**Ueberprueft ob sich der gegebene Punkt point in diesem Polygon befindet. + /**Checks if the point point is contained in this polygon. *

- * Hierzu wird eine Halbgerade von dem Punkt in Richtung der X-Achse gezogen (koennte aber auch beliebig sein). - * Dann wird die Anzahl der Ueberschneidungen der Halbgeraden mit den Seiten und Ecken des Polygons ermittelt. - * Ist die Anzahl der Ueberschneidungen ungerade, so befindet sich der Punkt in dem Polygon. - * Es gibt jedoch Ausnahmen, und zwar wenn die Halbgerade eine Ecke ueberschneidet, ueberschneidet sie sowohl auch zwei Seiten. - * Daher wird eine generelle Anzahl von Uerberschneidungen errechnet, gegeben durch die Anzahl der Ueberschneidungen mit den Seiten minus - * die mit den Ecken. - * Diese Zahl wird dann wie oben geschildert geprueft.*/ + * A ray is created, originating from the point and following an arbitrary direction (X-Axis was chosen). + * The number of intersections between the ray and this polygon's sides (including vertices) is counted. + * The amount of intersections with vertices is substracted form the previuos number. + * If the latter number is odd, the point is contained in the polygon.*/ def contains(point: Vector2D) = { val r = new Ray(point, Vector2D.i) var intersections = 0 diff --git a/src/sims/geometry/Projection.scala b/src/sims/geometry/Projection.scala index 5f2d0f0..0c340f0 100644 --- a/src/sims/geometry/Projection.scala +++ b/src/sims/geometry/Projection.scala @@ -8,25 +8,26 @@ package sims.geometry import sims.math._ -/**Projektion auf eine Achse. +/**Projection on an axis. *

- * Ueblicherweise werden Projektionen in SiMS fuer Kollisionserkennung benutzt. - * @param axis Achse der Projektion - * @param lower unterer Wert der Projektion - * @param upper oberer Wert der Projektion*/ + * Projections are commonly used in SiMS for collision detection. + * @param axis directional vector of the axis of the projection + * @param lower lower value of the projection + * @param upper upper value of the projection*/ case class Projection(axis: Vector2D, lower: Double, upper: Double) { - require(axis != Vector2D.Null) + require(axis != Vector2D.Null, "A projection's axis cannot be given by a null vector!") - /**Ueberprueft ob sich diese Projektion mit der Projektion other ueberschneidet.*/ + /**Checks this projection for overlap with another projection other. + * @throws IllegalArgumentExcepion if both projections axes aren't the same*/ def overlaps(other: Projection): Boolean = { require(axis == other.axis, "Cannot compare two projections on different axes!") !((other.lower - this.upper) > 0 || (this.lower - other.upper) > 0) } - - /**Ergibt die Ueberlappung dieser Projektion und der Projektion other.*/ + /**Returns the overlap between this projection and another projection other. + * @throws IllegalArgumentExcepion if both projections axes aren't the same*/ def overlap(other: Projection): Double = { require(axis == other.axis, "Cannot compare two projections on different axes!") (Math.max(lower, other.lower) - Math.min(upper, other.upper)).abs diff --git a/src/sims/geometry/Ray.scala b/src/sims/geometry/Ray.scala index c898e03..feb18a9 100644 --- a/src/sims/geometry/Ray.scala +++ b/src/sims/geometry/Ray.scala @@ -9,16 +9,16 @@ package sims.geometry import sims.math._ import Math._ -/**Eine Halbgerade wird definiert durch: - * @param point ein Aufpunkt - * @param direction ein Richtungsvektor*/ +/**A ray. + * @param point a point on the ray + * @param direction this ray's directional vector + * @throws IllegalArgumentException if the directional vector is the null vector*/ case class Ray(point: Vector2D, direction: Vector2D) { - //Ein Nullvektor hat keine Richtung - require(direction != Vector2D.Null) + require(direction != Vector2D.Null, "A ray's direction cannot be given by a null vector!") - /**Ueberprueft ob diese Halbgerade das gegebene Segment ueberschneidet. - * @param das auf Ueberschneidung zu uerberpruefende Segment*/ + /**Checks this ray and the given segment for intersection. + * @param s the segment to test for intersection*/ def intersects(s: Segment) = { val p1 = point val p2 = point + direction @@ -38,11 +38,7 @@ case class Ray(point: Vector2D, direction: Vector2D) { } } - /**Ueberprueft ob diese Halbgerade den gegebenen Punkt enthaelt. - *
- * Hierzu wird der Vektor von dem Ursprungspunkt zu dem zu ueberpruefenden Punkt gebildet. Dieser wird dann mit dem Richtungsvektor - * auf Kolinearitaet geprueft. - * @param p Ortsvektor des oben genannten Punkt*/ + /**Checks if this ray contains the point p.*/ def contains(p: Vector2D) = { val v = p - point p == point || diff --git a/src/sims/geometry/Segment.scala b/src/sims/geometry/Segment.scala index 8700979..4aaec21 100644 --- a/src/sims/geometry/Segment.scala +++ b/src/sims/geometry/Segment.scala @@ -6,37 +6,39 @@ package sims.geometry -/**Ein Segment wird durch seine beiden Extrempunkte gegeben. - * @param vertex1 Ortsvektor des 1. Extrempunkts - * @param vertex2 Ortsvektor des 2. Extrempunkts*/ +/**A segment is given by its vertices. + * @param vertex1 position vector of the first vertex + * @param vertex2 position vector of the second vertex + * @throws IllegalArgumentException if both vertices are equal + */ case class Segment(vertex1: Vector2D, vertex2: Vector2D){ require(vertex1 != vertex2, "A segment must have 2 distinct vertices!") - /**Laenge dieses Segments.*/ + /**Length of this segment.*/ val length = (vertex2 - vertex1).length - /**Vektor von EP1 zu EP2.*/ + /**Vector from vertex1 to vertex2.*/ val d = vertex2 - vertex1 - /**Einheitsrichtungsvektor.*/ + /**Unit directional vector.*/ val d0 = d.unit - /**Normalenvektor. Richtung: 90 Grad rechts zu d.*/ + /**Right normal vector.*/ val n = d.rightNormal - /**Normaleneinheitsvektor. Richtung: 90 Grad rechts zu d.*/ + /**Right normal unit vector.*/ val n0 = n.unit - /**Kleinster Abstand zwischen diesem Segment und dem Punkt p.*/ + /**Smallest distance between this segment and the point point.*/ def distance(point: Vector2D): Double = { - val v = point - vertex1 //Vektor von EP1 zu point + val v = point - vertex1 //vector from vertex1 to point val projection = v project d val alpha = if (d.x != 0) d.x / projection.x else d.y / projection.y - if (alpha >= 0 && projection.length <= length) //Punkt ist naeher zu der Geraden zwischen EP1 und EP2 + if (alpha >= 0 && projection.length <= length) //point is closer to line between vertex1 and vertex2 (v project n0).length - else if (alpha < 0) //Punkt ist naeher zu EP1 + else if (alpha < 0) //point is closer to vertex1 (point - vertex1).length - else if (alpha > 0) //Punkt ist naeher zu EP2 + else if (alpha > 0) //point is closer to vertex2 (point - vertex2).length else throw new IllegalArgumentException("Error occured trying to compute distance between segment and point.") diff --git a/src/sims/geometry/Vector2D.scala b/src/sims/geometry/Vector2D.scala index 03d1ea4..4468a90 100644 --- a/src/sims/geometry/Vector2D.scala +++ b/src/sims/geometry/Vector2D.scala @@ -8,54 +8,40 @@ package sims.geometry import scala.Math._ -/**Ein 2-dimensionaler Vektor. - * @param x 1. Komponente - * @param y 2. Komponente*/ +/**A 2D vector. + * @param x 1st component + * @param y 2nd component*/ case class Vector2D(x: Double, y: Double) { - /**Vektoraddition. - * @param v zu addierender Vektor - * @return dieser Vektor addiert mit v*/ + /**Vector addition.*/ def +(v: Vector2D): Vector2D = Vector2D(x + v.x, y + v.y) - /**Vektorsubstraktion. - * @param v zu substrahierender Vektor - * @return dieser Vektor substrahiert mit v*/ + /**Vector substraction.*/ def -(v: Vector2D): Vector2D = this + (v * -1) - /**Multiplikation mit einem Skalar. - * @param n Faktor - * @return dieser Vektor multipliziert mit n*/ + /**Scalar multiplication.*/ def *(n: Double): Vector2D = Vector2D(x * n, y * n) - /**Division durch ein Skalar. - * @param n Nenner - * @return dieser Vektor dividiert durch n*/ + /**Scalar division.*/ def /(n: Double): Vector2D = this * (1/n) - /**Minusvorzeichen.*/ + /**Unary minus.*/ def unary_- : Vector2D = Vector2D(-x, -y) - /**Skalarprodukt. - * @param v ein anderer Vektor - * @return Skalarprodukt von diesem Vektor mit v*/ + /**Dot product.*/ def dot(v: Vector2D): Double = x * v.x + y * v.y - /**Kreuzprodukt. (Norm des Kreuzproduktes) - * @param v ein anderer Vektor - * @return Norm des Kreuzproduktes dieses Vektors mit v. Die Richtung wuerde der x3-Achse entsprechen.*/ + /**Cross product. Length only because in 2D. The direction would be given by the x3-axis.*/ def cross(v: Vector2D): Double = x * v.y - y * v.x - /**Norm dieses Vektors.*/ + /**Norm or length of this vector.*/ val length: Double = Math.sqrt(x * x + y * y) - /**Einheitsvektor dieses Vektors.*/ + /**Unit vector.*/ def unit: Vector2D = if (!(x == 0.0 && y == 0.0)) Vector2D(x / length, y / length) else throw new IllegalArgumentException("Null vector does not have a unit vector.") - /**Errechnet die Projektion dieses- auf einen anderen Vektor. - * @param v oben gennanter Vektor - * @return Projektion dieses Vektors auf v*/ + /**Returns the projection of this vector onto the vector v.*/ def project(v: Vector2D): Vector2D = { if (v != Vector2D.Null) v * ((this dot v) / (v dot v)) @@ -63,37 +49,35 @@ case class Vector2D(x: Double, y: Double) { Vector2D.Null } - /**Errechnet eine Rotation dieses Vektors. - * @param angle Winkel in Radian - * @return der um angle rad rotierte Vektor*/ + /**Returns a rotation of this vector by angle radian.*/ def rotate(angle: Double): Vector2D = { Vector2D(cos(angle) * x - sin(angle) * y, cos(angle) * y + sin(angle) * x) } - /**Linker Normalenvektor. (-y, x)*/ + /**Left normal vector. (-y, x)*/ def leftNormal: Vector2D = Vector2D(-y, x) - /**Rechter Normalenvektor. (y, -x)*/ + /**Right normal vector. (y, -x)*/ def rightNormal: Vector2D = Vector2D(y, -x) - /**Ueberprueft, ob die Komponenten dieses Vektors gleich Null sind.*/ + /**Checks if this vector is the null vector.*/ def isNull: Boolean = this == Vector2D.Null - /**Ergibt eine Liste der Komponenten dieses Vektors.*/ + /**Returns a list of this vector's components.*/ def components = List(x, y) } -/**Dieses Objekt enthaelt spezielle Vektoren.*/ +/**Contains special vectors.*/ object Vector2D { - /**Nullvektor.*/ + /**Null vector.*/ val Null = Vector2D(0,0) - /**Ein horizontaler Einheitsvektor mit den Komponenten (1;0).*/ + /**Horizontal unit vector. (1,0)*/ val i = Vector2D(1,0) - /**Ein vertikaler Einheitsvektor mit den Komponenten (0;1).*/ + /**Vertical unit vector. (0,1)*/ val j = Vector2D(0,1) } diff --git a/src/sims/materials/Steel.scala b/src/sims/materials/Steel.scala index 71594b8..1d14563 100644 --- a/src/sims/materials/Steel.scala +++ b/src/sims/materials/Steel.scala @@ -2,4 +2,4 @@ package sims.materials object Steel { -} +} \ No newline at end of file diff --git a/src/sims/math/Matrix22.scala b/src/sims/math/Matrix22.scala index b10e02a..54d24c7 100644 --- a/src/sims/math/Matrix22.scala +++ b/src/sims/math/Matrix22.scala @@ -8,22 +8,20 @@ package sims.math import sims.geometry._ -/**Eine 2x2, quadratische Matrix. - * @param c11 Komponente 1,1 - * @param c12 Komponente 1,2 - * @param c21 Komponente 2,1 - * @param c22 Komponente 2,2 +/**A 2x2 matrix. + * @param c11 component 1,1 + * @param c12 component 1,2 + * @param c21 component 2,1 + * @param c22 component 2,2 */ case class Matrix22(c11: Double, c12: Double, c21: Double, c22: Double) { - /**Eine 2x2-dimensionale, quadratische Matrix kann auch mit zwei 2-dimensionalen - * Vektoren erstellt werden. In diesem Fall repraesentiert jeder Vektor eine Spalte. - * @param c1 erste Spalte - * @param c2 zweite Spalte*/ + /**A 2x2 matrix can be created with two 2D vectors. In this case, each column is represented by a vector. + * @param c1 first column + * @param c2 second column*/ def this(c1: Vector2D, c2: Vector2D) = this(c1.x, c2.x, c1.y, c2.y) - /**Ergibt die Determinante dieser Matrix. - * @return Determinante dieser Matrix*/ + /**Determinant of this matrix.*/ def det = c11 * c22 - c21 * c12 /**Addition.*/ @@ -31,17 +29,17 @@ case class Matrix22(c11: Double, c12: Double, c21: Double, c22: Double) { new Matrix22(c11 + m.c11, c12 + m.c12, c21 + m.c21, c22 + m.c22) - /**Multiplikation mit einem Skalar.*/ + /**Scalar multiplication.*/ def *(n: Double) = new Matrix22(c11 * n, c12 * n, c21 * n, c22 * n) - /**Multiplikation mit einer anderen 2x2-Matrix.*/ + /**Matrix multiplication.*/ def *(m: Matrix22) = new Matrix22(c11 * m.c11 + c12 * m.c21, c11 * m.c12 + c12 * m.c22, c21 * m.c11 + c22 * m.c21, c21 * m.c12 + c22 * m.c22) - /**Multiplikation mit einer 2x1-Matrix (2-dimensionaler Vektor).*/ + /**Multiplikation with a 2D vector.*/ def *(v: Vector2D) = new Vector2D(c11 * v.x + c12 * v.y, c21 * v.x + c22 * v.y) diff --git a/src/sims/util/Polar.scala b/src/sims/util/Polar.scala index 931be08..c4009b5 100644 --- a/src/sims/util/Polar.scala +++ b/src/sims/util/Polar.scala @@ -9,9 +9,9 @@ package sims.util import sims.geometry._ import scala.Math._ -/**Polare Koordinaten.*/ +/**Polar coordinates.*/ case class Polar(distance: Double, angle: Double) { - /**Ergibt die Vektorrepraesantation dieser polaren Koordinaten.*/ + /**Returns the vector representation of these polar coordinates.*/ def toCarthesian = Vector2D(distance * sin(angle), distance * cos(angle)) } diff --git a/src/sims/util/Positioning.scala b/src/sims/util/Positioning.scala index 433feaf..cf72276 100644 --- a/src/sims/util/Positioning.scala +++ b/src/sims/util/Positioning.scala @@ -9,7 +9,7 @@ package sims.util import sims.geometry._ import sims.dynamics._ -/**Objekt mit Hiflsfunktionen fuer komfortables Positionieren von Koerpern.*/ +/**Utility functions for comfortable positioning of bodies.*/ object Positioning { implicit def int2RelativeVector(x: Int): RelativeVector = new RelativeVector(x, 0) -- cgit v1.2.3