diff options
Diffstat (limited to 'kue/billiard.cpp')
-rw-r--r-- | kue/billiard.cpp | 87 |
1 files changed, 87 insertions, 0 deletions
diff --git a/kue/billiard.cpp b/kue/billiard.cpp new file mode 100644 index 00000000..7237d7f8 --- /dev/null +++ b/kue/billiard.cpp @@ -0,0 +1,87 @@ +#include "billiard.h" +#include "graphics.h" + +#include <math.h> +#include <tqstring.h> +#include <kdebug.h> + +const double FRICTIONAL_FORCE = 0.025; + +KueBilliard::KueBilliard(double x, double y, double r, const KueTexture &texture) : circle(x, y, r), _texture(texture) +{ +} + +KueBilliard::~KueBilliard() +{ +} + +void KueBilliard::step(double seconds) +{ + double delta_x; + double delta_y; + + if (isStopped()) + return; + + vector new_velocity = _velocity - (FRICTIONAL_FORCE * seconds); + + if (new_velocity.magnitude() < 0.0) + new_velocity.setMagnitude(0.0); + + delta_x = (_velocity.componentX() + new_velocity.componentX()) / 2.0 * seconds; + delta_y = (_velocity.componentY() + new_velocity.componentY()) / 2.0 * seconds; + + _pos_x += delta_x; + _pos_y += delta_y; + + _velocity = new_velocity; +} + +bool KueBilliard::isStopped() +{ + return (_velocity.magnitude() == 0.0); +} + +void KueBilliard::reflect(double normal) +{ + _velocity.setDirection(M_PI - (_velocity.direction()) + (normal * 2.0)); +} + +void KueBilliard::collide(KueBilliard &b) { + _velocity -= b._velocity; + + double mv = _velocity.magnitude(); + + vector unit1 = vector(*this, b); + unit1 = unit1.unit(); + + vector unit2 = _velocity.unit(); + + double cos = unit1 * unit2; + + unit1 *= mv * cos; + _velocity -= unit1; + _velocity += b._velocity; + + b._velocity += unit1; +} + +vector& KueBilliard::velocity() +{ + return _velocity; +} + +void KueBilliard::setVelocity(const vector &velocity) +{ + _velocity = velocity; +} + +KueTexture& KueBilliard::texture() +{ + return _texture; +} + +void KueBilliard::setTexture(const KueTexture &texture) +{ + _texture = texture; +} |