2
votes

I am trying to make a simple maze game in Scenekit. I have a sphere node and wall nodes. The sphere moves by SCNActions. The actions I use on the sphere are moveByX actions. My problem is that whenever the sphere hits a wall, it just moves the wall back with it. My sphere is kinematic and my walls are dynamic. I don't have any forces in the scene, only my moveByX actions. How can I make it so my sphere bounces off of the walls?

I am using Swift.

3

3 Answers

1
votes

Change walls to .static and sphere to .dynamic

0
votes

One way you could do it would be to have variables for xSpeed and ySpeed, and whenever you collide with a wall make the corresponding variable negative. This is a pretty basic approach and doesn't take diagonal walls into account.

For example:

var xSpeed = 5.0 var ySpeed = 5.0

if collision with wall in x direction { xSpeed = -xSpeed }

if collision with wall in y direction { ySpeed = -ySpeed }

It might be a bit tricky to detect the collisions, you might have to look up some tutorials on SpriteKit collisions.

0
votes

For Swift3

    let border  = SKPhysicsBody(edgeLoopFrom: self.frame)

    border.friction = 0
    border.restitution = 1

    self.physicsBody = border