SCNLookAtConstraint causing zoom past 3D model - swift3

UPDATE:
After further testing and evaluation it seems that the SCNLookAtConstraint is causing the issue. Basically because it removes the 2 finger Pan Gesture it is causing some weird behavior with the pinch and scroll gesture. Does anyone have a good idea how to fix this?
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
let quaternion = sceneView.pointOfView?.orientation
let position = sceneView?.pointOfView?.position
print("Orientation: ((quaternion?.x),(quaternion?.y),(quaternion?.z),(quaternion?.w)) Position: ((position?.x),(position?.y),(position?.z)")
}
but this just prints out the same values now matter the 2 finger zoom pressed.
func setupScene() {
let scene = SCNScene()
self.sceneView.scene = scene
let camera = SCNCamera()
let cameraNode = SCNNode()
cameraNode.camera = camera
cameraNode.position = SCNVector3(x: -1.0, y: 0.0, z: -1.0)
cameraNode.boundingBox.max = SCNVector3(x: 2, y: 2, z: 2)
cameraNode.boundingBox.min = SCNVector3(x: 0.1, y: 0.1, z: 0.1)
let centerNode = SCNNode()
centerNode.position = SCNVector3(x: 0.0, y: 0.0, z: 0.0)
let light = SCNLight()
let lightNode = SCNNode()
light.type = .ambient
light.color = UIColor.white
lightNode.light = light
scene.rootNode.addChildNode(cameraNode)
scene.rootNode.addChildNode(centerNode)
scene.rootNode.addChildNode(lightNode)
cameraNode.constraints = [SCNLookAtConstraint(target: centerNode)]
sceneView.pointOfView = cameraNode
}

Related

Realitykit / ArKit make a collision only between 2 objects

