I'm doing a physical simulation with ARkit+SceneKit.
We want to achieve the behavior of releasing the ball between the walls and repeating the reflection indefinitely while moving it in a straight line at equal speed.
We applied force to the ball and designated the restoration of the wall and the ball as 1.0, but it slows down every time it reflects.
Also, if you set it to a higher number (2.1 or higher), it will diverge and continue to accelerate.
What should I do to reflect it while moving it in a straight line at constant speed?
//Function to fire a ball
func shotBall (scene:SCNScene) {
letballGeo=SCNSsphere (radius: 0.05)
// physics setting
letphysicsShape = SCNPhysicsShape (geometry:ballGeo, options:nil)
letballBody = SCNPhysicsBody (type:.dynamic, shape:physicsShape)
// Set Reflectivity
ballBody.restriction=1.0
// colour
let material = SCNMaterial()
material.diffuse.content=UIColor.purple
// Node Configuration
let ballNode = SCNNode (geometry:ballGeo)
ballNode.physicsBody=SCNPhysicsBody.dynamic()
ballNode.physicsBody?isAffectedByGravity=true
ballNode.geometry?.materials=[material]
ballNode.position = SCNVector3(0,0,0)
screen.rootNode.addChildNode(ballNode)
// fire a ball with force
ballNode.physicsBody?.applyForce(SCNVector3(0,0,-4), asImpulse:true)
}
// wall-building function
func generateWallNode (position: SCNVector3, height: CGFloat, width: CGFloat, color: UIColor) - > SCNNode {
let wall = SCNBox (width: width, height: height, length: 0.1, chamferRadius:0)
// Set physical information
letwallShape = SCNPhysicsShape (geometry:wall, options:nil)
letwallBody = SCNPhysicsBody (type:.static, shape:wallShape)
wallBody.restriction=1.0
// Create Node
letwallNode=SCNNode(geometry:wall)
wallNode.physicsBody = wallBody
wallNode.position=position
return wallNode
}
I haven't dealt with SceneKit very much, so I'd like to ask you to try damping
and friction
as they also affect your behavior in the event of a collision.
ballBody.damping=0
ballBody.friction = 0
(The above code example is for ballBody
only, but it must also be set for all other SCNPhysicsBody
.)
Self-resolved.
The reason is a simple mistake.
wallNode.physicsBody=wallBody
The reason was that you did not set the physical information on the node.
© 2024 OneMinuteCode. All rights reserved.