Skip to content

Commit 67746bb

Browse files
committed
Scale trails to viewport + Cleanup
1 parent 01fc17c commit 67746bb

File tree

2 files changed

+42
-23
lines changed

2 files changed

+42
-23
lines changed

src/progressed/graphics/draw3d/Perspective.java

+11-11
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
public class Perspective{
1313
private static final Vec2 offsetPos = new Vec2();
14+
private static final Vec3 scalingPos = new Vec3();
1415
/** Viewport offset from the camera height in world units. */
1516
public static float viewportOffset = 16f;
1617
/** Field of View in degrees */
@@ -46,16 +47,9 @@ public static Vec2 drawPos(float x, float y, float z){
4647
//viewport
4748
float vw = viewportSize.x, vh = viewportSize.y;
4849
float cx = camera.position.x, cy = camera.position.y;
49-
float cz = cameraZ;
50-
51-
x -= cx;
52-
y -= cy;
53-
z = cz - z;
54-
55-
float vx = x / z * viewportOffset,
56-
vy = y / z * viewportOffset;
50+
Vec3 scaled = scaleToViewport(x, y, z);
5751

58-
offsetPos.set(vx / vw * camera.width, vy / vh * camera.height).add(cx, cy);
52+
offsetPos.set(scaled.x / vw * camera.width, scaled.y / vh * camera.height).add(cx, cy);
5953
return offsetPos;
6054
}
6155

@@ -122,7 +116,7 @@ public static void viewportSize(){
122116
}
123117
}
124118

125-
public static float dstToViewport(float x, float y, float z){
119+
public static Vec3 scaleToViewport(float x, float y, float z){
126120
float cx = camera.position.x, cy = camera.position.y;
127121
float cz = cameraZ;
128122

@@ -134,7 +128,13 @@ public static float dstToViewport(float x, float y, float z){
134128
vy = y / zz * viewportOffset;
135129
float vz = viewportZ();
136130

137-
return Math3D.dst(x, y, z, vx, vy, vz); //Distance between viewport and pos
131+
return scalingPos.set(vx, vy, vz);
132+
}
133+
134+
public static float dstToViewport(float x, float y, float z){
135+
Vec3 scaled = scaleToViewport(x, y, z);
136+
float cx = camera.position.x, cy = camera.position.y;
137+
return scaled.dst(x - cx, y - cy, z);
138138
}
139139

140140
/**

src/progressed/graphics/trails/ZTrail.java

+31-12
Original file line numberDiff line numberDiff line change
@@ -73,35 +73,53 @@ public void draw(Color color, float width, boolean fade){
7373
float lastAngle = 0;
7474
float[] items = points.items;
7575
float size = width / (int)(points.size / 4);
76+
float vz = Perspective.viewportZ();
77+
boolean calcLast = true;
7678

7779
for(int i = 0; i < points.size; i += 4){
7880
float px1 = items[i], py1 = items[i + 1], z1 = items[i + 3];
81+
float z2 = i < points.size - 4 ? items[i + 4 + 3] : lastZ;
82+
83+
if(z1 > vz && z2 > vz){
84+
calcLast = true;
85+
continue;
86+
}
87+
88+
if(z1 > vz){ //Scale to near plane
89+
Vec3 pos = Perspective.scaleToViewport(px1, py1, z1);
90+
px1 = pos.x;
91+
py1 = pos.y;
92+
z1 = pos.z;
93+
}
94+
7995
Vec2 pos1 = Perspective.drawPos(px1, py1, z1);
8096
float x1 = pos1.x, y1 = pos1.y, w1 = Perspective.scale(items[i], items[i + 1], z1);
81-
float x2, y2, px2, py2, w2, z2;
97+
float px2, py2, x2, y2, w2;
8298

8399
//last position is always lastX/Y/W
84100
if(i < points.size - 4){
85101
px2 = items[i + 4];
86102
py2 = items[i + 4 + 1];
87-
z2 = items[i + 4 + 3];
88-
Vec2 pos2 = Perspective.drawPos(px2, py2, z2);
89-
x2 = pos2.x;
90-
y2 = pos2.y;
91-
w2 = Perspective.scale(items[i + 4], items[i + 4 + 1], z2);
92103
}else{
93104
px2 = lastX;
94105
py2 = lastY;
95-
z2 = lastZ;
96-
Vec2 pos2 = Perspective.drawPos(px2, py2, z2);
97-
x2 = pos2.x;
98-
y2 = pos2.y;
99-
w2 = Perspective.scale(lastX, lastY, z2);
100106
}
101107

108+
if(z2 > vz){ //Scale to near plane
109+
Vec3 pos = Perspective.scaleToViewport(px2, py2, z2);
110+
px2 = pos.x;
111+
py2 = pos.y;
112+
z2 = pos.z;
113+
}
114+
115+
Vec2 pos2 = Perspective.drawPos(px2, py2, z2);
116+
x2 = pos2.x;
117+
y2 = pos2.y;
118+
w2 = Perspective.scale(px2, py2, z2);
119+
102120
float a2 = -Angles.angleRad(x1, y1, x2, y2);
103121
//end of the trail (i = 0) has the same angle as the next.
104-
float a1 = i == 0 ? a2 : lastAngle;
122+
float a1 = calcLast ? a2 : lastAngle;
105123
if(w1 <= 0.001f || w2 <= 0.001f) continue;
106124

107125
float
@@ -125,6 +143,7 @@ public void draw(Color color, float width, boolean fade){
125143
);
126144

127145
lastAngle = a2;
146+
calcLast = false;
128147
}
129148

130149
Draw.color();

0 commit comments

Comments
 (0)