Jav Solo
Jav Solo

Reputation: 736

Determine whether player character is at rest on static node

TL;DR I would like to know how to determine whether my sprite character is at rest/still to determine if they can jump or not. The player is a 2D sprite with a physicsBody, but the isResting property is false in the majority of calls to update(_ currentTime: TimeInterval) and I cannot figure out why.

Here is what I have tried:

SKPhysicsContactDelegate doesn't seem to work because it appears that it is for two dynamic objects. I am trying to determine whether a the player (dynamic) is at rest on some dynamic sprite (platform, ground, etc.)

I would think this would be possible with simply reading the isResting property, but it is only true ~1/30 called to the frame update.

Below is my code for setting up these physics bodies

let groundNode = Ground(size: groundSize) // Ground is just wrapper class for loading a sprite
groundNode.position.y = (-size.height/2) + groundSize.height * 8
groundNode.physicsBody = SKPhysicsBody(rectangleOf: groundSize)
groundNode.physicsBody?.collisionBitMask = 0b0001
groundNode.physicsBody?.affectedByGravity = false
groundNode.physicsBody?.isDynamic = false

playerNode = Player(name: "Player") // Player is just a wrapper class for loading a sprite
playerNode.physicsBody = SKPhysicsBody(texture: texture, size: self.size)
playerNode.physicsBody?.collisionBitMask = 0b0001
playerNode.physicsBody?.allowsRotation = false
playerNode.physicsBody?.affectedByGravity = true
playerNode.physicsBody?.friction = 0.8

Outside of the SKPhysicsBody setup, I animate the sprite continuously. Even stopping and preventing the animation doesn't appear to affect the state of isResting and the velocity property has a very small dy (2.50663077849822e-07) sometimes

How can I determine whether this dynamic sprite is at rest on the static sprite? My area of interest is shown in the screenshot

enter image description here

Upvotes: 0

Views: 57

Answers (0)

Related Questions