HitInfo Sphere::intersection(const Ray & ray){
	double a = Vector(ray.getDir()).dot(Vector(ray.getDir()));
	double b = 2*Vector(ray.getStart()).dot(ray.getDir());
	double c = Vector(ray.getStart()).dot(Vector(ray.getStart()))-1;

	double discr = pow(b,2) - 4*a*c;

	double t1 =(-b-sqrt(discr))/(2*a);
	double t2 =(-b+sqrt(discr))/(2*a);
	double t = t2;

	if (t1<t2){
		if(t1 >= 0){
			t = t1;
		}
	}

	Point intersect = ray.getPoint(t);

	return HitInfo(t, intersect, mtrl, Vector(intersect));
}
Ejemplo n.º 2
0
void TutorialMobManager::CollidePlayer()
{
	std::forward_list<Line> lines;
	playerPosition = player->GetPosition();

	AddCollideLine(lines);

	BoundingSphere sphere(playerPosition, 1.0f);


	for (auto& line : lines) {

		if (Collision::BoundingSphere_Line(sphere, line, HitInfo()) == false) {
			//line.Translate(Vector3(0, 5, 0)).Draw();
			continue;
		}


		if (IsAlreadyCollidePlayer(line))
			continue;

		//line.Translate(Vector3(0, 5, 0)).Draw(Color::Green);


		float p1 = (playerPosition - line.p1).Length();
		float p2 = (playerPosition - line.p2).Length();

		int score = (p1 < p2) ? p1 : p2;

		int getScore = scoreTable.GetScore(score);

		AddScore(getScore);
		mScore.Initialize(playerPosition, getScore);
		previousPlayerPosition = playerPosition;
	}

}
Ejemplo n.º 3
0
bool TutorialMobManager::IsAlreadyCollidePlayer(const Line & line)
{
	BoundingSphere sphere(previousPlayerPosition, 5.0f);
	Line l = Line(line.p1, line.p2);
	return Collision::BoundingSphere_Line(sphere, line, HitInfo());
}
Ejemplo n.º 4
0
Vector3
Specular::shade(const Ray& ray, const HitInfo& hit, const Scene& scene) const
{
    printf("Shading specular\n");
    Vector3 L = Vector3(0.0f, 0.0f, 0.0f);

    const Vector3 viewDir = -ray.d; // d is a unit vector

    const Lights *lightlist = scene.lights();
    Vector3 color = Vector3(m_kd);
    if (hit.material->hasTexture()) {
        Vector3 c = Vector3(hit.P);
		color = m_texture->getColor(c);
    }

    // loop over all of the lights

    HitInfo lightHitReflect = HitInfo(hit);
    HitInfo lightHitRefract = HitInfo(hit);
    lightHitReflect.hitNum = hit.hitNum;
    lightHitRefract.hitNum = hit.hitNum;

    Lights::const_iterator lightIter;
    for (lightIter = lightlist->begin(); lightIter != lightlist->end(); lightIter++)
    {
        PointLight* pLight = *lightIter;
        Ray lightRay;

        Vector3 l = pLight->position() - hit.P;
        float falloff = l.length2();
        l /= sqrt(falloff);

        /* Calculate the diffuse component - From Labertian Shading Model */
        float nDotL = dot(hit.N, l);
        Vector3 diffuseResult = pLight->color();
        diffuseResult *= color;
        L += std::max(0.0f, nDotL/falloff * pLight->wattage() / PI) * diffuseResult;

        /* Calculate the specular highlight - From Phong Shading Model */
        Vector3 h = (viewDir + l).normalize();
        float nDotH = dot(hit.N, h);
        Vector3 specResult = pLight->color();
        specResult *= m_ks;
        L += pow(std::max(0.0f, nDotH/falloff * pLight->wattage() / PI), m_p) * specResult;

        /* Calculate the specular reflectance */
        if(lightHitReflect.hitNum < 1){
            Vector3 r = reflect(hit.N, ray.d);
            lightRay.d = r.normalize();
            lightRay.o = hit.P;
            if(scene.trace(lightHitReflect, lightRay, 0.0001, r.length())){
                lightHitReflect.hitNum = lightHitReflect.hitNum+1;
                L += lightHitReflect.material->shade(lightRay, lightHitReflect, scene)*m_rl;
            }
        }

        /* Calculate the specular refractance */
        // Vector3 incident = viewDir - hit.P;

        if(lightHitRefract.hitNum < 1 && m_rf > 0.0f ){
            Vector3 t = refract(hit.N, ray.d, hit.material->n(), m_n);
            if(t != Vector3(0.0f))
            lightRay.d = t.normalize();
            else lightRay.d = t;
            lightRay.o = hit.P;
            if(scene.trace(lightHitRefract, lightRay, 0.0001, t.length())){
                lightHitRefract.hitNum = lightHitRefract.hitNum+1;
                    L+=lightHitRefract.material->shade(lightRay, lightHitRefract, scene)*m_rf;
            } else {
                L+=scene.getBGColor()*m_rf;
            }
        }
    }

    return L;
}