I have a ball and a cylinder. When i tap on the ball it moves in forward position. When the ball hits the cylinder they collide and the cylinder moves and changes color. Thats working fine.
I have 2 problems:
When i start the app the cylinder is constantly colliding and changes color immediately, so even when the ball is not near it. It looks like its colliding with something else.
The second thing is, there will be more cylinders in the scene, how can i make a collision event only between 2 objects. Lets say there are 2 cylinders in the scene how can ik set a filter or group.
import SwiftUI
import RealityKit
import ARKit
import FocusEntity
import Combine
struct ContentView : View {
var body: some View {
ARViewContainer()
}
}
struct ARViewContainer: UIViewRepresentable {
func makeUIView(context: Context) -> ARView {
let view = ARView()
let session = view.session
let config = ARWorldTrackingConfiguration()
config.planeDetection = .horizontal
session.run(config)
let coachingOverlay = ARCoachingOverlayView()
coachingOverlay.autoresizingMask = [.flexibleWidth, .flexibleHeight]
coachingOverlay.session = session
coachingOverlay.goal = .horizontalPlane
view.addSubview(coachingOverlay)
context.coordinator.view = view
session.delegate = context.coordinator
view.addGestureRecognizer(UITapGestureRecognizer(target: context.coordinator, action: #selector(Coordinator.handleTap)))
return view
}
func updateUIView(_ uiView: ARView, context: Context) {}
func makeCoordinator() -> Coordinator {
Coordinator()
}
//coordinator class
class Coordinator: NSObject,ARSessionDelegate {
weak var view: ARView?
var focusEntity: FocusEntity?
#Published var sceneIsPlaced:Bool = false //is de scene reeds geplaats na openen app
#Published var subscriptions: [AnyCancellable] = []
func session(_ session: ARSession, didAdd anchors: [ARAnchor]) {
guard let view = self.view else { return }
self.focusEntity = FocusEntity(on: view, style: .classic(color: .yellow))
}
//na tap op het scherm, wat te doen -> creeer locatie en plaats het object
#objc func handleTap(recognizer: UITapGestureRecognizer) {
guard let view = self.view, let focusEntity = self.focusEntity else { return }
//tap location van de tap gesture
let tapLocation = recognizer.location(in: self.view)
//Create Anchor
let anchor = AnchorEntity()
let importModel = try! Entity.load(named: "cilinder") //alle objects!
importModel.position = focusEntity.position
importModel.scale = SIMD3(repeating: 0.5)
//The cylinder
let kolomMiddleModel = importModel.findEntity(named: "cilinder")!.children[0] as! (ModelEntity & HasPhysicsBody & HasCollision)
let materialKolomMiddle = SimpleMaterial(color: .yellow, isMetallic: true)
kolomMiddleModel.model?.materials = [materialKolomMiddle]
kolomMiddleModel.generateCollisionShapes(recursive: false)
let physics = PhysicsBodyComponent(massProperties: .default, material: .default, mode: .dynamic)
kolomMiddleModel.components.set(physics)
//MAKE A BALL
let materialsBall = SimpleMaterial(color: .red, isMetallic: true)
let ballModel = ModelEntity(mesh: .generateSphere(radius: 0.1),
materials: [materialsBall])
as (Entity & HasPhysicsBody & HasCollision)
ballModel.position = [-0.1, -1.0,-0.1]
ballModel.generateCollisionShapes(recursive: true)
ballModel.name = "ballModel"
//LIGHTS --> let op voor de shadow moet de plane van occlusion material met dynamical lightning op tue
let directionalLight = DirectionalLight()
directionalLight.light.color = .white
directionalLight.light.intensity = 4000
directionalLight.light.isRealWorldProxy = true
directionalLight.shadow?.maximumDistance = 1.5
directionalLight.shadow?.depthBias = 7.0
directionalLight.orientation = simd_quatf(angle: .pi/1.5, axis: [0,1,0])
//maak een anchor voor het licht
let lightAnchor = AnchorEntity(world: [0, 0, 2.5])
lightAnchor.addChild(directionalLight)
//COLLISION EVENTS
DispatchQueue.main.async {
//collision of kolomModel
view.scene.subscribe(to: CollisionEvents.Began.self,
on: kolomMiddleModel) { _ in
print("Collision kolomModel detected!")
let material = SimpleMaterial(color: .red, isMetallic: true)
kolomMiddleModel.model?.materials = [material]
}.store(in: &self.subscriptions)
}
// view.installGestures(for: ballModel)
//
//PLACE SCENE IF NOT PLACED ALREADY
if !sceneIsPlaced {
//ADD MODELS TO ANCHOR
anchor.addChild(importModel)
anchor.addChild(ballModel)
anchor.addChild(lightAnchor)
view.scene.addAnchor(anchor)
sceneIsPlaced = true
//If the scene is already placed get the taplocation
}else{
if let locationEntity = view.entity(at: tapLocation) {
let entityTapped = locationEntity as! ModelEntity
//MAKE THE BALL GO FORWARD
print("entity tapped \(entityTapped)")
entityTapped.physicsBody = .init()
entityTapped.physicsBody?.mode = .kinematic
entityTapped.physicsMotion = PhysicsMotionComponent(linearVelocity: [0, 0, -0.5],
angularVelocity: [1, 3, 5])
}
}
}
}
}
Here is an example of two objects in RealityKit that can only collide with each other:
let sphere = ModelEntity(mesh: .generateSphere(radius: 0.1), materials: [SimpleMaterial()])
sphere.collision = CollisionComponent(shapes: [ShapeResource.generateSphere(radius: 0.1)])
sphere.collision?.filter = CollisionFilter(group: 1 << 2, categories: .default, mask: .default)
let cube = ModelEntity(mesh: .generateBox(size: [0.1, 0.1, 0.1]), materials: [SimpleMaterial()])
cube.collision = CollisionComponent(shapes: [ShapeResource.generateBox(size: [0.1, 0.1, 0.1])])
cube.collision?.filter = CollisionFilter(group: 1 << 1, categories: .default, mask: .default)
sphere.collision?.filter.collisionGroup = 1 << 1
cube.collision?.filter.collisionGroup = 1 << 2
arView.scene.addAnchor(sphere)
arView.scene.addAnchor(cube)
This code creates two objects: a sphere and a cube. Each object is given a CollisionComponent with a corresponding ShapeResource and CollisionFilter. The CollisionFilter for each object specifies that only objects in a different collisionGroup can collide with it. As a result, the sphere and cube can only collide with each other, not with other objects in the scene.
See more information about CollisionGroups here:
https://developer.apple.com/documentation/realitykit/collisionfilter

how to make infinite background in spriteKit swift 4

I try to make an endless background through the nodes, but the background has not become infinite and is interrupted, the third background is not yet shown. After the first show, the number of nodes in the scene grows, how can this be fixed?
import SpriteKit
import GameplayKit
class GameScene: SKScene {
var bgNode: SKNode!
var overlay: SKNode!
var overlayWidth: CGFloat!
//var viewSize: CGSize!
var levelPositionX: CGFloat = 0.0
//var speed: CGFloat = 5.5
override func didMove(to view: SKView) {
setupNode()
//viewSize = CGSize(width: frame.size.width, height:
frame.size.height )
}
func setupNode() {
let worldNode = childNode(withName: "World")!
bgNode = worldNode.childNode(withName: "Background")!
overlay = bgNode.childNode(withName: "Overlay")!.copy() as!
SKNode
overlayWidth = overlay.calculateAccumulatedFrame().width
}
func createBackgroundOverlay() {
let backgroundOverlay = overlay.copy() as! SKNode
backgroundOverlay.position = CGPoint(x: 0.0, y: 0.0)
bgNode.addChild(backgroundOverlay)
levelPositionX += overlayWidth
}
func update() {
bgNode.position.x -= 5
if bgNode.position.x <= -self.frame.size.width {
bgNode.position.x = self.frame.size.width * 2
createBackgroundOverlay()
}
}
override func update(_ currentTime: TimeInterval) {
update()
}
In my endless runner game, I have implemented an endless background and a ground(or floor) much similar to your app. Below I shall discuss the steps i have used in my game.
Step 1: In your GameScene.swift file add these variables.
var backgroundSpeed: CGFloat = 80.0 // speed may vary as you like
var deltaTime: TimeInterval = 0
var lastUpdateTimeInterval: TimeInterval = 0
Step 2: In GameScene file, make setUpBackgrouds method as follows
func setUpBackgrounds() {
//add background
for i in 0..<3 {
// add backgrounds, my images were namely, bg-0.png, bg-1.png, bg-2.png
let background = SKSpriteNode(imageNamed: "bg-\(i).png")
background.anchorPoint = CGPoint.zero
background.position = CGPoint(x: CGFloat(i) * size.width, y: 0.0)
background.size = self.size
background.zPosition = -5
background.name = "Background"
self.addChild(background)
}
for i in 0..<3 {
// I have used one ground image, you can use 3
let ground = SKSpriteNode(imageNamed: "Screen.png")
ground.anchorPoint = CGPoint(x: 0, y: 0)
ground.size = CGSize(width: self.size.width, height: ground.size.height)
ground.position = CGPoint(x: CGFloat(i) * size.width, y: 0)
ground.zPosition = 1
ground.name = "ground"
self.addChild(ground)
}
}
Step 3: Now we have to capture timeIntervals from update method
override func update(_ currentTime: TimeInterval) {
if lastUpdateTimeInterval == 0 {
lastUpdateTimeInterval = currentTime
}
deltaTime = currentTime - lastUpdateTimeInterval
lastUpdateTimeInterval = currentTime
}
Step 4: Here comes the most important part, moving our backgrounds and groungFloor by enumerating child nodes. Add these two methods in GameScene.swift file.
func updateBackground() {
self.enumerateChildNodes(withName: "Background") { (node, stop) in
if let back = node as? SKSpriteNode {
let move = CGPoint(x: -self.backgroundSpeed * CGFloat(self.deltaTime), y: 0)
back.position += move
if back.position.x < -back.size.width {
back.position += CGPoint(x: back.size.width * CGFloat(3), y: 0)
}
}
}
}
func updateGroundMovement() {
self.enumerateChildNodes(withName: "ground") { (node, stop) in
if let back = node as? SKSpriteNode {
let move = CGPoint(x: -self.backgroundSpeed * CGFloat(self.deltaTime), y: 0)
back.position += move
if back.position.x < -back.size.width {
back.position += CGPoint(x: back.size.width * CGFloat(3), y: 0)
}
}
}
}
Step 5: At this point you should get this error:"Binary operator '+=' cannot be applied to two 'CGPoint' operands" in updateBackground and updateGroundMovement methods.
Now we need to implement operator overloading to resolve this problem. Create a new Swift File and name it Extensions.swift and then implement as follows:
// Extensions.swift
import CoreGraphics
import SpriteKit
public func + (left: CGPoint, right: CGPoint) -> CGPoint {
return CGPoint(x: left.x + right.x, y: left.y + right.y)
}
public func += (left: inout CGPoint, right: CGPoint) {
left = left + right
}
Step 6: call setUpBackgrounds method in didMove(toView:)
override func didMove(to view: SKView) {
setUpBackgrounds()
}
Step 7: Finally call the updateBackground and updateGroundMovement methods in update(_ currentTime) method. updated code is given below:
override func update(_ currentTime: TimeInterval) {
if lastUpdateTimeInterval == 0 {
lastUpdateTimeInterval = currentTime
}
deltaTime = currentTime - lastUpdateTimeInterval
lastUpdateTimeInterval = currentTime
//MARK:- Last step:- add these methods here
updateBackground()
updateGroundMovement()
}

Swift 3 Transition Error

I am using transition function to have cube effect with function as shown and getting error *** Terminating app due to uncaught exception 'CALayerInvalidGeometry', reason: 'CALayer bounds contains NaN: [0 nan; 667 343]'
The above error occurs when popped among view controllers (UiNavigationcontroller with 5 view controllers in shared instance) after orientation is changed.
I am not able to fix it ,please help
func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
let containerView = transitionContext.containerView
let toViewController = transitionContext.viewController(forKey: UITransitionContextViewControllerKey.to)!
let fromViewController = transitionContext.viewController(forKey: UITransitionContextViewControllerKey.from)!
let toView = toViewController.view
let fromView = fromViewController.view
let direction: CGFloat = reverse ? -1 : 1
let const: CGFloat = -0.005
toView?.layer.anchorPoint = CGPoint()
toView?.layer.anchorPoint = CGPoint(x:direction == 1 ? 0 : 1,y: 0.5)
fromView?.layer.anchorPoint = CGPoint(x:direction == 1 ? 1 : 0,y: 0.5)
var viewFromTransform: CATransform3D = CATransform3DMakeRotation(direction * CGFloat(M_PI_2), 0.0, 1.0, 0.0)
var viewToTransform: CATransform3D = CATransform3DMakeRotation(-direction * CGFloat(M_PI_2), 0.0, 1.0, 0.0)
viewFromTransform.m34 = const
viewToTransform.m34 = const
containerView.transform = CGAffineTransform(translationX: direction * containerView.frame.size.width / 2.0, y: 0)
toView?.layer.transform = viewToTransform
print("container view frame: \(containerView.frame) subviews container \(containerView.subviews)")
containerView.addSubview(toView!)
// App crashes here giving error
UIView.animate(withDuration: transitionDuration(using: transitionContext), animations: {
containerView.transform = CGAffineTransform(translationX: -direction * containerView.frame.size.width / 2.0, y: 0)
fromView?.layer.transform = viewFromTransform
toView?.layer.transform = CATransform3DIdentity
}, completion: {
finished in
containerView.transform = .identity
fromView?.layer.transform = CATransform3DIdentity
toView?.layer.transform = CATransform3DIdentity
fromView?.layer.anchorPoint = CGPoint(x:0.5,y:0.5)
toView?.layer.anchorPoint = CGPoint(x:0.5,y:0.5)
if (transitionContext.transitionWasCancelled) {
toView?.removeFromSuperview()
} else {
fromView?.removeFromSuperview()
}
transitionContext.completeTransition(!transitionContext.transitionWasCancelled)
})
}

