-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPhysicalParameter.java
More file actions
178 lines (146 loc) · 3.08 KB
/
PhysicalParameter.java
File metadata and controls
178 lines (146 loc) · 3.08 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
//3D Physical Parmeter class
public class PhysicalParameter
{
//position
public float pX, pY, pZ;
//acelleration
private float aX, aY, aZ;
//speed
private float sX, sY, sZ;
private Point destination;
//properties
private float maxSpeed, spring, drag;
PhysicalParameter(float pX, float pY, float pZ, float maxSpeed, float spring, float drag)
{
this.pX = pX;
this.pY = pY;
this.pZ = pZ;
this.aX = aX;
this.aY = aY;
this.aZ = aZ;
this.maxSpeed = maxSpeed;
this.spring = spring;
this.drag = drag;
this.destination = null;
}
public void setPosition(float x, float y, float z)
{
this.pX = x;
this.pY = y;
this.pZ = z;
}
public void setMaxSpeed(float value)
{
this.maxSpeed = value;
}
public void setDrag(float value)
{
this.drag = value;
}
public void setSpring(float value)
{
this.spring = value;
}
public void setDestination(Point d)
{
this.destination = d;
}
public Point getDestination()
{
return this.destination;
}
public void clearDestination()
{
this.destination = null;
}
public float distanceFromDestination()
{
if(this.getDestination() != null)
{
return this.getPosition().distance(this.getDestination());
}
else
{
return (float) -1;
}
}
public void setPosition(Point a)
{
this.pX = a.x;
this.pY = a.y;
this.pZ = a.z;
}
private float calculateDragComponent(float a)
{
float d = 0;
if (a == 0) return 0;
if(a >= drag)
{
d = -drag;
}
else if(a <= -drag)
{
d = drag;
}
else
{
d = -a;
}
return d;
}
public Point getPosition()
{
return new Point(this.pX, this.pY, this.pZ);
}
public void step(float fX, float fY, float fZ)
{
if(this.destination != null)
{
Point force = new Point(fX, fY, fZ);
Point position = new Point(pX, pY, pZ);
float destDistance = position.distance(this.destination);
if (destDistance < 6)
{
this.clearDestination();
}
else
{
float destStrength = (destDistance/5);
if(destStrength > 13)
{
destStrength = 13;
}
Point destAtraction = destination.subtract(position).getNormalizedVector().multiply(destStrength);
Point finalStrength = force.add(destAtraction);
fX = finalStrength.x;
fY = finalStrength.y;
fZ = finalStrength.z;
}
}
float dX, dY, dZ;
dX = this.calculateDragComponent(sX);
dY = this.calculateDragComponent(sY);
dZ = this.calculateDragComponent(sZ);
aX = fX;
aY = fY;
aZ = fZ;
sX += aX + dX;
sY += aY + dY;
sZ += aZ + dZ;
if(sX > maxSpeed)
sX = maxSpeed;
if(sY > maxSpeed)
sY = maxSpeed;
if(sZ > maxSpeed)
sZ = maxSpeed;
if(sX < -maxSpeed)
sX = -maxSpeed;
if(sY < -maxSpeed)
sY = -maxSpeed;
if(sZ < -maxSpeed)
sZ = -maxSpeed;
pX += sX;
pY += sY;
pZ += sZ;
}
}