spring 使用 Sprite Kit 的问题(它就像一根棍子)
Problems with spring going Sprite Kit (It acts like a stick)
所以我有一个简单的目标。我只是想让两个 skspritenode 相互靠近,就像用橡皮筋固定一样。我认为 SKSpringJoint 非常适合这个,因为文档说它具有类似的属性,但是这对我来说效果不太好!
我能够在两个精灵之间建立联系,但是没有 "spring like" 事情发生,而是它们一直保持相同的距离,就好像是一根棍子将它们连接在一起,而不是什么东西可以扩展和收缩。
你能看看我的代码并告诉我我做错了什么吗?模拟很简单,2 个球和一些墙。当点击屏幕时,两个球之间的连接就会建立。
import SpriteKit
class GameScene: SKScene {
var it1 = SKSpriteNode()
var it2 = SKSpriteNode()
override func didMoveToView(view: SKView) {
/* Setup your scene here */
it1 = SKSpriteNode(texture: SKTexture(imageNamed: "Spaceship"))
it1.position.x = self.frame.size.width / 2
it1.position.y = self.frame.size.height / 2
it1.size.width = self.frame.size.height / 10
it1.size.height = self.frame.size.height / 10
it1.physicsBody = SKPhysicsBody(circleOfRadius: it1.size.width / 2)
it2 = SKSpriteNode(texture: SKTexture(imageNamed: "Spaceship"))
it2.position.x = self.frame.size.width / 2 + 2
it2.position.y = self.frame.size.height
it2.size.width = self.frame.size.height / 10
it2.size.height = self.frame.size.height / 10
it2.physicsBody = SKPhysicsBody(circleOfRadius: it2.size.width / 2)
self.addChild(it1)
self.addChild(it2)
var ground = SKNode() // just an object
ground.position = CGPointMake(0, 0) //borrom left
ground.physicsBody = SKPhysicsBody(rectangleOfSize: CGSizeMake(self.frame.size.width * 2, 1))
ground.physicsBody!.dynamic = false
self.addChild(ground)
var ground2 = SKNode() // just an object
ground2.position = CGPointMake(0, 0) //borrom left
ground2.physicsBody = SKPhysicsBody(rectangleOfSize: CGSizeMake(1, self.frame.size.height * 2))
ground2.physicsBody!.dynamic = false
self.addChild(ground2)
var ground3 = SKNode() // just an object
ground3.position = CGPointMake(self.frame.size.width, 0) //borrom left
ground3.physicsBody = SKPhysicsBody(rectangleOfSize: CGSizeMake(1, self.frame.size.height * 2))
ground3.physicsBody!.dynamic = false
self.addChild(ground3)
}
var it = SKPhysicsJointSpring()
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
/* Called when a touch begins */
var poso = CGPointMake(0, 0)
var posm = CGPointMake(0.5, 0.5)
var p1s = it1.position
var p2s = it2.position
var p1 = p1s
var p2 = p2s
it = SKPhysicsJointSpring.jointWithBodyA(it1.physicsBody, bodyB: it2.physicsBody, anchorA: p1, anchorB: p2)
//it.frequency = 2349.0
//it.damping = 0.0
self.physicsWorld.addJoint(it)
//Make the tether
}
}
如果您有任何想法,请告诉我!
9 月 17 日:
我只是尝试将物理脉冲应用于模拟中的一个球。仍然没有运气。两个球好像只是用一根棍子连在一起,没有spring动作:(
只是想 post 我的最终代码,供那些有一天想要类似效果的人使用,或者只是一个如何使用 SpringJoints 的好例子。它基本上是一个二维蜘蛛侠模拟。代码是非常可重用的,您需要做的就是将绘制函数放在更新循环中,然后将函数放在项目中,然后简单地调用函数传递一个 sprinted 和 point。享受吧!
import SpriteKit
class GameScene: SKScene {
var it1 = SKSpriteNode()
var it2 = SKSpriteNode()
override func didMoveToView(view: SKView) {
/* Setup your scene here */
it1 = SKSpriteNode(texture: SKTexture(imageNamed: "Spaceship"))
it1.position.x = 0
it1.size.width = self.frame.size.height / 10
it1.size.height = self.frame.size.height / 10
it1.position.y = 0 + (it1.size.width / 2)
it1.physicsBody = SKPhysicsBody(circleOfRadius: it1.size.width / 2)
it2 = SKSpriteNode(texture: SKTexture(imageNamed: "Spaceship"))
it2.position.x = self.frame.size.width / 2 + 2
it2.position.y = self.frame.size.height
it2.size.width = self.frame.size.height / 10
it2.size.height = self.frame.size.height / 10
it2.position.y = 0 + (it1.size.width / 2)
it2.physicsBody = SKPhysicsBody(circleOfRadius: it2.size.width / 2)
it2.physicsBody?.dynamic = false
self.addChild(it1)
//self.addChild(it2)
let ground = SKNode() // just an object
ground.position = CGPointMake(0, 0) //borrom left
ground.physicsBody = SKPhysicsBody(rectangleOfSize: CGSizeMake(self.frame.size.width * 2, 1))
ground.physicsBody!.dynamic = false
self.addChild(ground)
let ground2 = SKNode() // just an object
ground2.position = CGPointMake(0, 0) //borrom left
ground2.physicsBody = SKPhysicsBody(rectangleOfSize: CGSizeMake(1, self.frame.size.height * 2))
ground2.physicsBody!.dynamic = false
self.addChild(ground2)
let ground3 = SKNode() // just an object
ground3.position = CGPointMake(self.frame.size.width, 0) //borrom left
ground3.physicsBody = SKPhysicsBody(rectangleOfSize: CGSizeMake(1, self.frame.size.height * 2))
ground3.physicsBody!.dynamic = false
self.addChild(ground3)
self.physicsWorld.gravity = CGVectorMake(-1, self.physicsWorld.gravity.dy)
//drawmeclose(it1, point: it2.position)
}
var it = SKPhysicsJointSpring()
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
// it1.physicsBody?.applyImpulse(CGVectorMake(0, -200))
for touch in (touches )
{
let location:CGPoint = touch.locationInNode(self)
if (havespring == false)
{
//drawmeclose(it1, point: it2.position)
drawmeclose(it1, point: location)
}
else
{
undrawme()
}
}
}
var sd = false
override func update(currentTime: NSTimeInterval) {
drawtether()
}
var sp = CGPointMake(0, 0)
var spring = SKPhysicsJointSpring()
var string = SKShapeNode()
var havespring = false
var haveline = false
var bA = SKSpriteNode()
func drawmeclose(bodyA: SKSpriteNode, point: CGPoint)
{
sp = bodyA.position
let tetherp = SKSpriteNode()
tetherp.position = point
tetherp.physicsBody = SKPhysicsBody()
tetherp.physicsBody?.dynamic = false
self.addChild(tetherp)
bodyA.position = tetherp.position
spring = SKPhysicsJointSpring.jointWithBodyA(bodyA.physicsBody!, bodyB: tetherp.physicsBody!, anchorA: bodyA.position, anchorB: tetherp.position)
spring.frequency = 0.8
spring.damping = 0.5
self.physicsWorld.addJoint(spring)
bodyA.position = sp
havespring = true
bA = bodyA
sp = point
}
func drawtether()
{
if (haveline == true)
{
string.removeFromParent()
haveline = false
}
if (havespring == true)
{
let path = CGPathCreateMutable()
CGPathMoveToPoint(path, nil, bA.position.x, bA.position.y)
CGPathAddLineToPoint(path, nil, sp.x, sp.y)
CGPathCloseSubpath(path);
string = SKShapeNode(path: path )
self.addChild(string)
haveline = true
}
}
func undrawme()
{
self.physicsWorld.removeJoint(spring)
havespring = false
}
}
试试这个:
it.frequency = 1.0
it.damping = 0.0
self.physicsWorld.addJoint(it)
我认为你的频率太高了,看不到任何东西,默认值是0.0。
所以我有一个简单的目标。我只是想让两个 skspritenode 相互靠近,就像用橡皮筋固定一样。我认为 SKSpringJoint 非常适合这个,因为文档说它具有类似的属性,但是这对我来说效果不太好!
我能够在两个精灵之间建立联系,但是没有 "spring like" 事情发生,而是它们一直保持相同的距离,就好像是一根棍子将它们连接在一起,而不是什么东西可以扩展和收缩。
你能看看我的代码并告诉我我做错了什么吗?模拟很简单,2 个球和一些墙。当点击屏幕时,两个球之间的连接就会建立。
import SpriteKit
class GameScene: SKScene {
var it1 = SKSpriteNode()
var it2 = SKSpriteNode()
override func didMoveToView(view: SKView) {
/* Setup your scene here */
it1 = SKSpriteNode(texture: SKTexture(imageNamed: "Spaceship"))
it1.position.x = self.frame.size.width / 2
it1.position.y = self.frame.size.height / 2
it1.size.width = self.frame.size.height / 10
it1.size.height = self.frame.size.height / 10
it1.physicsBody = SKPhysicsBody(circleOfRadius: it1.size.width / 2)
it2 = SKSpriteNode(texture: SKTexture(imageNamed: "Spaceship"))
it2.position.x = self.frame.size.width / 2 + 2
it2.position.y = self.frame.size.height
it2.size.width = self.frame.size.height / 10
it2.size.height = self.frame.size.height / 10
it2.physicsBody = SKPhysicsBody(circleOfRadius: it2.size.width / 2)
self.addChild(it1)
self.addChild(it2)
var ground = SKNode() // just an object
ground.position = CGPointMake(0, 0) //borrom left
ground.physicsBody = SKPhysicsBody(rectangleOfSize: CGSizeMake(self.frame.size.width * 2, 1))
ground.physicsBody!.dynamic = false
self.addChild(ground)
var ground2 = SKNode() // just an object
ground2.position = CGPointMake(0, 0) //borrom left
ground2.physicsBody = SKPhysicsBody(rectangleOfSize: CGSizeMake(1, self.frame.size.height * 2))
ground2.physicsBody!.dynamic = false
self.addChild(ground2)
var ground3 = SKNode() // just an object
ground3.position = CGPointMake(self.frame.size.width, 0) //borrom left
ground3.physicsBody = SKPhysicsBody(rectangleOfSize: CGSizeMake(1, self.frame.size.height * 2))
ground3.physicsBody!.dynamic = false
self.addChild(ground3)
}
var it = SKPhysicsJointSpring()
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
/* Called when a touch begins */
var poso = CGPointMake(0, 0)
var posm = CGPointMake(0.5, 0.5)
var p1s = it1.position
var p2s = it2.position
var p1 = p1s
var p2 = p2s
it = SKPhysicsJointSpring.jointWithBodyA(it1.physicsBody, bodyB: it2.physicsBody, anchorA: p1, anchorB: p2)
//it.frequency = 2349.0
//it.damping = 0.0
self.physicsWorld.addJoint(it)
//Make the tether
}
}
如果您有任何想法,请告诉我!
9 月 17 日: 我只是尝试将物理脉冲应用于模拟中的一个球。仍然没有运气。两个球好像只是用一根棍子连在一起,没有spring动作:(
只是想 post 我的最终代码,供那些有一天想要类似效果的人使用,或者只是一个如何使用 SpringJoints 的好例子。它基本上是一个二维蜘蛛侠模拟。代码是非常可重用的,您需要做的就是将绘制函数放在更新循环中,然后将函数放在项目中,然后简单地调用函数传递一个 sprinted 和 point。享受吧!
import SpriteKit
class GameScene: SKScene {
var it1 = SKSpriteNode()
var it2 = SKSpriteNode()
override func didMoveToView(view: SKView) {
/* Setup your scene here */
it1 = SKSpriteNode(texture: SKTexture(imageNamed: "Spaceship"))
it1.position.x = 0
it1.size.width = self.frame.size.height / 10
it1.size.height = self.frame.size.height / 10
it1.position.y = 0 + (it1.size.width / 2)
it1.physicsBody = SKPhysicsBody(circleOfRadius: it1.size.width / 2)
it2 = SKSpriteNode(texture: SKTexture(imageNamed: "Spaceship"))
it2.position.x = self.frame.size.width / 2 + 2
it2.position.y = self.frame.size.height
it2.size.width = self.frame.size.height / 10
it2.size.height = self.frame.size.height / 10
it2.position.y = 0 + (it1.size.width / 2)
it2.physicsBody = SKPhysicsBody(circleOfRadius: it2.size.width / 2)
it2.physicsBody?.dynamic = false
self.addChild(it1)
//self.addChild(it2)
let ground = SKNode() // just an object
ground.position = CGPointMake(0, 0) //borrom left
ground.physicsBody = SKPhysicsBody(rectangleOfSize: CGSizeMake(self.frame.size.width * 2, 1))
ground.physicsBody!.dynamic = false
self.addChild(ground)
let ground2 = SKNode() // just an object
ground2.position = CGPointMake(0, 0) //borrom left
ground2.physicsBody = SKPhysicsBody(rectangleOfSize: CGSizeMake(1, self.frame.size.height * 2))
ground2.physicsBody!.dynamic = false
self.addChild(ground2)
let ground3 = SKNode() // just an object
ground3.position = CGPointMake(self.frame.size.width, 0) //borrom left
ground3.physicsBody = SKPhysicsBody(rectangleOfSize: CGSizeMake(1, self.frame.size.height * 2))
ground3.physicsBody!.dynamic = false
self.addChild(ground3)
self.physicsWorld.gravity = CGVectorMake(-1, self.physicsWorld.gravity.dy)
//drawmeclose(it1, point: it2.position)
}
var it = SKPhysicsJointSpring()
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
// it1.physicsBody?.applyImpulse(CGVectorMake(0, -200))
for touch in (touches )
{
let location:CGPoint = touch.locationInNode(self)
if (havespring == false)
{
//drawmeclose(it1, point: it2.position)
drawmeclose(it1, point: location)
}
else
{
undrawme()
}
}
}
var sd = false
override func update(currentTime: NSTimeInterval) {
drawtether()
}
var sp = CGPointMake(0, 0)
var spring = SKPhysicsJointSpring()
var string = SKShapeNode()
var havespring = false
var haveline = false
var bA = SKSpriteNode()
func drawmeclose(bodyA: SKSpriteNode, point: CGPoint)
{
sp = bodyA.position
let tetherp = SKSpriteNode()
tetherp.position = point
tetherp.physicsBody = SKPhysicsBody()
tetherp.physicsBody?.dynamic = false
self.addChild(tetherp)
bodyA.position = tetherp.position
spring = SKPhysicsJointSpring.jointWithBodyA(bodyA.physicsBody!, bodyB: tetherp.physicsBody!, anchorA: bodyA.position, anchorB: tetherp.position)
spring.frequency = 0.8
spring.damping = 0.5
self.physicsWorld.addJoint(spring)
bodyA.position = sp
havespring = true
bA = bodyA
sp = point
}
func drawtether()
{
if (haveline == true)
{
string.removeFromParent()
haveline = false
}
if (havespring == true)
{
let path = CGPathCreateMutable()
CGPathMoveToPoint(path, nil, bA.position.x, bA.position.y)
CGPathAddLineToPoint(path, nil, sp.x, sp.y)
CGPathCloseSubpath(path);
string = SKShapeNode(path: path )
self.addChild(string)
haveline = true
}
}
func undrawme()
{
self.physicsWorld.removeJoint(spring)
havespring = false
}
}
试试这个:
it.frequency = 1.0
it.damping = 0.0
self.physicsWorld.addJoint(it)
我认为你的频率太高了,看不到任何东西,默认值是0.0。