1

is there a way to trigger a function, when a node (e.g. a tossed ball) enters a certain bounding box (e.g. a basket) in SceneKit / ARKit? If not, how would you implement this problem?

Pascal
  • 2,590
  • 3
  • 21
  • 46

2 Answers2

1

The way I would probably approach this. After obtaining a basketball hoop 3D dae model.

https://3dwarehouse.sketchup.com/model/fa42320b3def2c6b4741187cebbc52b3/Basketball-Hoop

I would first setup up the physicsShapes for the different elements.... backboard, pole & ring/hoop, floor & ball. I would then work out the cylinder size to fit inside the ring.

Using:

let cylinder = SCNCylinder(radius: 1.0, height: 0.1)

This becomes the physicBody for the hoop.

hoopNode.physicsBody = SCNPhysicsBody(type: .static, shape: SCNPhysicsShape(geometry:cylinder))

I would then workout the collisions the ball has with each different basketball element/node.

Don’t forget to use the debug feature

SceneView.debugOptions = .showPhysicsShapes

This will help to visually debug the collisions with the physics shapes & make sure the physic shapes are accurately scaled and in the correct position.

How to do setup collisions has already been answered in a previous post here...

how to setup SceneKit collision detection

You probably want the ball to bounce off the backboard, pole and ring, but if it hits the hoop physic shape... you might want to make the ball disappear and create a nice spark/flame animation or something. You could then have a score that registers as well.

This Arkit game on github will give you a nice template for handling all those things, collisions, animations & scoring

https://github.com/farice/ARShooter

Clay
  • 1,721
  • 2
  • 10
  • 18
0

You have to implement it using the ball's world position and the basket world position + bounding box.

Calculate the size of the basket using it's bounding box:

var (basketBoundingBoxMin, basketBoundingBoxMax) = (basket?.presentation.boundingBox)!
let size = basketBoundingBoxMax - basketBoundingBoxMin

Use this size and the basket's world position to calculate:

  • Basket's minimum x,y,z
  • Basket's maximum x,y,z

Then check if ball's world position is inside the basket's minimum and maximum position.

Marcos Tanaka
  • 3,112
  • 3
  • 25
  • 41