SKSpriteNode 碰撞识别速度不够快
SKSpriteNode collision not being recognized fast enough
我有一个 SKSpriteNode ("Known as ball"),它通过向其 physicsBody 施加脉冲来启动。在我的 didBeginContact 方法中,我检查节点是否与障碍物发生碰撞。如果是这样,我希望 SKSpriteNode 立即停止移动(例如 "stick" 到它碰撞的对象)。问题是,当意识到球已经与障碍物发生碰撞并且球的速度设置为 0 时,球已经反弹,而不是碰到它碰撞的障碍物,现在是 30 像素左右离开
func didBeginContact(contact: SKPhysicsContact) {
ballInMidAir = false
var firstBody : SKPhysicsBody = contact.bodyA
var secondBody : SKPhysicsBody = contact.bodyB
if (firstBody.categoryBitMask == PhysicsCatagory.Obstacle && secondBody.categoryBitMask == PhysicsCatagory.Ball){
(secondBody.node as! SKSpriteNode).removeAllActions()
secondBody.velocity = CGVectorMake(0, 0)
(secondBody.node as! SKSpriteNode).runAction(SKAction.moveBy(obstacleVector, duration: 9.0))
}
else if (firstBody.categoryBitMask == PhysicsCatagory.ObstacleStill && secondBody.categoryBitMask == PhysicsCatagory.Ball){
(secondBody.node as! SKSpriteNode).removeAllActions()
secondBody.velocity = CGVectorMake(0, 0)
}
}
理论上我可以计算出球应该与障碍物碰撞的点,并在碰撞后将球移动到那个点,但是大多数障碍物正在积极四处走动,所以这将非常困难。有人有解决办法吗?提前致谢 (:
一些想法...
- 正如 CH Buckingham 评论的那样,将物理体的
usesPreciseCollisionDetection
设置为 true
对于快速移动的精灵是个好主意。从文档中,
If this property is set to YES on either body, the simulation performs a more precise and more expensive calculation to detect these collisions. This property should be set to YES on small, fast moving bodies.
- 正如 Whirlwind 指出的那样,设置
restitution
属性 会影响碰撞的弹性。将此设置为零将防止球从障碍物弹回
This property is used to determine how much energy the physics body
loses when it bounces off another object. The property must be a value
between 0.0 and 1.0. The default value is 0.2.
- 在其中一个物体上设置
collisionBitMask = 0
会导致球穿过障碍物而不是从障碍物上弹开
- 使用
SKPhysicsContact
对象的 contactPoint
属性 重新定位小球,其中 contactPoint
是
... point between the two physics bodies [where the impact occurred], in
scene coordinates
也可以使用collisionImpulse
属性来判断
[t]he impulse that specifies how hard these two bodies struck each other
in newton-seconds.
和给出碰撞方向的normalVector
属性
我有一个 SKSpriteNode ("Known as ball"),它通过向其 physicsBody 施加脉冲来启动。在我的 didBeginContact 方法中,我检查节点是否与障碍物发生碰撞。如果是这样,我希望 SKSpriteNode 立即停止移动(例如 "stick" 到它碰撞的对象)。问题是,当意识到球已经与障碍物发生碰撞并且球的速度设置为 0 时,球已经反弹,而不是碰到它碰撞的障碍物,现在是 30 像素左右离开
func didBeginContact(contact: SKPhysicsContact) {
ballInMidAir = false
var firstBody : SKPhysicsBody = contact.bodyA
var secondBody : SKPhysicsBody = contact.bodyB
if (firstBody.categoryBitMask == PhysicsCatagory.Obstacle && secondBody.categoryBitMask == PhysicsCatagory.Ball){
(secondBody.node as! SKSpriteNode).removeAllActions()
secondBody.velocity = CGVectorMake(0, 0)
(secondBody.node as! SKSpriteNode).runAction(SKAction.moveBy(obstacleVector, duration: 9.0))
}
else if (firstBody.categoryBitMask == PhysicsCatagory.ObstacleStill && secondBody.categoryBitMask == PhysicsCatagory.Ball){
(secondBody.node as! SKSpriteNode).removeAllActions()
secondBody.velocity = CGVectorMake(0, 0)
}
}
理论上我可以计算出球应该与障碍物碰撞的点,并在碰撞后将球移动到那个点,但是大多数障碍物正在积极四处走动,所以这将非常困难。有人有解决办法吗?提前致谢 (:
一些想法...
- 正如 CH Buckingham 评论的那样,将物理体的
usesPreciseCollisionDetection
设置为true
对于快速移动的精灵是个好主意。从文档中,
If this property is set to YES on either body, the simulation performs a more precise and more expensive calculation to detect these collisions. This property should be set to YES on small, fast moving bodies.
- 正如 Whirlwind 指出的那样,设置
restitution
属性 会影响碰撞的弹性。将此设置为零将防止球从障碍物弹回
This property is used to determine how much energy the physics body loses when it bounces off another object. The property must be a value between 0.0 and 1.0. The default value is 0.2.
- 在其中一个物体上设置
collisionBitMask = 0
会导致球穿过障碍物而不是从障碍物上弹开 - 使用
SKPhysicsContact
对象的contactPoint
属性 重新定位小球,其中contactPoint
是
... point between the two physics bodies [where the impact occurred], in scene coordinates
也可以使用collisionImpulse
属性来判断
[t]he impulse that specifies how hard these two bodies struck each other in newton-seconds.
和给出碰撞方向的normalVector
属性