I'm making a 2d top down game with lwjgl and am having trouble converting my 2d screen coordinates to 2d world coordinates.
Here's what I have so far
private Vector2f toWorldCoords(Vector4f eyeCoords) {
Matrix4f invertedView = Matrix4f.invert(MathUtil.createViewMatrix(camera), null);
Vector4f rayWorld = Matrix4f.transform(invertedView, eyeCoords, null);
Vector2f mouseRay = new Vector2f(rayWorld.x, rayWorld.y);
mouseRay.normalise();
return mouseRay;
}
public Vector2f screenToWorld(Vector2f mouseCoords) {
Vector2f normalizedCoords = CursorHandler.getNormalisedDeviceCoordinates();
Vector4f clipCoords = new Vector4f(normalizedCoords.x, normalizedCoords.y, -1.0f, 1.0f);
Vector2f worldRay = toWorldCoords(clipCoords);
return worldRay;
}
I understand it's been asked before, and I've tried following some 3d tutorials that deal with the same topic, but so far I haven't been able to use the code successfully.
Thanks