| 407 | } |
| 408 | |
| 409 | Nz::Vector3f DampedString(const Nz::Vector3f& currentPos, const Nz::Vector3f& targetPos, float frametime, float springStrength) |
| 410 | { |
| 411 | // Je ne suis pas l'auteur de cette fonction |
| 412 | // Je l'ai reprise du programme "Floaty Camera Example" et adaptée au C++ |
| 413 | // Trouvé ici: http://nccastaff.bournemouth.ac.uk/jmacey/RobTheBloke/www/opengl_programming.html#4 |
| 414 | // Tout le mérite revient à l'auteur (Qui me permettra ainsi d'améliorer les démos, voire même le moteur) |
| 415 | |
| 416 | // calculate the displacement between the target and the current position |
| 417 | Nz::Vector3f displacement = targetPos - currentPos; |
| 418 | |
| 419 | // whats the distance between them? |
| 420 | float displacementLength = displacement.GetLength(); |
| 421 | |
| 422 | // Stops small position fluctuations (integration errors probably - since only using euler) |
| 423 | if (Nz::NumberEquals(displacementLength, 0.f)) |
| 424 | return currentPos; |
| 425 | |
| 426 | float invDisplacementLength = 1.f / displacementLength; |
| 427 | |
| 428 | const float dampConstant = 0.000065f; // Something v.small to offset 1/ displacement length |
| 429 | |
| 430 | // the strength of the spring increases the further away the camera is from the target. |
| 431 | float springMagitude = springStrength*displacementLength + dampConstant*invDisplacementLength; |
| 432 | |
| 433 | // Normalise the displacement and scale by the spring magnitude |
| 434 | // and the amount of time passed |
| 435 | float scalar = std::min(invDisplacementLength * springMagitude * frametime, 1.f); |
| 436 | displacement *= scalar; |
| 437 | |
| 438 | // move the camera a bit towards the target |
| 439 | return currentPos + displacement; |
| 440 | } |