Object Flying through rings (Without using Colliders) - Puzzle

Okay, so I went for a job interview today and completely sort of bombed this question by assuming rings were only in vertical orientation …now, i REALLY want to know the answer. Ive tried doing stuff in unity, but since i cant model i cant find a ring model :(.

He said something about calculating the normal for the ring, then something about inversing something…I dont quite remember, it was so fast. I would really love it if someone could better my understanding of this task.

Now - lets imagine superman is flying…and in each Update() tick I have a startPoint and endPoint.
startPoint represents the position from teh LAST tick, and endPoint represents the position from the current tick. Lets say ---- represents superman and O represents rings. -o- represents a ring that is sort of flat in the sky.

------     O      

                     O           

                                -o-

I was tasked with writing the method in the Ring Class…FlownThrough returning a boolean True/False to detect when superman has flown through a ring.

The ring class: Vector3 position; Vector3 normal; int radius;
public bool FlownThrough(startPoint,endPoint){
bool flownThrough=false;

//how do i detect whether or not StartPoint->End Point passes through the ring (remember the ring can be in different orientations, layin flat, angled 45 deg , angled 10deg )

return flownThrough;
}

bump.
anyone?

Doesn’t take any modeling to make a ring. Just grab Blender (FREE!). When you start it up, delete the cube, insert a torus, and export to FBX format. Shouldn’t take very long to find the full instructions on how to do that either :slight_smile:

As far as the calculation they’re talking about, dunno. Colliders are definitely the simplest way. Without using those then you’re stuck calculating what point along the movement line he’d have passed through the ring and check if that point falls within the ring’s area. Not sure of the exact calculation off the top of my head.

Ya, I realize unity i could j ust use colliders. but this interview question was for objective c - not really using any framework with colliders built in. he wanted to see the math. I am trying to figure out the logic behind it and how to mathematically do it. He did say something about inversing some transform and then doing some comparison.
I will try to scrape together an example.

" Without using those then you’re stuck calculating what point along the movement line he’d have passed through the ring and check if that point falls within the ring’s area. Not sure of the exact calculation off the top of my head. " - Yes this is precisely what I need to do. I was wondering if anyone could direct me on how to do this. Blender is completely intimidating, i tried making a ring, I didnt know there was an insert torus. I will try that (even when i made a ring and imported it I could only see half of the actually ring i did this with the circle tool then extrude circle)…

Sigh, I will try again

Ok so there is the prototype:

Here are the scripts:
Superman has a FlyingController Component
attached

Ring1, Ring2, Ring 3 have the Ring Component attached

using UnityEngine;
using System.Collections;

public class FlyingController : MonoBehaviour {
	
	public Vector3 startPos;
	public Vector3 endPos;
	public float accelerationSpeed=10f;
	
	private Ring ring;
	private Ring ring2;
	private Ring ring3;
	
	// Use this for initialization
	void Start () {
		startPos=transform.position;
		endPos=transform.position;
		
		ring=GameObject.Find ("ring").GetComponent<Ring>();
		ring2=GameObject.Find ("ring2").GetComponent<Ring>();
		ring3=GameObject.Find ("ring3").GetComponent<Ring>();
	}
	
	// Update is called once per frame
	void Update () {
		
		startPos=transform.position;
		
		Vector3 newPosition=transform.position;
		
		if (Input.GetKey (KeyCode.A)){
			newPosition.x--;
		}else if(Input.GetKey (KeyCode.D)){
			newPosition.x++;	
		}
		
		if (Input.GetKey (KeyCode.W)){
			newPosition.y++;
		}else if (Input.GetKey (KeyCode.S)){
			newPosition.y--;
		}
		
		if (Input.GetKey (KeyCode.E)){
			newPosition+=Vector3.forward*(Time.deltaTime*accelerationSpeed);
		}
		transform.position=newPosition;
		endPos=newPosition;
		
		if (startPos!=endPos){
			Debug.Log ("SP: "+startPos+", EP: "+endPos);
			//check for flythrough
			if (ring.flownThrough (startPos,endPos)){
				Destroy (ring.gameObject);
			}else{
				Debug.Log ("Did not fly through Ring 1");
			}
			
			
			if (ring2.flownThrough (startPos,endPos)){
				Destroy (ring2.gameObject);
			}else{
				Debug.Log ("Did not fly through Ring 2");
			}
			
			if (ring3.flownThrough (startPos,endPos)){
				Destroy (ring3.gameObject);
			}else{
				Debug.Log ("Did not fly through Ring 3");
			}
			startPos=endPos;
		}
		
	}
	
	void LateUpdate(){
		
	}
}

I need help implementing the following method.
Colliders are not an acceptable solution - I want to figure out how to do this mathematically.
The interviewer said something about transforming the ring into local space or something, and co mparing supermans start/end position to the ring in local space…id ont know, I have NO clue where to start implementing.

My answer durin the interview was check if startPos.x is before the ring.X, endPos.X is after the ring X. startPos.Y and endPos.Y is between the ring height (r*2) - however this only works when the ring is in a somewhat vertical position - and this method would completely fail on ring 3.

using UnityEngine;
using System.Collections;

public class Ring : MonoBehaviour {
        public float radius;
	public bool flownThrough(Vector3 startPos, Vector3 endPos){
		//transform.forward - represents the normal (facing direction)
		//transform.position - position of the ring
		//radius - radius of the ring - i know we need this
                radius=? <<how do i calculate this based on the model
    
		//Debug.Log ("Superman StartPos: "+startPos);
		//Debug.Log ("Superman EndPos: "+endPos);
		//Debug.Log ("Ring Is At: "+transform.position);
		//Debug.Log ("Ring Orientation: "+transform.forward);
		bool flownThrough=false;
		
                //?????????????????????????? what goes here :(
		return flownThrough;
	}
}

I’d google a quick formula to test for line intersection. The two lines would be top of ring to bottom of ring, superman start to superman finish.

If in 3D you’d need to do an line → plane intersection, where the point on the plane is within a certain radius of the middle of the plane. The plan represents the ring, the line superman movement.