Scenekit Won't Scale a Dynamic Body

SceneKit won't scale a dynamic body

You wouldn't just need a "scaling velocity" to interact with other objects as one changes size, you'd need a "scaling force". How strongly should it push on things that it collides with while growing? And what happens if it's in a situation that depends on its mass, like balancing on a seesaw?

Game engines are already loose approximations of real-world physics, so asking them to figure out Ant-Man physics on their own is a bit of a stretch. If they could do that, they could probably also start building killer robots, and that'd kinda ruin your day. :)

Anyway, depending on how you want your expanding sphere to affect things, you have a few options. One is to delete and re-create the physics body at intervals:

let duration = 5.0
node.runAction(.customActionWithDuration(duration, actionBlock: { node, progress in
let scale = 1.0 + progress / CGFloat(duration)
node.physicsBody = nil
node.scale = SCNVector3(x: scale, y: scale, z: scale)
node.physicsBody = .dynamicBody()
}))

This does it every frame, which could be costly — you might want to throw some gating on progress in there so it happens less often. (And depending on what other effects need to happen as a result of the sphere changing size, you could set things like mass when you re-create the physics body.)

Another option might be to look at SCNPhysicsField. Use a radial gravity field to make a region that shoves everything out of its area of influence, then animate its parameters to change size and strength over time.

when dynamic body falls down, its `_ball.position.y` value doesn't change

the position of its presentationNode will change. The model values are left untouched by the physics engine.

SceneKit - Movement + Gravity = Strange Movement

Dynamic physics bodies should only be moved using physics, e.g.applyForce(_:at:asImpulse:) You may be able to get it to work by calling resetTransform() after updating the position, but at a cost to performance.

https://developer.apple.com/documentation/scenekit/scnphysicsbody/1514782-resettransform

PhysicsBody stays after translation and rotation

From the documentation for SCNPhysicsBodyTypeStatic we learn that it is

A physics body that is unaffected by forces or collisions and cannot move

However you can use resetTransform to explicitly tell the engine that the node has moved and that the physics body needs to be updated:

If you change the position or orientation of a node with an attached
static or dynamic physics body, call this method afterward to ensure
that the physics simulation incorporates the change. You need not call
this method for kinematic bodies.

Note that dynamic and physics bodies
are designed to be moved only by the physics simulation or not at all.
You may use this method to move them regardless of this restriction,
but at a cost to performance.

SceneKit – Making a custom physics body

SceneKit physics bodies model solid shapes. If you're trying to model an open space enclosed by boundaries — like a room or hallway — a single physics body won't help you. That'd fill in the volume of the room with an impassible area, and other physics bodies (with overlapping collision masks) would be forced out of that area.

If you want to make an open space enclosed by boundaries, you need to create physics bodies for the boundaries. The SceneKitVehicle sample code illustrates doing this, creating separate physics bodies for the floor and walls of a room using SCNFloor and SCNBox geometries for each.



Related Topics



Leave a reply



Submit