Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a specific object defy gravity? (SpriteKit / Objective-C)

I'm working on a piece of code, and I want there to be gravity in the view but I want specific projectiles to defy gravity and fly across the screen. I know to get rid of the gravity on the whole view its just:

self.physicsWorld.gravity = CGVectorMake(0, 0);

But as stated I want gravity on the scene.

So I'm wondering if there is a way to take the gravity off one specific item? (i.e. the SKSpriteNode _debris item in my case)

like image 991
Liam Stockhomme Avatar asked Dec 19 '22 16:12

Liam Stockhomme


2 Answers

By setting the physicsBody to not be affected by gravity. E.g.

myNoGravityObject.physicsBody.affectedByGravity = NO;

See the documentation of SKPhysicsBody.

like image 85
Groot Avatar answered Dec 22 '22 07:12

Groot


If you don't want a node to interact with physics calculations at all, but still want it to have a physicsBody (i.e. to make it begin to interact with things later on, or to check for collisions with other nodes), you can set

node.physicsBody.dynamic = NO;

This will cause the node to ignore gravity, as well as collisions, impulses, and the like. If you are setting up the contact delegate, note that at least one node in any given contact must be dynamic for the contact delegate to be notified of the event.

like image 34
webbcode Avatar answered Dec 22 '22 07:12

webbcode