From 41967b11f7a92da5c910c024e06b295bed556063 Mon Sep 17 00:00:00 2001 From: genxium Date: Sat, 12 Nov 2022 11:20:16 +0800 Subject: [PATCH] Updated CLI unit tests. --- collider_visualizer/worldColliderDisplay.go | 42 +++++++++++-------- frontend/assets/resources/map/simple/map.tmx | 10 ++++- .../assets/scripts/collision_test_nodejs.js | 39 ----------------- .../scripts/collision_test_nodejs.js.meta | 9 ---- frontend/collision_test_nodejs.js | 39 +++++++++++++++++ 5 files changed, 71 insertions(+), 68 deletions(-) delete mode 100644 frontend/assets/scripts/collision_test_nodejs.js delete mode 100644 frontend/assets/scripts/collision_test_nodejs.js.meta create mode 100644 frontend/collision_test_nodejs.js diff --git a/collider_visualizer/worldColliderDisplay.go b/collider_visualizer/worldColliderDisplay.go index d1f4a40..2d32b3a 100644 --- a/collider_visualizer/worldColliderDisplay.go +++ b/collider_visualizer/worldColliderDisplay.go @@ -2,6 +2,7 @@ package main import ( . "dnmshared" + . "dnmshared/sharedprotos" "fmt" "github.com/hajimehoshi/ebiten/v2" "github.com/solarlune/resolv" @@ -37,7 +38,7 @@ func NewWorldColliderDisplay(game *Game, stageDiscreteW, stageDiscreteH, stageTi space := resolv.NewSpace(int(spaceW), int(spaceH), 16, 16) for i, playerPos := range playerPosList.Eles { playerCollider := GenerateRectCollider(playerPos.X, playerPos.Y, playerColliderRadius*2, playerColliderRadius*2, spaceOffsetX, spaceOffsetY, "Player") // [WARNING] Deliberately not using a circle because "resolv v0.5.1" doesn't yet align circle center with space cell center, regardless of the "specified within-object offset" - Logger.Info(fmt.Sprintf("Player Collider#%d: playerPos.X=%v, playerPos.Y=%v, radius=%v, spaceOffsetX=%v, spaceOffsetY=%v, shape=%v; calibrationCheckX=playerPos.X-radius+spaceOffsetX=%v", i, playerPos.X, playerPos.Y, playerColliderRadius, spaceOffsetX, spaceOffsetY, playerCollider.Shape, playerPos.X-playerColliderRadius+spaceOffsetX)) + Logger.Info(fmt.Sprintf("Player Collider#%d: playerPos.X=%v, playerPos.Y=%v, radius=%v, spaceOffsetX=%v, spaceOffsetY=%v, shape=%v", i, playerPos.X, playerPos.Y, playerColliderRadius, spaceOffsetX, spaceOffsetY, playerCollider.Shape)) playerColliders[i] = playerCollider space.Add(playerCollider) } @@ -52,26 +53,31 @@ func NewWorldColliderDisplay(game *Game, stageDiscreteW, stageDiscreteH, stageTi world.Space = space - moveToCollide := true + moveToCollide := false if moveToCollide { + effPushback := Vec2D{X: float64(0), Y: float64(0)} toTestPlayerCollider := playerColliders[0] - oldDx, oldDy := -2.98, -50.0 - dx, dy := oldDx, oldDy - if collision := toTestPlayerCollider.Check(oldDx, oldDy, "Barrier"); collision != nil { - playerShape := toTestPlayerCollider.Shape.(*resolv.ConvexPolygon) - barrierShape := collision.Objects[0].Shape.(*resolv.ConvexPolygon) - if overlapped, pushbackX, pushbackY := CalcPushbacks(oldDx, oldDy, playerShape, barrierShape); overlapped { - Logger.Info(fmt.Sprintf("Collided & overlapped: player.X=%v, player.Y=%v, oldDx=%v, oldDy=%v, playerShape=%v, toCheckBarrier=%v, pushbackX=%v, pushbackY=%v", toTestPlayerCollider.X, toTestPlayerCollider.Y, oldDx, oldDy, ConvexPolygonStr(playerShape), ConvexPolygonStr(barrierShape), pushbackX, pushbackY)) - dx -= pushbackX - dy -= pushbackY - } else { - Logger.Info(fmt.Sprintf("Collider BUT not overlapped: player.X=%v, player.Y=%v, oldDx=%v, oldDy=%v, playerShape=%v, toCheckBarrier=%v", toTestPlayerCollider.X, toTestPlayerCollider.Y, oldDx, oldDy, ConvexPolygonStr(playerShape), ConvexPolygonStr(barrierShape))) - } - } - - toTestPlayerCollider.X += dx - toTestPlayerCollider.Y += dy + toTestPlayerCollider.X += -2.98 + toTestPlayerCollider.Y += -50.0 toTestPlayerCollider.Update() + oldDx, oldDy := float64(0), float64(0) + if collision := toTestPlayerCollider.Check(oldDx, oldDy); collision != nil { + playerShape := toTestPlayerCollider.Shape.(*resolv.ConvexPolygon) + for _, obj := range collision.Objects { + barrierShape := obj.Shape.(*resolv.ConvexPolygon) + if overlapped, pushbackX, pushbackY := CalcPushbacks(oldDx, oldDy, playerShape, barrierShape); overlapped { + Logger.Info(fmt.Sprintf("Overlapped: a=%v, b=%v, pushbackX=%v, pushbackY=%v", ConvexPolygonStr(playerShape), ConvexPolygonStr(barrierShape), pushbackX, pushbackY)) + effPushback.X += pushbackX + effPushback.Y += pushbackY + } else { + Logger.Info(fmt.Sprintf("Collider BUT not overlapped: a=%v, b=%v", ConvexPolygonStr(playerShape), ConvexPolygonStr(barrierShape))) + } + } + toTestPlayerCollider.X -= effPushback.X + toTestPlayerCollider.Y -= effPushback.Y + toTestPlayerCollider.Update() + Logger.Info(fmt.Sprintf("effPushback={%v, %v}", effPushback.X, effPushback.Y)) + } } return world diff --git a/frontend/assets/resources/map/simple/map.tmx b/frontend/assets/resources/map/simple/map.tmx index acd6779..5b1df99 100644 --- a/frontend/assets/resources/map/simple/map.tmx +++ b/frontend/assets/resources/map/simple/map.tmx @@ -1,5 +1,5 @@ - + @@ -8,7 +8,7 @@ - + @@ -36,5 +36,11 @@ + + + + + + diff --git a/frontend/assets/scripts/collision_test_nodejs.js b/frontend/assets/scripts/collision_test_nodejs.js deleted file mode 100644 index 43ccb40..0000000 --- a/frontend/assets/scripts/collision_test_nodejs.js +++ /dev/null @@ -1,39 +0,0 @@ -const collisions = require('./modules/Collisions'); - -const collisionSys = new collisions.Collisions(); - -/* -Backend result reference - -2022-10-22T12:11:25.156+0800 INFO collider_visualizer/worldColliderDisplay.go:77 Collided: player.X=1257.665, player.Y=1415.335, oldDx=-2.98, oldDy=-50, playerShape=&{[[0 0] [64 0] [64 64] [0 64]] 1254.685 1365.335 true}, toCheckBarrier=&{[[628.626 54.254500000000064] [0 56.03250000000003] [0.42449999999999477 1.1229999999999905] [625.9715000000001 0]] 1289.039 1318.0805 true}, pushbackX=-0.15848054013127655, pushbackY=-56.03205175509715, result=&{56.03227587710039 -0.0028283794946841584 -0.9999960001267175 false false [0.9988052279193613 -0.04886836073527201]} -*/ -function polygonStr(body) { - let coords = []; - let cnt = body._coords.length; - for (let ix = 0, iy = 1; ix < cnt; ix += 2, iy += 2) { - coords.push([body._coords[ix], body._coords[iy]]); - } - return JSON.stringify(coords); -} - -const playerCollider = collisionSys.createPolygon(1257.665, 1415.335, [[0, 0], [64, 0], [64, 64], [0, 64]]); -const barrierCollider = collisionSys.createPolygon(1289.039, 1318.0805, [[628.626, 54.254500000000064], [0, 56.03250000000003], [0.42449999999999477, 1.1229999999999905], [625.9715000000001, 0]]); - -const oldDx = -2.98; -const oldDy = -50.0; - -playerCollider.x += oldDx; -playerCollider.y += oldDy; - -collisionSys.update(); -const result = collisionSys.createResult(); - -const potentials = playerCollider.potentials(); - -let overlapCheckId = 0; -for (const barrier of potentials) { - if (!playerCollider.collides(barrier, result)) continue; - const pushbackX = result.overlap * result.overlap_x; - const pushbackY = result.overlap * result.overlap_y; - console.log("For overlapCheckId=" + overlapCheckId + ", the overlap: a=", polygonStr(result.a), ", b=", polygonStr(result.b), ", pushbackX=", pushbackX, ", pushbackY=", pushbackY); -} diff --git a/frontend/assets/scripts/collision_test_nodejs.js.meta b/frontend/assets/scripts/collision_test_nodejs.js.meta deleted file mode 100644 index a64f7e4..0000000 --- a/frontend/assets/scripts/collision_test_nodejs.js.meta +++ /dev/null @@ -1,9 +0,0 @@ -{ - "ver": "1.0.5", - "uuid": "fce86138-76fc-44d5-8eac-2731b3b0cefd", - "isPlugin": false, - "loadPluginInWeb": true, - "loadPluginInNative": true, - "loadPluginInEditor": false, - "subMetas": {} -} \ No newline at end of file diff --git a/frontend/collision_test_nodejs.js b/frontend/collision_test_nodejs.js new file mode 100644 index 0000000..3db8003 --- /dev/null +++ b/frontend/collision_test_nodejs.js @@ -0,0 +1,39 @@ +const collisions = require('./assets/scripts/modules/Collisions'); + +const collisionSys = new collisions.Collisions(); + +function polygonStr(body) { + let coords = []; + let cnt = body._coords.length; + for (let ix = 0, iy = 1; ix < cnt; ix += 2, iy += 2) { + coords.push([body._coords[ix], body._coords[iy]]); + } + return JSON.stringify(coords); +} + +const playerCollider = collisionSys.createPolygon(1269.665, 1353.335, [[0, 0], [64, 0], [64, 64], [0, 64]]); + +const barrierCollider1 = collisionSys.createPolygon(1277.7159000000001, 1570.5575, [[642.5696, 319.159], [0, 319.15680000000003], [5.7286, 0], [643.7451, 0.9014999999999986]]); +const barrierCollider2 = collisionSys.createPolygon(1289.039, 1318.0805, [[628.626, 54.254500000000064], [0, 56.03250000000003], [0.42449999999999477, 1.1229999999999905], [625.9715000000001, 0]]); +const barrierCollider3 = collisionSys.createPolygon(1207, 1310, [[69, 581], [0, 579], [8, 3], [79, 0]]); + +playerCollider.x += -2.98; +playerCollider.y += -50.0; +collisionSys.update(); + +const effPushback = [0.0, 0.0]; + +const result = collisionSys.createResult(); + +const potentials = playerCollider.potentials(); + +for (const barrier of potentials) { + if (!playerCollider.collides(barrier, result)) continue; + const pushbackX = result.overlap * result.overlap_x; + const pushbackY = result.overlap * result.overlap_y; + console.log(`Overlapped: a=${polygonStr(result.a)}, b=${polygonStr(result.b)}, pushbackX=${pushbackX}, pushbackY=${pushbackY}`); + effPushback[0] += pushbackX; + effPushback[1] += pushbackY; +} + +console.log(`effPushback=${effPushback}`);