From 64b218f65064794380a484f4d91fa55a28c7343e Mon Sep 17 00:00:00 2001 From: lajbel Date: Sun, 6 Oct 2024 11:08:11 -0300 Subject: [PATCH 1/3] fix: fromScreen not using viewport scale --- src/components/transform/pos.ts | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/components/transform/pos.ts b/src/components/transform/pos.ts index f9c9766a..8fb22d0a 100644 --- a/src/components/transform/pos.ts +++ b/src/components/transform/pos.ts @@ -1,6 +1,6 @@ import { isFixed } from "../../game/utils"; -import { getViewportScale } from "../../gfx"; -import { k } from "../../kaplay"; +import { getViewportScale, width } from "../../gfx"; +import { globalOpt, k } from "../../kaplay"; import { Vec2, vec2, type Vec2Args } from "../../math/math"; import type { Comp, GameObj } from "../../types"; import type { FixedComp } from "./fixed"; @@ -130,8 +130,10 @@ export function pos(...args: Vec2Args): PosComp { // Transform a world point (relative to the root) to a local point (relative to this) fromWorld(this: GameObj, p: Vec2): Vec2 { return this.parent - ? this.parent.transform.invert().multVec2(p).sub(this.pos) - : p.sub(this.pos); + ? this.parent.transform.invert().multVec2( + p.scale(1 / getViewportScale()), + ).sub(this.pos) + : p.sub(this.pos).scale(1 / getViewportScale()); }, // Transform a screen point (relative to the camera) to a local point (relative to this) @@ -167,7 +169,7 @@ export function pos(...args: Vec2Args): PosComp { const pos = this.toWorld(p); return isFixed(this) ? pos - : k.toScreen(pos); + : k.toScreen(pos.scale(1 / getViewportScale())); }, // Transform a screen point (relative to the camera) to a local point (relative to this) From ceaf7df605d0cf1063f34f23fc5a8a849422d17c Mon Sep 17 00:00:00 2001 From: lajbel Date: Sun, 6 Oct 2024 11:10:17 -0300 Subject: [PATCH 2/3] chore: remove unused imports --- src/components/transform/pos.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/components/transform/pos.ts b/src/components/transform/pos.ts index 8fb22d0a..c1b16f05 100644 --- a/src/components/transform/pos.ts +++ b/src/components/transform/pos.ts @@ -1,6 +1,6 @@ import { isFixed } from "../../game/utils"; -import { getViewportScale, width } from "../../gfx"; -import { globalOpt, k } from "../../kaplay"; +import { getViewportScale } from "../../gfx"; +import { k } from "../../kaplay"; import { Vec2, vec2, type Vec2Args } from "../../math/math"; import type { Comp, GameObj } from "../../types"; import type { FixedComp } from "./fixed"; From 738122690088ddd758abfe9f54b7a3e330cc4b40 Mon Sep 17 00:00:00 2001 From: lajbel Date: Sun, 6 Oct 2024 11:52:33 -0300 Subject: [PATCH 3/3] chore: use gfx.viewport pos to calculate the mousePos --- src/app/app.ts | 6 +++++- src/components/transform/pos.ts | 8 +++----- src/game/camera.ts | 10 +++++++--- 3 files changed, 15 insertions(+), 9 deletions(-) diff --git a/src/app/app.ts b/src/app/app.ts index 4408754a..d38e49da 100644 --- a/src/app/app.ts +++ b/src/app/app.ts @@ -21,6 +21,7 @@ import { } from "../utils"; import GAMEPAD_MAP from "../data/gamepad.json"; +import { gfx } from "../kaplay"; import { type ButtonBinding, type ButtonsDef, @@ -867,7 +868,10 @@ export const initApp = (opt: { const pd = opt.pixelDensity || 1; canvasEvents.mousemove = (e) => { - const mousePos = new Vec2(e.offsetX, e.offsetY); + const mousePos = new Vec2( + e.offsetX - gfx.viewport.x, + e.offsetY - gfx.viewport.y, + ); const mouseDeltaPos = new Vec2(e.movementX, e.movementY); if (isFullscreen()) { diff --git a/src/components/transform/pos.ts b/src/components/transform/pos.ts index c1b16f05..f9c9766a 100644 --- a/src/components/transform/pos.ts +++ b/src/components/transform/pos.ts @@ -130,10 +130,8 @@ export function pos(...args: Vec2Args): PosComp { // Transform a world point (relative to the root) to a local point (relative to this) fromWorld(this: GameObj, p: Vec2): Vec2 { return this.parent - ? this.parent.transform.invert().multVec2( - p.scale(1 / getViewportScale()), - ).sub(this.pos) - : p.sub(this.pos).scale(1 / getViewportScale()); + ? this.parent.transform.invert().multVec2(p).sub(this.pos) + : p.sub(this.pos); }, // Transform a screen point (relative to the camera) to a local point (relative to this) @@ -169,7 +167,7 @@ export function pos(...args: Vec2Args): PosComp { const pos = this.toWorld(p); return isFixed(this) ? pos - : k.toScreen(pos.scale(1 / getViewportScale())); + : k.toScreen(pos); }, // Transform a screen point (relative to the camera) to a local point (relative to this) diff --git a/src/game/camera.ts b/src/game/camera.ts index 2e8d2be7..4ad5f217 100644 --- a/src/game/camera.ts +++ b/src/game/camera.ts @@ -1,5 +1,5 @@ import { color, fixed, opacity, rect } from "../components"; -import { center, height, width } from "../gfx"; +import { center, getViewportScale, height, width } from "../gfx"; import { game } from "../kaplay"; import { type Color, rgb } from "../math/color"; import { type Mat4, type Vec2, vec2, type Vec2Args } from "../math/math"; @@ -50,9 +50,13 @@ export function shake(intensity: number = 12) { } export function toScreen(p: Vec2): Vec2 { - return game.cam.transform.multVec2(p); + const viewportScale = getViewportScale(); + + return game.cam.transform.multVec2(p).scale(viewportScale); } export function toWorld(p: Vec2): Vec2 { - return game.cam.transform.invert().multVec2(p); + const viewportFactor = 1 / getViewportScale(); + + return game.cam.transform.invert().multVec2(p).scale(viewportFactor); }