Example #1
0
Hit* HitDetection::hitDetect(Ray* r, float grav, bool proj){
	float ox = r->getOrigin()->get(X);
	float oy = r->getOrigin()->get(Y);
	float oz = r->getOrigin()->get(Z);
	float ody = r->getDirection()->get(Y);
	
	float x = ox, y = oy, z = oz;
	float dx = r->getDirection()->get(X)*0.01f;
	float dy = ody*0.01f;
	float dz = r->getDirection()->get(Z)*0.01f;
	
	float g = grav*0.0001f;
		
	float time = 0;
		
	Coordinate* c = World::getCoord((int)x, (int)z);
	Coordinate* temp = c;
	bool firstTime = true;

	Hit* hit = new Hit(proj);
	while(hit->getDuration() < -1){
		if(y < 0){
			//cout << "HIT GROUND!" << endl << endl;
			hit->setType(HIT_GROUND);
			hit->setDuration(1000);  // Arbitrary
			r->getOrigin()->set(ox, oy, oz);
			r->getDirection()->set(Y, ody);
			return hit;
		}
		else if(c == NULL){
			//cout << "WENT OUT OF BOUNDS!" << endl << endl;
			hit->setType(HIT_BOUNDS);
			hit->setDuration(1000);  // Arbitrary
			r->getOrigin()->set(ox, oy, oz);
			r->getDirection()->set(Y, ody);
			return hit;
		}
		if(firstTime)
			firstTime = false;
		else // Prevents shooting/seeing self
			intersectCharacter(r, c, hit, time);	
		intersectBreakable(r, c, hit, time);

		while(temp == c){
			dy -= g; 
			x += dx; 
			y += dy; 
			z += dz;
			r->getOrigin()->set(x, y, z);
			r->getDirection()->set(Y, dy);	
			temp = World::getCoord((int)x, (int)z);	
			time += 0.01f;
		}
		c = temp;
	}

	r->getOrigin()->set(ox, oy, oz);
	r->getDirection()->set(Y, ody);
	return hit;
}