Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
STREGA committed Dec 13, 2023
1 parent 665ad01 commit 28044ca
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -296,13 +296,15 @@ extension AxisAlignedBoundingBox3D {
extension AxisAlignedBoundingBox3D {
@inline(__always)
public func planes() -> [Plane3D] {
return [Plane3D(origin: position + radius, normal: .right),
Plane3D(origin: position + radius, normal: .up),
Plane3D(origin: position + radius, normal: .forward),

Plane3D(origin: position - radius, normal: .left),
Plane3D(origin: position - radius, normal: .down),
Plane3D(origin: position - radius, normal: .backward)]
return [
Plane3D(origin: position + radius, normal: .right),
Plane3D(origin: position + radius, normal: .up),
Plane3D(origin: position + radius, normal: .backward),

Plane3D(origin: position - radius, normal: .left),
Plane3D(origin: position - radius, normal: .down),
Plane3D(origin: position - radius, normal: .forward)
]
}

@inline(__always)
Expand Down Expand Up @@ -330,15 +332,19 @@ extension AxisAlignedBoundingBox3D {
}

public extension AxisAlignedBoundingBox3D {
@inline(__always)
func isIntersected(by ray: Ray3D) -> Bool {
for plane in self.planes() {
if self.contains(plane.intersectionOfRay(ray)) {
return true
}
}
return false
}
// Not working as expected
// @inline(__always)
// func isIntersected(by ray: Ray3D) -> Bool {
// if self.contains(ray.origin) {
// return true
// }
// for plane in self.planes() {
// if self.contains(plane.intersectionOfRay(ray)) {
// return true
// }
// }
// return false
// }

@inline(__always)
func surfacePoint(for ray: Ray3D) -> Position3? {
Expand Down
80 changes: 44 additions & 36 deletions Sources/GateEngine/ECS/3D Specific/Physics/Collision3DSystem.swift
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,9 @@ extension Collision3DSystem {
) {
guard
let collider: BoundingEllipsoid3D = collisionComponent.collider as? BoundingEllipsoid3D
else { return }
else {
return
}

@inline(__always)
func wall(_ position: Position3, _ direction: Direction3) -> (
Expand Down Expand Up @@ -347,7 +349,6 @@ extension Collision3DSystem {
r.right.interpolated(to: forward, .linear(0.5)),
r.left.interpolated(to: forward, .linear(0.5)),
]

}()
var doSides = false
for direction in directions1 {
Expand All @@ -358,7 +359,7 @@ extension Collision3DSystem {
}

if doSides {
@inline(__always)
@_transparent
var directions2: [Direction3] {
let r = transformComponent.rotation
return [
Expand Down Expand Up @@ -394,14 +395,18 @@ extension Collision3DSystem {
offset: .zero,
radius: collider.radius.x
)
let triangles = trianglesNear(collisionComponent.collider.boundingBox, filter: filter)
.filter({ $0.interpenetration(comparing: sphere)?.isColiding == true })

var match:
[(
edge: Line3D, point: Position3, angle: Degrees, distance: Float,
triangle: CollisionTriangle
)] = []
let triangles = trianglesNear(
collisionComponent.collider.boundingBox,
filter: filter
).filter({ $0.interpenetration(comparing: sphere)?.isColiding == true })

var match: [(
edge: Line3D,
point: Position3,
angle: Degrees,
distance: Float,
triangle: CollisionTriangle
)] = []
for triangle in triangles {
let edge = triangle.edgeNear(transformComponent.position)
let point = edge.pointNear(transformComponent.position)
Expand Down Expand Up @@ -470,16 +475,19 @@ extension Collision3DSystem {

@inline(__always)
func respondToCollision(dynamicEntity: Entity, triangle: CollisionTriangle) -> Bool {
guard let collisionComponent = dynamicEntity.component(ofType: Collision3DComponent.self)
else { return false }
guard
let interpenetration = triangle.interpenetration(
comparing: collisionComponent.collider
), interpenetration.isColiding
else { return false }
guard
let collisionComponent = dynamicEntity.component(ofType: Collision3DComponent.self),
let interpenetration = triangle.interpenetration(comparing: collisionComponent.collider),
interpenetration.isColiding
else {
return false
}
collisionComponent.touching.append((triangle, interpenetration))
guard let transformComponent = dynamicEntity.component(ofType: Transform3Component.self)
else { return false }
guard
let transformComponent = dynamicEntity.component(ofType: Transform3Component.self)
else {
return false
}
let depth = interpenetration.depth + 0.001 //Keep the objects touching a little
switch triangle.surfaceType {
case .floor, .ramp:
Expand All @@ -498,10 +506,10 @@ extension Collision3DSystem {
staticEntity: Entity,
interpenetration: Interpenetration3D
) {
guard let transformComponent = dynamicEntity.component(ofType: Transform3Component.self)
else { return }
let depth = interpenetration.depth + 0.001 //Keep the objects touching a little
transformComponent.position -= Position3(interpenetration.direction * depth)
if let transformComponent = dynamicEntity.component(ofType: Transform3Component.self) {
let depth = interpenetration.depth + 0.001 //Keep the objects touching a little
transformComponent.position -= Position3(interpenetration.direction * depth)
}
}

@inline(__always)
Expand All @@ -510,18 +518,17 @@ extension Collision3DSystem {
dynamicEntity: Entity,
interpenetration: Interpenetration3D
) {
guard let transformComponent = dynamicEntity.component(ofType: Transform3Component.self)
else { return }
let depth = interpenetration.depth + 0.001 //Keep the objects touching a little
transformComponent.position -= Position3(interpenetration.direction * depth)
if let transformComponent = dynamicEntity.component(ofType: Transform3Component.self) {
let depth = interpenetration.depth + 0.001 //Keep the objects touching a little
transformComponent.position -= Position3(interpenetration.direction * depth)
}
}
}

extension Collision3DSystem {
@_transparent
private var octrees: [OctreeComponent] {
return game.entities.filter({ $0.hasComponent(OctreeComponent.self) }).map({
$0[OctreeComponent.self]
})
return game.entities.compactMap({ $0.component(ofType: OctreeComponent.self) })
}

@inline(__always)
Expand Down Expand Up @@ -558,6 +565,8 @@ extension Collision3DSystem {
switch collider {
case let meshCollider as MeshCollider:
hits.append(contentsOf: meshCollider.trianglesHit(by: ray))
case let skinCollider as SkinCollider:
hits.append(contentsOf: skinCollider.trianglesHit(by: ray))
default:
break
}
Expand Down Expand Up @@ -591,7 +600,7 @@ extension Collision3DSystem {
filter?(entity) ?? true
{
let collider = useRayCastCollider ? (collisionComponent.rayCastCollider ?? collisionComponent.collider) : collisionComponent.collider
if collider.boundingBox.isIntersected(by: ray) {
if collider.boundingBox.surfacePoint(for: ray) != nil {
entities.append(entity)
}
}
Expand All @@ -608,7 +617,7 @@ extension Collision3DSystem {
var entities: [Entity] = []

for entity in game.entities {
if
if
let collisionComponent = entity.component(ofType: Collision3DComponent.self),
filter?(entity) ?? true,
collisionComponent.collider.boundingBox.interpenetration(comparing: collider)?.isColiding == true
Expand All @@ -631,7 +640,6 @@ extension Collision3DSystem {
for entity in entities {
if let collisionComponent = entity.component(ofType: Collision3DComponent.self) {
let collider = useRayCastCollider ? (collisionComponent.rayCastCollider ?? collisionComponent.collider) : collisionComponent.collider
guard collider is MeshCollider == false else { continue }
if let impact = collider.surfaceImpact(comparing: ray) {
hits.append((impact, entity))
}
Expand Down Expand Up @@ -677,8 +685,8 @@ extension Collision3DSystem {
return (triangleHit.position, triangleHit.triangle.normal, triangleHit.triangle, nil)
} else {
return (
entityHit.surfaceImpact.position,
entityHit.surfaceImpact.normal,
entityHit.surfaceImpact.position,
entityHit.surfaceImpact.normal,
entityHit.surfaceImpact.triangle,
entityHit.entity
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import MetalKit
import Collections

class MetalGeometry: GeometryBackend, SkinnedGeometryBackend {
final class MetalGeometry: GeometryBackend, SkinnedGeometryBackend {
let primitive: DrawCommand.Flags.Primitive
let attributes: ContiguousArray<GeometryAttribute>
let buffer: any MTLBuffer
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import MetalKit
import GameMath

class MetalRenderTarget: RenderTargetBackend {
final class MetalRenderTarget: RenderTargetBackend {
var size: Size2 = Size2(2)
var isFirstPass = true

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import MetalKit
import GameMath
import Shaders

class MetalRenderer: RendererBackend {
final class MetalRenderer: RendererBackend {
@inline(__always)
var renderingAPI: RenderingAPI { .metal }
static var isSupported: Bool = MTLCreateSystemDefaultDevice() != nil
Expand Down

0 comments on commit 28044ca

Please sign in to comment.