How to keep a 30˚ distance between two lines anchored at a point - swift

I am trying to create two lines that are anchored at a certain point (sprite) and rotate to form a 30 degree angle between them. Below is an image what I want to achieve.
This is what I've done so far:
extension Int {
var degreesToRadians: Double { return Double(self) * .pi / 180 }
}
extension FloatingPoint {
var degreesToRadians: Self { return self * .pi / 180 }
var radiansToDegrees: Self { return self * 180 / .pi }
}
class GameScene: SKScene, SKPhysicsContactDelegate {
var anchorSprite = SKSpriteNode(imageNamed: "anchorSprite")
var armLeft = SKSpriteNode(imageNamed: "lineSprite")
var armRight = SKSpriteNode(imageNamed: "lineSprite")
override func didMove(to view: SKView) {
self.physicsWorld.gravity = CGVector(dx: 0, dy: -1.8)
self.physicsWorld.contactDelegate = self
var tealBg = SKSpriteNode(imageNamed: "tealBg")
tealBg.position = CGPoint(x: frame.midX, y: frame.midY)
tealBg.zPosition = 10
addChild(tealBg)
anchorSprite.position = CGPoint(x: frame.midX, y: frame.midY + frame.midY/2)
anchorSprite.zPosition = 20
anchorSprite.physicsBody = SKPhysicsBody(rectangleOf: anchorSprite.frame.size)
anchorSprite.physicsBody?.categoryBitMask = pinCategory
anchorSprite.physicsBody?.isDynamic = false
addChild(anchorSprite)
armRight.anchorPoint = CGPoint(x: 0.5, y: 1)
armRight.position = anchorSprite.position
armRight.zPosition = 20
armRight.physicsBody = SKPhysicsBody(rectangleOf: armRight.frame.size)
armRight.zRotation = CGFloat(Double(15).degreesToRadians)//CGFloat(Double.pi/6)
armRight.physicsBody!.isDynamic = true
addChild(armRight)
armLeft.anchorPoint = CGPoint(x: 0.5, y: 1)
armLeft.position = anchorSprite.position
armLeft.zPosition = 20
armLeft.physicsBody = SKPhysicsBody(rectangleOf: armRight.frame.size)
armLeft.zRotation = CGFloat(Double(-15).degreesToRadians)//CGFloat(-Double.pi/6)
armLeft.physicsBody!.isDynamic = true
addChild(armLeft)
//Pin joints
var pinAndRightArmJoint = SKPhysicsJointPin.joint(withBodyA: anchorSprite.physicsBody!, bodyB: armRight.physicsBody!, anchor: CGPoint(x: anchorSprite.position.x, y: self.armRight.frame.maxY))
self.physicsWorld.add(pinAndRightArmJoint)
var pinAndLeftArmJoint = SKPhysicsJointPin.joint(withBodyA: anchorSprite.physicsBody!, bodyB: armLeft.physicsBody!, anchor: CGPoint(x: anchorSprite.position.x, y: self.armLeft.frame.maxY))
self.physicsWorld.add(pinAndLeftArmJoint)
}
Below is an image from running the above code (they are close together).
How can I make sure the lines are always 30˚ apart and maintain 30˚ apart even when rotated?

To keep your two lines separated by exactly 30°, you can use an SKPhysicsJointFixed, which is just what it sounds like: it pins two physicsBodies together in a fixed position. Since you already have them positioned the way you want, just add this code where you have the other SKPhysicsJoints to hold them that way:
let fixArms = SKPhysicsJointFixed.joint(withBodyA: armLeft.physicsBody!, bodyB: armRight.physicsBody!, anchor: CGPoint.zero)
self.physicsWorld.add(fixArms)
Result:

If you make the line nodes children of the anchor sprite (instead of the scene), rotating the anchor sprite node will rotate all the lines along with it without doing anything special with physics. You just need to mind the anchor points so that they align properly (i.e. line's anchor at its extremity rather than center)

Related

How to create realistic spinning wheel in SpriteKit

I am trying to create a spinning fortune wheel action via SKAction. I have a SKNode which used as wheel, this SKNode is a circle that divided to four quarters (each quarter in different color). also I set an SKAction (which is repeating for 10 counts) that spin the SKNode around fixed point (the node's center). The problem is that the action is running well but it stops suddenly and not slowing down - like a real wheel. I don't really have an idea how to set this animation, I mean to slow the spinning down before the action is stop.
Here is my code so far:
class GameScene: SKScene {
let colors = [SKColor.yellow, SKColor.red, SKColor.blue, SKColor.purple]
override func didMove(to view: SKView) {
createWheel()
let sq = CGRect(x: size.width/2, y: size.height/2, width: 300, height: 300)
let sqx = SKShapeNode(rect: sq)
sqx.lineWidth = 2
sqx.fillColor = .clear
sqx.setScale(1.0)
addChild(sqx)
}
func createWheel() {
let path = UIBezierPath()
path.move(to: CGPoint(x: 0, y: 0))
path.addLine(to: CGPoint(x: 0, y: -200))
path.addArc(withCenter: CGPoint.zero,radius: 200,startAngle: CGFloat(0.0), endAngle: CGFloat(3.0 * Double.pi / 2),clockwise: false)
path.addLine(to: CGPoint(x: 200, y: 0))
let obstacle = obstacleByDuplicatingPath(path, clockwise: true)
obstacle.position = CGPoint(x: size.width/2, y: size.height/2)
addChild(obstacle)
let rotateAction = SKAction.rotate(byAngle: CGFloat((3.0 * CGFloat(Double.pi / 2)) - 90), duration: 0.5)
//obstacle.run(SKAction.repeatForever(rotateAction))
obstacle.run(SKAction.repeat(rotateAction, count: 10))
}
func obstacleByDuplicatingPath(_ path: UIBezierPath, clockwise: Bool) -> SKNode {
let container = SKNode()
var rotationFactor = CGFloat(Double.pi / 2)
if !clockwise {
rotationFactor *= -1
}
for i in 0...3 {
let section = SKShapeNode(path: path.cgPath)
section.fillColor = colors[i]
section.strokeColor = colors[i]
section.zRotation = rotationFactor * CGFloat(i);
let origin = CGPoint(x: 0.0, y: 0.0)
switch i {
case 0:
section.position = CGPoint(x: (origin.x + 10), y: (origin.y - 10))
case 1:
section.position = CGPoint(x: (origin.x + 10), y: (origin.y + 10))
case 2:
section.position = CGPoint(x: (origin.x - 10), y: (origin.y + 10))
case 3:
section.position = CGPoint(x: (origin.x - 10), y: (origin.y - 10))
default:
print("bolbol")
}
container.addChild(section)
}
return container
}
}
edit:
I was thinking about it and I tried to do it via SKAction, I set another action but this time I set their duration to a long one. first it run a action of duration 0.5, then of 2 and at end of 4. I looks pretty good but still not smooth as I want it to be.
here is my code:
let rotateAction = SKAction.rotate(byAngle: CGFloat(2.0 * CGFloat(M_PI)), duration: 0.5)
let rotateAction2 = SKAction.rotate(byAngle: CGFloat(2.0 * CGFloat(M_PI)), duration: 2)
let rotateAction3 = SKAction.rotate(byAngle: CGFloat(2.0 * CGFloat(M_PI)), duration: 4)
let wait = SKAction.wait(forDuration: 5)
let g1 = SKAction.repeat(rotateAction, count: 10)
let group = SKAction.group([wait, g1, rotateAction2, rotateAction3])
what do you think? there is any way to do it better??
edit 2:
Continued to #Ali Beadle answer, I tried to do it via physics body, the problem now is the when I drag finger on the screen the SKShapeNode (shape) in continue to rotate and never stops. can you detect what is wrong?
class GameScene: SKScene {
var start: CGPoint?
var end:CGPoint?
var startTime: TimeInterval?
let shape = SKShapeNode.init(rectOf: CGSize(width: 150, height: 150))
override func didMove(to view: SKView) {
self.physicsWorld.gravity = CGVector(dx: 0, dy: -9.8)
let sceneBody = SKPhysicsBody.init(edgeLoopFrom: self.frame)
sceneBody.friction = 0
self.physicsBody = sceneBody
shape.fillColor = SKColor.red
shape.position = CGPoint(x: self.size.width/2, y: self.size.height/2)
shape.physicsBody = SKPhysicsBody.init(rectangleOf: CGSize(width: 50, height: 50))
shape.physicsBody?.affectedByGravity = false
shape.physicsBody?.isDynamic = true
addChild(shape)
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
guard let touch = touches.first else {return}
self.start = touch.location(in: self)
self.startTime = touch.timestamp
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
guard let touch = touches.first else {return}
self.end = touch.location(in: self)
var dx = ((self.end?.x)! - (self.start?.x)!)
var dy = ((self.end?.y)! - (self.start?.y)!)
let magnitude:CGFloat = sqrt(dx*dx+dy*dy)
if(magnitude >= 25){
let dt:CGFloat = CGFloat(touch.timestamp - self.startTime!)
if dt > 0.1 {
let speed = magnitude / dt
dx = dx / magnitude
dy = dy / magnitude
print("dx: \(dx), dy: \(dy), speed: \(speed) ")
}
}
let touchPosition = touch.location(in: self)
if touchPosition.x < (self.frame.width / 2) {
self.shape.physicsBody?.angularVelocity = 10
self.shape.physicsBody?.applyAngularImpulse(-180)
} else {
self.shape.physicsBody?.angularVelocity = 10
self.shape.physicsBody?.applyAngularImpulse(180)
}
}}
I have created an open source prize spinning wheel in Spritekit that uses physics for realistic movement and flapper control. It also allows the user to drag the wheel to spin or generates a random spin by pushing the center of the wheel.
https://github.com/hsilived/SpinWheel
You can add realistic movement like this by using the built-in Physics simulation of SpriteKit. This will allow you to give your wheel a mass and friction and then use forces to rotate it. It will then slow down realistically.
In outline see Simulating Physics in the Apple Documentation:
To use physics in your game, you need to:
Attach physics bodies to nodes in the node tree and configure their physical properties. See SKPhysicsBody.
Define global characteristics of the scene’s physics simulation, such as gravity. See SKPhysicsWorld.
Where necessary to support your gameplay, set the velocity of physics bodies in the scene or apply forces or impulses to them. ...
The most appropriate method for your wheel is probably to make the wheel pinned to the scene and then rotate it with applyAngularImpulse.

How to rotate sprites around a joint

I need to make the arms and hands rotate around the center of the hook, as shown in the image below without them separating or changing their shape (no changes in the angles between arms and hands just rotation at A), as in the image below:
I tried rotating the arms but this made them separate and change form. You can check out my code below:
let hookCategoryName = "hook"
let leftArmCategoryName = "leftArm"
let rightArmCategoryName = "rightArm"
let leftHandCategoryName = "leftHand"
let rightHandCategoryName = "rightHand"
let hookCategory : UInt32 = 0x1 << 0
let leftArmCategory : UInt32 = 0x1 << 1
let rightArmCategory : UInt32 = 0x1 << 2
let leftHandCategory : UInt32 = 0x1 << 3
let rightHandCategory : UInt32 = 0x1 << 4
extension Int {
var degreesToRadians: Double { return Double(self) * .pi / 180 }
}
extension FloatingPoint {
var degreesToRadians: Self { return self * .pi / 180 }
var radiansToDegrees: Self { return self * 180 / .pi }
}
class GameScene: SKScene, SKPhysicsContactDelegate {
var hook = SKSpriteNode(imageNamed: "hook")
var leftArm = SKSpriteNode(imageNamed: "arm")
var rightArm = SKSpriteNode(imageNamed: "arm")
var leftHand = SKSpriteNode(imageNamed: "leftHand")
var rightHand = SKSpriteNode(imageNamed: "rightHand")
override func didMove(to view: SKView) {
self.physicsWorld.gravity = CGVector(dx: 0, dy: 0)
self.physicsWorld.contactDelegate = self
var yellowBg = SKSpriteNode(imageNamed: "yellowBg")
yellowBg.position = CGPoint(x: frame.midX, y: frame.midY)
yellowBg.zPosition = 2
addChild(yellowBg)
hook.position = CGPoint(x: frame.midX, y: frame.midY + frame.midY/2)
hook.zPosition = 5
hook.name = hookCategoryName
hook.physicsBody = SKPhysicsBody(rectangleOf: hook.frame.size)
hook.physicsBody?.categoryBitMask = hookCategory
hook.physicsBody?.isDynamic = false
addChild(hook)
rightArm.anchorPoint = CGPoint(x: 0.5, y: 1)
rightArm.position = hook.position
rightArm.zPosition = 5
rightArm.name = rightArmCategoryName
rightArm.physicsBody = SKPhysicsBody(rectangleOf: rightArm.frame.size)
rightArm.physicsBody?.categoryBitMask = rightArmCategory
rightArm.physicsBody!.isDynamic = true
addChild(rightArm)
leftArm.anchorPoint = CGPoint(x: 0.5, y: 1)
leftArm.position = hook.position
leftArm.zPosition = 5
leftArm.name = leftArmCategoryName
leftArm.physicsBody = SKPhysicsBody(rectangleOf: leftArm.frame.size)
leftArm.physicsBody?.categoryBitMask = leftArmCategory
leftArm.physicsBody!.isDynamic = true
addChild(leftArm)
// leftHand
leftHand.anchorPoint = CGPoint(x: 0.5, y: 0.5)
leftHand.position = CGPoint(x: leftArm.frame.minX - 22, y: leftArm.frame.minY + 7) //CGPoint(x: armLeft.position.x, y: armLeft.position.y)
leftHand.zPosition = 5
leftHand.name = leftHandCategoryName
leftHand.physicsBody = SKPhysicsBody(rectangleOf: leftHand.frame.size)
leftHand.physicsBody?.categoryBitMask = leftHandCategory
leftHand.zRotation = CGFloat(Double(-30).degreesToRadians)//CGFloat(-Double.pi/6)
//armLeft.physicsBody?.categoryBitMask = armCategory
leftHand.physicsBody!.isDynamic = true
addChild(leftHand)
// rightHand
rightHand.anchorPoint = CGPoint(x: 0.5, y: 0.5)
rightHand.position = CGPoint(x: rightArm.frame.minX + 30, y: rightArm.frame.minY + 7) //CGPoint(x: armLeft.position.x, y: armLeft.position.y)
rightHand.zPosition = 5
rightHand.name = rightHandCategoryName
rightHand.physicsBody = SKPhysicsBody(rectangleOf: rightHand.frame.size)
rightHand.physicsBody?.categoryBitMask = rightHandCategory
rightHand.zRotation = CGFloat(Double(30).degreesToRadians)//CGFloat(-Double.pi/6)
//armLeft.physicsBody?.categoryBitMask = armCategory
rightHand.physicsBody!.isDynamic = true
addChild(rightHand)
leftArm.zRotation = CGFloat(Double(-45).degreesToRadians)
rightArm.zRotation = CGFloat(Double(45).degreesToRadians)
rightHand.physicsBody?.contactTestBitMask = rightHandCategory
leftHand.physicsBody?.contactTestBitMask = leftHandCategory
rightHand.physicsBody?.collisionBitMask = rightHandCategory
leftHand.physicsBody?.collisionBitMask = leftHandCategory
let hookAndRightArmJoint = SKPhysicsJointPin.joint(withBodyA: hook.physicsBody!, bodyB: rightArm.physicsBody!, anchor: CGPoint(x: hook.position.x, y: self.rightArm.frame.maxY))
self.physicsWorld.add(hookAndRightArmJoint)
let hookAndLeftArmJoint = SKPhysicsJointPin.joint(withBodyA: hook.physicsBody!, bodyB: leftArm.physicsBody!, anchor: CGPoint(x: hook.position.x, y: self.leftArm.frame.maxY))
self.physicsWorld.add(hookAndLeftArmJoint)
let armsFixedJoint = SKPhysicsJointFixed.joint(withBodyA: leftArm.physicsBody!, bodyB: rightArm.physicsBody!, anchor: CGPoint.zero)
self.physicsWorld.add(armsFixedJoint)
//left arm and hand joint
let leftArmAndHandJoint = SKPhysicsJointPin.joint(withBodyA: leftArm.physicsBody!, bodyB: leftHand.physicsBody!, anchor: CGPoint(x: self.leftArm.frame.minX, y: self.leftArm.frame.minY)/*CGPoint(x: armLeft.position.x, y: self.armLeft.frame.minY)*/)
self.physicsWorld.add(leftArmAndHandJoint)
//right arm and hand joint
let rightArmAndHandJoint = SKPhysicsJointPin.joint(withBodyA: rightArm.physicsBody!, bodyB: rightHand.physicsBody!, anchor: CGPoint(x: self.rightArm.frame.maxX, y: self.rightArm.frame.minY)/*CGPoint(x: armLeft.position.x, y: self.armLeft.frame.minY)*/)
self.physicsWorld.add(rightArmAndHandJoint)
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
hook.run(SKAction.rotate(byAngle: CGFloat(Double(60).degreesToRadians), duration: 0.5))
}
}
Also rotating the hook has no effect on the arms and hands as seen in the image below when the above code is run:
How can I get the rotation in the image first image?
You can make the arms and hands children of a common parent node. You could create a blank SKNode for just this purpose like so:
let armsParent = SKNode()
Then instead of adding the arms and hands as children to the scene directly, add them as children of armsParent like so:
armsParent.addChild(leftArm)
armsParent.addChild(rightArm) // same for hands...
Then you can simply rotate armsParent with an SKAction to achieve what you want.
OR, to make it even simpler, you could just add the arms and hands as children to hook directly like this:
hook.addChild(leftArm) // same for other arm and hands...
Since the arms and hands are children of hook or armParent their positions will now be determined relative to their parent. So you might have to change all your .position = initialization code to accommodate this.

How to rotate SKNode in swift

I want my SKNode To rotate like Image below :)
Image Here
Instead it is rotating around the bottom left corner of the screen!
Click Here To View Video of what is happening that I do not want
How do i get it to rotate counterClockWise or Clockwise in one position like image show above?
Thank you ahead of time for someone who can help me out. not sure if i have to change anchor points or what... thank you
Here is my code below in swift.
import SpriteKit
class GameScene: SKScene, SKPhysicsContactDelegate {
var top = SKSpriteNode()
var bottom = SKSpriteNode()
var line = SKSpriteNode()
var RightSide = SKSpriteNode()
var LeftSide = SKSpriteNode()
var pointBar = SKSpriteNode()
var Second_point_Bar_For_First_Hoop = SKSpriteNode()
override func didMove(to view: SKView) {
physicsWorld.contactDelegate = self
createHoop()
}
func createHoop() {
top = SKSpriteNode(imageNamed: "top")
top.position = CGPoint(x: self.frame.width / 2, y: self.frame.height / 2 + 15)
top.size = CGSize(width: 100, height: 60)
top.zPosition = 0
bottom = SKSpriteNode(imageNamed: "bottom")
bottom.position = CGPoint(x: self.frame.width / 2, y: self.frame.height / 2 - 45)
bottom.size = CGSize(width: 100, height: 60)
bottom.zPosition = 2
LeftSide = SKSpriteNode()
LeftSide.position = CGPoint(x: bottom.position.x - 40, y: bottom.position.y)
LeftSide.size = CGSize(width: 10, height: 10)
LeftSide.zPosition = 0
LeftSide.color = UIColor.blue
RightSide = SKSpriteNode()
RightSide.position = CGPoint(x: bottom.position.x + 40, y: bottom.position.y)
RightSide.size = CGSize(width: 5, height: 10)
RightSide.zPosition = 0
RightSide.color = UIColor.blue
pointBar = SKSpriteNode()
pointBar.position = CGPoint(x: bottom.position.x, y: bottom.position.y + 10)
pointBar.size = CGSize(width: 90, height: 2)
pointBar.zPosition = 100
pointBar.color = UIColor.green
pointBar.zPosition = 100
Second_point_Bar_For_First_Hoop = SKSpriteNode()
Second_point_Bar_For_First_Hoop.position = CGPoint(x: top.position.x, y: top.position.y - 10)
Second_point_Bar_For_First_Hoop.size = CGSize(width: 90, height: 2)
Second_point_Bar_For_First_Hoop.zPosition = 100
Second_point_Bar_For_First_Hoop.color = UIColor.green
Second_point_Bar_For_First_Hoop.zPosition = 100
let hoopPair = SKNode()
hoopPair.addChild(top)
hoopPair.addChild(pointBar)
hoopPair.addChild(Second_point_Bar_For_First_Hoop)
hoopPair.addChild(bottom)
hoopPair.addChild(LeftSide)
hoopPair.addChild(RightSide)
let rotate = SKAction.rotate(byAngle: 1, duration: 5)
let repeatRotation = SKAction.repeatForever(rotate)
hoopPair.run(repeatRotation)
self.addChild(hoopPair)
}
override func update(_ currentTime: CFTimeInterval) {
/* Called before each frame is rendered */
}
}
By default, SKNode anchor point is always 0.5, 0.5. This means you need to work the positions so that everything goes off of the center of the node.
Now everything is going to be relative, so your top and bottom are relative to your hoop node.
Then you need to move the hoopnode position so that it is where you want it.
Here is the code to do that:
(Note I took out all needless code to get your image to rotate on center)
(Another Node: if size does not work, use frame.size)
import SpriteKit
class GameScene: SKScene, SKPhysicsContactDelegate {
var top = SKSpriteNode() var bottom = SKSpriteNode() var line = SKSpriteNode() var RightSide = SKSpriteNode() var LeftSide = SKSpriteNode() var pointBar = SKSpriteNode() var Second_point_Bar_For_First_Hoop = SKSpriteNode()
override func didMove(to view: SKView) {
physicsWorld.contactDelegate = self
createHoop()
}
func createHoop() {
top = SKSpriteNode(imageNamed: "top")
top.size = CGSize(width: 100, height: 60)
top.position = CGPoint(x: 0, y: top.size.height/2)
top.zPosition = 0
bottom = SKSpriteNode(imageNamed: "bottom")
bottom.size = CGSize(width: 100, height: 60)
bottom.position = CGPoint(x: 0, y: -bottom.size.height/2)
bottom.zPosition = 2
let hoopPair = SKNode()
hoopPair.addChild(top)
hoopPair.addChild(bottom)
let rotate = SKAction.rotate(byAngle: 1, duration: 5)
let repeatRotation = SKAction.repeatForever(rotate)
hoopPair.position = CGPoint(x:self.size.width/2,self.size.height/2)
hoopPair.run(repeatRotation)
self.addChild(hoopPair) }
override func update(_ currentTime: CFTimeInterval) {
/* Called before each frame is rendered */
} }
What is the anchor point of the sprite? Sprites rotate about their anchor point and yours appears to be set to (0,0) i.e. the bottom-left corner. If so, try changing it to (0.5,0.5)

How to code the accelerometer to have a fixed x position

I have a ship that moves based on the direction of gravity which is changed by the accelerometer, but I want it to only move along the width of the screen (I already know how to do that), but I do not know how to keep it on a fixed horizontal line. Here's my code
class GameScene: SKScene {
var manager = CMMotionManager()
var ship = SKSpriteNode()
override func didMoveToView(view: SKView) {
/* Setup your scene here */
let shipTexture = SKTexture(imageNamed: "EvadersShipVert2.png")
ship = SKSpriteNode(texture: shipTexture)
ship.position = CGPoint(x: CGRectGetMidX(self.frame), y: CGRectGetMidY(self.frame) - 250)
ship.size = CGSize(width: 90, height: 115)
shipTexture.filteringMode = SKTextureFilteringMode.Nearest
ship.zPosition = 2
ship.physicsBody = SKPhysicsBody(texture: shipTexture, size: CGSize(width: 90, height: 115))
ship.physicsBody?.dynamic = true
self.addChild(ship)
manager.startAccelerometerUpdates()
manager.accelerometerUpdateInterval = 0.1
manager.startAccelerometerUpdatesToQueue(NSOperationQueue.mainQueue()) {
(data, error) in
self.physicsWorld.gravity = CGVectorMake(CGFloat((data?.acceleration.x)!), CGFloat((data?.acceleration.y)!))
}
}
thanks in advance!
Replace
self.physicsWorld.gravity = CGVectorMake(CGFloat((data?.acceleration.x)!), CGFloat((data?.acceleration.y)!))
With
self.physicsWorld.gravity = CGVectorMake(0.0, CGFloat((data?.acceleration.y)!)
So you're not affecting the x position

SKPhysicsJointFixed in SpriteKit and Swift

I'm making a game in Sprite Kit and Swift and I have a Sprite at the bottom of the screen and falling Sprites from the top which I want to catch and stick to the Sprite at the bottom, so I'm trying to use SKPhysicsJointFixed but when the objects collide instead of the falling object sticking to the one at the bottom which is supposed to catch and have it attached, it seems as if the bottom Sprite adapts the physics of the falling sprite and then falls off the screen with it. Here's the code I have in my didBeginContact method. and skewer is the name of the Sprite at the bottom which should always be at the bottom and not disappear.
if contact.bodyA.node!.name == "Skewer"
{
let boundX = skewer.physicsBody?.node?.position.x
let fixedJoint = SKPhysicsJointFixed.jointWithBodyA(contact.bodyA.node!.physicsBody, bodyB: contact.bodyB.node!.physicsBody, anchor: CGPoint(x: boundX!, y: boundY))
physicsWorld.addJoint(fixedJoint)
// contact.bodyB.node!.removeFromParent()
}
else
{
contact.bodyA!.node!.removeFromParent()
}
and the physics for the bottom screen Sprite are here
func makeSkewer()
{
skewer.name = "Skewer"
skewer.position = CGPoint(x: size.width * 0.5, y: size.height * 0.244)
skewer.physicsBody = SKPhysicsBody(rectangleOfSize: skewer.size)
skewer.physicsBody?.affectedByGravity = false
skewer.physicsBody?.categoryBitMask = kSkewerCategory
skewer.physicsBody?.contactTestBitMask = kFoodCategory
skewer.physicsBody?.collisionBitMask = kSceneEdgeCategory
addChild(skewer)
}
and physics for the falling Sprites are here
func random() ->CGFloat
{
return CGFloat(Float(arc4random()) / 0xFFFFFFFF)
}
func random(#min: CGFloat,max: CGFloat) -> CGFloat
{
return random() * (max - min) + min
}
func addFood()
{
let randomCatchIndex = Int(arc4random_uniform(UInt32(foods.count)))
let food = SKSpriteNode(imageNamed: foods[randomCatchIndex])
let actualX = random(min: food.size.width/2, max: size.width - food.size.width/2)
let actualDuration = random(min: CGFloat(1.5), max: CGFloat(8.0))
let actionMove = SKAction.moveTo(CGPoint(x: actualX, y: -food.size.height/2), duration: NSTimeInterval(actualDuration))
let actionMoveDone = SKAction.removeFromParent()
food.physicsBody = SKPhysicsBody(rectangleOfSize: food.size)
food.position = CGPoint(x: actualX, y: size.height + food.size.height/2)
food.physicsBody?.categoryBitMask = kFoodCategory
food.physicsBody?.contactTestBitMask = kSkewerCategory
food.physicsBody?.collisionBitMask = 0x0
food.physicsBody?.dynamic = true
food.runAction(SKAction.sequence([actionMove, actionMoveDone]))
addChild(food)
}
Set the skewer to not have dynamic physics. What you have currently is it not being affected by gravity, and as soon as it locks onto the food (which is traveling down and has momentum), the skewer moves with it.
In the creation of the skewer, run the following line:
skewer.physicsBody?.dynamic = false
You can also now ignore the affectedByGravity as that is something that only affects dynamic objects.