-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathObject2D.cpp
More file actions
126 lines (101 loc) · 1.92 KB
/
Object2D.cpp
File metadata and controls
126 lines (101 loc) · 1.92 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#include <cmath>
#include "Object2D.h"
Object2D::Object2D(Image* i)
{
image = i;
angle = 0.0f;
}
GLfloat Object2D::GetWidth() const
{
return image->GetWidth();
}
GLfloat Object2D::GetHeight() const
{
return image->GetHeight();
}
GLfloat Object2D::GetX() const
{
return xpos;
}
GLfloat Object2D::GetY() const
{
return ypos;
}
GLfloat Object2D::GetLastX()
{
return xhist[1];
}
GLfloat Object2D::GetLastY()
{
return yhist[1];
}
void Object2D::SetPos(GLfloat x, GLfloat y)
{
xpos = x;
ypos = y;
for (int i = (NO2DHIST - 1); i > 0; i--)
{
xhist[i] = xhist[i - 1];
yhist[i] = yhist[i - 1];
}
xhist[0] = x;
yhist[0] = y;
//---- Estimate velocity as slope of last 5 observations ----
// get mean
GLfloat xmean = 0;
GLfloat ymean = 0;
for (int i = 0; i < NO2DHIST; i++)
{
xmean += xhist[i];
ymean += yhist[i];
}
xmean = xmean/NO2DHIST;
ymean = ymean/NO2DHIST;
// compute xvel as slope of data over last few 5 samples
xvel = (-2*xhist[4] -1*xhist[3] + 1*xhist[1] + 2*xhist[0])*SAMPRATE/10;
yvel = (-2*yhist[4] -1*yhist[3] + 1*yhist[1] + 2*yhist[0])*SAMPRATE/10;
}
GLfloat Object2D::GetXVel()
{
return xvel;
}
GLfloat Object2D::GetYVel()
{
return yvel;
}
GLfloat Object2D::GetVel()
{
return sqrtf(xvel*xvel + yvel*yvel);
}
void Object2D::SetAngle(GLfloat theta)
{
angle = theta;
}
void Object2D::Draw()
{
image->Draw(xpos, ypos, angle); //draw is subject to the image draw flag
}
void Object2D::Draw(GLfloat width, GLfloat height)
{
image->Draw(xpos, ypos, width, height, angle); //draw is subject to the image draw flag
}
float Object2D::Distance(Object2D* ob1, Object2D* ob2)
{
return Distance(ob1, ob2->GetX(), ob2->GetY());
}
float Object2D::Distance(Object2D* ob1, GLfloat x, GLfloat y)
{
return sqrtf(powf(x - ob1->xpos, 2.0f) + powf(y - ob1->ypos, 2.0f));
}
void Object2D::On()
{
image->On();
}
void Object2D::Off()
{
image->Off();
}
int Object2D::DrawState()
{
return(image->DrawState());
}