-
Notifications
You must be signed in to change notification settings - Fork 0
/
MuscularDemo.pde
210 lines (165 loc) · 5.09 KB
/
MuscularDemo.pde
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
// =============================================
// DEMO MAIN (for the Muscular Model Framework)
// by Guillem Benavent (Updated: 23/8/2023)
// For Research Project at Taradell School
// =============================================
// Handles the interactive demo for multiple muscular models
// =============================================
//General libraries
import g4p_controls.*;
import java.awt.Font;
int FRAME_RATE = 60;
int currentModel;
int selectedModel;
int simulationLevel;
int modelVariantIndex;
int modelWeightIndex;
int muscleGroupIndex;
float activation;
int pulseLastZeroTime = 0;
boolean activationPulse = false;
boolean activationPulseEnabled = false;
// Window objects (apart from default)
GWindow graphWindow, controlWindow;
// Model object
MuscularModel model;
//Graph objects
LiveGraph graph1, graph2, graph3, graph4;
// Control objects
GButton playPauseButton, reloadButton;
GDropList modelDropList, variantDropList, levelDropList, weightDropList, muscleGroupDropList;
GSlider activationSlider;
GButton saveGraph1, saveGraph2, saveGraph3, saveGraph4;
void settings()
{
//set size of Main Window
size(displayHeight*3/5, displayHeight*3/5);
smooth();
}
void setup()
{
// SetUp Box2D
box2d = new Box2DProcessing(this);
box2d.createWorld();
// Set default currents
currentModel = 1;
selectedModel = 1;
simulationLevel = MuscularModel.SIMULATION_LEVEL_WEIGHTS;
modelVariantIndex = 0;
modelWeightIndex = 1;
// Load current MuscularModel
reloadSelectedModel();
// Setup Windows
setupAllWindows();
setupGraphsForCurrentModel();
setupControlWindow();
}
void draw()
{
background(50);
if (model.isSimulationRunning==true)
{
model.step();
if (activationPulse)
{
pulseLastZeroTime = millis();
model.setMuscleGroupActivation(muscleGroupIndex, 1);
activationPulse = false;
}
else
{
model.setMuscleGroupActivation(muscleGroupIndex, activationSlider.getValueF());
//model.setMuscleGroupTension(muscleGroupIndex, activationSlider.getValueF()*500);
}
}
model.display();
addDataPointsToGraphs();
}
// Model Functions ============================
void reloadSelectedModel()
{
if (selectedModel == 0)
{
model = new SampleModel(this, 0.0, 0.3, 75, FRAME_RATE, simulationLevel, modelVariantIndex, modelWeightIndex);
model.setGridProperties(0.05, 10);
model.setGravity(new Vec2(0, -9.8));
}
else if (selectedModel == 1)
{
model = new ArmModel(this, -0.20, 0.30, 50, FRAME_RATE, simulationLevel, modelVariantIndex, modelWeightIndex);
model.setGridProperties(0.05, 10);
model.setGravity(new Vec2(0, -9.8));
}
else if (selectedModel == 2)
{
model = new LegModel(this, -0.30, 0.30, 50, FRAME_RATE, simulationLevel, modelVariantIndex, modelWeightIndex);
model.setGridProperties(0.05, 10);
model.setGravity(new Vec2(0, -9.8));
}
}
// Windows Functions ===========================
void setupAllWindows()
{
//General setup
frameRate(FRAME_RATE);
// Set Default Window
surface.setLocation(-7, 0);
surface.setTitle("Simulation Panel");
textSize(100); //stop blurry text
// Create and Set Graph Window
graphWindow = GWindow.getWindow(this, "Graph Panel", displayHeight*3/5-5, 0, displayWidth-displayHeight*3/5, displayHeight-70, JAVA2D);
graphWindow.addDrawHandler(this, "drawGraphPanel");
graphWindow.textSize(100); //stop blurry text
// Create and Set Control Window
int controlWindowHeight = displayHeight*2/5-100;
int controlWindowWidth = displayHeight*3/5;
controlWindow = GWindow.getWindow(this, "Control Panel", -7, displayHeight*3/5+30, controlWindowWidth, controlWindowHeight, JAVA2D);
controlWindow.addDrawHandler(this, "drawControlPanel");
controlWindow.textSize(100); //stop blurry text
}
public void drawGraphPanel(PApplet window, GWinData data)
{
window.background(50);
if (this.graph1 == null)
{
delay(200);
}
graph1.display();
graph2.display();
graph3.display();
graph4.display();
}
public void drawControlPanel(PApplet window, GWinData data)
{
window.background(70);
// Draw Text Control Panel
window.textSize(20);
window.text("Model", 55, 90);
window.text("Variant", 210, 90);
window.text("Pes", 390, 90);
window.text("Simulació", 525, 90);
window.text("Múscul Controlat", 10, 165);
}
// Simulation Events =======================================
void keyPressed()
{
// Triggers an activation pulse
if (key == ' ' && activationPulseEnabled) {activationPulse = true; activationPulseEnabled = false;}
}
void keyReleased()
{
// Re-enable activation pulse triggering
if (key == ' ') {activationPulseEnabled = true;}
}
void mouseWheel(MouseEvent event)
{
// Zooms Simulation content
float wheelChange = event.getCount();
model.incrementDisplayScale(-2*wheelChange);
}
void mousePressed() {
// Reset Simulation Zoom
if(mouseButton == RIGHT){
model.resetDisplayScale();
}
}