Swift中的SpriteKit物理-球在墙上滑动而不是反射 [英] SpriteKit physics in Swift - Ball slides against wall instead of reflecting

查看:100
本文介绍了Swift中的SpriteKit物理-球在墙上滑动而不是反射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在学习SpriteKit时,我一直在基于Breakout创建自己的非常简单的测试游戏(使用Ray Wenderlich等人的Tutorials的iOS游戏),看看我是否可以应用所学的概念。我决定通过使用.sks文件创建精灵节点并替换我的手动边界检查和与物理物体的碰撞来简化代码。

I have been creating my own very simple test game based on Breakout while learning SpriteKit (using iOS Games by Tutorials by Ray Wenderlich et al.) to see if I can apply concepts that I have learned. I have decided to simplify my code by using an .sks file to create the sprite nodes and replacing my manual bounds checking and collision with physics bodies.

但是,我的球不断每当它与壁/其他矩形以陡峭的角度碰撞时,它们就会平行于壁/其他矩形运行(例如,简单地上下滑动)。这是相关的代码-我已将物理机体属性移到代码中以使其更加可见:

However, my ball keeps running parallel to walls/other rectangles (as in, simply sliding up and down them) any time it collides with them at a steep angle. Here is the relevant code--I have moved the physics body properties into code to make them more visible:

import SpriteKit

struct PhysicsCategory {
  static let None:        UInt32 = 0      //  0
  static let Edge:        UInt32 = 0b1    //  1
  static let Paddle:      UInt32 = 0b10   //  2
  static let Ball:        UInt32 = 0b100  //  4
}

var paddle: SKSpriteNode!
var ball: SKSpriteNode!

class GameScene: SKScene, SKPhysicsContactDelegate {

  override func didMoveToView(view: SKView) {

    physicsWorld.gravity = CGVector.zeroVector

    let edge = SKNode()
    edge.physicsBody = SKPhysicsBody(edgeLoopFromRect: frame)
    edge.physicsBody!.usesPreciseCollisionDetection = true
    edge.physicsBody!.categoryBitMask = PhysicsCategory.Edge
    edge.physicsBody!.friction = 0
    edge.physicsBody!.restitution = 1
    edge.physicsBody!.angularDamping = 0
    edge.physicsBody!.linearDamping = 0
    edge.physicsBody!.dynamic = false
    addChild(edge)

    ball = childNodeWithName("ball") as SKSpriteNode
    ball.physicsBody = SKPhysicsBody(rectangleOfSize: ball.size))
    ball.physicsBody!.usesPreciseCollisionDetection = true
    ball.physicsBody!.categoryBitMask = PhysicsCategory.Ball
    ball.physicsBody!.collisionBitMask = PhysicsCategory.Edge | PhysicsCategory.Paddle
    ball.physicsBody!.allowsRotation = false
    ball.physicsBody!.friction = 0
    ball.physicsBody!.restitution = 1
    ball.physicsBody!.angularDamping = 0
    ball.physicsBody!.linearDamping = 0

    physicsWorld.contactDelegate = self
}

以前忘记提及这一点,但是我添加了一个简单的touchesBegan函数来调试弹跳-它只是调整速度以将球指向接触点:

Forgot to mention this before, but I added a simple touchesBegan function to debug the bounces - it just adjusts the velocity to point the ball at the touch point:

override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
  let touch = touches.anyObject() as UITouch
  let moveToward = touch.locationInNode(self)
  let targetVector = (moveToward - ball.position).normalized() * 300.0
  ball.physicsBody!.velocity = CGVector(point: targetVector)
}

normalized()函数只是将球/触摸位置变化量减小为单位矢量,并且有一个超越

The normalized() function just reduces the ball/touch position delta to a unit vector, and there is an override of the minus operator that allows for CGPoint subtraction.

球/边碰撞应始终以正好相反的角度反射球,但由于某些原因球似乎确实有直角的东西。当然,我可以实现一些解决方法来手动反映球的角度,但要点是我想使用SpriteKit中的内置物理功能来完成所有这些工作。

The ball/edge collisions should always reflect the ball at a precisely opposite angle but for some reason the ball really seems to have a thing for right angles. I can of course implement some workaround to reflect the ball's angle manually, but the point is that I want to do this all using the built in physics functionality in SpriteKit. Is there something obvious that I am missing?

推荐答案

这似乎与碰撞检测有关。大多数人已经找到解决方案,方法是使用didBeginContact并在相反的方向重新施加力。请注意,他说didMoveToView,但在后来对didBeginContact的评论中进行了纠正。

This appears to be an issue with collision detection. Most have found solutions by using the didBeginContact and reapplying the force at an opposite direction. Note he says didMoveToView but corrects himself in a later comment to didBeginContact.

请参阅Ray Wenderlich教程底部的评论此处

See comments at the bottom of the Ray Wenderlich tutorial here


如果球
撞到一个浅角度(@ aziz76和@colinf),我可以解决球越轨的问题。我添加了另一个
类别 BorderCategory,并将其分配给我们在didMoveToView中创建的边界PhysicsBody

I have a fix for the problem with the ball "riding the rail" if it strikes at a shallow angle (@aziz76 and @colinf). I added another category, "BorderCategory" and assigned it to the border PhysicsBody we create in didMoveToView.

以及类似的SO问题此处解释其发生原因。

and a similar SO question here explaining why it is happening.

尽管如此,但由于
浮点舍入错误,许多物理引擎(包括
SpriteKit的)都会遇到这种情况。我发现,当我希望一个物体在碰撞后保持
的恒定速度时,最好将其强制-
使用didEndContact:或didSimulatePhysics处理程序来重置移动的
物体的速度,因此它的速度与
碰撞前的速度相同(但方向相反)。

Even if you do that, though, many physics engines (including SpriteKit's) have trouble with situations like this because of floating point rounding errors. I've found that when I want a body to keep a constant speed after a collision, it's best to force it to -- use a didEndContact: or didSimulatePhysics handler to reset the moving body's velocity so it's going the same speed it was before the collision (but in the opposite direction).

我注意到的是,您使用的是正方形而不是圆形的球,您可能要考虑使用...

Also another thing I noticed is you are using a square instead of a circle for your ball and you may want to consider using...

ball.physicsBody = SKPhysicsBody(circleOfRadius: ball.size.width/2)

所以事实证明您不是't crazy,总是很高兴听到别人的声音,希望这会帮助您找到最适合您的应用程序的解决方案。

So turns out you aren't crazy which is always good to hear from someone else and hopefully this will help you find a solution that works best for your application.

这篇关于Swift中的SpriteKit物理-球在墙上滑动而不是反射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