Skip to content

Commit

Permalink
Update PhysicsComponent.swift
Browse files Browse the repository at this point in the history
- Rename maxVelocity to maximumVelocity
- Add maximumAngularVelocity
  • Loading branch information
ShinryakuTako committed Dec 26, 2019
1 parent 6a46e40 commit 525490c
Showing 1 changed file with 23 additions and 12 deletions.
35 changes: 23 additions & 12 deletions Sources/OctopusKit/Components/Physics/PhysicsComponent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,21 +42,25 @@ public final class PhysicsComponent: OctopusComponent, OctopusUpdatableComponent
}

/// The scalar to limit the velocity of the `physicsBody` to.
public var maxVelocity: CGFloat?
public var maximumVelocity: CGFloat?

/// The angular velocity in Newton-meters to limit the `physicsBody` to.
public var maximumAngularVelocity: CGFloat?

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

public init(
physicsBody: SKPhysicsBody? = nil,
maxVelocity: CGFloat? = nil,
createBodyFromNodeFrame: Bool = false)
public init(physicsBody: SKPhysicsBody? = nil,
createBodyFromNodeFrame: Bool = false,
maximumVelocity: CGFloat? = nil,
maximumAngularVelocity: CGFloat? = nil)
{
self.physicsBody = physicsBody
self.maxVelocity = maxVelocity
self.physicsBody = physicsBody
self.createBodyFromNodeFrame = createBodyFromNodeFrame
self.maximumVelocity = maximumVelocity
self.maximumAngularVelocity = maximumAngularVelocity
super.init()
}

Expand Down Expand Up @@ -113,8 +117,8 @@ public final class PhysicsComponent: OctopusComponent, OctopusUpdatableComponent

if physicsBody.node == nil {
node.physicsBody = self.physicsBody
}
else if physicsBody.node! != node {

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

public override func update(deltaTime seconds: TimeInterval) {
super.update(deltaTime: seconds)

guard let physicsBody = self.physicsBody else { return }

if let maxVelocity = self.maxVelocity {
physicsBody.velocity.clampMagnitude(to: maxVelocity)
if let maximumVelocity = self.maximumVelocity {
physicsBody.velocity.clampMagnitude(to: maximumVelocity)
}

if let maximumAngularVelocity = self.maximumAngularVelocity,
abs(physicsBody.angularVelocity) > maximumAngularVelocity
{
// CHECK: Find a better way?
physicsBody.angularVelocity = maximumAngularVelocity * CGFloat(sign(Float(physicsBody.angularVelocity)))
}
}
}

0 comments on commit 525490c

Please sign in to comment.