Skip to content

Commit 525490c

Browse files
committed
Update PhysicsComponent.swift
- Rename maxVelocity to maximumVelocity - Add maximumAngularVelocity
1 parent 6a46e40 commit 525490c

File tree

1 file changed

+23
-12
lines changed

1 file changed

+23
-12
lines changed

Sources/OctopusKit/Components/Physics/PhysicsComponent.swift

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -42,21 +42,25 @@ public final class PhysicsComponent: OctopusComponent, OctopusUpdatableComponent
4242
}
4343

4444
/// The scalar to limit the velocity of the `physicsBody` to.
45-
public var maxVelocity: CGFloat?
45+
public var maximumVelocity: CGFloat?
46+
47+
/// The angular velocity in Newton-meters to limit the `physicsBody` to.
48+
public var maximumAngularVelocity: CGFloat?
4649

4750
/// Overrides this component's `physicsBody` property and creates a new rectangular `SKPhysicsBody` from the frame of the entity's `SpriteKitComponent` node.
4851
///
4952
/// As creating physics bodies may be a costly runtime operation, this setting defaults to `false`.
5053
public var createBodyFromNodeFrame: Bool = false
5154

52-
public init(
53-
physicsBody: SKPhysicsBody? = nil,
54-
maxVelocity: CGFloat? = nil,
55-
createBodyFromNodeFrame: Bool = false)
55+
public init(physicsBody: SKPhysicsBody? = nil,
56+
createBodyFromNodeFrame: Bool = false,
57+
maximumVelocity: CGFloat? = nil,
58+
maximumAngularVelocity: CGFloat? = nil)
5659
{
57-
self.physicsBody = physicsBody
58-
self.maxVelocity = maxVelocity
60+
self.physicsBody = physicsBody
5961
self.createBodyFromNodeFrame = createBodyFromNodeFrame
62+
self.maximumVelocity = maximumVelocity
63+
self.maximumAngularVelocity = maximumAngularVelocity
6064
super.init()
6165
}
6266

@@ -113,8 +117,8 @@ public final class PhysicsComponent: OctopusComponent, OctopusUpdatableComponent
113117

114118
if physicsBody.node == nil {
115119
node.physicsBody = self.physicsBody
116-
}
117-
else if physicsBody.node! != node {
120+
121+
} else if physicsBody.node! != node {
118122
// ℹ️ DESIGN: Log an error and detach from the entity, as an `PhysicsComponent` with a body that belongs to another node, has no valid behavior.
119123
OctopusKit.logForErrors.add("\(physicsBody) already associated with \(physicsBody.node!) — Detaching from entity")
120124
self.removeFromEntity()
@@ -146,11 +150,18 @@ public final class PhysicsComponent: OctopusComponent, OctopusUpdatableComponent
146150
}
147151

148152
public override func update(deltaTime seconds: TimeInterval) {
149-
super.update(deltaTime: seconds)
153+
150154
guard let physicsBody = self.physicsBody else { return }
151155

152-
if let maxVelocity = self.maxVelocity {
153-
physicsBody.velocity.clampMagnitude(to: maxVelocity)
156+
if let maximumVelocity = self.maximumVelocity {
157+
physicsBody.velocity.clampMagnitude(to: maximumVelocity)
158+
}
159+
160+
if let maximumAngularVelocity = self.maximumAngularVelocity,
161+
abs(physicsBody.angularVelocity) > maximumAngularVelocity
162+
{
163+
// CHECK: Find a better way?
164+
physicsBody.angularVelocity = maximumAngularVelocity * CGFloat(sign(Float(physicsBody.angularVelocity)))
154165
}
155166
}
156167
}

0 commit comments

Comments
 (0)