Updated CLI unit tests.

This commit is contained in:
genxium 2022-11-12 11:20:16 +08:00
parent 98daeff408
commit 41967b11f7
5 changed files with 71 additions and 68 deletions

View File

@ -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

View File

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<map version="1.2" tiledversion="1.2.3" orientation="isometric" renderorder="right-down" width="50" height="50" tilewidth="64" tileheight="64" infinite="0" nextlayerid="11" nextobjectid="214">
<map version="1.2" tiledversion="1.2.3" orientation="isometric" renderorder="right-down" width="50" height="50" tilewidth="64" tileheight="64" infinite="0" nextlayerid="11" nextobjectid="215">
<tileset firstgid="1" source="Tile_W64_H64_S01.tsx"/>
<tileset firstgid="17" source="Tile_W300_H300_S01.tsx"/>
<layer id="1" name="GroundFloor" width="50" height="50" locked="1">
@ -8,7 +8,7 @@
</data>
</layer>
<objectgroup id="2" name="PlayerStartingPos">
<object id="135" x="1442.33" y="2063">
<object id="135" x="1516.33" y="2113">
<point/>
</object>
<object id="137" x="2270" y="1640">
@ -36,5 +36,11 @@
</properties>
<polyline points="1101.33,-342 470.926,284.848 526.26,339.333 1152.93,-285.091"/>
</object>
<object id="214" x="988" y="1632">
<properties>
<property name="boundary_type" value="barrier"/>
</properties>
<polyline points="-3,1 -70,72 514,640 588,572"/>
</object>
</objectgroup>
</map>

View File

@ -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);
}

View File

@ -1,9 +0,0 @@
{
"ver": "1.0.5",
"uuid": "fce86138-76fc-44d5-8eac-2731b3b0cefd",
"isPlugin": false,
"loadPluginInWeb": true,
"loadPluginInNative": true,
"loadPluginInEditor": false,
"subMetas": {}
}

View File

@ -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}`);