Kukatz 3D  0.1
Török Attila szakdolgozata
projects/Kukatz 3D/src/overviewcamera.cpp
00001 /*
00002  * overviewcamera.cpp - Kukatz 3D
00003  * Copyright (c) 2011 - TÖRÖK Attila (torokati44@gmail.com)
00004  *
00005  * This software is provided 'as-is', without any express or implied
00006  * warranty. In no event will the authors be held liable for any damages
00007  * arising from the use of this software.
00008  *
00009  * Permission is granted to anyone to use this software for any purpose,
00010  * including commercial applications, and to alter it and redistribute it
00011  * freely, subject to the following restrictions:
00012  *
00013  *       1. The origin of this software must not be misrepresented; you must not
00014  *                      claim that you wrote the original software. If you use this software
00015  *                      in a product, an acknowledgment in the product documentation would be
00016  *                      appreciated but is not required.
00017  *
00018  *       2. Altered source versions must be plainly marked as such, and must not be
00019  *                      misrepresented as being the original software.
00020  *
00021  *       3. This notice may not be removed or altered from any source
00022  *                      distribution.
00023  */
00024 
00025 #include "overviewcamera.hpp"
00026 
00027 #include <SFML/System/Randomizer.hpp>
00028 #include <algorithm>
00029 
00030 #include "misc.hpp"
00031 
00032 OverviewCamera::OverviewCamera(const std::vector< Kukac* >& k):
00033         Camera(sf::Vector3f(0,0,0), sf::Vector3f(1,0,0), sf::Vector3f(0,0,1), sf::Vector2i(0,0), sf::Vector2i(100,100)),
00034         kukacs(k), time_accum(0.0f), change_time(sf::Randomizer::Random(15.0f, 30.0f))
00035 {
00036         update_object();
00037         
00038         assert(k.size() >= 3);
00039         
00040         for (unsigned int i = 0; i < 3; ++i)
00041         {
00042                 current.push_back(kukacs[i]);
00043         }
00044         
00045         sf::Vector3f head_0 = current[0]->head->object->get_position();
00046         sf::Vector3f head_1 = current[1]->head->object->get_position();
00047         sf::Vector3f head_2 = current[2]->head->object->get_position();
00048         
00049         sf::Vector3f tri_center = (head_0 + head_1 + head_2) / 3.0f;
00050         float dist = std::max(length(tri_center - head_0), std::max(length(tri_center - head_1), length(tri_center - head_2)));
00051         sf::Vector3f cp = normalize(crossprod(head_0 - head_1, head_0 - head_2));
00052         
00053         pos = target_pos = tri_center + cp * dist / tanf(fov * M_PI / 360.0f) * 1.1f * ((size.y > size.x)?((float)size.y / (float)size.x):(1.0f));
00054         center = target_center = tri_center;
00055         up = target_up = crossprod(pos - tri_center, head_0 - tri_center);
00056         
00057         up = normalize(up);
00058 }
00059 
00060 void OverviewCamera::update(float dt)
00061 {
00062         if (kukacs.size() > 3)
00063         {
00064                 time_accum += dt;
00065                 if (time_accum >= change_time)
00066                 {
00067                         time_accum = 0;
00068                         change_time = sf::Randomizer::Random(15.0f, 30.0f);
00069                         
00070                         Kukac* next;
00071                         do
00072                         {
00073                                 next = kukacs[sf::Randomizer::Random(0, 4)];
00074                         } while (count(current.begin(), current.end(), next));
00075                         
00076                         current[sf::Randomizer::Random(0, 2)] = next;
00077                 }
00078         }
00079         
00080         sf::Vector3f head_0 = current[0]->head->object->get_position();
00081         sf::Vector3f head_1 = current[1]->head->object->get_position();
00082         sf::Vector3f head_2 = current[2]->head->object->get_position();
00083         
00084         float speed = (current[0]->get_speed() + current[1]->get_speed() + current[2]->get_speed()) / 2.0f;
00085         
00086         sf::Vector3f tri_center = (head_0 + head_1 + head_2) / 3.0f;
00087         
00088         float dist = std::max(length(tri_center - head_0), std::max(length(tri_center - head_1), length(tri_center - head_2)));
00089         
00090         sf::Vector3f cp = normalize(crossprod(head_0 - head_1, head_0 - head_2));
00091         
00092         target_pos = tri_center + cp * dist / tanf(fov * M_PI / 360.0f) * 1.1f *
00093                 ((size.y > size.x)?((float)size.y / (float)size.x):(1.0f));
00094         target_center = tri_center;
00095         
00096         
00097         pos += normalize(target_pos - pos) * std::min(speed * dt, length(target_pos - pos));
00098         center += normalize(target_center - center) * std::min(speed * dt, length(target_center - center));
00099         
00100         sf::Vector3f forward_normal = normalize(center - pos);
00101         up = normalize(crossprod(crossprod(forward_normal, up), forward_normal));
00102         
00103         update_object();
00104 }
00105 
00106 OverviewCamera::~OverviewCamera()
00107 {
00108         
00109 }
 Összes Osztályok