| 1 |
/* Copyright (c) 2013 Scott Lembcke and Howling Moon Software |
| 2 |
* |
| 3 |
* Permission is hereby granted, free of charge, to any person obtaining a copy |
| 4 |
* of this software and associated documentation files (the "Software"), to deal |
| 5 |
* in the Software without restriction, including without limitation the rights |
| 6 |
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 7 |
* copies of the Software, and to permit persons to whom the Software is |
| 8 |
* furnished to do so, subject to the following conditions: |
| 9 |
* |
| 10 |
* The above copyright notice and this permission notice shall be included in |
| 11 |
* all copies or substantial portions of the Software. |
| 12 |
* |
| 13 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 14 |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 15 |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 16 |
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 17 |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 18 |
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 19 |
* SOFTWARE. |
| 20 |
*/ |
| 21 |
|
| 22 |
#import <XCTest/XCTest.h> |
| 23 |
#import "ObjectiveChipmunk/ObjectiveChipmunk.h" |
| 24 |
#import "ObjectiveChipmunk/ChipmunkAutoGeometry.h" |
| 25 |
|
| 26 |
|
| 27 |
@interface MiscTest : XCTestCase {} |
| 28 |
@end |
| 29 |
|
| 30 |
@implementation MiscTest |
| 31 |
|
| 32 |
#define AssertNearlyZero(__exp__) XCTAssertTrue((__exp__) < 1e-5, @"") |
| 33 |
|
| 34 |
-(void)testSlerp { |
| 35 |
{ |
| 36 |
cpVect a = cpvmult(cpvforangle(0.0), 1.0); |
| 37 |
cpVect b = cpvmult(cpvforangle(1.0), 1.0); |
| 38 |
cpVect c = a; |
| 39 |
cpVect v = cpvslerp(a, b, 0.0); |
| 40 |
AssertNearlyZero(cpvdist(v, c)); |
| 41 |
} |
| 42 |
|
| 43 |
{ |
| 44 |
cpVect a = cpvmult(cpvforangle(0.0), 1.0); |
| 45 |
cpVect b = cpvmult(cpvforangle(1.0), 1.0); |
| 46 |
cpVect c = b; |
| 47 |
cpVect v = cpvslerp(a, b, 1.0); |
| 48 |
AssertNearlyZero(cpvdist(v, c)); |
| 49 |
} |
| 50 |
|
| 51 |
{ |
| 52 |
cpVect a = cpvmult(cpvforangle(0.0), 1.0); |
| 53 |
cpVect b = cpvmult(cpvforangle(1.0), 1.0); |
| 54 |
cpVect c = cpvmult(cpvforangle(0.5), 1.0); |
| 55 |
cpVect v = cpvslerp(a, b, 0.5); |
| 56 |
AssertNearlyZero(cpvdist(v, c)); |
| 57 |
} |
| 58 |
|
| 59 |
{ |
| 60 |
cpVect a = cpvmult(cpvforangle(-1.0), 1.0); |
| 61 |
cpVect b = cpvmult(cpvforangle( 1.0), 1.0); |
| 62 |
cpVect c = cpvmult(cpvforangle( 0.0), 1.0); |
| 63 |
cpVect v = cpvslerp(a, b, 0.5); |
| 64 |
AssertNearlyZero(cpvdist(v, c)); |
| 65 |
} |
| 66 |
|
| 67 |
{ |
| 68 |
cpVect a = cpvmult(cpvforangle(0.0), 1.0); |
| 69 |
cpVect b = cpvmult(cpvforangle(CP_PI/2.0), 2.0); |
| 70 |
cpVect c = cpvadd(cpvmult(a, cpfcos(CP_PI/4.0)), cpvmult(b, cpfsin(CP_PI/4.0))); |
| 71 |
cpVect v = cpvslerp(a, b, 0.5); |
| 72 |
AssertNearlyZero(cpvdist(v, c)); |
| 73 |
} |
| 74 |
|
| 75 |
{ |
| 76 |
cpVect a = cpvmult(cpvforangle(0.0), 1.0); |
| 77 |
cpVect b = a; |
| 78 |
cpVect c = a; |
| 79 |
cpVect v = cpvslerp(a, b, 0.5); |
| 80 |
AssertNearlyZero(cpvdist(v, c)); |
| 81 |
} |
| 82 |
|
| 83 |
// TODO should it handle this? |
| 84 |
// { |
| 85 |
// cpVect a = cpv( 1.0, 0.01); |
| 86 |
// cpVect b = cpv(-1.0, 0.0); |
| 87 |
// cpVect v = cpvslerp(a, b, 0.5); |
| 88 |
// GHAssertLessThan(cpvdot(a, v)); |
| 89 |
// GHAssertLessThan(cpvdot(b, v)); |
| 90 |
// GHAssertLessThan(cpvlength(v) - 1.0); |
| 91 |
// } |
| 92 |
|
| 93 |
// Slerp const |
| 94 |
{ |
| 95 |
cpVect a = cpvmult(cpvforangle(0.0), 1.0); |
| 96 |
cpVect b = cpvmult(cpvforangle(CP_PI/2.0), 1.0); |
| 97 |
cpVect c = cpvadd(cpvmult(a, cpfcos(CP_PI/4.0)), cpvmult(b, cpfsin(CP_PI/4.0))); |
| 98 |
cpVect v = cpvslerpconst(a, b, CP_PI/4.0); |
| 99 |
AssertNearlyZero(cpvdist(v, c)); |
| 100 |
} |
| 101 |
|
| 102 |
{ |
| 103 |
cpVect a = cpvmult(cpvforangle(0.0), 1.0); |
| 104 |
cpVect b = cpvmult(cpvforangle(CP_PI/2.0), 1.0); |
| 105 |
cpVect c = b; |
| 106 |
cpVect v = cpvslerpconst(a, b, CP_PI/2.0); |
| 107 |
AssertNearlyZero(cpvdist(v, c)); |
| 108 |
} |
| 109 |
|
| 110 |
{ |
| 111 |
cpVect a = cpvmult(cpvforangle(0.0), 1.0); |
| 112 |
cpVect b = cpvmult(cpvforangle(CP_PI/2.0), 1.0); |
| 113 |
cpVect c = b; |
| 114 |
cpVect v = cpvslerpconst(a, b, INFINITY); |
| 115 |
AssertNearlyZero(cpvdist(v, c)); |
| 116 |
} |
| 117 |
|
| 118 |
{ |
| 119 |
cpVect a = cpvmult(cpvforangle(0.0), 1.0); |
| 120 |
cpVect b = cpvmult(cpvforangle(CP_PI/2.0), 1.0); |
| 121 |
cpVect c = a; |
| 122 |
cpVect v = cpvslerpconst(a, b, 0); |
| 123 |
AssertNearlyZero(cpvdist(v, c)); |
| 124 |
} |
| 125 |
|
| 126 |
{ |
| 127 |
cpVect a = cpvmult(cpvforangle(0.0), 1.0); |
| 128 |
cpVect b = cpvmult(cpvforangle(CP_PI/2.0), 1.0); |
| 129 |
cpVect c = cpvmult(cpvforangle(CP_PI/4.0), 1.0); |
| 130 |
cpVect v = cpvslerpconst(a, b, CP_PI/4.0); |
| 131 |
AssertNearlyZero(cpvdist(v, c)); |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
-(void)testBBSegmentQuery |
| 136 |
{ |
| 137 |
{ // left |
| 138 |
cpVect a = cpv(-2, 0); |
| 139 |
cpVect b = cpv( 0, 0); |
| 140 |
cpBB bb = cpBBNew(-1, -1, 1, 1); |
| 141 |
cpFloat t = cpBBSegmentQuery(bb, a, b); |
| 142 |
XCTAssertEqualWithAccuracy(t, 0.5, 1e-5); |
| 143 |
} |
| 144 |
|
| 145 |
{ // right |
| 146 |
cpVect a = cpv( 2, 0); |
| 147 |
cpVect b = cpv( 0, 0); |
| 148 |
cpBB bb = cpBBNew(-1, -1, 1, 1); |
| 149 |
cpFloat t = cpBBSegmentQuery(bb, a, b); |
| 150 |
XCTAssertEqualWithAccuracy(t, 0.5, 1e-5); |
| 151 |
} |
| 152 |
|
| 153 |
{ // bottom |
| 154 |
cpVect a = cpv(0, -2); |
| 155 |
cpVect b = cpv(0, 0); |
| 156 |
cpBB bb = cpBBNew(-1, -1, 1, 1); |
| 157 |
cpFloat t = cpBBSegmentQuery(bb, a, b); |
| 158 |
XCTAssertEqualWithAccuracy(t, 0.5, 1e-5); |
| 159 |
} |
| 160 |
|
| 161 |
{ // top |
| 162 |
cpVect a = cpv(0, 2); |
| 163 |
cpVect b = cpv(0, 0); |
| 164 |
cpBB bb = cpBBNew(-1, -1, 1, 1); |
| 165 |
cpFloat t = cpBBSegmentQuery(bb, a, b); |
| 166 |
XCTAssertEqualWithAccuracy(t, 0.5, 1e-5); |
| 167 |
} |
| 168 |
|
| 169 |
{ // diagonal corner |
| 170 |
cpVect a = cpv(-2, -2); |
| 171 |
cpVect b = cpv( 0, 0); |
| 172 |
cpBB bb = cpBBNew(-1, -1, 1, 1); |
| 173 |
cpFloat t = cpBBSegmentQuery(bb, a, b); |
| 174 |
XCTAssertEqualWithAccuracy(t, 0.5, 1e-5); |
| 175 |
} |
| 176 |
|
| 177 |
{ // diagonal edge |
| 178 |
cpVect a = cpv(-2, -1); |
| 179 |
cpVect b = cpv( 0, 1); |
| 180 |
cpBB bb = cpBBNew(-1, -1, 1, 1); |
| 181 |
cpFloat t = cpBBSegmentQuery(bb, a, b); |
| 182 |
XCTAssertEqualWithAccuracy(t, 0.5, 1e-5); |
| 183 |
} |
| 184 |
|
| 185 |
{ // x-aligned low. |
| 186 |
cpVect a = cpv(-2, -1); |
| 187 |
cpVect b = cpv( 0, -1); |
| 188 |
cpBB bb = cpBBNew(-1, -1, 1, 1); |
| 189 |
cpFloat t = cpBBSegmentQuery(bb, a, b); |
| 190 |
XCTAssertEqual(t, 0.5); |
| 191 |
} |
| 192 |
|
| 193 |
{ // x-aligned high. |
| 194 |
cpVect a = cpv(-2, 1); |
| 195 |
cpVect b = cpv( 0, 1); |
| 196 |
cpBB bb = cpBBNew(-1, -1, 1, 1); |
| 197 |
cpFloat t = cpBBSegmentQuery(bb, a, b); |
| 198 |
XCTAssertEqual(t, 0.5); |
| 199 |
} |
| 200 |
|
| 201 |
{ // y-aligned low. |
| 202 |
cpVect a = cpv(-1, -2); |
| 203 |
cpVect b = cpv(-1, 0); |
| 204 |
cpBB bb = cpBBNew(-1, -1, 1, 1); |
| 205 |
cpFloat t = cpBBSegmentQuery(bb, a, b); |
| 206 |
XCTAssertEqual(t, 0.5); |
| 207 |
} |
| 208 |
|
| 209 |
{ // y-aligned high. |
| 210 |
cpVect a = cpv(1, -2); |
| 211 |
cpVect b = cpv(1, 0); |
| 212 |
cpBB bb = cpBBNew(-1, -1, 1, 1); |
| 213 |
cpFloat t = cpBBSegmentQuery(bb, a, b); |
| 214 |
XCTAssertEqual(t, 0.5); |
| 215 |
} |
| 216 |
|
| 217 |
{ // Miss, x-aligned low. |
| 218 |
cpVect a = cpv( 2, -2); |
| 219 |
cpVect b = cpv( 0, -2); |
| 220 |
cpBB bb = cpBBNew(-1, -1, 1, 1); |
| 221 |
cpFloat t = cpBBSegmentQuery(bb, a, b); |
| 222 |
XCTAssertEqual(t, INFINITY); |
| 223 |
} |
| 224 |
|
| 225 |
{ // Miss, x-aligned high. |
| 226 |
cpVect a = cpv( 2, 2); |
| 227 |
cpVect b = cpv( 0, 2); |
| 228 |
cpBB bb = cpBBNew(-1, -1, 1, 1); |
| 229 |
cpFloat t = cpBBSegmentQuery(bb, a, b); |
| 230 |
XCTAssertEqual(t, INFINITY); |
| 231 |
} |
| 232 |
|
| 233 |
{ // Miss, y-aligned low. |
| 234 |
cpVect a = cpv(-2, 2); |
| 235 |
cpVect b = cpv(-2, 0); |
| 236 |
cpBB bb = cpBBNew(-1, -1, 1, 1); |
| 237 |
cpFloat t = cpBBSegmentQuery(bb, a, b); |
| 238 |
XCTAssertEqual(t, INFINITY); |
| 239 |
} |
| 240 |
|
| 241 |
{ // Miss, y-aligned high. |
| 242 |
cpVect a = cpv(2, 2); |
| 243 |
cpVect b = cpv(2, 0); |
| 244 |
cpBB bb = cpBBNew(-1, -1, 1, 1); |
| 245 |
cpFloat t = cpBBSegmentQuery(bb, a, b); |
| 246 |
XCTAssertEqual(t, INFINITY); |
| 247 |
} |
| 248 |
} |
| 249 |
|
| 250 |
-(void)testImageSamplerLA |
| 251 |
{ |
| 252 |
{ |
| 253 |
NSBundle *bundle = [NSBundle bundleForClass:self.class]; |
| 254 |
CGImageRef image = [ChipmunkImageSampler loadImage:[bundle URLForResource:@"TestImageLA" withExtension:@"png"]]; |
| 255 |
ChipmunkAbstractSampler *sampler = [[ChipmunkImageSampler alloc] initWithImage:image isMask:TRUE contextWidth:0 contextHeight:0]; |
| 256 |
|
| 257 |
XCTAssertEqualWithAccuracy([sampler sample:cpv(0.5, 0.5)], (cpFloat)0.0, 1e-5, @""); |
| 258 |
XCTAssertEqualWithAccuracy([sampler sample:cpv(0.5, 3.5)], (cpFloat)1.0, 1e-5, @""); |
| 259 |
XCTAssertEqualWithAccuracy([sampler sample:cpv(3.5, 0.5)], (cpFloat)1.0, 1e-5, @""); |
| 260 |
|
| 261 |
XCTAssertEqualWithAccuracy([sampler sample:cpv(2.0 - 1e-5, 0.5)], (cpFloat)0.0, 1e-5, @""); |
| 262 |
XCTAssertEqualWithAccuracy([sampler sample:cpv(2.0 + 1e-5, 0.5)], (cpFloat)1.0, 1e-5, @""); |
| 263 |
|
| 264 |
XCTAssertEqualWithAccuracy([sampler sample:cpv(0.5, 2.0 - 1e-5)], (cpFloat)0.0, 1e-5, @""); |
| 265 |
XCTAssertEqualWithAccuracy([sampler sample:cpv(0.5, 2.0 + 1e-5)], (cpFloat)1.0, 1e-5, @""); |
| 266 |
|
| 267 |
[sampler release]; |
| 268 |
} |
| 269 |
|
| 270 |
{ |
| 271 |
NSBundle *bundle = [NSBundle bundleForClass:self.class]; |
| 272 |
CGImageRef image = [ChipmunkImageSampler loadImage:[bundle URLForResource:@"TestImageLA" withExtension:@"png"]]; |
| 273 |
ChipmunkAbstractSampler *sampler = [[ChipmunkImageSampler alloc] initWithImage:image isMask:FALSE contextWidth:0 contextHeight:0]; |
| 274 |
|
| 275 |
XCTAssertEqualWithAccuracy([sampler sample:cpv(0.5, 0.5)], (cpFloat)1.0, 1e-5, @""); |
| 276 |
XCTAssertEqualWithAccuracy([sampler sample:cpv(0.5, 3.5)], (cpFloat)1.0, 1e-5, @""); |
| 277 |
XCTAssertEqualWithAccuracy([sampler sample:cpv(3.5, 0.5)], (cpFloat)1.0, 1e-5, @""); |
| 278 |
XCTAssertEqualWithAccuracy([sampler sample:cpv(3.5, 3.5)], (cpFloat)0.0, 1e-5, @""); |
| 279 |
|
| 280 |
XCTAssertEqualWithAccuracy([sampler sample:cpv(2.0 - 1e-5, 3.5)], (cpFloat)1.0, 1e-5, @""); |
| 281 |
XCTAssertEqualWithAccuracy([sampler sample:cpv(2.0 + 1e-5, 3.5)], (cpFloat)0.0, 1e-5, @""); |
| 282 |
|
| 283 |
XCTAssertEqualWithAccuracy([sampler sample:cpv(3.5, 2.0 - 1e-5)], (cpFloat)1.0, 1e-5, @""); |
| 284 |
XCTAssertEqualWithAccuracy([sampler sample:cpv(3.5, 2.0 + 1e-5)], (cpFloat)0.0, 1e-5, @""); |
| 285 |
|
| 286 |
[sampler release]; |
| 287 |
} |
| 288 |
} |
| 289 |
|
| 290 |
-(void)testMultiGrabSort |
| 291 |
{ |
| 292 |
ChipmunkSpace *space = [[ChipmunkSpace alloc] init]; |
| 293 |
ChipmunkMultiGrab *multiGrab = [[ChipmunkMultiGrab alloc] initForSpace:space withSmoothing:0.0 withGrabForce:1.0]; |
| 294 |
|
| 295 |
ChipmunkBody *body = [space add:[ChipmunkBody bodyWithMass:1.0 andMoment:1.0]]; |
| 296 |
ChipmunkShape *big = [space add:[ChipmunkCircleShape circleWithBody:body radius:10.0 offset:cpvzero]]; |
| 297 |
ChipmunkShape *small = [space add:[ChipmunkCircleShape circleWithBody:body radius:5.0 offset:cpvzero]]; |
| 298 |
|
| 299 |
// Used for the custom sorting orders. |
| 300 |
big.userData = @0; |
| 301 |
small.userData = @1; |
| 302 |
|
| 303 |
ChipmunkGrab *grab1 = [multiGrab beginLocation:cpvzero]; |
| 304 |
XCTAssertEqual(grab1.grabbedShape, big, @"Should have grabbed 'big' since it has the largest penetration depth."); |
| 305 |
|
| 306 |
multiGrab.grabSort = ^(ChipmunkShape *shape, cpFloat depth){ |
| 307 |
NSNumber *n = shape.userData; |
| 308 |
return (cpFloat)n.floatValue; |
| 309 |
}; |
| 310 |
|
| 311 |
// Should grab small since it's sorting order will be the largest; |
| 312 |
ChipmunkGrab *grab2 = [multiGrab beginLocation:cpvzero]; |
| 313 |
XCTAssertEqual(grab2.grabbedShape, small, @"Should have grabbed 'small' since it has the highest custom sort value."); |
| 314 |
|
| 315 |
[multiGrab release]; |
| 316 |
[space release]; |
| 317 |
} |
| 318 |
|
| 319 |
-(void)testPolyArea |
| 320 |
{ |
| 321 |
cpFloat area1 = cpAreaForCircle(0.0, 1.0); |
| 322 |
cpFloat area2 = cpAreaForPoly(1, (cpVect[]){cpvzero}, 1.0); |
| 323 |
XCTAssertEqualWithAccuracy(area1, area2, 1e-3, @""); |
| 324 |
|
| 325 |
cpFloat area3 = cpAreaForSegment(cpv(-1,0), cpv(1,0), 1.0); |
| 326 |
cpFloat area4 = cpAreaForPoly(2, (cpVect[]){cpv(-1,0), cpv(1,0)}, 1.0); |
| 327 |
XCTAssertEqualWithAccuracy(area3, area4, 1e-3, @""); |
| 328 |
|
| 329 |
cpFloat area5 = area1 + 4.0; |
| 330 |
cpFloat area6 = cpAreaForPoly(2, (cpVect[]){cpv(-1,-1), cpv(1,-1), cpv(1,1), cpv(-1,1)}, 1.0); |
| 331 |
XCTAssertEqualWithAccuracy(area5, area6, 1e-3, @""); |
| 332 |
} |
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
@end |