-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathWeight.pde
More file actions
138 lines (116 loc) · 4.38 KB
/
Weight.pde
File metadata and controls
138 lines (116 loc) · 4.38 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
// =============================================
// CLASS WEIGHT (from the Muscular Model Framework)
// by Guillem Benavent (Updated: 08/7/2023)
// For Research Project at Taradell School
// =============================================
// A weight uses a Box2D body to model a phisical metal disc.
// It is defined by its location (x,y) and it's measurements (length and width).
// It's displayed as a blue circle with the measurements set from its mass.
// =============================================
class Weight
{
color WEIGHT_COLOR = color(#7F82BC);
float WEIGHT_FRICTION = 8;
float WEIGHT_ELASTICITY = 0.5;
float WEIGHT_DENSITY = 7874; // Iron density: 7874 kg/m3
// Bone Properties
public Body body;
float posX;
float posY;
float sizeRadius;
float sizeDepth = 0.10;
float mass;
String name;
Weight(float posX, float posY, float mass)
{
this(new String("Unknown Weight #")+random(100,999), posX, posY, mass);
}
Weight(String name, float posX, float posY, float mass)
{
this.name = name;
this.posX = posX;
this.posY = posY;
this.mass = mass;
this.sizeRadius = sqrt(mass/(WEIGHT_DENSITY * sizeDepth * PI));
if (this.mass>0)
{
// Create BodyDef
BodyDef bodyDef = new BodyDef();
bodyDef.type = BodyType.DYNAMIC;
bodyDef.position.set(posX, posY);
bodyDef.linearDamping = WEIGHT_FRICTION;
bodyDef.angularDamping = WEIGHT_FRICTION;
body = box2d.createBody(bodyDef);
// Create Shape
CircleShape shape = new CircleShape();
shape.m_radius = this.sizeRadius;
//^ setAsBox(w,h) > [-w,h][+w,h][+w,-h][-w,-h]
// Create fixture with shape
FixtureDef fixDef = new FixtureDef();
fixDef.shape = shape;
fixDef.density = WEIGHT_DENSITY*sizeDepth; // Divided by depth to get the 2d density kg/m2
fixDef.friction = WEIGHT_FRICTION;
fixDef.restitution = WEIGHT_ELASTICITY;
fixDef.isSensor = true;
// Attach Shape to Body through Fixture
body.createFixture(fixDef);
}
println("Weight "+name+" Created: Weight = "+body.m_mass+" kg");
}
void display(float displayScale)
{
//Vec2 pix = box2d.getBodyPixelCoord(body);
Vec2 posCenter = body.getWorldCenter();
Vec2 pix = new Vec2(box2d.coordWorldToPixels(posCenter.x*displayScale, posCenter.y*displayScale));
float pixDiameter = box2d.scalarWorldToPixels(this.sizeRadius*2)*displayScale;
fill(WEIGHT_COLOR);
noStroke();
ellipse(pix.x, pix.y, pixDiameter, pixDiameter);
fill(50);
ellipse(pix.x, pix.y, box2d.scalarWorldToPixels(0.0254)*displayScale, box2d.scalarWorldToPixels(0.0254)*displayScale);
}
void printDetails(String name)
{
println("Bone Details of '"+name+"'");
int fixtureCounter=0;
for (Fixture f = body.getFixtureList(); f!=null ; f = f.getNext())
{
fixtureCounter++;
println("- Fixture #"+fixtureCounter+":");
ShapeType shapeType = f.getType();
if ( shapeType == ShapeType.POLYGON )
{
PolygonShape polygonShape = (PolygonShape)f.getShape();
int vertexCount = polygonShape.getVertexCount();
println(" - Type: POLYGON ("+vertexCount+" vertices)");
// Relative Corners
float relSumX = 0;
float relSumY = 0;
print(" - Relative Vertices: ");
for (int v=0; v<vertexCount; v++)
{
Vec2 vertex = polygonShape.getVertex(v);
print("("+(vertex.x)+","+(vertex.y)+") ");
relSumX = relSumX+vertex.x;
relSumY = relSumY+vertex.y;
}
println();
println(" - Relative Average Center: ("+(relSumX/vertexCount)+", "+(relSumY/vertexCount)+")");
// Absolute Corners
float absSumX = 0;
float absSumY = 0;
print(" - Absolute Vertices: ");
for (int v=0; v<vertexCount; v++)
{
Vec2 vertex = polygonShape.getVertex(v);
print("("+(posX+vertex.x)+","+(posY+vertex.y)+") ");
absSumX = absSumX+posX+vertex.x;
absSumY = absSumY+posY+vertex.y;
}
println();
println(" - Absolute Average Center: ("+(absSumX/vertexCount)+", "+(absSumY/vertexCount)+")");
}
}
println();
}
}