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      

Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

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.