-
-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement even more handlers for HFW types and misc cleanups
- Loading branch information
1 parent
3b2385e
commit 8c75e74
Showing
63 changed files
with
1,865 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 91 additions & 0 deletions
91
...e-horizon-forbidden-west/src/main/java/com/shade/decima/game/hfw/data/jolt/JoltUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package com.shade.decima.game.hfw.data.jolt; | ||
|
||
import com.shade.decima.game.hfw.data.jolt.geometry.AABox; | ||
import com.shade.decima.game.hfw.data.jolt.math.Mat44; | ||
import com.shade.decima.game.hfw.data.jolt.math.Quat; | ||
import com.shade.decima.game.hfw.data.jolt.math.Vec3; | ||
import com.shade.decima.game.hfw.data.jolt.math.Vec4; | ||
import com.shade.util.NotNull; | ||
import com.shade.util.io.BinaryReader; | ||
|
||
import java.io.IOException; | ||
import java.util.List; | ||
|
||
public final class JoltUtils { | ||
private JoltUtils() { | ||
// prevents instantiation | ||
} | ||
|
||
@NotNull | ||
public static Vec3 readVec3(@NotNull BinaryReader reader) throws IOException { | ||
var x = reader.readFloat(); | ||
var y = reader.readFloat(); | ||
var z = reader.readFloat(); | ||
|
||
return new Vec3(x, y, z); | ||
} | ||
|
||
@NotNull | ||
public static Vec3 readAlignedVector3(@NotNull BinaryReader reader) throws IOException { | ||
var x = reader.readFloat(); | ||
var y = reader.readFloat(); | ||
var z = reader.readFloat(); | ||
var w = reader.readFloat(); | ||
|
||
if (z != w) { | ||
throw new IllegalArgumentException("z and w must be equal"); | ||
} | ||
|
||
return new Vec3(x, y, z); | ||
} | ||
|
||
@NotNull | ||
public static Vec4 readVec4(@NotNull BinaryReader reader) throws IOException { | ||
var x = reader.readFloat(); | ||
var y = reader.readFloat(); | ||
var z = reader.readFloat(); | ||
var w = reader.readFloat(); | ||
|
||
return new Vec4(x, y, z, w); | ||
} | ||
|
||
@NotNull | ||
public static Quat readQuaternion(@NotNull BinaryReader reader) throws IOException { | ||
var x = reader.readFloat(); | ||
var y = reader.readFloat(); | ||
var z = reader.readFloat(); | ||
var w = reader.readFloat(); | ||
|
||
return new Quat(x, y, z, w); | ||
} | ||
|
||
@NotNull | ||
public static Mat44 readMatrix4(@NotNull BinaryReader reader) throws IOException { | ||
var col0 = readVec4(reader); | ||
var col1 = readVec4(reader); | ||
var col2 = readVec4(reader); | ||
var col3 = readVec4(reader); | ||
|
||
return new Mat44(col0, col1, col2, col3); | ||
} | ||
|
||
@NotNull | ||
public static AABox readAABox(@NotNull BinaryReader reader) throws IOException { | ||
return new AABox(readAlignedVector3(reader), readAlignedVector3(reader)); | ||
} | ||
|
||
@NotNull | ||
public static <T> List<T> readObjects(@NotNull BinaryReader reader, @NotNull BinaryReader.ObjectMapper<T> mapper) throws IOException { | ||
return reader.readObjects(Math.toIntExact(reader.readLong()), mapper); | ||
} | ||
|
||
@NotNull | ||
public static byte[] readBytes(@NotNull BinaryReader reader) throws IOException { | ||
return reader.readBytes(Math.toIntExact(reader.readLong())); | ||
} | ||
|
||
@NotNull | ||
public static String readString(@NotNull BinaryReader reader) throws IOException { | ||
return reader.readString(Math.toIntExact(reader.readLong())); | ||
} | ||
} |
72 changes: 72 additions & 0 deletions
72
...orizon-forbidden-west/src/main/java/com/shade/decima/game/hfw/data/jolt/core/Factory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package com.shade.decima.game.hfw.data.jolt.core; | ||
|
||
import com.shade.util.NotNull; | ||
import com.shade.util.hash.Hashing; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
public class Factory { | ||
private static final String[] TYPE_NAMES = { | ||
"SkeletalAnimation", | ||
"Skeleton", | ||
"CompoundShapeSettings", | ||
"StaticCompoundShapeSettings", | ||
"MutableCompoundShapeSettings", | ||
"TriangleShapeSettings", | ||
"SphereShapeSettings", | ||
"BoxShapeSettings", | ||
"CapsuleShapeSettings", | ||
"TaperedCapsuleShapeSettings", | ||
"CylinderShapeSettings", | ||
"ScaledShapeSettings", | ||
"MeshShapeSettings", | ||
"ConvexHullShapeSettings", | ||
"HeightFieldShapeSettings", | ||
"RotatedTranslatedShapeSettings", | ||
"OffsetCenterOfMassShapeSettings", | ||
"RagdollSettings", | ||
"PointConstraintSettings", | ||
"SixDOFConstraintSettings", | ||
"SliderConstraintSettings", | ||
"SwingTwistConstraintSettings", | ||
"DistanceConstraintSettings", | ||
"HingeConstraintSettings", | ||
"FixedConstraintSettings", | ||
"ConeConstraintSettings", | ||
"PathConstraintSettings", | ||
"VehicleConstraintSettings", | ||
"WheeledVehicleControllerSettings", | ||
"PathConstraintPath", | ||
"PathConstraintPathHermite", | ||
"MotorSettings", | ||
"PhysicsScene", | ||
"PhysicsMaterial", | ||
"PhysicsMaterialSimple", | ||
"GroupFilter", | ||
"GroupFilterTable", | ||
}; | ||
|
||
private static final Map<Integer, String> HASH_TO_TYPE_NAME_MAP = new HashMap<>(); | ||
|
||
static { | ||
for (String name : TYPE_NAMES) { | ||
HASH_TO_TYPE_NAME_MAP.put(computeHash(name), name); | ||
} | ||
} | ||
|
||
@NotNull | ||
public static String getTypeName(int hash) { | ||
var name = HASH_TO_TYPE_NAME_MAP.get(hash); | ||
if (name == null) { | ||
throw new IllegalArgumentException("Unknown type hash: %#010x".formatted(hash)); | ||
} | ||
return name; | ||
} | ||
|
||
@SuppressWarnings("UseHashCodeMethodInspection") | ||
private static int computeHash(@NotNull String name) { | ||
var hash = Hashing.fnv1a().hashString(name).asLong(); | ||
return (int) (hash ^ (hash >>> 32)); | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
...izon-forbidden-west/src/main/java/com/shade/decima/game/hfw/data/jolt/geometry/AABox.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package com.shade.decima.game.hfw.data.jolt.geometry; | ||
|
||
import com.shade.decima.game.hfw.data.jolt.math.Vec3; | ||
import com.shade.util.NotNull; | ||
|
||
public record AABox(@NotNull Vec3 min, @NotNull Vec3 max) { | ||
} |
7 changes: 7 additions & 0 deletions
7
...izon-forbidden-west/src/main/java/com/shade/decima/game/hfw/data/jolt/geometry/Plane.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package com.shade.decima.game.hfw.data.jolt.geometry; | ||
|
||
import com.shade.decima.game.hfw.data.jolt.math.Vec3; | ||
import com.shade.util.NotNull; | ||
|
||
public record Plane(@NotNull Vec3 normal, float distance) { | ||
} |
6 changes: 6 additions & 0 deletions
6
...-horizon-forbidden-west/src/main/java/com/shade/decima/game/hfw/data/jolt/math/Mat44.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package com.shade.decima.game.hfw.data.jolt.math; | ||
|
||
import com.shade.util.NotNull; | ||
|
||
public record Mat44(@NotNull Vec4 col0, @NotNull Vec4 col1, @NotNull Vec4 col2, @NotNull Vec4 col3) { | ||
} |
4 changes: 4 additions & 0 deletions
4
...e-horizon-forbidden-west/src/main/java/com/shade/decima/game/hfw/data/jolt/math/Quat.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
package com.shade.decima.game.hfw.data.jolt.math; | ||
|
||
public record Quat(float x, float y, float z, float w) { | ||
} |
4 changes: 4 additions & 0 deletions
4
...e-horizon-forbidden-west/src/main/java/com/shade/decima/game/hfw/data/jolt/math/Vec3.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
package com.shade.decima.game.hfw.data.jolt.math; | ||
|
||
public record Vec3(float x, float y, float z) { | ||
} |
4 changes: 4 additions & 0 deletions
4
...e-horizon-forbidden-west/src/main/java/com/shade/decima/game/hfw/data/jolt/math/Vec4.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
package com.shade.decima.game.hfw.data.jolt.math; | ||
|
||
public record Vec4(float x, float y, float z, float w) { | ||
} |
6 changes: 6 additions & 0 deletions
6
...orizon-forbidden-west/src/main/java/com/shade/decima/game/hfw/data/jolt/package-info.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
/** | ||
* This package contains a minimal implementation of the JoltPhysics data structures. | ||
* | ||
* @see <a href="https://github.com/jrouwe/JoltPhysics">Jolt Physics</a> | ||
*/ | ||
package com.shade.decima.game.hfw.data.jolt; |
92 changes: 92 additions & 0 deletions
92
.../src/main/java/com/shade/decima/game/hfw/data/jolt/physics/body/BodyCreationSettings.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package com.shade.decima.game.hfw.data.jolt.physics.body; | ||
|
||
import com.shade.decima.game.hfw.data.jolt.JoltUtils; | ||
import com.shade.decima.game.hfw.data.jolt.math.Quat; | ||
import com.shade.decima.game.hfw.data.jolt.math.Vec3; | ||
import com.shade.decima.game.hfw.data.jolt.physics.collision.CollisionGroup; | ||
import com.shade.decima.game.hfw.data.jolt.physics.collision.GroupFilter; | ||
import com.shade.decima.game.hfw.data.jolt.physics.collision.PhysicsMaterial; | ||
import com.shade.decima.game.hfw.data.jolt.physics.collision.shape.Shape; | ||
import com.shade.util.NotNull; | ||
import com.shade.util.io.BinaryReader; | ||
|
||
import java.io.IOException; | ||
import java.util.List; | ||
|
||
public class BodyCreationSettings { | ||
public enum OverrideMassProperties { | ||
CalculateMassAndInertia, | ||
CalculateInertia, | ||
MassAndInertiaProvided | ||
} | ||
|
||
public Vec3 position; | ||
public Quat rotation; | ||
public CollisionGroup collisionGroup; | ||
public short objectLayer; | ||
public MotionType motionType; | ||
public boolean allowDynamicOrKinematic; | ||
public MotionQuality motionQuality; | ||
public boolean allowSleeping; | ||
public float friction; | ||
public float restitution; | ||
public float linearDamping; | ||
public float angularDamping; | ||
public float maxLinearVelocity; | ||
public float maxAngularVelocity; | ||
public float gravityFactor; | ||
public OverrideMassProperties overrideMassProperties; | ||
public float inertiaMultiplier; | ||
public MassProperties massPropertiesOverride; | ||
public Shape shape; | ||
|
||
@NotNull | ||
public static BodyCreationSettings sRestoreWithChildren( | ||
@NotNull BinaryReader reader, | ||
@NotNull List<Shape> shapeMap, | ||
@NotNull List<PhysicsMaterial> materialMap, | ||
@NotNull List<GroupFilter> groupFilterMap | ||
) throws IOException { | ||
BodyCreationSettings settings = new BodyCreationSettings(); | ||
settings.restoreBinaryState(reader); | ||
settings.shape = Shape.sRestoreWithChildren(reader, shapeMap, materialMap); | ||
|
||
int groupFilterId = reader.readInt(); | ||
if (groupFilterId != ~0) { | ||
GroupFilter groupFilter; | ||
|
||
if (groupFilterId >= groupFilterMap.size()) { | ||
assert groupFilterId == groupFilterMap.size(); | ||
groupFilter = GroupFilter.sRestoreFromBinaryState(reader); | ||
groupFilterMap.add(groupFilter); | ||
} else { | ||
groupFilter = groupFilterMap.get(groupFilterId); | ||
} | ||
|
||
settings.collisionGroup.groupFilter = groupFilter; | ||
} | ||
|
||
return settings; | ||
} | ||
|
||
public void restoreBinaryState(@NotNull BinaryReader reader) throws IOException { | ||
position = JoltUtils.readAlignedVector3(reader); | ||
rotation = JoltUtils.readQuaternion(reader); | ||
collisionGroup = CollisionGroup.restoreFromBinaryState(reader); | ||
objectLayer = reader.readShort(); | ||
motionType = MotionType.values()[reader.readByte()]; | ||
allowDynamicOrKinematic = reader.readByteBoolean(); | ||
motionQuality = MotionQuality.values()[reader.readByte()]; | ||
allowSleeping = reader.readByteBoolean(); | ||
friction = reader.readFloat(); | ||
restitution = reader.readFloat(); | ||
linearDamping = reader.readFloat(); | ||
angularDamping = reader.readFloat(); | ||
maxLinearVelocity = reader.readFloat(); | ||
maxAngularVelocity = reader.readFloat(); | ||
gravityFactor = reader.readFloat(); | ||
overrideMassProperties = OverrideMassProperties.values()[reader.readByte()]; | ||
inertiaMultiplier = reader.readFloat(); | ||
massPropertiesOverride = MassProperties.restoreFromBinaryState(reader); | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
...n-west/src/main/java/com/shade/decima/game/hfw/data/jolt/physics/body/MassProperties.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package com.shade.decima.game.hfw.data.jolt.physics.body; | ||
|
||
import com.shade.decima.game.hfw.data.jolt.JoltUtils; | ||
import com.shade.decima.game.hfw.data.jolt.math.Mat44; | ||
import com.shade.util.NotNull; | ||
import com.shade.util.io.BinaryReader; | ||
|
||
import java.io.IOException; | ||
|
||
public record MassProperties(float mass, @NotNull Mat44 inertia) { | ||
@NotNull | ||
public static MassProperties restoreFromBinaryState(@NotNull BinaryReader reader) throws IOException { | ||
var mass = reader.readFloat(); | ||
var inertia = JoltUtils.readMatrix4(reader); | ||
|
||
return new MassProperties(mass, inertia); | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
...en-west/src/main/java/com/shade/decima/game/hfw/data/jolt/physics/body/MotionQuality.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package com.shade.decima.game.hfw.data.jolt.physics.body; | ||
|
||
public enum MotionQuality { | ||
Discrete, | ||
LinearCast | ||
} |
7 changes: 7 additions & 0 deletions
7
...idden-west/src/main/java/com/shade/decima/game/hfw/data/jolt/physics/body/MotionType.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package com.shade.decima.game.hfw.data.jolt.physics.body; | ||
|
||
public enum MotionType { | ||
Static, | ||
Kinematic, | ||
Dynamic | ||
} |
21 changes: 21 additions & 0 deletions
21
...t/src/main/java/com/shade/decima/game/hfw/data/jolt/physics/collision/CollisionGroup.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package com.shade.decima.game.hfw.data.jolt.physics.collision; | ||
|
||
import com.shade.util.NotNull; | ||
import com.shade.util.io.BinaryReader; | ||
|
||
import java.io.IOException; | ||
|
||
public final class CollisionGroup { | ||
public int groupId; | ||
public int subGroupId; | ||
public GroupFilter groupFilter; | ||
|
||
@NotNull | ||
public static CollisionGroup restoreFromBinaryState(@NotNull BinaryReader reader) throws IOException { | ||
CollisionGroup result = new CollisionGroup(); | ||
result.groupId = reader.readInt(); | ||
result.subGroupId = reader.readInt(); | ||
|
||
return result; | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
...west/src/main/java/com/shade/decima/game/hfw/data/jolt/physics/collision/GroupFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package com.shade.decima.game.hfw.data.jolt.physics.collision; | ||
|
||
import com.shade.decima.game.hfw.data.jolt.core.Factory; | ||
import com.shade.util.NotImplementedException; | ||
import com.shade.util.NotNull; | ||
import com.shade.util.io.BinaryReader; | ||
|
||
import java.io.IOException; | ||
|
||
public class GroupFilter { | ||
@NotNull | ||
public static GroupFilter sRestoreFromBinaryState(@NotNull BinaryReader reader) throws IOException { | ||
var hash = reader.readInt(); | ||
var name = Factory.getTypeName(hash); | ||
var result = switch (name) { | ||
case "GroupFilterTable" -> new GroupFilterTable(); | ||
default -> throw new NotImplementedException(); | ||
}; | ||
result.restoreBinaryState(reader); | ||
return result; | ||
} | ||
|
||
public void restoreBinaryState(@NotNull BinaryReader reader) throws IOException { | ||
// no-op | ||
} | ||
} |
Oops, something went wrong.