summaryrefslogtreecommitdiff
path: root/src/sims/dynamics/Rectangle.scala
blob: 89ab4c0b35f3808bb708d1b159afb28a6bb5f5a9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
/*
 * Simple Mechanics Simulator (SiMS)
 * copyright (c) 2009 Jakob Odersky
 * made available under the MIT License
*/

package sims.dynamics

import sims.geometry._
import sims.collision._

/**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,
                     density: Double) extends Shape with ConvexPolygon{
  
  val volume = halfWidth * halfHeight * 4
  
  val I = 1.0 / 12.0  * mass * ((2 * halfWidth) * (2 * halfWidth) + (2 * halfHeight) * (2 * halfHeight))
  
  /**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)
  
  /**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
  
}