There are several options for animating a 3D model with SceneKit, but if you want to animate the arms and legs of a 3D model at will, the options will be SCNIKConstraint or SCN Animation.
This article is detailed about the differences between them.
How to perform animation in SceneKit and how to use it properly
SCN Animation can be freely operated if it is defined in advance, but SCNIK Constraint is suitable when you want to interactively operate the speed and movement amount of animation.
So, I will explain how to animate the robot's arm freely with SCNIK Constraint.
The IK of SCNIKConstraint is called Inverse kinematics.
I will quote this article because it was easy to understand. Inverse Kinematics Implementation Note
IK is an action that makes the connected thing go to a certain point so that the body moves in conjunction with the hand, wrist, arm, and shoulder like when a human reaches for a certain place. say.
SCNIKConstraint is a mechanism that allows you to apply this IK to SceneKit nodes.
reference: Apple Official: SCNIK Constraint
This time, we will create an animation in which the left arm of the robot moves according to the amount of drag.
** Prepare a 1.3D model **
The 3D model must have bone settings. This time we will use the existing model.
A 3D model of the robot (filename: robot.usdz) is included in Apple's ARKit sample project, so use this.
In this article, there was an example of making with a modeling tool.
[SceneKit] How to use SCNIK Constraint
** 2. Create a Game project from Xcode project creation **
** 3. Drag and drop the usdz file into your project **
See this article for how to load a 3D model in usdz format into SceneKit.
Load 3D model in usdz format into SceneKit and animate it
** 4. Detect when the screen is dragged **
Register UIPanGestureRecognizer with viewDidLoad.
GameViewController.swift
let tapGesture = UIPanGestureRecognizer(target: self, action: #selector(handleDrag(_:)))
scnView.addGestureRecognizer(tapGesture)
** 5. Determine the two joints that will be the starting point and the ending point **
While checking the bone information of the model with Xcode, determine the two joints that will be the starting point and the ending point (the part that actually moves). This time, I moved the left hand as the starting point and the left hand as the ending point.
If you don't choose the two joints properly, they will either not move or behave strangely.
For example, if I chose two points, the waist and the left hand, it would look like John Travolta of Saturday Night Fever.
** 6. Make the robot's arm move when dragged **
Obtains the amount of drag movement and determines the destination of the robot's arm based on it.
GameViewController.swift
@objc
func handleDrag(_ gestureRecognize: UIPanGestureRecognizer) {
//Get the starting point of the animation (here, the shoulder of the left arm)
guard let root = scene.rootNode.childNode(withName: "left_shoulder_1_joint", recursively: true) else { return }
//Get the place to actually move (here, the left arm hand)
guard let target = scene.rootNode.childNode(withName: "left_hand_joint", recursively: true) else { return }
//Specify the starting point and the location to move to SCNIKConstraint
let ik:SCNIKConstraint = SCNIKConstraint.inverseKinematicsConstraint(chainRootNode: root)
target.constraints = [ik]
//Determine the amount of movement of the robot's arm from the amount of movement of the drag
let move = gestureRecognize.translation(in: view)
let targetPosition = SCNVector3(target.position.x + Float(move.x * 0.1), target.position.y + Float(move.y * 0.1 * -1), target.position.z)
//Move the arm
ik.targetPosition = targetPosition
}
We provide information on iOS application development. Please follow us on Twitter as well. https://twitter.com/jugemjugemjugem
Recommended Posts