Sprite Kit Collision without bouncing
Asked Answered
D

4

6

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

Declaim answered 4/4, 2014 at 12:46 Comment(2)
Really, the answer is just to set restitution to zero. That is SpriteKit's term for "bounciness". However, you must set both.Ethbin
Thanks for bringing this up again, jboi's answer should is the way to go.Declaim
A
8

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

Apodosis answered 28/11, 2014 at 12:47 Comment(0)
I
3

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>);
    }
}
Interknit answered 4/4, 2014 at 12:58 Comment(0)
M
2

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

_hero.physicsbody.restitution = 0;

It controls the bounciness...

Mudstone answered 4/4, 2014 at 13:34 Comment(0)
S
0

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

    }
Slade answered 14/9, 2015 at 22:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.