-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpoint.cpp
More file actions
87 lines (68 loc) · 1.66 KB
/
point.cpp
File metadata and controls
87 lines (68 loc) · 1.66 KB
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
//
// Created by Avery Drennan on 4/27/23.
//
#pragma once
#include "point.h"
#include <cmath>
point::point(double x, double y) {
pos = std::make_pair(x,y);
old_pos = pos;
init_pos = pos;
is_pinned = false;
}
double point::get_x() const {
return pos.first;
}
double point::get_y() const {
return pos.second;
}
void point::set_x(double x) {
pos.first = x;
}
void point::set_y(double y) {
pos.second = y;
}
void point::pin() {
is_pinned = true;
}
void point::add_stick(stick *stick,int index)
{
switch(index) {
case 0:
this->sticks.first = stick;
break;
case 1:
this->sticks.second = stick;
break;
default:
printf("stick indexing error on point :%p\n",this);
}
}
void point::update(double delta_time,double drag,std::pair<double,double> acceleration)
{
if(this->pinned()) {
this->pos = init_pos;
return;
}
double vel_x = pos.first - old_pos.first;
double vel_y = pos.second - old_pos.second;
double pos_x = pos.first + vel_x * (1.f - drag) + acceleration.first * (1.f - drag) * delta_time * delta_time;
double pos_y = pos.second + vel_y * (1.f - drag) + acceleration.second * (1.f - drag) * delta_time * delta_time;
old_pos = pos;
pos.first = pos_x;
pos.second = pos_y;
}
bool point::pinned() const {
return is_pinned;
}
void point::keep_in_bounds()
{
if(this->pos.second > 600)
this->pos.second = 600;
if(this->pos.second < 0)
this->pos.second = 0;
if(this->pos.first > 800)
this->pos.first = 800;
if(this->pos.first < 0)
this->pos.first = 0;
}