我正在尝试用SKSpriteNode
创建一辆简单的汽车。物理学和一切都很好,除了一件小事:当我的汽车从更远的地方降落在地面上时,汽车车身被推下然后回到原位。
见图片:
我想得到左图,但我得到右图。
当它降落在地面上时,我不希望车身进入车轮,我希望它相对于车轮处于固定的位置,反之亦然。
所以我就是这样做的:
这是在汽车类:
carWheelJoint1 = SKPhysicsJointPin.jointWithBodyA(self.physicsBody!, bodyB: leftWheel.physicsBody!, anchor: leftWheel.position)
carWheelJoint2 = SKPhysicsJointPin.jointWithBodyA(self.physicsBody!, bodyB: rightWheel.physicsBody!, anchor: rightWheel.position)
这是在GameScene课程中:
//Add vehicle
vehicle = DevCar(color: UIColor.blueColor(), size: CGSize(width: 250, height: 50), cameraThreshold: cameraThreshold)
vehicle.position = (CGPoint(x: 0, y: cameraThreshold))
addChild(vehicle)
self.physicsWorld.addJoint(vehicle.carWheelJoint1)
self.physicsWorld.addJoint(vehicle.carWheelJoint2)
我尝试使用SKPhysicsJointFixed
,但这也不起作用。
有什么想法吗?
编辑:这就是物理体的定义方式:
这是汽车类中的车身
汽车等级是SKSpriteNode
,是汽车本身,车轮是它的孩子。
let baseBody = SKPhysicsBody(rectangleOfSize: size)
baseBody.mass = CGFloat(1)
baseBody.dynamic = true
baseBody.affectedByGravity = true
baseBody.friction = 0.2
baseBody.allowsRotation = false
baseBody.density = CGFloat(10)
self.physicsBody = baseBody
这是同一类轮子:
leftWheel = SKShapeNode(circleOfRadius: wheelSize)
leftWheel.position = CGPoint(x: size.width / -2 + wheelSize, y: size.height / -2 - wheelSize)
rightWheel = SKShapeNode(circleOfRadius: wheelSize)
rightWheel.position = CGPoint(x: size.width / 2 - wheelSize, y: size.height / -2 - wheelSize)
leftWheel.physicsBody = SKPhysicsBody(circleOfRadius: wheelSize)
leftWheel.physicsBody!.mass = CGFloat(0.2)
leftWheel.physicsBody!.dynamic = true
leftWheel.physicsBody!.affectedByGravity = true
leftWheel.physicsBody!.friction = 0.3
rightWheel.physicsBody = SKPhysicsBody(circleOfRadius: wheelSize)
rightWheel.physicsBody!.mass = CGFloat(0.2)
rightWheel.physicsBody!.dynamic = true
rightWheel.physicsBody!.affectedByGravity = true
rightWheel.physicsBody!.friction = 0.3
addChild(leftWheel)
addChild(rightWheel)
EDIT2:已添加视频 https://www.youtube.com/watch?v=TTwa-t4u4pI