diff options
author | Mavridis Philippe <mavridisf@gmail.com> | 2024-08-07 19:13:02 +0300 |
---|---|---|
committer | Mavridis Philippe <mavridisf@gmail.com> | 2024-08-07 19:23:24 +0300 |
commit | 04b5a62b8d9f5ff8240f25361046f2a5d58e8262 (patch) | |
tree | 98b126454cdf68d544e138d7e8b31d5fd45b72c2 /kue/billiard.cpp | |
parent | 83ba00b7e569587d50383ff06a70148042ca780e (diff) | |
download | tdegames-feat/kue.tar.gz tdegames-feat/kue.zip |
Add Kue billiards gamefeat/kue
Signed-off-by: Mavridis Philippe <mavridisf@gmail.com>
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; +} |