-
Notifications
You must be signed in to change notification settings - Fork 0
/
Verlet.cpp
62 lines (53 loc) · 1.15 KB
/
Verlet.cpp
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
#include "include/Verlet.h"
#include <ctime>
#include <SDL2/SDL.h>
#include <SDL2/SDL_events.h>
// #include <cmath>
#define CLICK_RANGE 400
inline unsigned int hypot2(int dx, int dy) {
return dx * dx + dy * dy;
}
Verlet::Verlet(double x, double y, double z)
{
//ctor
m_x = m_xp = x;
m_y = m_yp = y;
m_z = m_zp = z;
held = false;
m_mass = 100;
m_xa = m_ya = m_za = 0;
}
void Verlet::move()
{
time_t timenow = time(NULL);
if (!held)
{
m_x = 2 * m_x - m_xp + m_xa * (timenow - m_t) * (timenow - m_t) / m_mass;
m_y = 2 * m_y - m_yp + m_ya * (timenow - m_t) * (timenow - m_t) / m_mass;
m_z = 2 * m_z - m_zp + m_za * (timenow - m_t) * (timenow - m_t) / m_mass;
}
m_xp = m_x;
m_yp = m_y;
m_zp = m_z;
m_t = timenow;
}
void Verlet::grab(unsigned int x, unsigned int y)
{
if (hypot2(x - m_x, y - m_y) < CLICK_RANGE)
{
held = true;
m_x = x;
m_y = y;
}
}
void Verlet::release() {
held = false;
}
void Verlet::drag(unsigned int x, unsigned int y) {
// printf("woo!\n");
if (held) {
m_x = x; m_y = y;
}
}
Verlet::~Verlet() {
}