Reputation: 19
I'm currently making a football (soccer) game using SpriteKit, but I've run into a little problem. When the characters (players) jump, I don't want them to bounce when they hit the ground after (that looks really weird and isn't very realistic).
Fine! Just set the restitution of the ground to zero!
But not so fast ... that means the ball won't bounce either.
Here are the important parts of my code:
struct cType {
static let p1:UInt32 = 0
static let ball:UInt32 = 1
static let ground:UInt32 = 2
}
Moving on, in the didMove(to view: SKView)
i define some properties of my objects:
ball.physicsBody?.categoryBitMask = cType.ball
ball.physicsBody?.collisionBitMask = cType.p1 | cType.p2 | cType.ground
ball.physicsBody?.contactTestBitMask = cType.p1 | cType.p2 | cType.ground
p1.physicsBody?.categoryBitMask = cType.p1
p1.physicsBody?.collisionBitMask = cType.p2 | cType.ball | cType.ground
p1.physicsBody?.contactTestBitMask = cType.p2 | cType.ball | cType.ground
As for the collision detection itself, I just use the didMove(_ contact: SKPhysicsContact)
and it works perfectly.
Now, of course I have a player 2 (p2
), but that sprite has the exact same code as player 1 (p1
) except cType.p2
is changed to cType.p1
and vice versa.
I also have some code ground.physicsBody?.restitution = 0.5
but this of course sets the restitution to 0.5 for any sprite who collides with the ground. This is where I'm really unsure. So my question is: How can I make sure the ball bounces when it hits the ground (with restitution 0.5), but my players (p1, p2) don't?
Upvotes: 0
Views: 55