diff options
author | Jakob Odersky <jodersky@gmail.com> | 2009-11-05 21:02:40 +0000 |
---|---|---|
committer | Jakob Odersky <jodersky@gmail.com> | 2009-11-05 21:02:40 +0000 |
commit | 9d20024aa35cd7f923ebfc1ed9a2ffbf2731da70 (patch) | |
tree | 4e97fadc391b310ee1cc7156fda590dff414b2c3 /src/sims/prefabs | |
parent | 034bc5930ea6f01745f64a6070711d899d2c27ae (diff) | |
download | sims-9d20024aa35cd7f923ebfc1ed9a2ffbf2731da70.tar.gz sims-9d20024aa35cd7f923ebfc1ed9a2ffbf2731da70.tar.bz2 sims-9d20024aa35cd7f923ebfc1ed9a2ffbf2731da70.zip |
Initial import.
Diffstat (limited to 'src/sims/prefabs')
-rw-r--r-- | src/sims/prefabs/Net.scala | 47 | ||||
-rw-r--r-- | src/sims/prefabs/Prefab.scala | 15 | ||||
-rw-r--r-- | src/sims/prefabs/Pylon.scala | 47 | ||||
-rw-r--r-- | src/sims/prefabs/Ragdoll.scala | 43 |
4 files changed, 152 insertions, 0 deletions
diff --git a/src/sims/prefabs/Net.scala b/src/sims/prefabs/Net.scala new file mode 100644 index 0000000..d3f4d57 --- /dev/null +++ b/src/sims/prefabs/Net.scala @@ -0,0 +1,47 @@ +/* + * Simple Mechanics Simulator (SiMS) + * copyright (c) 2009 Jakob Odersky + * made available under the MIT License +*/ + +package sims.prefabs + +import sims.geometry._ +import sims.dynamics._ +import sims.dynamics.joints._ + +class Net(width: Int, height: Int, initPos: Vector2D) extends Prefab { + val nodeDistance: Double = 0.2 + val nodeRadius: Double = 0.05 + val nodeDensity: Double = 4 + + val springConstant: Double = 50 + val springDamping: Double = 0 + + private val connectors: Array[Array[Body]] = + makeConnectors(width, height) + + override val bodies: List[Body] = for (row <- connectors.toList; elem <- row) yield elem + override val joints = connect(connectors) + + private def makeConnectors(w: Int, h: Int): Array[Array[Body]] = { + for(i <- (0 until w).toArray[Int]) yield + for(j <- (0 until h).toArray[Int]) yield + new Body(new Circle(nodeRadius, nodeDensity) {pos = Vector2D(nodeDistance * i, nodeDistance * j) + initPos}) + } + + private def connect(connectors: Array[Array[Body]]): List[DistanceJoint] = { + var r: List[DistanceJoint] = Nil + for(i <- 0 to connectors.length - 1; j <- 0 to connectors(i).length - 1) { + if (i > 0) + r = connect(connectors(i-1)(j),connectors(i)(j)) :: r + if (j > 0) + r = connect(connectors(i)(j-1),connectors(i)(j)) :: r + } + r + } + + private def connect(s1: Body, s2: Body): DistanceJoint = + new DistanceJoint(s1, s1.pos, s2, s2.pos) + +}
\ No newline at end of file diff --git a/src/sims/prefabs/Prefab.scala b/src/sims/prefabs/Prefab.scala new file mode 100644 index 0000000..84bb3b7 --- /dev/null +++ b/src/sims/prefabs/Prefab.scala @@ -0,0 +1,15 @@ +/* + * Simple Mechanics Simulator (SiMS) + * copyright (c) 2009 Jakob Odersky + * made available under the MIT License +*/ + +package sims.prefabs + +import sims.dynamics._ +import sims.dynamics.joints._ + +trait Prefab { + val bodies: Iterable[Body] = Nil + val joints: Iterable[Joint] = Nil +} diff --git a/src/sims/prefabs/Pylon.scala b/src/sims/prefabs/Pylon.scala new file mode 100644 index 0000000..7f3211f --- /dev/null +++ b/src/sims/prefabs/Pylon.scala @@ -0,0 +1,47 @@ +/* + * Simple Mechanics Simulator (SiMS) + * copyright (c) 2009 Jakob Odersky + * made available under the MIT License +*/ + +package sims.prefabs + +import sims.geometry._ +import sims.dynamics._ +import sims.dynamics.joints._ + +class Pylon extends Prefab{ + val position: Vector2D = Vector2D(2,1) + val nodeDensity: Double = 100 + val beamHeight: Double = 1 + val beamWidth: Double = 0.5 + val beamNumber: Int = 10 + + private val nodeRow1 = (for (i <- 0 to beamNumber) yield (new Circle(0.01, nodeDensity) { + pos = position + Vector2D(0, i * beamHeight)}).asBody).toList + private val nodeRow2 = (for (i <- 0 to beamNumber) yield (new Circle(0.01, nodeDensity) { + pos = position + Vector2D(beamWidth, i * beamHeight)}).asBody).toList + + private val beamRow1 = (for (i <- 0 until nodeRow1.length - 1) yield + new DistanceJoint(nodeRow1(i), nodeRow1(i).pos, nodeRow1(i+1), nodeRow1(i + 1).pos)).toList + private val beamRow2 = (for (i <- 0 until nodeRow1.length - 1) yield + new DistanceJoint(nodeRow2(i), nodeRow2(i).pos, nodeRow2(i+1), nodeRow2(i + 1).pos)).toList + + private val latBeams = (for (i <- 0 to beamNumber) yield + new DistanceJoint(nodeRow1(i), nodeRow2(i))).toList + private val diagBeams1 = (for (i <- 0 until beamNumber) yield + new DistanceJoint(nodeRow1(i), nodeRow2(i + 1))).toList + private val diagBeams2 = (for (i <- 0 until beamNumber) yield + new DistanceJoint(nodeRow2(i), nodeRow1(i + 1))).toList + + + + + lazy val nodes = nodeRow1 ++ nodeRow2 + lazy val beams = beamRow1 ++ beamRow2 ++ latBeams ++ diagBeams1 ++ diagBeams2 + + override val bodies = nodes + override val joints = beams + + +} diff --git a/src/sims/prefabs/Ragdoll.scala b/src/sims/prefabs/Ragdoll.scala new file mode 100644 index 0000000..ad58653 --- /dev/null +++ b/src/sims/prefabs/Ragdoll.scala @@ -0,0 +1,43 @@ +/* + * Simple Mechanics Simulator (SiMS) + * copyright (c) 2009 Jakob Odersky + * made available under the MIT License +*/ + +package sims.prefabs + +import sims.dynamics._ +import sims.dynamics.joints._ +import sims.geometry._ + +class Ragdoll(position: Vector2D) extends Prefab { + val headTorso = new Body(new Circle(0.1, 1) {pos = position}, + new Rectangle(0.09, 0.35, 1) {pos = position + Vector2D(0, -0.45)}) + val thigh1 = (new Rectangle(0.075, 0.17, 1) {pos = position + Vector2D(0, -0.97)}).asBody + val thigh2 = (new Rectangle(0.075, 0.17, 1) {pos = position + Vector2D(0, -0.97)}).asBody + val tibia1 = (new Rectangle(0.075, 0.25, 1) {pos = position + Vector2D(0, -1.39)}).asBody + val tibia2 = (new Rectangle(0.075, 0.25, 1) {pos = position + Vector2D(0, -1.39)}).asBody + val foot1 = (new Circle(0.08, 1) {pos = position + Vector2D(0, -1.72)}).asBody + val foot2 = (new Circle(0.08, 1) {pos = position + Vector2D(0, -1.72)}).asBody + val upperArm1 = (new Rectangle(0.17, 0.06, 1) {pos = position + Vector2D(0.17, -0.16)}).asBody + val upperArm2 = (new Rectangle(0.17, 0.06, 1) {pos = position + Vector2D(-0.17, -0.16)}).asBody + val forearm1 = (new Rectangle(0.15, 0.06, 1) {pos = position + Vector2D(0.49, -0.16)}).asBody + val forearm2 = (new Rectangle(0.15, 0.06, 1) {pos = position + Vector2D(-0.49, -0.16)}).asBody + val hand1 = (new Circle(0.07, 1) {pos = position + Vector2D(0.71, -0.16)}).asBody + val hand2 = (new Circle(0.07, 1) {pos = position + Vector2D(-0.71, -0.16)}).asBody + + override val bodies = List(headTorso, + thigh1, thigh2, + tibia1, tibia2, + foot1, foot2, + upperArm1, upperArm2, + forearm1, forearm2, + hand1, hand2) + private val shapes = bodies.flatMap(_.shapes) + for (s <- shapes) s.transientShapes ++= shapes + + val shoulder1 = RevoluteJoint(headTorso, upperArm1, position + Vector2D(0, -0.16)) + val shoulder2 = RevoluteJoint(headTorso, upperArm2, position + Vector2D(0, -0.16)) + override val joints = List(shoulder1, shoulder2) + +} |