Aidan Kaiser
Aidan Kaiser

Reputation: 521

Locking Physics Body to X Axis Spritekit Swift

I have a node that I would like to lock to the x axis. I have an impulse on the node (shown below).

    rectangle.physicsBody!.applyImpulse(CGVectorMake(-11,0))

The node starts off on the x axis, but as soon as it makes contact with another physics body, it flies off on the y axis as well as the x. I would like to know how to lock it to the x axis so it cannot move up or down. Any help would be appreciated.

Upvotes: 0

Views: 745

Answers (1)

some_id
some_id

Reputation: 29886

You could lock the movement in the update function for the node if you create a SKNode/SKSpriteNode etc. subclass.

This way you always lock the movement to the axis you want. You wouldnt lock the physics body, but the node itself.

Update

You will need to store the point of the node as the collision occurs, using the collision delegates.

e.g. collisionPoint = node.center

You can lock its position to an axis like this

func update(dt: NSTimeInterval)
{
    // locks the position to collision points y coordintate
    self.position = CGPointMake(self.position.x, collisionPoint.y) 
}

call this update function from the scene's

override func update(currentTime: NSTimeInterval)

Upvotes: 1

Related Questions