예제 #1
0
파일: BossAI.cs 프로젝트: cryizzle/Shoot
	bool DetectCollision(VectorLine self, VectorLine other)
	{
		float[] constants = VectorLine.GetIntersectConstants(self, other);
		if (constants[0] < 0 || constants[1] < 0) //both have to be positive for eminent collision
		{
			return false;
		}
		float collision_threshold = 0.5f;   //give and take 0.5s
		Vector3 collision_pt = VectorLine.GetIntersectionPoint(self, other);

		//calculate time it takes for self
		float self_disp = Mathf.Pow((collision_pt - self.GetPoint()).sqrMagnitude, 0.5f);
		float self_time = self_disp / self.GetSpeed();

		//calculate time it takes for other
		float other_disp = Mathf.Pow((collision_pt - other.GetPoint()).sqrMagnitude, 0.5f);
		float other_time = other_disp / other.GetSpeed();

		float time_diff = Mathf.Abs(self_time - other_time);

		Debug.Log("self time: " + self_time + "other time: " + other_time);
		Debug.Log("time diff: " + time_diff);

		return time_diff <= collision_threshold;    //collision detected if they collide within 0.5s of each other
	}
예제 #2
0
파일: BossAI.cs 프로젝트: cryizzle/Shoot
	void Flee(Collider other)
	{
		if (other.tag != "Asteroid")
		//if (other.tag != "Bolt")
		{
			return;
		}
		Debug.Log("own pos: " + GetComponent<Rigidbody>().position.ToString());
		Debug.Log("hazard: " + other.tag.ToString());
		Debug.Log("hazard pos: " + other.GetComponent<Rigidbody>().position.ToString());

		VectorLine hazard = new VectorLine(other.GetComponent<Rigidbody>().transform.position, other.GetComponent<Rigidbody>().velocity);
		//bool collision = hazard.VectorOnLineFront(GetComponent<Rigidbody>().transform.position);

		/*Debug.Log("Collision eminent: " + collision.ToString());
		if (!collision)
		{
			return;
		}*/
		Vector3 relative_velocity = other.GetComponent<Rigidbody>().velocity - GetComponent<Rigidbody>().velocity;
		Vector3 relative_displacement = other.GetComponent<Rigidbody>().transform.position - GetComponent<Rigidbody>().transform.position;
		//Vector3 relative_displacement = other.GetComponent<Rigidbody>().transform.position + GetComponent<Rigidbody>().transform.position;
		/*if (relative_displacement.magnitude > minDistance)
		{
			return;
		}*/

		if (GetComponent<Rigidbody>().velocity.magnitude > 0)
		{
			VectorLine self = new VectorLine(GetComponent<Rigidbody>().transform.position, GetComponent<Rigidbody>().velocity);
			bool collision = DetectCollision(self, hazard);
			if (collision)
			{
				Vector3 collision_pt = VectorLine.GetIntersectionPoint(self, hazard);
				relative_displacement = collision_pt - GetComponent<Rigidbody>().transform.position;
			}

		}

		float self_time = relative_displacement.magnitude / hazard.GetSpeed();

		Vector3 new_pos = other.GetComponent<Rigidbody>().position - relative_velocity * self_time;

		target_position = new Vector3(
				Mathf.Clamp(new_pos.x, boundary.xMin, boundary.xMax),
				0.0f,
				Mathf.Clamp(new_pos.z, boundary.zMin, boundary.zMax));
		/*target_position = new Vector3(
				Mathf.Clamp(-1* relative_displacement.x, boundary.xMin, boundary.xMax),
				0.0f,
				Mathf.Clamp(-1* relative_displacement.z, boundary.zMin, boundary.zMax));*/

		Move(target_position.normalized);
	}