You can stop any inertia by using ship.physicsBody.velocity = CGVectorMake(0,0);
Sprite Kit: Prevent SKSpriteNode From Gliding Around
-
14-07-2023 - |
Question
I'm working on a spaceship game for OS X using SpriteKit. This is how I setup physicsBody
for my spaceship node.
// SKSpriteNode
ship.physicsBody = [SKPhysicsBody bodyWithPolygonFromPath:triangle];
ship.physicsBody.categoryBitMask = playerCategory;
ship.physicsBody.contactTestBitMask = enemyCategory | edgeCategory;
ship.physicsBody.collisionBitMask = enemyCategory | edgeCategory;
ship.physicsBody.allowsRotation = NO;
ship.physicsBody.dynamic = YES;
ship.physicsBody.linearDamping = 0;
ship.physicsBody.angularDamping = 0.1;
ship.physicsBody.mass = 0.1;
ship.physicsBody.restitution = 0;
This is how I setup physicsWorld
and physicsBody
in my scene file.
// SKScene
self.physicsWorld.contactDelegate = self;
self.physicsWorld.gravity = CGVectorMake(0, 0);
self.physicsBody = [SKPhysicsBody bodyWithEdgeLoopFromRect:self.frame];
self.physicsBody.categoryBitMask = edgeCategory;
self.physicsBody.contactTestBitMask = missileCategory | enemyCategory | playerCategory;
self.physicsBody.collisionBitMask = playerCategory;
self.physicsBody.restitution = 0;
In my scene's update
method, I call [ship accelerate:@"up"];
, which ends up calling this.
[self.physicsBody applyForce:CGVectorMake(self.engineThrust * cosf(self.shipOrientation),
self.engineThrust * sinf(self.shipOrientation))];
Is there a way to disable the ship's inertia? I don't want my ship gliding along my scene. I want the ship to stop moving as soon as I release the key. I also want the ship to immediately change direction when toggling between the up arrow and the down arrow.
Is there something in physicsBody
that I can change or will I have to implement this?
Solution
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow