Creating a Linear Gravity Field in Swift

血红的双手。 提交于 2019-12-12 09:12:31

问题


It seems that linear gravity field with vector method has been deprecated in Swift. There's a bunch of new SKFieldNode in this new language, but I can't seem to find one that resembles linear gravity. The property "direction" isn't even available in Swift. Maybe it can be done using this generic method customFieldWithEvaluationBlock but I'm not sure how. I'm new to SpriteKit.

So, how can we make the same physics effect in Swift?


回答1:


In Swift 2 (part of Xcode 7), SIMD vectors are available to Swift, so API that uses them (such as linearGravityFieldWithVector, the new GameplayKit and Model I/O frameworks, etc) is are now imported into Swift, too... no workarounds necessary anymore.

let field = SKFieldNode.linearGravityFieldWithVector(float3(1,0,0))

Previously... The SKFieldNode class method linearGravityFieldWithVector does this, but it's not available in Swift. In ObjC vector parameter uses the vector_float3 type from the SIMD library, which uses special C compiler magic to make vectors that use SSE (on OS X / x86) or NEON (on iOS / ARM) hardware acceleration and pack nicely for sending to GPUs. That kind of voodoo doesn't currently exist in Swift, so any ObjC APIs that rely on it aren't imported into Swift.

For now, you can access those APIs from ObjC -- in a Swift project, you can make yourself a little ObjC wrapper function and expose it to Swift in your bridging header.




回答2:


If you want to apply linear gravity to all nodes you can try setting physicsWorld.gravity, it's a CGVector



来源:https://stackoverflow.com/questions/25558754/creating-a-linear-gravity-field-in-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!