From 92b66c9e014ceeba7c953077936b2f25c767969f Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Wed, 11 Mar 2009 13:28:08 +0100 Subject: [PATCH] Added the class TestResult in the new package sd.collision. TestResult will be used to return results of Shape-Shape tests. --- src/net/habraun/sd/collision/TestResult.scala | 50 ++++++++++++++ test/net/habraun/sd/collision/TestResultTest.scala | 79 ++++++++++++++++++++++ 2 files changed, 129 insertions(+) create mode 100644 src/net/habraun/sd/collision/TestResult.scala create mode 100644 test/net/habraun/sd/collision/TestResultTest.scala diff --git a/src/net/habraun/sd/collision/TestResult.scala b/src/net/habraun/sd/collision/TestResult.scala new file mode 100644 index 0000000..389169a --- /dev/null +++ b/src/net/habraun/sd/collision/TestResult.scala @@ -0,0 +1,50 @@ +/* + Copyright (c) 2009 Hanno Braun + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + + + +package net.habraun.sd.collision + + + +import math._ + + + +/** + * This is the result of a collision test between two shapes. + * A test result has the following attributes: + * * t: The time of impact. This is a value between 0.0 (beginning of the movement) and 1.0 (end of the + * movement). + * * contact: The point of contact between the two shapes. + * * normal: The first shape's surface normal at the point of contact. + */ + +case class TestResult(t: Double, contact: Vec2D, normal: Vec2D) { + // Check if t is valid. + if (t < 0.0 || t > 1.0) + throw new IllegalArgumentException("The time must be between 0.0 and 1.0 (t: " + t + ").") + + // Check if contact is valid. + if (contact == null) + throw new NullPointerException("Contact point must not be null.") + + // Check if normal is valid. + if (normal == null) + throw new NullPointerException("Normal must not be null.") + if (!normal.unit) + throw new IllegalArgumentException("Normal must be a unit vector (normal: " + normal + ").") +} diff --git a/test/net/habraun/sd/collision/TestResultTest.scala b/test/net/habraun/sd/collision/TestResultTest.scala new file mode 100644 index 0000000..38ce2a6 --- /dev/null +++ b/test/net/habraun/sd/collision/TestResultTest.scala @@ -0,0 +1,79 @@ +/* + Copyright (c) 2009 Hanno Braun + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + + + +package net.habraun.sd.collision + + + +import math._ + +import org.junit._ +import org.junit.Assert._ + + + +class TestResultTest { + + @Test + def verifyHasAttributes { + TestResult(0.5, Vec2D(5, 5), Vec2D(1, 0)) + } + + + + @Test { val expected = classOf[IllegalArgumentException] } + def createWithNegativeT { + TestResult(-1.0, Vec2D(5, 5), Vec2D(1, 0)) + } + + + + @Test { val expected = classOf[IllegalArgumentException] } + def createWithTGreater1 { + TestResult(1.1, Vec2D(5, 5), Vec2D(1, 0)) + } + + + + @Test { val expected = classOf[NullPointerException] } + def createWithContactNull { + TestResult(0.5, null, Vec2D(1, 0)) + } + + + + @Test { val expected = classOf[NullPointerException] } + def createWithNormalNull { + TestResult(0.5, Vec2D(5, 5), null) + } + + + + @Test { val expected = classOf[IllegalArgumentException] } + def createWithNonNormalNormal { + TestResult(0.5, Vec2D(5, 5), Vec2D(1, 1)) + } + + + + @Test + def createWithNonNormalNormalExpectTolerance { + TestResult(0.5, Vec2D(5, 5), Vec2D(1.02, 0)) + TestResult(0.5, Vec2D(5, 5), Vec2D(0.98, 0)) + } +} -- 2.11.4.GIT