gpt4 book ai didi

swift - SKSpriteNode 碰撞识别速度不够快

转载 作者:可可西里 更新时间:2023-11-01 01:05:28 27 4
gpt4 key购买 nike

我有一个 SKSpriteNode(“称为球”),它是通过向其 physicsBody 施加脉冲来启动的。在我的 didBeginContact 方法中,我检查节点是否与障碍物发生碰撞。如果是这样,我希望 SKSpriteNode 立即停止移动(例如,“坚持”到它碰撞的对象)。问题是,当意识到球已经与障碍物发生碰撞并且球的速度设置为 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)
}
}

理论上,我可以计算出球应该与障碍物碰撞的点,并在碰撞后将球移动到该点,但大多数障碍物都在主动移动,所以那将是非常困难的。有人有解决办法吗?提前致谢 (:

最佳答案

一些想法...

  1. 正如 CH Buckingham 评论的那样,对于快速移动的 Sprite ,将物理体的 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.

  1. 正如 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.

  1. 在其中一个物体上设置 collisionBitMask = 0 将使球穿过障碍物而不是从障碍物上弹开
  2. 使用 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 属性。

关于swift - SKSpriteNode 碰撞识别速度不够快,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31839284/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com