Tutorial To Learn ARKit: Part II
Posted By : Aditya Kumar Sharma | 06-Mar-2018
If you are new to this tutorial of ARKit, please start from Part 1.
Step 6: Setting up Control Buttons
We have seen the application is running and the object is been displayed on the screen but it’s motionless. So now we will add controls to it.
We will be having two set of controls, one will be responsible for moving up and down, left and right. Another set will be responsible for rotating and moving forward and backwards. To set controls, we will be using
- moveBy(x:y:z:duration:)
By this method is used for creating an action to move object relative to its current position.
The parameters are:
- x: distance to move in the x-direction.
- y: distance to move in the y-direction.
- z: distance to move in the z-direction.
- duration: duration of animation(in seconds).
- rotateTo(x:y:z:duration:)
This method is responsible for creating an action which rotates the node with principal angles.
The parameters are:
- x: rotating the node counterclockwise around x-axis(in radians).
- y: rotating the node counterclockwise around y-axis(in radians).
- z: rotating the node counterclockwise around the z-axis(in radians).
- duration: duration of animation(in seconds).
Let set the control of buttons. We will be using Long Press Gesture Recogniser to control movements. This allows a user to move the object as long as the button is been pressed.
Set Control button of up, down, left and right by placing the images and then add long press gesture recogniser to it.
Note: Change the minimum duration of long press to 0. So that it starts moving as user press the control buttons.
Now let’s create an action for left, right, up and down button and assign them some actions.
So please paste these declarations in begin with main class:
let kMovingLengthPerLoop: CGFloat = 0.5
let kRotationRadianPerLoop: CGFloat = 0.2
Move object up:
@IBAction func upLongPressed(_ sender: UILongPressGestureRecognizer) {
let action = SCNAction.moveBy(x: 0, y: kMovingLengthPerLoop, z: 0, duration: kAnimationDurationMoving)
execute(action: action, sender: sender)
}
Move object down:
@IBAction func downLongPressed(_ sender: UILongPressGestureRecognizer) {
let action = SCNAction.moveBy(x: 0, y: -kMovingLengthPerLoop, z: 0, duration: kAnimationDurationMoving)
execute(action: action, sender: sender)
}
Move object left:
@IBAction func moveLeftLongPressed(_ sender: UILongPressGestureRecognizer) {
let x = -deltas().cos
let z = deltas().sin
moveObject(x: x, z: z, sender: sender)
}
Move object right:
@IBAction func moveRightLongPressed(_ sender: UILongPressGestureRecognizer) {
let x = deltas().cos
let z = -deltas().sin
moveObject(x: x, z: z, sender: sender)
}
In additional we need to couple more methods:
private func execute(action: SCNAction, sender: UILongPressGestureRecognizer) {
let loopAction = SCNAction.repeatForever(action)
if sender.state == .began {
object.runAction(loopAction)
} else if sender.state == .ended {
object.removeAllActions()
}
}
private func deltas() -> (sin: CGFloat, cos: CGFloat) {
return (sin: kMovingLengthPerLoop * CGFloat(sin(object.eulerAngles.y)), cos: kMovingLengthPerLoop * CGFloat(cos(object.eulerAngles.y)))
}
private func moveObject(x: CGFloat, z: CGFloat, sender: UILongPressGestureRecognizer) {
let action = SCNAction.moveBy(x: x, y: 0, z: z, duration: kAnimationDurationMoving)
execute(action: action, sender: sender)
}
execute(action:sender:) is the method which is used for controlling the action and keep on moving the object by repeating the process until control button is pressed. deltas() gets its current rotation angle and calculates its coordinate accordingly for correct movement. moveObject(x:z:sender:) is used for configuring the object and executing the correct movement of it.
Now let set remaining four control buttons which would be responsible for rotating the object.
Rotate object left:
@IBAction func rotateLeftLongPressed(_ sender: UILongPressGestureRecognizer) {
rotateObject(yRadian: kRotationRadianPerLoop, sender: sender)
}
Rotate object right:
@IBAction func rotateRightLongPressed(_ sender: UILongPressGestureRecognizer) {
rotateObject(yRadian: -kRotationRadianPerLoop, sender: sender)
}
Move object forward:
@IBAction func moveForwardLongPressed(_ sender: UILongPressGestureRecognizer) {
let x = -deltas().sin
let z = -deltas().cos
moveObject(x: x, z: z, sender: sender)
}
Move object backward:
@IBAction func moveBackLongPressed(_ sender: UILongPressGestureRecognizer) {
let x = deltas().sin
let z = deltas().cos
moveObject(x: x, z: z, sender: sender)
}
Additional method required for determining rotation angle and executing the rotation:
private func rotateObject(yRadian: CGFloat, sender: UILongPressGestureRecognizer) {
let action = SCNAction.rotateBy(x: 0, y: yRadian, z: 0, duration: kAnimationDurationMoving)
execute(action: action, sender: sender)
}
Now we would test our Augmented reality app and will try to control with buttons.
Watch the video demo here.
Thanks
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Aditya Kumar Sharma
Aditya is a bright iOS developer, have knowledge of objective C, swift, swift 3, JSON, Core data and iPhone development. Apart from that he loves to travel and explore new things.