Optimize IsoZombie.isVehicleBetween performance: implement IsoZombieVehiclePerformancePatch. Add SegmentReach class for efficient distance checks and ZombieVehicleHooks for targeted vehicle intersection. Include tests for new functionality and integrate patch into B42PatchSet.
This commit is contained in:
1 parent
d7151f60e8
commit
da7ae49618
8 files changed
+446
-6
No files matched your search
+57
@@ -0,0 +1,57 @@
|
||||
package dev.overlord.zomboidforge.compat.b42.hooks;
|
||||
|
||||
import dev.overlord.zomboidforge.compat.b42.perf.SegmentReach;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.joml.Vector3f;
|
||||
|
||||
import zombie.core.physics.WorldSimulation;
|
||||
import zombie.scripting.objects.VehicleScript;
|
||||
import zombie.vehicles.BaseVehicle;
|
||||
|
||||
/** Hooks called by patched {@code zombie.characters.IsoZombie} code. */
|
||||
public final class ZombieVehicleHooks
|
||||
{
|
||||
/**
|
||||
* World units added to the bounding radius. Float resolution near the world coordinates
|
||||
* (about 10000) is 0.001, so this covers rounding differences to the vanilla test.
|
||||
*/
|
||||
private static final float MARGIN = 0.1F;
|
||||
|
||||
private ZombieVehicleHooks() {}
|
||||
|
||||
/**
|
||||
* Replaces the call {@code vehicle.getIntersectPoint(start, end, result)} in
|
||||
* {@code IsoZombie.isVehicleBetween}. Returns what that call returns.
|
||||
*
|
||||
* <p>The vanilla test is 2-dimensional: {@code getLocalPos} drops the height of both
|
||||
* points, and the vehicle's box is centered on its physics origin with the half extents
|
||||
* {@code extents * 0.5 + centerOfMassOffset}. Whatever its rotation, the box lies inside
|
||||
* the circle around that origin with the radius of its half diagonal. A segment that stays
|
||||
* outside the circle cannot hit the box, so the vanilla call returns {@code null} for it
|
||||
* and is skipped here.
|
||||
*/
|
||||
public static @Nullable Vector3f getIntersectPoint(
|
||||
@NotNull BaseVehicle vehicle,
|
||||
@NotNull Vector3f start,
|
||||
@NotNull Vector3f end,
|
||||
@Nullable Vector3f result
|
||||
) {
|
||||
VehicleScript script = vehicle.getScript();
|
||||
Vector3f extents = script.getExtents();
|
||||
Vector3f centerOfMass = script.getCenterOfMassOffset();
|
||||
|
||||
float halfX = extents.x * 0.5F + centerOfMass.x;
|
||||
float halfZ = extents.z * 0.5F + centerOfMass.z;
|
||||
float radius = (float) Math.sqrt(halfX * halfX + halfZ * halfZ) + ZombieVehicleHooks.MARGIN;
|
||||
|
||||
// Same position that BaseVehicle.getWorldPos and getLocalPos use for the physics origin.
|
||||
float centerX = vehicle.jniTransform.origin.x + WorldSimulation.instance.offsetX;
|
||||
float centerY = vehicle.jniTransform.origin.z + WorldSimulation.instance.offsetY;
|
||||
|
||||
if (SegmentReach.missesCircle(start.x, start.y, end.x, end.y, centerX, centerY, radius)) return null;
|
||||
|
||||
return vehicle.getIntersectPoint(start, end, result);
|
||||
}
|
||||
}
|
||||
+38
@@ -0,0 +1,38 @@
|
||||
package dev.overlord.zomboidforge.compat.b42.perf;
|
||||
|
||||
/** Distance checks in the XY plane. Uses no game classes, so it can be tested on its own. */
|
||||
public final class SegmentReach
|
||||
{
|
||||
private SegmentReach() {}
|
||||
|
||||
/**
|
||||
* Whether every point of the segment from ({@code startX}, {@code startY}) to
|
||||
* ({@code endX}, {@code endY}) is farther than {@code radius} from
|
||||
* ({@code centerX}, {@code centerY}).
|
||||
*
|
||||
* <p>False when any input is NaN. A caller that skips work only on {@code true} therefore
|
||||
* still runs the full computation for such input.
|
||||
*/
|
||||
public static boolean missesCircle(
|
||||
float startX, float startY, float endX, float endY,
|
||||
float centerX, float centerY, float radius
|
||||
) {
|
||||
float directionX = endX - startX;
|
||||
float directionY = endY - startY;
|
||||
float lengthSquared = directionX * directionX + directionY * directionY;
|
||||
|
||||
// Position of the point on the segment closest to the center, from 0 (start) to 1 (end).
|
||||
// A segment of length 0 is its start. NaN passes through every comparison unchanged.
|
||||
float along = lengthSquared == 0.0F
|
||||
? 0.0F
|
||||
: ((centerX - startX) * directionX + (centerY - startY) * directionY) / lengthSquared;
|
||||
|
||||
if (along < 0.0F) along = 0.0F;
|
||||
else if (along > 1.0F) along = 1.0F;
|
||||
|
||||
float offsetX = startX + along * directionX - centerX;
|
||||
float offsetY = startY + along * directionY - centerY;
|
||||
|
||||
return offsetX * offsetX + offsetY * offsetY > radius * radius;
|
||||
}
|
||||
}
|
||||
+88
@@ -0,0 +1,88 @@
|
||||
package dev.overlord.zomboidforge.compat.b42.perf;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
class SegmentReachTest
|
||||
{
|
||||
@Test
|
||||
void segmentFarFromTheCircleMisses()
|
||||
{
|
||||
assertTrue(SegmentReach.missesCircle(0, 10, 20, 10, 10, 0, 5));
|
||||
}
|
||||
|
||||
@Test
|
||||
void segmentThroughTheCenterDoesNotMiss()
|
||||
{
|
||||
assertFalse(SegmentReach.missesCircle(0, 0, 20, 0, 10, 0, 1));
|
||||
}
|
||||
|
||||
@Test
|
||||
void segmentThatEndsBeforeTheCircleMissesEvenWhenItsLineWouldHit()
|
||||
{
|
||||
// The line y = 0 passes through the circle at (10, 0); the segment stops at x = 5.
|
||||
assertTrue(SegmentReach.missesCircle(0, 0, 5, 0, 10, 0, 2));
|
||||
}
|
||||
|
||||
@Test
|
||||
void segmentThatStartsAfterTheCircleMissesEvenWhenItsLineWouldHit()
|
||||
{
|
||||
assertTrue(SegmentReach.missesCircle(15, 0, 30, 0, 10, 0, 2));
|
||||
}
|
||||
|
||||
@Test
|
||||
void segmentThatTouchesTheCircleEdgeDoesNotMiss()
|
||||
{
|
||||
assertFalse(SegmentReach.missesCircle(0, 5, 20, 5, 10, 0, 5));
|
||||
}
|
||||
|
||||
@Test
|
||||
void segmentJustOutsideTheCircleMisses()
|
||||
{
|
||||
assertTrue(SegmentReach.missesCircle(0, 5.001F, 20, 5.001F, 10, 0, 5));
|
||||
}
|
||||
|
||||
@Test
|
||||
void segmentStartingInsideTheCircleDoesNotMiss()
|
||||
{
|
||||
assertFalse(SegmentReach.missesCircle(10, 1, 100, 100, 10, 0, 2));
|
||||
}
|
||||
|
||||
@Test
|
||||
void segmentOfLengthZeroIsItsStart()
|
||||
{
|
||||
assertFalse(SegmentReach.missesCircle(10, 1, 10, 1, 10, 0, 2));
|
||||
assertTrue(SegmentReach.missesCircle(50, 50, 50, 50, 10, 0, 2));
|
||||
}
|
||||
|
||||
@Test
|
||||
void diagonalSegmentIsMeasuredToItsClosestPoint()
|
||||
{
|
||||
// Closest point of (0,0)-(10,10) to (10,0) is (5,5), distance sqrt(50) = 7.07.
|
||||
assertTrue(SegmentReach.missesCircle(0, 0, 10, 10, 10, 0, 7.0F));
|
||||
assertFalse(SegmentReach.missesCircle(0, 0, 10, 10, 10, 0, 7.2F));
|
||||
}
|
||||
|
||||
@Test
|
||||
void worksAtWorldCoordinates()
|
||||
{
|
||||
assertFalse(SegmentReach.missesCircle(10720, 10195, 10760, 10195, 10740, 10196, 2));
|
||||
assertTrue(SegmentReach.missesCircle(10720, 10195, 10760, 10195, 10740, 10210, 2));
|
||||
}
|
||||
|
||||
@Test
|
||||
void neverMissesOnNaN()
|
||||
{
|
||||
float nan = Float.NaN;
|
||||
|
||||
assertFalse(SegmentReach.missesCircle(nan, 0, 20, 0, 10, 100, 1));
|
||||
assertFalse(SegmentReach.missesCircle(0, nan, 20, 0, 10, 100, 1));
|
||||
assertFalse(SegmentReach.missesCircle(0, 0, nan, 0, 10, 100, 1));
|
||||
assertFalse(SegmentReach.missesCircle(0, 0, 20, nan, 10, 100, 1));
|
||||
assertFalse(SegmentReach.missesCircle(0, 0, 20, 0, nan, 100, 1));
|
||||
assertFalse(SegmentReach.missesCircle(0, 0, 20, 0, 10, nan, 1));
|
||||
assertFalse(SegmentReach.missesCircle(0, 0, 20, 0, 10, 100, nan));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user