Skip to content

Commit 08672e3

Browse files
committed
Move coord convert from c++ => go (invert y)
1 parent 2748958 commit 08672e3

2 files changed

Lines changed: 172 additions & 4 deletions

File tree

Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
package enginewrap
2+
3+
// This file provides coordinate system adapters for all managers.
4+
// SPX uses Y-axis pointing up, while Godot uses Y-axis pointing down.
5+
// All coordinate conversions are handled here by overriding manager methods.
6+
7+
import (
8+
. "github.com/goplus/spbase/mathf"
9+
gdx "github.com/goplus/spx/v2/pkg/gdspx/pkg/engine"
10+
)
11+
12+
// flipY flips the Y coordinate to convert between SPX and Godot coordinate systems
13+
func flipY(pos Vec2) Vec2 {
14+
return Vec2{X: pos.X, Y: -pos.Y}
15+
}
16+
17+
// ============================================================================
18+
// SpriteMgr Coordinate Adapters
19+
// ============================================================================
20+
21+
func (pself *SpriteMgrImpl) SetPosition(obj gdx.Object, pos Vec2) {
22+
pself.spriteMgrImpl.SetPosition(obj, flipY(pos))
23+
}
24+
25+
func (pself *SpriteMgrImpl) GetPosition(obj gdx.Object) Vec2 {
26+
return flipY(pself.spriteMgrImpl.GetPosition(obj))
27+
}
28+
29+
func (pself *SpriteMgrImpl) SetChildPosition(obj gdx.Object, path string, pos Vec2) {
30+
pself.spriteMgrImpl.SetChildPosition(obj, path, flipY(pos))
31+
}
32+
33+
func (pself *SpriteMgrImpl) GetChildPosition(obj gdx.Object, path string) Vec2 {
34+
return flipY(pself.spriteMgrImpl.GetChildPosition(obj, path))
35+
}
36+
37+
func (pself *SpriteMgrImpl) SetVelocity(obj gdx.Object, velocity Vec2) {
38+
pself.spriteMgrImpl.SetVelocity(obj, flipY(velocity))
39+
}
40+
41+
func (pself *SpriteMgrImpl) GetVelocity(obj gdx.Object) Vec2 {
42+
return flipY(pself.spriteMgrImpl.GetVelocity(obj))
43+
}
44+
45+
func (pself *SpriteMgrImpl) SetPivot(obj gdx.Object, pivot Vec2) {
46+
pself.spriteMgrImpl.SetPivot(obj, flipY(pivot))
47+
}
48+
49+
func (pself *SpriteMgrImpl) GetPivot(obj gdx.Object) Vec2 {
50+
return flipY(pself.spriteMgrImpl.GetPivot(obj))
51+
}
52+
53+
func (pself *SpriteMgrImpl) CheckCollisionWithPoint(obj gdx.Object, point Vec2, isTrigger bool) bool {
54+
return pself.spriteMgrImpl.CheckCollisionWithPoint(obj, flipY(point), isTrigger)
55+
}
56+
57+
// ============================================================================
58+
// SceneMgr Coordinate Adapters
59+
// ============================================================================
60+
61+
func (pself *SceneMgrImpl) CreateRenderSprite(texturePath string, pos Vec2, degree float64, scale Vec2, zindex int64, pivot Vec2) gdx.Object {
62+
return pself.sceneMgrImpl.CreateRenderSprite(texturePath, flipY(pos), degree, scale, zindex, flipY(pivot))
63+
}
64+
65+
func (pself *SceneMgrImpl) CreateStaticSprite(texturePath string, pos Vec2, degree float64, scale Vec2, zindex int64, pivot Vec2, colliderType int64, colliderPivot Vec2, colliderParams gdx.Array) gdx.Object {
66+
return pself.sceneMgrImpl.CreateStaticSprite(texturePath, flipY(pos), degree, scale, zindex, flipY(pivot), colliderType, flipY(colliderPivot), colliderParams)
67+
}
68+
69+
// ============================================================================
70+
// PhysicMgr Coordinate Adapters
71+
// ============================================================================
72+
73+
func (pself *PhysicMgrImpl) Raycast(from Vec2, to Vec2, collisionMask int64) gdx.Object {
74+
return pself.physicMgrImpl.Raycast(flipY(from), flipY(to), collisionMask)
75+
}
76+
77+
// RaycastWithDetails returns array: [collide, sprite_gid, pos.x, pos.y, normal.x, normal.y]
78+
// This conversion should be handled at upper layer due to gdx.Array access limitations
79+
func (pself *PhysicMgrImpl) RaycastWithDetails(from Vec2, to Vec2, ignoreSprites gdx.Array, collisionMask int64, collideWithAreas bool, collideWithBodies bool) gdx.Array {
80+
return pself.physicMgrImpl.RaycastWithDetails(flipY(from), flipY(to), ignoreSprites, collisionMask, collideWithAreas, collideWithBodies)
81+
}
82+
83+
func (pself *PhysicMgrImpl) CheckCollisionRect(pos Vec2, size Vec2, collisionMask int64) gdx.Array {
84+
return pself.physicMgrImpl.CheckCollisionRect(flipY(pos), size, collisionMask)
85+
}
86+
87+
func (pself *PhysicMgrImpl) CheckCollisionCircle(pos Vec2, radius float64, collisionMask int64) gdx.Array {
88+
return pself.physicMgrImpl.CheckCollisionCircle(flipY(pos), radius, collisionMask)
89+
}
90+
91+
// ============================================================================
92+
// InputMgr Coordinate Adapters
93+
// ============================================================================
94+
95+
func (pself *InputMgrImpl) GetMousePos() Vec2 {
96+
return flipY(pself.inputMgrImpl.GetMousePos())
97+
}
98+
99+
// ============================================================================
100+
// DebugMgr Coordinate Adapters
101+
// ============================================================================
102+
103+
func (pself *DebugMgrImpl) DebugDrawCircle(pos Vec2, radius float64, color Color) {
104+
pself.debugMgrImpl.DebugDrawCircle(flipY(pos), radius, color)
105+
}
106+
107+
func (pself *DebugMgrImpl) DebugDrawRect(pos Vec2, size Vec2, color Color) {
108+
pself.debugMgrImpl.DebugDrawRect(flipY(pos), size, color)
109+
}
110+
111+
func (pself *DebugMgrImpl) DebugDrawLine(from Vec2, to Vec2, color Color) {
112+
pself.debugMgrImpl.DebugDrawLine(flipY(from), flipY(to), color)
113+
}
114+
115+
// ============================================================================
116+
// TilemapMgr Coordinate Adapters
117+
// ============================================================================
118+
119+
func (pself *TilemapMgrImpl) PlaceTile(pos Vec2, texturePath string) {
120+
pself.tilemapMgrImpl.PlaceTile(flipY(pos), texturePath)
121+
}
122+
123+
func (pself *TilemapMgrImpl) PlaceTileWithLayer(pos Vec2, texturePath string, layerIndex int64) {
124+
pself.tilemapMgrImpl.PlaceTileWithLayer(flipY(pos), texturePath, layerIndex)
125+
}
126+
127+
func (pself *TilemapMgrImpl) EraseTile(pos Vec2) {
128+
pself.tilemapMgrImpl.EraseTile(flipY(pos))
129+
}
130+
131+
func (pself *TilemapMgrImpl) EraseTileWithLayer(pos Vec2, layerIndex int64) {
132+
pself.tilemapMgrImpl.EraseTileWithLayer(flipY(pos), layerIndex)
133+
}
134+
135+
func (pself *TilemapMgrImpl) GetTile(pos Vec2) string {
136+
return pself.tilemapMgrImpl.GetTile(flipY(pos))
137+
}
138+
139+
func (pself *TilemapMgrImpl) GetTileWithLayer(pos Vec2, layerIndex int64) string {
140+
return pself.tilemapMgrImpl.GetTileWithLayer(flipY(pos), layerIndex)
141+
}
142+
143+
func (pself *TilemapMgrImpl) SetLayerOffset(index int64, offset Vec2) {
144+
pself.tilemapMgrImpl.SetLayerOffset(index, flipY(offset))
145+
}
146+
147+
func (pself *TilemapMgrImpl) GetLayerOffset(index int64) Vec2 {
148+
return flipY(pself.tilemapMgrImpl.GetLayerOffset(index))
149+
}
150+
151+
// ============================================================================
152+
// NavigationMgr Coordinate Adapters
153+
// ============================================================================
154+
155+
// FindPath returns array: [x0, y0, x1, y1, x2, y2, ...]
156+
// This conversion should be handled at upper layer due to gdx.Array access limitations
157+
func (pself *NavigationMgrImpl) FindPath(from Vec2, to Vec2, withJump bool) gdx.Array {
158+
var arr = pself.navigationMgrImpl.FindPath(flipY(from), flipY(to), withJump)
159+
result := arr.([]float32)
160+
if result == nil {
161+
return nil
162+
}
163+
// flipY to convert godot coord to spx coord
164+
for i := 0; i+1 < len(result); i += 2 {
165+
result[i+1] = -result[i+1]
166+
}
167+
return result
168+
}

