UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

SceneKit: How to make a custom SCNPhysicsField?

Forums > Swift

I want to make a custom water friction field, here is my code:

let waterResistanceField = SCNPhysicsField.customField { (position, velocity, mass, charge, time) -> SCNVector3 in
    let waterDensity: Float = 1000.0
    let speed = velocity
    let dragCoefficient: Float = 0.2
    let dragForce: SCNVector3 = -dragCoefficient * waterDensity * speed * speed * velocity
    return dragForce
}

And I customed some operation of SCNVector3

extension SCNVector3 {
    static func -(lhs: SCNVector3, rhs: SCNVector3) -> SCNVector3 {
        SCNVector3(lhs.x - rhs.x, lhs.y - rhs.y, lhs.z - rhs.z)
    }
    static func +(lhs: SCNVector3, rhs: SCNVector3) -> SCNVector3 {
        SCNVector3(lhs.x + rhs.x, lhs.y + rhs.y, lhs.z + rhs.z)
    }
    static func *(lhs: CGFloat, rhs: SCNVector3) -> SCNVector3 {
        let c = Float(lhs)
        return SCNVector3(c * rhs.x, c * rhs.y, c * rhs.z)
    }
}

But I get an warning in the first line of the first code: The compiler is unable to type-check this expression in reasonable time; try breaking up the expression into distinct sub-expressions.

How can I solve it?

2      

let dragForce: SCNVector3 = -dragCoefficient * waterDensity * speed * speed * velocity

Subbing in the types of your variables, this is:

SCNVector3 = Float * Float * SCNVector3 * SCNVector3 * SCNVector3

Now, granted, you made a custom operator *(lhs: CGFloat, rhs: SCNVector3) but that takes a CGFloat, which is not interchangeable with a Float.

I can't say for certain that's your problem, but I think that's probably your problem.

2      

BUILD THE ULTIMATE PORTFOLIO APP Most Swift tutorials help you solve one specific problem, but in my Ultimate Portfolio App series I show you how to get all the best practices into a single app: architecture, testing, performance, accessibility, localization, project organization, and so much more, all while building a SwiftUI app that works on iOS, macOS and watchOS.

Get it on Hacking with Swift+

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.