-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathBallSystem.swift
More file actions
126 lines (107 loc) · 5.46 KB
/
BallSystem.swift
File metadata and controls
126 lines (107 loc) · 5.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
//
// CustomSystem.swift
//
//
// Copyright (C) Untold Engine Studios
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
#if os(macOS)
import simd
import SwiftUI
import UntoldEngine
/// BallState represents the different states a ball can be in during gameplay.
/// This makes it easier to manage transitions like idle → kick → moving → decelerating.
enum BallState: Codable {
case idle
case kick
case moving
case decelerating
}
/// BallComponent is a custom ECS component that stores the ball's state and motion data.
/// Every entity with this component will behave like a ball in the game.
public class BallComponent: Component, Codable {
var motionAccumulator: simd_float3 = .zero // Used to accumulate velocity for smoother force application
var state: BallState = .idle // Current state of the ball (idle, moving, etc.)
var velocity: simd_float3 = .zero // Current velocity of the ball
public required init() {}
}
/// ballSystemUpdate runs once per frame and updates all entities that have a BallComponent.
/// This is where we apply physics, update states, and define how the ball behaves.
public func ballSystemUpdate(deltaTime: Float) {
// Get the ID of the BallComponent so we can query entities that use it
let customId = getComponentId(for: BallComponent.self)
let entities = queryEntitiesWithComponentIds([customId], in: scene)
for entity in entities {
guard let ballComponent = scene.get(component: BallComponent.self, for: entity) else { continue }
// Apply drag to simulate resistance as the ball moves
setLinearDragCoefficient(entityId: entity, coefficients: simd_float2(0.7, 0.0))
setAngularDragCoefficient(entityId: entity, coefficients: simd_float2(0.07, 0.0))
// Update the ball based on its current state
switch ballComponent.state {
case .idle:
// Do nothing, the ball is at rest
break
case .kick:
// Transition to moving when the ball is kicked
ballComponent.state = .moving
applyVelocity(finalVelocity: ballComponent.velocity * 5.0, deltaTime: deltaTime, ball: entity)
case .moving:
// If the velocity drops below a threshold, start decelerating
if simd_length(getVelocity(entityId: entity)) <= 0.1 {
ballComponent.state = .decelerating
}
case .decelerating:
// Gradually slow down the ball
decelerate(deltaTime: deltaTime, ball: entity)
if simd_length(getVelocity(entityId: entity)) < 0.1 {
// You could transition back to .idle here if desired
}
}
}
}
/// Apply a force to the ball to simulate a kick or strong push.
/// Uses an accumulator to smooth motion and applies both linear and angular forces.
func applyVelocity(finalVelocity: simd_float3, deltaTime: Float, ball: EntityID) {
guard let customComponent = scene.get(component: BallComponent.self, for: ball) else { return }
let mass: Float = getMass(entityId: ball)
let ballDim = getDimension(entityId: ball)
// Blend previous motion with new input for smoother physics
let bias: Float = 0.4
let vComp: simd_float3 = finalVelocity * (1.0 - bias)
customComponent.motionAccumulator = customComponent.motionAccumulator * bias + vComp
// Apply linear force based on mass and deltaTime
var force: simd_float3 = (customComponent.motionAccumulator * mass) / deltaTime
applyForce(entityId: ball, force: force)
// Apply angular force so the ball spins as it moves
let upAxis = simd_float3(0.0, ballDim.depth / 2.0, 0.0)
force *= 0.25
applyMoment(entityId: ball, force: force, at: upAxis)
// Reset velocity so physics is only applied through forces
clearVelocity(entityId: ball)
clearAngularVelocity(entityId: ball)
}
/// Gradually slow down the ball by applying counter-forces.
/// Works similarly to applyVelocity, but reduces motion instead of adding it.
func decelerate(deltaTime: Float, ball: EntityID) {
guard let customComponent = scene.get(component: BallComponent.self, for: ball) else { return }
let ballDim = getDimension(entityId: ball)
let velocity: simd_float3 = getVelocity(entityId: ball)
// Blend down velocity for smoother deceleration
let bias: Float = 0.5
let vComp: simd_float3 = velocity * (1.0 - bias)
customComponent.motionAccumulator = customComponent.motionAccumulator * bias + vComp
// Apply counter-force to slow down
var force: simd_float3 = (customComponent.motionAccumulator * getMass(entityId: ball)) / deltaTime
force *= 0.15
applyForce(entityId: ball, force: force)
// Apply spin reduction
let upAxis = simd_float3(0.0, ballDim.depth / 2.0, 0.0)
force *= 0.25
applyMoment(entityId: ball, force: force, at: upAxis)
// Clear velocity so deceleration is handled through applied forces
clearVelocity(entityId: ball)
clearAngularVelocity(entityId: ball)
}
#endif