Skip to content

Commit

Permalink
[ark-physics-kit] tests: test physics body
Browse files Browse the repository at this point in the history
  • Loading branch information
markusyeo committed Apr 20, 2024
1 parent b48d7f6 commit 0c89ff5
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 3 deletions.
4 changes: 4 additions & 0 deletions ArkKit.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@
28A032DF2BAD4F2A00851BFF /* ArkTimeSystem.swift in Sources */ = {isa = PBXBuildFile; fileRef = 28A032DE2BAD4F2A00851BFF /* ArkTimeSystem.swift */; };
28A032E52BAD781900851BFF /* AbstractPhysicsArkSimulator.swift in Sources */ = {isa = PBXBuildFile; fileRef = 28A032E42BAD781900851BFF /* AbstractPhysicsArkSimulator.swift */; };
28A032E72BAD783D00851BFF /* SKSimulator.swift in Sources */ = {isa = PBXBuildFile; fileRef = 28A032E62BAD783D00851BFF /* SKSimulator.swift */; };
28A34CF92BD3990000BD3F4C /* ArkSKPhysicsBodyTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 28A34CF82BD3990000BD3F4C /* ArkSKPhysicsBodyTests.swift */; };
28D006572BAF116A001B4BD4 /* TankGameCollisionStrategyManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 28D006562BAF116A001B4BD4 /* TankGameCollisionStrategyManager.swift */; };
28D0065F2BAFD7E1001B4BD4 /* EntityManagerTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 28D0065E2BAFD7E1001B4BD4 /* EntityManagerTests.swift */; };
28D006612BAFEE3D001B4BD4 /* ArkPhysicsKitTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 28D006602BAFEE3D001B4BD4 /* ArkPhysicsKitTests.swift */; };
Expand Down Expand Up @@ -535,6 +536,7 @@
28A032DE2BAD4F2A00851BFF /* ArkTimeSystem.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ArkTimeSystem.swift; sourceTree = "<group>"; };
28A032E42BAD781900851BFF /* AbstractPhysicsArkSimulator.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AbstractPhysicsArkSimulator.swift; sourceTree = "<group>"; };
28A032E62BAD783D00851BFF /* SKSimulator.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SKSimulator.swift; sourceTree = "<group>"; };
28A34CF82BD3990000BD3F4C /* ArkSKPhysicsBodyTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ArkSKPhysicsBodyTests.swift; sourceTree = "<group>"; };
28D006562BAF116A001B4BD4 /* TankGameCollisionStrategyManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TankGameCollisionStrategyManager.swift; sourceTree = "<group>"; };
28D0065E2BAFD7E1001B4BD4 /* EntityManagerTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EntityManagerTests.swift; sourceTree = "<group>"; };
28D006602BAFEE3D001B4BD4 /* ArkPhysicsKitTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ArkPhysicsKitTests.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1375,6 +1377,7 @@
isa = PBXGroup;
children = (
28D006602BAFEE3D001B4BD4 /* ArkPhysicsKitTests.swift */,
28A34CF82BD3990000BD3F4C /* ArkSKPhysicsBodyTests.swift */,
);
path = "ark-physics-kit-tests";
sourceTree = "<group>";
Expand Down Expand Up @@ -2424,6 +2427,7 @@
281AE1282BD3911F00B79B91 /* HeapTests.swift in Sources */,
AD3ECF432BAF3148002C758D /* ArkEventKitTests.swift in Sources */,
AD787A682B9C6373003EBBD0 /* ArkKitTests.swift in Sources */,
28A34CF92BD3990000BD3F4C /* ArkSKPhysicsBodyTests.swift in Sources */,
02A438312BB00DC0000135BC /* MockAudioContext.swift in Sources */,
281AE12A2BD3928100B79B91 /* QueueTests.swift in Sources */,
281AE12E2BD3928D00B79B91 /* OrderedDictionaryTests.swift in Sources */,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import SpriteKit
class ArkSKPhysicsBody: AbstractArkPhysicsBody {
private(set) var node: SKNode
private let nodeNoPhysicsBodyFailureMessage = "SKNode does not contain an associated SKPhysicsBody."
private let physicsBodyFailedToCreateMessage = "Failed to create an SKPhysicsBody."

init(rectangleOf size: CGSize, at position: CGPoint = .zero) {
let physicsBody = SKPhysicsBody(rectangleOf: size)
Expand All @@ -19,11 +20,20 @@ class ArkSKPhysicsBody: AbstractArkPhysicsBody {
}

init(polygonOf vertices: [CGPoint], at position: CGPoint = .zero) {
guard vertices.count >= 3 else {
self.node = SKNode()
assertionFailure(physicsBodyFailedToCreateMessage)
return
}

node = SKNode()
node.position = position

let cgPath = CGMutablePath()
cgPath.addLines(between: vertices)
cgPath.closeSubpath()

let physicsBody = SKPhysicsBody(polygonFrom: cgPath)
node = SKNode()
node.position = position
node.physicsBody = physicsBody
}

Expand Down
76 changes: 76 additions & 0 deletions ArkKitTests/ark-physics-kit-tests/ArkSKPhysicsBodyTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import XCTest
@testable import ArkKit

class ArkSKPhysicsBodyTests: XCTestCase {

func testInitWithRectangle() {
let body = ArkSKPhysicsBody(rectangleOf: CGSize(width: 100, height: 50))
XCTAssertNotNil(body.node.physicsBody, "Physics body should not be nil.")
}

func testInitWithCircle() {
let body = ArkSKPhysicsBody(circleOf: 30)
XCTAssertNotNil(body.node.physicsBody, "Physics body should not be nil.")
}

func testPolygonInitWithValidVertices() {
let vertices = [CGPoint(x: -50, y: -50), CGPoint(x: 50, y: -50), CGPoint(x: 50, y: 50), CGPoint(x: -50, y: 50)]
let body = ArkSKPhysicsBody(polygonOf: vertices)
XCTAssertNotNil(body.node.physicsBody, "Physics body should not be nil for a valid polygon.")
}

func testPropertyAccessorsAndMutators() {
let body = ArkSKPhysicsBody(circleOf: 10)
body.position = CGPoint(x: 10, y: 10)
body.mass = 5.0
body.velocity = CGVector(dx: 5, dy: 5)
body.isDynamic = false
body.affectedByGravity = false
body.linearDamping = 0.5
body.angularDamping = 0.5
body.allowsRotation = false
body.friction = 0.25
body.restitution = 0.25
body.categoryBitMask = 0x00000001
body.collisionBitMask = 0x00000002
body.contactTestBitMask = 0x00000004

XCTAssertEqual(body.position, CGPoint(x: 10, y: 10))
XCTAssertEqual(body.mass, 5.0, accuracy: 0.0001)
XCTAssertEqual(body.velocity.dx, 5.0, accuracy: 0.0001)
XCTAssertEqual(body.velocity.dy, 5.0, accuracy: 0.0001)
XCTAssertFalse(body.isDynamic)
XCTAssertFalse(body.affectedByGravity)
XCTAssertEqual(body.linearDamping, 0.5, accuracy: 0.0001)
XCTAssertEqual(body.angularDamping, 0.5, accuracy: 0.0001)
XCTAssertFalse(body.allowsRotation)
XCTAssertEqual(body.friction, 0.25, accuracy: 0.0001)
XCTAssertEqual(body.restitution, 0.25, accuracy: 0.0001)
XCTAssertEqual(body.categoryBitMask, 0x00000001)
XCTAssertEqual(body.collisionBitMask, 0x00000002)
XCTAssertEqual(body.contactTestBitMask, 0x00000004)
}

func testApplyImpulse() {
let body = ArkSKPhysicsBody(circleOf: 20)
let impulse = CGVector(dx: 10, dy: 10)
body.applyImpulse(impulse)
}

func testApplyAngularImpulse() {
let body = ArkSKPhysicsBody(circleOf: 20)
let impulse: CGFloat = 5.0
body.applyAngularImpulse(impulse)
}

func testEqualityAndHashable() {
let body1 = ArkSKPhysicsBody(circleOf: 20)
let body2 = body1
let body3 = ArkSKPhysicsBody(circleOf: 20)

XCTAssertEqual(body1, body2,
"Same instances should be equal")
XCTAssertNotEqual(body1, body3,
"Different instances with same properties should not be equal due to node identity")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class OrderedDictionaryTests: XCTestCase {
iteratedKeys.append(key)
}

XCTAssertEqual(iteratedKeys, ["one", "two", "three"],
XCTAssertEqual(iteratedKeys, ["one", "two", "three"],
"Keys should be iterated in the order they were inserted.")
}

Expand Down

0 comments on commit 0c89ff5

Please sign in to comment.