Search code examples
iosobjective-cscenekitscnnodeskphysicsworld

I programmed a cube to slide across the floor in scenekit for iOS, it sinks into the floor though


I currently have a scene where a cube is dropped onto a floor in scenekit. I animated the cube to move on the x axis across the floor, when it does though it sinks into the floor a bit and then repositions to be on top of the floor.

Here's just some of the code:

// create floor
SCNFloor *mainFloor = [SCNFloor floor];
SCNNode *floorNode = [SCNNode nodeWithGeometry:mainFloor];
floorNode.position = SCNVector3Make(0.0, -4.0, 0.0);
[myScene.rootNode addChildNode:floorNode];
SCNPhysicsShape *ground = [SCNPhysicsShape shapeWithGeometry:mainFloor options:nil];
SCNPhysicsBodyType kin = SCNPhysicsBodyTypeKinematic;
SCNPhysicsBody *solid = [SCNPhysicsBody bodyWithType:kin shape:ground];
floorNode.physicsBody = solid;
myScene.physicsWorld.gravity = SCNVector3Make(0.0, -9.8, 0.0);

Can someone help me find out why the cube sinks? If you can't figure out why, maybe you could provide example code of a working floor that a character can walk on? (in objective c hopefully, but i can translate from swift if need be)


Solution

  • Using ProBlaster's advice, I used [SCNPhysicsBody applyForce] instead and that solved the problem.