5
votes

I setup a hero and some platforms that are moving from the top downwards. With these I have collisionBitMasks that detect when the hero lands on a platform if the hero comes from above (to let the hero jump through the platforms)

if (_hero.physicsBody.velocity.dy > 0) {
                            _hero.physicsBody.collisionBitMask = 0;
                        }
                        else {_hero.physicsBody.collisionBitMask = platformCategory;
                                }

Everything works fine, except that the hero keeps bouncing on the platform. Is there a way to let him sit on it, while the platform is moving down?

I tried using physicsBody.resting and physicsBody.friction, but without any success.

Thanks for help Guys

4
Really, the answer is just to set restitution to zero. That is SpriteKit's term for "bounciness". However, you must set both. - Fattie
Thanks for bringing this up again, jboi's answer should is the way to go. - Elio_

4 Answers

7
votes

Had the same issue just a minute ago. It works with setting the restitution but you need to set both restitutions. The one of the hero AND the one of the platform (or in my case the scene boundaries).

so:

hero.physicsBody.collisionBitMask = platformCategory
hero.physicsBody.restitution = 0.0
platform.physicsBody.restitution = 0.0
any_other_object_that_should_still_bounce.physicsBody.restitution = 1.0

will do it. All other objects on the screen still bounce on the platform as long as you set their restitution > 0

3
votes

The trick is to avoid any physics behavior while the hero is on the platform by resetting the hero body's velocity and setting the hero's position to a fixed vertical offset from the platform's position.

In semi-pseudo-code:

-(void) didSimulatePhysics
{
    if (<hero on platform>)
    {
        hero.physicsBody.velocity = CGPointZero;
        hero.position = CGPointMake(hero.position.x, 
                                    platform.position.y + <offset as needed>);
    }
}
2
votes

Have you tried altering the restitution property of the nodes' physics bodies?

_hero.physicsbody.restitution = 0;

It controls the bounciness...

0
votes

You can use the Collision category:

When the velocity.dy is < 0 the hero is falling.

func collisionWithPlayer(player: SKNode) {
    // 1
    if player.physicsBody?.velocity.dy < 0 && (player.position.y - player.frame.size.height / 2.0) >= (self.position.y){
        // 2
        player.physicsBody?.collisionBitMask = CollisionCategoryBitMask.Platform

    }