summaryrefslogtreecommitdiffstats
path: root/kue/billiard.cpp
blob: 7237d7f813572ecfa991425026276aecb9dcb83f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
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;
}