Skip to content

Commit b9da9c1

Browse files
committed
fix: resolve aspect ratio distortion and simplify hit detection to body only
1 parent d651f02 commit b9da9c1

1 file changed

Lines changed: 19 additions & 9 deletions

File tree

  • example/src/main/java/dev/eatgrapes/live2d/example

example/src/main/java/dev/eatgrapes/live2d/example/Main.java

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,9 @@ private void init() {
4646
try (MemoryStack s = MemoryStack.stackPush()) {
4747
IntBuffer wb = s.mallocInt(1), hb = s.mallocInt(1);
4848
glfwGetWindowSize(win, wb, hb);
49-
float nx = (float) (x / (wb.get(0) / 2.0) - 1.0);
49+
float aspect = (float) wb.get(0) / hb.get(0);
50+
// Map screen to NDC then to Model Space aspect
51+
float nx = (float) (x / (wb.get(0) / 2.0) - 1.0) * aspect;
5052
float ny = (float) (1.0 - y / (hb.get(0) / 2.0));
5153
if (model != null) model.setDragging(nx, ny);
5254
}
@@ -59,15 +61,13 @@ private void init() {
5961
IntBuffer wb = s.mallocInt(1), hb = s.mallocInt(1);
6062
glfwGetCursorPos(win, xb, yb);
6163
glfwGetWindowSize(win, wb, hb);
62-
float nx = (float) (xb.get(0) / (wb.get(0) / 2.0) - 1.0);
64+
65+
float aspect = (float) wb.get(0) / hb.get(0);
66+
float nx = (float) (xb.get(0) / (wb.get(0) / 2.0) - 1.0) * aspect;
6367
float ny = (float) (1.0 - yb.get(0) / (hb.get(0) / 2.0));
6468

65-
// Check Head first
66-
if (model.isHit("ArtMesh12", nx, ny)) {
67-
System.out.println("Head!");
68-
model.startMotion(motionCache.get("m01"), 3, null);
69-
} else if (model.isHit("HitArea", nx, ny)) {
70-
System.out.println("Body!");
69+
if (model.isHit("HitArea", nx, ny)) {
70+
System.out.println("Hit Body!");
7171
model.startMotion(motionCache.get("m04"), 3, null);
7272
}
7373
} catch (Exception e) { e.printStackTrace(); }
@@ -95,11 +95,21 @@ private void setup() throws Exception {
9595
private void loop() {
9696
while (!glfwWindowShouldClose(window)) {
9797
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
98+
9899
try (MemoryStack s = MemoryStack.stackPush()) {
99100
IntBuffer w = s.mallocInt(1), h = s.mallocInt(1);
100101
glfwGetWindowSize(window, w, h);
101102
glViewport(0, 0, w.get(0), h.get(0));
103+
104+
float aspect = (float) w.get(0) / h.get(0);
105+
// Simple ortho projection to maintain aspect ratio
106+
for (int i = 0; i < 16; i++) mvp[i] = 0;
107+
mvp[0] = 1.0f / aspect;
108+
mvp[5] = 1.0f;
109+
mvp[10] = 1.0f;
110+
mvp[15] = 1.0f;
102111
}
112+
103113
model.update(0.016f);
104114
model.draw(mvp);
105115
glfwSwapBuffers(window);
@@ -140,4 +150,4 @@ private int loadTex(String p) throws Exception {
140150
}
141151

142152
public static void main(String[] args) throws Exception { new Main().run(); }
143-
}
153+
}

0 commit comments

Comments
 (0)