SimpleNarrowPhase: Replaced if-conditions with pattern matching.
[scd.git] / src / net / habraun / sd / SimpleNarrowPhase.scala
blob9c49849ae80db1d1f1ef71bd67c13ddc3208700f
1 /*
2 Copyright (c) 2009 Hanno Braun <hanno@habraun.net>
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
8 http://www.apache.org/licenses/LICENSE-2.0
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
19 package net.habraun.sd
23 import collision._
24 import math._
28 class SimpleNarrowPhase extends NarrowPhase {
30 var testCircleCircle: CircleCircleTest = new ContinuousCircleCircleTest
31 var testCircleLineSegment: CircleLineSegmentTest = new ContinuousCircleLineSegmentTest
35 def inspectCollision(delta: Double, b1: Body, b2: Body) = b1.shape match {
36 case s1: Circle =>
37 b2.shape match {
38 case s2: Circle =>
39 val c1 = s1
40 val c2 = s2
41 val p1 = b1.position
42 val p2 = b2.position
43 val v1 = b1.velocity
44 val v2 = b2.velocity
46 for (r <- testCircleCircle(c1, c2, p1, p2, v1 * delta, v2 * delta)) yield {
47 Collision(r.t, Contact(b1, b2, r.normal, -r.normal, r.contact))
50 case s2: LineSegment =>
51 val c = s1
52 val ls = s2
53 val pc = b1.position
54 val pls = b2.position
55 val vc = b1.velocity * delta
56 val vls = b2.velocity * delta
58 for (r <- testCircleLineSegment(c, ls, pc, pls, vc, vls)) yield {
59 Collision(r.t, Contact(b1, b2, r.normal, -r.normal, r.contact))
62 case _ =>
63 None
66 case s1: LineSegment =>
67 b2.shape match {
68 case s2: Circle =>
69 val c = s2
70 val ls = s1
71 val pc = b2.position
72 val pls = b1.position
73 val vc = b2.velocity * delta
74 val vls = b1.velocity * delta
76 for (r <- testCircleLineSegment(c, ls, pc, pls, vc, vls)) yield {
77 Collision(r.t, Contact(b1, b2, r.normal, -r.normal, r.contact))
80 case s2: LineSegment =>
81 None
83 case _ =>
84 None
87 case _ =>
88 None