SceneKit collision occasionally fails
Asked Answered
C

2

9

I'm trying to simulate a soccer game. I have a SCNPlane that simulates the court. I have imported a Soccer goal 3d model (.dae file) and also a ball model (.dae).

My ball has a dynamic physics body, the plane static and the goal is kinematic. I have set the categoryBitMask and contactTestBitMask for each one of the SCNNodes.

When I shoot the ball against the goal then sometimes the ball bounces and behaves as expected, but some other times the ball goes through the goal net and crosses it.

I have also assigned the SCNPhysicsContactDelegate and the didBeginContact is triggered when the ball bounces agains the goal but when the ball crosses it then the method is not called.

Do you know what might be happening?

Thank you!

Covington answered 27/10, 2017 at 19:57 Comment(0)
C
1

Might be an issue with the ball moving too fast for the physics engine to calculate correctly. Try changing the "timeStep" value:

SceneKit processes the physics simulation and updates the state of all physics bodies once per the time interval specified by this property. The default value is 1/60 second (a rate of 60 Hz). A faster simulation rate provides more accuracy in simulation results—such as collisions between fast-moving objects—but at a higher cost in CPU time (which may in turn slow down your app’s rendering frame rate). Typically, you should set this property to match your target rendering frame rate (as defined by the preferredFramesPerSecond property of the SCNView object rendering your scene). https://developer.apple.com/documentation/scenekit/scnphysicsworld/1512881-timestep

Clements answered 5/9, 2018 at 13:18 Comment(0)
T
0

Instance property categoryBitMask defines which categories a physics body belongs to, and contactTestBitMask defines which categories of bodies cause intersection notifications with this physics body.

You need a collisionBitMask instance property that defines which categories of physics bodies can collide with this physics body.

var collisionBitMask: Int { get set }

When two physics bodies contact each other, a collision may occur. SceneKit compares the body’s collision mask to the other body’s category mask by performing a bitwise AND operation. If the result is a nonzero value, then the body is affected by the collision. Each body independently chooses whether it wants to be affected by the other body. For example, you might choose to avoid collision calculations that would make negligible changes to a body’s velocity. The default value is all (a bit mask whose every bit is enabled), specifying that the body will collide with bodies of all other categories.

static var all: SCNPhysicsCollisionCategory { get }

all is default Type Property for a physics body’s collisionBitMask property.

Trejo answered 5/9, 2018 at 11:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.