physic.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,9 @@ func (p *rayCastResult) ToArray() engine.Array {
8585
}
8686
ary[1] = p.SpriteId
8787
ary[2] = engine.ConvertToInt64(p.PosX)
88-
ary[3] = engine.ConvertToInt64(p.PosY)
88+
ary[3] = -engine.ConvertToInt64(p.PosY) // here need flipY to convert spx coord to godot coord
8989
ary[4] = engine.ConvertToInt64(p.NormalX)
90-
ary[5] = engine.ConvertToInt64(p.NormalY)
90+
ary[5] = -engine.ConvertToInt64(p.NormalY)
9191
return ary
9292
}
9393
func tryRaycastResult(ary engine.Array) (*rayCastResult, error) {
@@ -103,9 +103,9 @@ func tryRaycastResult(ary engine.Array) (*rayCastResult, error) {
103103
p.Hited = dataAry[0] != 0
104104
p.SpriteId = dataAry[1]
105105
p.PosX = engine.ConvertToFloat64(dataAry[2])
106-
p.PosY = engine.ConvertToFloat64(dataAry[3])
106+
p.PosY = -engine.ConvertToFloat64(dataAry[3]) // here need flipY to convert to godot coord to spx coord
107107
p.NormalX = engine.ConvertToFloat64(dataAry[4])
108-
p.NormalY = engine.ConvertToFloat64(dataAry[5])
108+
p.NormalY = -engine.ConvertToFloat64(dataAry[5])
109109
return p, nil
110110
}
111111
func raycast(from, to mathf.Vec2, ignoreSprites []int64, mask int64) *rayCastResult {

0 commit comments

Comments
 (0)