-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLink.java
More file actions
58 lines (39 loc) · 1.46 KB
/
Link.java
File metadata and controls
58 lines (39 loc) · 1.46 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
public class Link {
public PointMass p0;
public PointMass p1;
private int restingDistance;
private int tearingDistance;
public Link( PointMass p0, PointMass p1, int linkLength, int linkStrength ){
this.p0 = p0;
this.p1 = p1;
restingDistance = linkLength;
tearingDistance = linkLength*linkStrength;
}
public boolean solve() {
float distX = p0.x - p1.x;
float distY = p0.y - p1.y;
float linearDist = sqrt( distX*distX + distY*distY );
float difference = linearDist > 0 ? (restingDistance-linearDist)/linearDist : 1; // 1 is a fallback if dist == 0
float translateX = distX * .6f * difference;
float translateY = distY * .6f * difference;
// if one point is pinned, the other needs to translate more to stay in line
// if both are pinned, it doesn't matter, but this is simplest
if( p0.pinned || p1.pinned ){
translateX*=2;
translateY*=2;
}
p0.translate( translateX, translateY );
p1.translate( -translateX, -translateY );
return linearDist > tearingDistance; // shouldTear
}
public void draw( Render r ){
r.drawColoredLine( (int)p0.x, (int)p0.y, p0.c, (int)p1.x, (int)p1.y, p1.c);
}
// calculates the sqrt of a number
public float sqrt( float x ){
return x * Float.intBitsToFloat( 0x5f3759d5 - (Float.floatToIntBits(x) >> 1));
}
// triangle is defined by three points, and made up by three links which are
// each made up of 2 points
// PointMasses constructed first, then links, then triangles
}