Synchronizing Physics in TableTopKit

I am working on adding synchronized physical properties to EntityEquipment in TableTopKit, allowing seamless coordination during GroupActivities sessions between players.

Treating EntityEquipment's state to DieState is not a way, because it doesn't support custom collision shapes.

I have also tried adding PhysicsBodyComponent and CollisionComponent to EntityEquipment's Entity. However, the main issue is that the position of EntityEquipment itself does not synchronize with the Entity's physics body, resulting in two separate instances of one object.

struct PlayerPawn: EntityEquipment {
    let id: ID
    let entity: Entity
    var initialState: BaseEquipmentState
    
    init(id: ID, entity: Entity) {
        self.id = id
        
        let massProperties = PhysicsMassProperties(mass: 1.0)
        let material = PhysicsMaterialResource.generate(friction: 0.5, restitution: 0.5)
        let shape = ShapeResource.generateBox(size: [0.4, 0.2, 0.2])
        let physicsBody = PhysicsBodyComponent(massProperties: massProperties, material: material, mode: .dynamic)
        
        let collisionComponent = CollisionComponent(shapes: [shape])
        
        entity.components.set(physicsBody)
        entity.components.set(collisionComponent)
        
        self.entity = entity
        
        initialState = .init(parentID: .tableID, pose: .init(position: .init(), rotation: .zero), entity: self.entity)
    }
}

I’d appreciate any guidance on the recommended approach to adding synchronized physical properties to EntityEquipment.

Answered by Vision Pro Engineer in 816662022

Hello @matt_novoselov,

As you noted, TabletopKit's physics simulation supports the shapes listed here. For other shapes including a custom shape, I encourage you to please file an enhancement request using Feedback Assistant. Once you file the request, please post the FB number here.

If you're not familiar with how to file enhancement requests, take a look at Bug Reporting: How and Why?

startinteraction is a mechanism to programmatically manipulate TabletopKit equipment that is synchronized over the network. Using this along with setPose is an appropriate way to use another physics simulation alongside TabletopKit when simulating unsupported shapes.

Thanks,
John

Accepted Answer

One potential solution we explored involves creating a dummy GameEquipment of a different type, adding it to the scene, and overriding the default implementation of GameRenderer's onUpdate() function to handle synchronization.

extension GameRenderer {
    func onUpdate(timeInterval: Double, snapshot: TableSnapshot, visualState: TableVisualState) {
        self.startInteractionOnProxy()
    }
    
    public func startInteractionOnProxy(){
        guard let game = game else { return }
        
        let proxyEquipments: [ProxyEquipment] = game.tabletopGame.equipment(of: ProxyEquipment.self)
        
        if let proxy = proxyEquipments.first {
            _ = game.tabletopGame.startInteraction(onEquipmentID: proxy.id)
        }
    }
}

While this approach works to some extent, it has certain drawbacks and limitations, particularly in terms of performance and architectural complexity. It’s the closest we’ve come to achieving the desired synchronization, but we believe there might be a more robust and efficient way to accomplish this.

We would still greatly appreciate any guidance from Apple engineers on a more suitable solution to synchronize physical properties in EntityEquipment effectively.

Hello @matt_novoselov,

As you noted, TabletopKit's physics simulation supports the shapes listed here. For other shapes including a custom shape, I encourage you to please file an enhancement request using Feedback Assistant. Once you file the request, please post the FB number here.

If you're not familiar with how to file enhancement requests, take a look at Bug Reporting: How and Why?

startinteraction is a mechanism to programmatically manipulate TabletopKit equipment that is synchronized over the network. Using this along with setPose is an appropriate way to use another physics simulation alongside TabletopKit when simulating unsupported shapes.

Thanks,
John

Synchronizing Physics in TableTopKit
 
 
Q