I'm using SpriteKit for a game where there is a ball and a character. The character is moving in a line with SKAction and the user can move the ball by touch. When the ball and character makes contact, I'd like the ball to be attached to the character.
func didBeginContact(contact: SKPhysicsContact) {
if contact.bodyA.categoryBitMask == charMask && contact.bodyB.categoryBitMask == ballMask{
println("contact")
if contactMade {
let movingBall = contact.bodyB.node!.copy() as! SKSpriteNode
//this line of code doesn't affect anything no matter what coordinates i set it to
movingBall.position = contact.bodyA.node!.position
contact.bodyB.node!.removeFromParent()
contact.bodyA.node!.removeAllActions()
contact.bodyA.node!.addChild(movingBall)
}
}
}
The current issue is that when I do contact.bodyA.node!.addChild(ball) (character node), the ball gets attached to the far left side of the character instead of on the character and it seems setting movingball.position does not affect it. I've also tried doing addChild to the scene and although it adds the ball to the correct place, it doesn't move with the character without another SKAction. What did I do wrong/what can I do to fix it?
Also, instead of using 2 sprites like that, should I use separate sprites so that theres a character sprite without a ball, character sprite with a ball and the ball itself, then just change the node's texture when they contact?
as movingBall is to be a child of bodyA.node, it's position needs to be in bodyA.node's coordinate space. So if you gave it coordinates of (0,0) it'd be in the middle:
Replace:
movingBall.position = contact.bodyA.node!.position
with
movingBall.position = CGPointZero
and see how that looks. the ball should be positioned directly on top of bodyA.node.
Related
I am trying to fix the camera to a sprite node “players.first!” and I managed to do so using SKConstraints as follows
func setupWorld(){
let playerCamera = SKCameraNode()
let background = SKSpriteNode(imageNamed: platformType + "BG")
var cameraFollow = [SKConstraint]()
cameraFollow.append(SKConstraint.distance(SKRange(constantValue: 0), to: players.first!))
playerCamera.constraints = cameraFollow
background.zPosition = layers().backgroundLayer
background.constraints = cameraFollow
background.size = self.size
self.addChild(playerCamera)
self.camera = playerCamera
self.addChild(background)
physicsWorld.contactDelegate = self
addEmitter()
}
But this keeps the camera fixed to the exact location of the node, I want the camera to be shifted to the right of the node “players.first!” (only in X dimension) and I couldn’t manage to do so with SKConstraints, note that the node is moving fast so updating the position of the camera in the update function makes the camera jitter.
This image is explaining my issue
Constrain the camera to an empty SKNode and make it a child node of the first player which is offset to the right in the frame of the player. This can be accomplished in the scene editor or programmatically by setting this dummy node's position to something like CGPoint(x: 100, y: 0). When the player moves, this node will also move, dragging the camera along with it; and since the camera is focused on this node, the nodes in the same 'world' of the player will appropriately appear to move in the opposite direction while maintaining the look you want for the player.
EDIT: If the player rotates
If the player needs to rotate, the above configuration will result in the entire node world revolving around the fixed empty node. To prevent this, instead place an empty SKNode that acts as the fixed camera point which will be called "cameraLocation" and the player node into another empty SKNode which will be called "pseudoPlayer". Constrain the camera to "cameraLocation". Moving the "pseudoPlayer" node will then move both the camera's fixed point (so that the camera moves) and the player node while only resulting in the rotation of the player and not the entire world.
NOTE: The only potential drawback is that in order to move the player correctly through the world, you must move the "pseudoPlayer" instead.
I have simple gameplay that functions like this:
My goal is to detect when the ball hits the white part (game over scene would load) or when the ball hits the gray part (game continues)
Because the shapes shrink and have a lot of stuff going on, I figured it would be a lot more efficient to just create the SKPhysicsBody when I need it, and then remove it after the collision checks happen. So when I click the screen this happens: (after 5*0.0325 seconds--how long it takes the circle to shrink--has passed, it will add the physicsbody to both the gray and white part so I can detect which the ball is touching)
DispatchQueue.main.asyncAfter(deadline: .now() + seconds, execute: {
let trackPhysics = SKPhysicsBody(texture: track.texture!, size: track.texture!.size())
trackPhysics.isDynamic = false
trackPhysics.affectedByGravity = false
track.physicsBody = trackPhysics
let goalPhysics = SKPhysicsBody(texture: goal.texture!, size: goal.texture!.size())
goalPhysics.isDynamic = false
goalPhysics.affectedByGravity = false
goal.physicsBody = goalPhysics
})
Which works. The SKPhysicsBody are applied to both the gray and white part perfectly without any loss in framerate and follow the rotation. The problem is... how do I detect which is the ball touching? Since they did not formally collide, it will not call the collision at all (which makes sense since they did not really collide
This is the basic logic, and it works perfectly until I want to check the results:
someone touches the screen
circles shrink
right after they shrink it will create an SKPhysicsBody for both the gray and white part based on its texture
Problem is here... how do I detect which the ball is touching?
I have tried this:
func didBegin(_ contact: SKPhysicsContact) {
print("touching!")
}
func didEnd(_ contact: SKPhysicsContact) {
print("not touching")
}
which has no messages at all, and have tried using allContactedBodies() 1 second after the SKPhysicsBody were applied, but returns a count with 0 for all of them. I have even tried making this 5 seconds and still not working
DispatchQueue.main.asyncAfter(deadline: .now() + seconds + 1, execute: {
print(track.physicsBody!.allContactedBodies().count)
print(goal.physicsBody!.allContactedBodies().count)
})
This is what it looks like with the SKPhysicsBody applied, you can see the physics is applied perfectly
Am I doing something wrong? Any ideas?
The solution (as discovered in the comments above), was:
Set the isDynamic property of nodes to true for collision detection to work properly.
Ensure that didBegin(contact:) is being called, by double-checking that the scene's physicsWorld.contactDelegate is set.
Ensure the masks and physics bodies are set at the proper times and locations.
For a game I'm creating, an SKSpriteNode gradually makes its way down the user's screen. There's another SKSpriteNode (position is static) near the bottom of the screen, leaving only a little bit of space for the original SKSpriteNode to fit in. I need to detect when the first SKSpriteNode is COMPLETELY below the second SKSpriteNode, which I'm having a bit of trouble with. Here's the code I'm currently using:
if (pos.y > barPosY) //pos.y = 1st SKSpriteNode, barPosY = 2nd SKSpriteNode
{
touchedTooEarly = true
}
For some reason, when the first SKSpriteNode goes just a little bit over the 2nd SKSpriteNode (not all the way), it still detects it as being completely over. Is there a coordinate space issue I'm missing?
The logic
A sprite a covers a sprite b if
b.frame is inside a.frame
b.zPosition is below a.zPosition
The extension
Now let's build an extension
extension SKSpriteNode {
func isCoveredBy(otherSprite: SKSpriteNode) -> Bool {
let otherFrame = CGRect(
origin: convertPoint(otherSprite.position, fromNode: otherSprite),
size: otherSprite.frame.size
)
return zPosition < otherSprite.zPosition && CGRectContainsRect(frame, otherFrame)
}
}
Problem #1: transparency
This mechanism totally ignores transparency.
Problem #2: same sprite
If you compare 2 sprites of the same type the function CGRectContainsRect will return true only when they are exactly aligned. Althoug you can solve this problem creating a smaller rectangle when you compare the sprites.
I have two SkSpriteNodes called left-ball and right-ball.THey flow from sides of the screen.
I implemented the collision detection for these two balls. However I noticed that before I add collision detection, the balls were moving smoothly without any shaking effect, but after I add the collision detection, the balls were shaking and moving in their path.
I am using the same bit category for both of these balls:
static const int ballHitCategory = 1;
without shaking:
with shaking:
Here is what I tried in code:
_leftBall.physicsBody=[SKPhysicsBody bodyWithRectangleOfSize:_leftBall.frame.size];
_leftBall.physicsBody.categoryBitMask=ballHitCategory;
_leftBall.physicsBody.contactTestBitMask=ballHitCategory;
_leftBall.physicsBody.collisionBitMask=ballHitCategory;
_leftBall.physicsBody.dynamic=YES;
_leftBall.physicsBody.usesPreciseCollisionDetection=YES;
_rightBall.physicsBody=[SKPhysicsBody bodyWithRectangleOfSize:_rightBall.frame.size];
_rightBall.physicsBody.categoryBitMask=ballHitCategory;
_rightBall.physicsBody.contactTestBitMask=ballHitCategory;
_rightBall.physicsBody.collisionBitMask=ballHitCategory;
_rightBall.physicsBody.dynamic=YES;
_rightBall.physicsBody.usesPreciseCollisionDetection=YES;
-(void)didBeginContact:(SKPhysicsContact *)contact
{
_leftBall = (SKSpriteNode*)contact.bodyA.node;
_rightBall = (SKSpriteNode *)contact.bodyB.node;
if(_leftBall.physicsBody .categoryBitMask == ballHitCategory || _rightBall.physicsBody.categoryBitMask == ballHitCategory)
{
NSLog(#" hit the ");
//setup your methods and other things here
[_leftBall removeFromParent];
[_rightBall removeFromParent];
}
}
Please note that shake happens only after I add the collision detection, If I remove the above code, everything is fine.
What is the proper way to add collision detection without effecting the skspritenode ?
so youre moving the sprites using non physics code.. but theyre still part of the physics simulation. I'm guessing theyre fighting against the force of gravity.
either set affectedByGravity on each sprite to false
or you can turn off gravity in your game completely
[self.physicsWorld setGravity:CGVectorMake(0, 0)];
Don't worry about the Gravity. You should set your spritenode:
_leftBall.physicsBody.dynamic=YES;
_leftBall.physicsBody.affectedByGravity=NO;
Same on _rightBall
So in my game a have this paddle and a ball, and i want to calculate which side of the paddle that the ball hits, i found this answer in C# which shows drawing two lines like this:
So what i want to do is draw two hypothetical lines(meaning invisible to the user) so i can calculate if the ball is greater then both the lines or if its in contact with one of the sides of the paddle.
How do i draw these lines in sprite kit and swift?
UPDATE
I found the original answer, its HERE
You can check the position of the ball in relation to the paddle.
If(ballNode.position.x > paddleNode.position.x) {
// ball is on the right of the paddle
} else {
// ball is on the left of the paddle
}
If(ballNode.position.y > paddleNode.position.y) {
// ball is above the paddle
} else {
// ball is below the paddle
}