Can you customize the physics engine using SCNPhysicsBody.velocity?

Hello, developer community!


I recently decided to do some app development, and I was wondering a few things about SceneKit's default physics engine.


I want to make a game that involves a little bit of physics puzzler to get a ball to a goal - something simple just to get me started. I have coding experience, just not with Swift, so this is a sort of "first project" for me.


Anyway, I wanted gain more control over the physics, so that instead of using trial and error with the defaul physics from SceneKit, I could design the levels around something that I can predict with pretty near precision. Not to mention I also wanted to incorperate a few obstacles that are kind of narrow, and I wanted to make sure the ball wouldn't fall off of them due to the default physics engine not behaving as intended.


So, would directly modifying the SCNPhysicsBody.Velocity be a viable way to get the effect that I'm looking for? I wanted to ask around just to see if anyone's had experience with this before I potentially spend hours doing something that people think would be inefficient or won't work exactly as I think it does.


I was also wondering about the properties of objects when using the default physics - I applied a texture to the ball while testing the waters a bit, and when I apply some force to make it roll around, it appears to glide across the solid surface I placed it on, rather than rolling across it for some reason... like sliding something across ice. I mean it does rotate a little, but not in the way you'd expect to see if you were to, say, roll a baseball down a hill or something.

Replies

You can certainly change the velocity whenever you want, but it's not clear what that helps you do. The physics engine is basically iterative, so if you set a velocity and let the physics body move for some number of frames, you "lose control" of the object for those frames. OTOH, if you change the velocity every frame, to make sure it's what you expect, then you're not really using the physics engine at all (at least as far as velocity is concerned).


Regarding a rolling ball, the effect you want is a combination of [linear] velocity and angularVelocity. In the physics engine, linear velocity will produce angularVelocity according to the physical properties of the object and surface (e.g. friction, resilience, etc), so getting this to work naturally involves a careful reading of the documentation to make sure you're setting the correct properties to enable the behavior.

SCNPhysicsBody.velocity is an instance property used to customize a physics body - I wouldn't consider it a lever that can be pulled to customize the physics engine.


After you've assigned the physics body, the physics engine simulates the physics for the app.

Thanks for all of the information. I wasn't sure how modifying velocity directly would affect everything... It would appear that I can stop modifying velocity, and SceneKit's phsyics would take over again... interesting.

If you're new to all this, don't overlook SCNActions as another way of moving something, specifically SCNAction.move . If you push things around with physics impulses, control gets pretty unpredictable real quick. So if you're making something that requires precise control, I would probably lean toward SCNActions for control.


For example, lets say you're making a ball jump. You can still give it a physics body for collision detection purposes, but to move the ball around, you could just make an action sequence. Go up 50 on the Y, then go down 50 on the Y.