Draw a rolling line in a UIView

I have the following view in IB (Using Swift 3)
The green UIImageView is nested inside a UIView.
When I press begin, I'd ike to draw a line, indication the sound level currently beeing recorded.
I'm very new to Core Graphics, and have found the following code in SO that explains how to draw on a UYIImageView.
func drawLine(from fromPoint: CGPoint, to toPoint: CGPoint) {
UIGraphicsBeginImageContextWithOptions(view.bounds.size, false, 0)
plotArea.image?.draw(in: view.bounds)
let context = UIGraphicsGetCurrentContext()
context?.move(to: fromPoint)
context?.addLine(to: toPoint)
context?.setLineCap(CGLineCap.round)
context?.setLineWidth(brushWidth)
context?.setStrokeColor(red: red, green: green, blue: blue, alpha: 1.0)
context?.setBlendMode(CGBlendMode.normal)
context?.strokePath()
plotArea.image = UIGraphicsGetImageFromCurrentImageContext()
plotArea.alpha = opacity
UIGraphicsEndImageContext()
}
This draws a nice line, when called, inside the green UIImageView (called plotArea). What I want to happen is that the line draws on top of the UIView (called rangeView) (indicating to the user, that when the line is over the green imageview, he's at the correct level)
Can anyone point me towards refactoring my drawLine function to draw on the UIView in stead of the UIImageView
When I solve that, I will also need for the line to be drawn continuously - meaning that when it reaches 2/3 of the view, it should continue drawing at that fixed x coordinate, and disappear to the left (like a rolling line)
Right now I'm calling the drawLine func every 0.1 seconds with this function:
func animatePin() {
let viewHeight = self.rangeView.frame.height
let ypos = viewHeight/maxDb*CGFloat(self.calculateLevel())
let p = CGPoint(x:self.lastPoint.x+10, y: ypos)
self.drawLine(from: self.lastPoint, to: p)
self.lastPoint = p
}
EDIT: Solved the first part, by calling this method in stead:
func drawLineFromPoint(start : CGPoint, toPoint end:CGPoint, ofColor lineColor: UIColor, inView view:UIView) {
let path = UIBezierPath()
path.move(to: start)
path.addLine(to: end)
let shapeLayer = CAShapeLayer()
shapeLayer.path = path.cgPath
shapeLayer.strokeColor = lineColor.cgColor
shapeLayer.lineWidth = 1.0
view.layer.addSublayer(shapeLayer)
}
EDIT2 - Managed to get the line "rolling" by refactoring the function to
func drawLineFromPoint(start : CGPoint, toPoint end:CGPoint, ofColor lineColor: UIColor, inView view:UIView) {
let path = UIBezierPath()
path.move(to: start)
path.addLine(to: end)
let shapeLayer = CAShapeLayer()
shapeLayer.path = path.cgPath
shapeLayer.strokeColor = lineColor.cgColor
shapeLayer.lineWidth = 1.0
if(end.x > view.frame.width*0.95){
let newRect = CGRect(x: view.frame.origin.x-10, y: view.frame.origin.y, width: view.frame.width+10, height: view.frame.height)
view.frame = newRect
}
if(start != CGPoint.zero){
view.layer.addSublayer(shapeLayer)
}
}
Function to draw the line:
func drawLineFromPoint(start : CGPoint, toPoint end:CGPoint, ofColor lineColor: UIColor, inView view:UIView) {
//create a path
let path = UIBezierPath()
path.move(to: start)
path.addLine(to: end)
//create a shape, and add the path to it
let shapeLayer = CAShapeLayer()
shapeLayer.path = path.cgPath
shapeLayer.strokeColor = lineColor.cgColor
shapeLayer.lineWidth = 1.0
//if we are at the end of the view, move the view left by 10, and add the 10 to the right, making it roll
if(end.x > view.frame.width*0.95){
let newRect = CGRect(x: view.frame.origin.x-10, y: view.frame.origin.y, width: view.frame.width+10, height: view.frame.height)
view.frame = newRect
}
//wait till there iss data to show, so we don't get a huge spike from 0.0
if(start != CGPoint.zero){
view.layer.addSublayer(shapeLayer)
}
}

draw animated circle in swift 3

Refrence : https://stackoverflow.com/a/26578895/6619234
how to erase and redraw circle on click evnet?
i tried to call addCircleView method on click event but circle is overlapping every time.
class CircleClosing: UIView {
var circleLayer: CAShapeLayer!
override init(frame: CGRect) {
super.init(frame: frame)
self.backgroundColor = UIColor.clear
// Use UIBezierPath as an easy way to create the CGPath for the layer.
// The path should be the entire circle.
let circlePath : UIBezierPath!
circlePath = UIBezierPath(arcCenter: CGPoint(x: frame.size.width / 2.0, y: frame.size.height / 2.0), radius: (frame.size.width - 5)/2, startAngle: 0.0, endAngle: CGFloat(M_PI * 2.0), clockwise: true)
// Setup the CAShapeLayer with the path, colors, and line width
circleLayer = CAShapeLayer()
circleLayer.path = circlePath.cgPath
circleLayer.fillColor = UIColor.clear.cgColor
circleLayer.strokeColor = UIColor.blue.cgColor
circleLayer.lineWidth = 20.0;
// Don't draw the circle initially
circleLayer.strokeEnd = 0.0
// Add the circleLayer to the view's layer's sublayers
}
override func layoutSubviews()
{
let frame = self.layer.bounds
circleLayer.frame = frame
layer.addSublayer(circleLayer)
}
required init?(coder aDecoder: NSCoder)
{ super.init(coder: aDecoder) }
func animateCircle(duration: TimeInterval) {
// We want to animate the strokeEnd property of the circleLayer
let animation = CABasicAnimation(keyPath: "strokeEnd")
// Set the animation duration appropriately
animation.duration = duration
// Animate from 0 (no circle) to 1 (full circle)
animation.fromValue = 0
animation.toValue = 1
// Do a linear animation (i.e. the speed of the animation stays the same)
animation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionLinear)
// Set the circleLayer's strokeEnd property to 1.0 now so that it's the
// right value when the animation ends.
circleLayer.strokeEnd = 1.0
// Do the actual animation
circleLayer.add(animation, forKey: "animateCircle")
}
}
Add in your subview
func addCircleView() {
var circleView : CircleClosing!
let diceRoll = CGFloat(510) //CGFloat(Int(arc4random_uniform(7))*50)
let diceRolly = CGFloat(70)
let circleWidth = CGFloat(40)
let circleHeight = circleWidth
// Create a new CircleView
circleView = CircleClosing(frame:CGRect(x:diceRoll,y: diceRolly,width: circleWidth,height: circleHeight) )
view.addSubview(circleView)
// Animate the drawing of the circle over the course of 1 second
circleView.animateCircle(duration: 20.0)
}
Thanks in Advance
var circleView : CircleClosing!
func addCircleView() {
let diceRoll = CGFloat(510) //CGFloat(Int(arc4random_uniform(7))*50)
let diceRolly = CGFloat(70)
let circleWidth = CGFloat(40)
let circleHeight = circleWidth
//Add this line here to remove from superview
circleView.removeFromSuperview()
circleView = CircleClosing(frame:CGRect(x:diceRoll,y: diceRolly,width: circleWidth,height: circleHeight) )
view.addSubview(circleView)
// Animate the drawing of the circle over the course of 1 second
circleView.animateCircle(duration: 20.0)
}