Replies: 1 comment
-
|
Interesting, might take in account! |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Problem
Kaplay uses three distinct coordinate spaces:
pos) - relative to parent objectworldPos) - relative to root/scenescreenPos,mousePos) - relative to camera/viewportCurrently, all of these are typed as
Vec2, which means it's easy to accidentally mix coordinate spaces. For example:These bugs are subtle and can be hard to catch, especially when objects are at the root level where local and world coordinates happen to coincide.
Proposed Solution: Branded Types
TypeScript's type system can provide compile-time safety using branded types or separate classes. Here's how it could work:
This provides compile-time safety - the type system prevents mixing coordinate spaces:
Implementation Options
Here are three approaches that could work in TypeScript:
Option 1: Branded Types (Recommended)
Pros: Simple, zero runtime cost, works with existing Vec2 class
Cons: Requires type assertions for conversions
Option 2: Separate Classes
Pros: More explicit, can add conversion methods
Cons: Slightly more complex, need to ensure instanceof checks work
Option 3: Generic Type Parameter
Pros: Single type definition, very flexible
Cons: More complex type signatures, harder to work with in some contexts
API Changes
The API would change from:
To:
Benefits
Migration Path
This could be introduced gradually:
worldPosTyped(): WorldVec2)Or introduce it as an opt-in feature via a TypeScript configuration option.
Questions
Real-World Impact
This type of coordinate space bug is particularly insidious because:
Adding type safety would catch these issues at compile time, making Kaplay more robust and easier to use correctly.
Related: This would also help clarify the coordinate space expectations for
Math.Rect,Math.Circle, etc. - currently it's ambiguous whether they expect local or world coordinates depending on usage context.Beta Was this translation helpful? Give feedback.
All reactions