Skip to content

Commit

Permalink
Update entity; update math library
Browse files Browse the repository at this point in the history
squid233 committed Aug 2, 2024

Verified

This commit was signed with the committer’s verified signature.
squid233 squid233
1 parent 98ad86c commit da75736
Showing 17 changed files with 340 additions and 44 deletions.
Original file line number Diff line number Diff line change
@@ -163,7 +163,7 @@ private void renderWorld(GLStateMgr gl, double partialTick) {
0.01f,
1000.0f
), camera.updateViewMatrix());
RenderSystem.setModelMatrix(Matrix4f.IDENTITY);
RenderSystem.setModelMatrix(Matrix4f.identity());

RenderSystem.useProgram(positionColorTexProgram);
RenderSystem.updateMatrices();
@@ -245,8 +245,8 @@ private void renderGui(GLStateMgr gl, double partialTick) {
client.scaledFramebufferHeight(),
-300.0f,
300.0f),
Matrix4f.IDENTITY);
RenderSystem.setModelMatrix(Matrix4f.IDENTITY);
Matrix4f.identity());
RenderSystem.setModelMatrix(Matrix4f.identity());
renderScreen(guiGraphics, gl, partialTick);
}

Original file line number Diff line number Diff line change
@@ -28,9 +28,9 @@ public final class RenderSystem {
private static GLStateMgr stateMgr = null;
private static GLProgram currentProgram = null;
private static Texture textureBinding2D = null;
private static Matrix4f projectionMatrix = Matrix4f.IDENTITY;
private static Matrix4f viewMatrix = Matrix4f.IDENTITY;
private static Matrix4f modelMatrix = Matrix4f.IDENTITY;
private static Matrix4f projectionMatrix = Matrix4f.identity();
private static Matrix4f viewMatrix = Matrix4f.identity();
private static Matrix4f modelMatrix = Matrix4f.identity();

public static void initialize(GLStateMgr gl) {
logger.info("Initializing render system");
Original file line number Diff line number Diff line change
@@ -50,7 +50,7 @@ public void update(int width, int height) {

public void render(GuiGraphics graphics, GLStateMgr gl, double partialTick) {
RenderSystem.setProjectionViewMatrix(Matrix4f.setOrtho(0.0f, width, 0.0f, height, -300.0f, 300.0f),
Matrix4f.IDENTITY);
Matrix4f.identity());
RenderSystem.setModelMatrix(Matrix4f.translation(width * 0.5f, height * 0.5f, 0.0f));

RenderSystem.useProgram(gameRenderer.positionColorTexProgram());
Original file line number Diff line number Diff line change
@@ -28,6 +28,7 @@
import freeworld.world.block.BlockHitResult;
import freeworld.world.block.BlockType;
import freeworld.world.entity.Entity;
import freeworld.world.entity.EntityChangeListener;
import org.slf4j.Logger;
import reactor.core.Disposable;
import reactor.core.publisher.Flux;
@@ -57,17 +58,29 @@ public final class WorldRenderer implements GLResource, WorldListener {
.buildPool();
@Deprecated
private final Map<Vector3i, ClientChunk> chunks = new HashMap<>(2048);
@Deprecated
private final Disposable chunkGC;
private final Queue<Runnable> chunkGCQueue = new LinkedBlockingQueue<>();
@Deprecated
private Vector3i playerChunkPos = Vector3i.ZERO;
private final Queue<Runnable> chunkGCQueue = new LinkedBlockingQueue<>();

public WorldRenderer(GameRenderer gameRenderer, World world) {
this.gameRenderer = gameRenderer;
this.world = world;
world.addListener(this);
this.chunkGC = Flux.interval(Duration.ofSeconds(45))
.subscribe(_ -> chunkGCQueue.offer(this::uninstallChunks));
gameRenderer.client().player().setChangeListener(new EntityChangeListener() {
private Vector3i chunkPos = gameRenderer.client().player().chunkPos();

@Override
public void onEntityPositionUpdated() {
Vector3i chunkPos1 = gameRenderer.client().player().chunkPos();
if (!chunkPos.equals(chunkPos1)) {
chunkPos = chunkPos1;
uninstallChunks();
}
}
});
}

private static DefaultVertexBuilder createVertexBuilder() {
@@ -112,13 +125,6 @@ public void renderChunks(GLStateMgr gl, Entity player) {
}
});

Vector3d playerPos = gameRenderer.client().player().position();
Vector3i playerChunkPos = ChunkPos.toChunkPos(playerPos.toVector3iFloor());
if (!this.playerChunkPos.equals(playerChunkPos)) {
this.playerChunkPos = playerChunkPos;
uninstallChunks();
}

Runnable chunkGCTask;
while ((chunkGCTask = chunkGCQueue.poll()) != null) {
chunkGCTask.run();
Original file line number Diff line number Diff line change
@@ -81,6 +81,6 @@ public void renderBlockModel(VertexBuilder builder, BlockModel model, Matrix4f m
}

public void renderBlockModel(VertexBuilder builder, BlockModel model, int x, int y, int z, Predicate<Direction> shouldCullFace) {
renderBlockModel(builder, model, Matrix4f.IDENTITY, x, y, z, shouldCullFace);
renderBlockModel(builder, model, Matrix4f.identity(), x, y, z, shouldCullFace);
}
}
Original file line number Diff line number Diff line change
@@ -46,6 +46,7 @@ public class Chunk {
private final int height;
private final int depth;
private final BlockType[] blocks;
@Deprecated
private final List<Entity> entities = new ArrayList<>();

public Chunk(World world, int x, int y, int z) {
@@ -66,6 +67,7 @@ public Chunk(World world, int x, int y, int z) {
Arrays.fill(blocks, BlockTypes.AIR);
}

@Deprecated
public void tick() {
world.motionSystem().process(world, entities);
// TODO: test
@@ -81,14 +83,11 @@ public void tick() {
}
}

@Deprecated
public void addEntity(Entity entity) {
entities.add(entity);
}

public void removeEntity(Entity entity) {
entities.remove(entity);
}

public void generateTerrain() {
for (int bx = 0; bx < width; bx++) {
for (int bz = 0; bz < depth; bz++) {
Original file line number Diff line number Diff line change
@@ -31,9 +31,10 @@ public class Entity {
public Vector3d acceleration = Vector3d.ZERO;
private Vector3i blockPos = Vector3i.ZERO;
public AABBox boundingBox;
private EntityChangeListener changeListener = EntityChangeListener.EMPTY;
private Vector3i chunkPos = Vector3i.ZERO;
private final Vector3d dimension;
public double eyeHeight;
private final double eyeHeight;
public boolean flying = false;
public boolean onGround = false;
private Vector3d previousPosition = Vector3d.ZERO;
@@ -49,6 +50,10 @@ public Entity(EntityType<? extends Entity> type, World world) {
setPosition(new Vector3d(0.0, 0.0, 0.0));
}

public void setChangeListener(EntityChangeListener changeListener) {
this.changeListener = changeListener;
}

private AABBox calculateBoundingBox() {
// TODO: use EntityDimension
final double hw = dimension.x() * 0.5;
@@ -93,6 +98,7 @@ public void setPos(Vector3d pos) {
this.chunkPos = chunkPos;
}
}
changeListener.onEntityPositionUpdated();
}
}

@@ -101,11 +107,11 @@ public void setBoundingBox(AABBox boundingBox) {
}

public Vector3d interpolatedPosition(double partialTick) {
return previousPosition.lerp(position, partialTick);
return previousPosition.linearInterpolate(position, partialTick);
}

public Vector3d getCameraPos(double partialTick) {
return interpolatedPosition(partialTick).add(0.0, getEyeHeight(), 0.0);
return interpolatedPosition(partialTick).add(0.0, eyeHeight, 0.0);
}

public World world() {
@@ -136,6 +142,10 @@ public Vector3i chunkPos() {
return chunkPos;
}

public double eyeHeight() {
return eyeHeight;
}

public boolean flying() {
return flying;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* freeworld - 3D sandbox game
* Copyright (C) 2024 XenFork Union
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation;
* only version 2.1 of the License.
*/

package freeworld.world.entity;

/**
* @author squid233
* @since 0.1.0
*/
public interface EntityChangeListener {
EntityChangeListener EMPTY = () -> {
};

void onEntityPositionUpdated();
}
Original file line number Diff line number Diff line change
@@ -30,11 +30,11 @@ public static float fma(float a, float b, float c) {
return a * b + c;
}

public static double lerp(double a, double b, double t) {
public static double linearInterpolate(double a, double b, double t) {
return fma(b - a, t, a);
}

public static float lerp(float a, float b, float t) {
public static float linearInterpolate(float a, float b, float t) {
return fma(b - a, t, a);
}

Original file line number Diff line number Diff line change
@@ -53,7 +53,7 @@ public record Matrix4f(
* {@link #PROPERTY_AFFINE} in this implementation.
*/
public static final byte PROPERTY_ORTHONORMAL = 1 << 4;
public static final Matrix4f IDENTITY = new Matrix4f(
private static final Matrix4f IDENTITY = new Matrix4f(
PROPERTY_IDENTITY | PROPERTY_AFFINE | PROPERTY_TRANSLATION | PROPERTY_ORTHONORMAL,
1.0f, 0.0f, 0.0f, 0.0f,
0.0f, 1.0f, 0.0f, 0.0f,
@@ -104,6 +104,15 @@ private static int determineProperties(
return properties;
}

/**
* {@return an identity matrix}
* <p>
* This method keeps consistent with other matrix initializers that use methods.
*/
public static Matrix4f identity() {
return IDENTITY;
}

//region get

public MemorySegment get(MemorySegment dest) {
@@ -414,9 +423,76 @@ private Matrix4f rotateYInternal(float ang) {
);
}

public static Matrix4f rotationZ(float ang) {
float sin = (float) Math.sin(ang);
float cos = (float) Math.cos(ang);
return new Matrix4f(
PROPERTY_AFFINE | PROPERTY_ORTHONORMAL,
cos, sin, 0.0f, 0.0f,
-sin, cos, 0.0f, 0.0f,
0.0f, 0.0f, 1.0f, 0.0f,
0.0f, 0.0f, 0.0f, 1.0f
);
}

public Matrix4f rotateZ(float ang) {
if ((properties & PROPERTY_IDENTITY) != 0)
return rotationZ(ang);
else if ((properties & PROPERTY_TRANSLATION) != 0) {
float x = m30(), y = m31(), z = m32();
return rotationZ(ang).setTranslation(x, y, z);
}
return rotateZInternal(ang);
}

private Matrix4f rotateZInternal(float ang) {
float sin = (float) Math.sin(ang);
float cos = (float) Math.cos(ang);
return rotateTowardsXY(sin, cos);
}

public static Matrix4f rotationTowardsXY(float dirX, float dirY) {
return new Matrix4f(
PROPERTY_AFFINE | PROPERTY_ORTHONORMAL,
dirY, dirX, 0.0f, 0.0f,
-dirX, dirY, 0.0f, 0.0f,
0.0f, 0.0f, 1.0f, 0.0f,
0.0f, 0.0f, 0.0f, 1.0f
);
}

public Matrix4f rotateTowardsXY(float dirX, float dirY) {
if ((properties & PROPERTY_IDENTITY) != 0)
return rotationTowardsXY(dirX, dirY);
float nm00 = Maths.fma(m00(), dirY, m10() * dirX);
float nm01 = Maths.fma(m01(), dirY, m11() * dirX);
float nm02 = Maths.fma(m02(), dirY, m12() * dirX);
float nm03 = Maths.fma(m03(), dirY, m13() * dirX);
return new Matrix4f(
properties & ~(PROPERTY_PERSPECTIVE | PROPERTY_IDENTITY | PROPERTY_TRANSLATION),
Maths.fma(m00(), -dirX, m10() * dirY),
Maths.fma(m01(), -dirX, m11() * dirY),
Maths.fma(m02(), -dirX, m12() * dirY),
Maths.fma(m03(), -dirX, m13() * dirY),
nm00,
nm01,
nm02,
nm03,
m20(),
m21(),
m22(),
m23(),
m30(),
m31(),
m32(),
m33()
);
}

//endregion

//region scale

public static Matrix4f scaling(float x, float y, float z) {
boolean one = Maths.absEqualsOne(x) && Maths.absEqualsOne(y) && Maths.absEqualsOne(z);
return new Matrix4f(
36 changes: 34 additions & 2 deletions modules/freeworld.math/src/main/java/freeworld/math/Vector2d.java
Original file line number Diff line number Diff line change
@@ -4,8 +4,8 @@
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
* License as published by the Free Software Foundation;
* only version 2.1 of the License.
*/

package freeworld.math;
@@ -26,4 +26,36 @@ public static double distanceSquared(double x1, double y1, double x2, double y2)
final double dy = y1 - y2;
return dx * dx + dy * dy;
}

public Vector2d add(double x, double y) {
return new Vector2d(x() + x, y() + y);
}

public Vector2d add(Vector2d v) {
return new Vector2d(x() + v.x(), y() + v.y());
}

public Vector2d sub(double x, double y) {
return new Vector2d(x() - x, y() - y);
}

public Vector2d sub(Vector2d v) {
return new Vector2d(x() - v.x(), y() - v.y());
}

public Vector2d mul(double x, double y) {
return new Vector2d(x() * x, y() * y);
}

public Vector2d mul(Vector2d v) {
return new Vector2d(x() * v.x(), y() * v.y());
}

public Vector2d div(double x, double y) {
return new Vector2d(x() / x, y() / y);
}

public Vector2d div(Vector2d v) {
return new Vector2d(x() / v.x(), y() / v.y());
}
}
20 changes: 20 additions & 0 deletions modules/freeworld.math/src/main/java/freeworld/math/Vector2f.java
Original file line number Diff line number Diff line change
@@ -25,11 +25,31 @@ public Vector2f add(float x, float y) {
return new Vector2f(x() + x, y() + y);
}

public Vector2f add(Vector2f v) {
return new Vector2f(x() + v.x, y() + v.y);
}

public Vector2f sub(float x, float y) {
return new Vector2f(x() - x, y() - y);
}

public Vector2f sub(Vector2f v) {
return new Vector2f(x() - v.x, y() - v.y);
}

public Vector2f mul(float x, float y) {
return new Vector2f(x() * x, y() * y);
}

public Vector2f mul(Vector2f v) {
return new Vector2f(x() * v.x, y() * v.y);
}

public Vector2f div(float x, float y) {
return new Vector2f(x() / x, y() / y);
}

public Vector2f div(Vector2f v) {
return new Vector2f(x() / v.x, y() / v.y);
}
}
41 changes: 28 additions & 13 deletions modules/freeworld.math/src/main/java/freeworld/math/Vector3d.java
Original file line number Diff line number Diff line change
@@ -21,32 +21,47 @@ public Vector3d(double d) {
this(d, d, d);
}

public Vector3d add(double x, double y, double z) {
return new Vector3d(x() + x, y() + y, z() + z);
}

public Vector3d add(Vector3d v) {
return add(v.x(), v.y(), v.z());
return new Vector3d(x() + v.x(), y() + v.y(), z() + v.z());
}

public Vector3d add(double x, double y, double z) {
return new Vector3d(this.x + x, this.y + y, this.z + z);
public Vector3d sub(double x, double y, double z) {
return new Vector3d(x() - x, y() - y, z() - z);
}

public Vector3d sub(Vector3d v) {
return new Vector3d(x() - v.x(), y() - v.y(), z() - v.z());
}

public Vector3d mul(double x, double y, double z) {
return new Vector3d(this.x * x, this.y * y, this.z * z);
return new Vector3d(x() * x, y() * y, z() * z);
}

public Vector3d mul(Vector3d v) {
return new Vector3d(x() * v.x(), y() * v.y(), z() * v.z());
}

public Vector3d div(double x, double y, double z) {
return new Vector3d(x() / x, y() / y, z() / z);
}

public Vector3d lerp(Vector3d v, double t) {
public Vector3d div(Vector3d v) {
return new Vector3d(x() / v.x(), y() / v.y(), z() / v.z());
}

public Vector3d linearInterpolate(Vector3d v, double t) {
return new Vector3d(
Maths.fma(v.x() - x, t, x),
Maths.fma(v.y() - y, t, y),
Maths.fma(v.z() - z, t, z)
Maths.linearInterpolate(x, v.x(), t),
Maths.linearInterpolate(y, v.y(), t),
Maths.linearInterpolate(z, v.z(), t)
);
}

public Vector3f toVector3f() {
return new Vector3f((float) x, (float) y, (float) z);
}

@Deprecated
public Vector3i toVector3iFloor() {
return new Vector3i(Maths.floorToInt(x), Maths.floorToInt(y), Maths.floorToInt(z));
}
}
24 changes: 24 additions & 0 deletions modules/freeworld.math/src/main/java/freeworld/math/Vector3f.java
Original file line number Diff line number Diff line change
@@ -25,10 +25,34 @@ public Vector3f add(float x, float y, float z) {
return new Vector3f(x() + x, y() + y, z() + z);
}

public Vector3f add(Vector3f v) {
return new Vector3f(x() + v.x(), y() + v.y(), z() + v.z());
}

public Vector3f sub(float x, float y, float z) {
return new Vector3f(x() - x, y() - y, z() - z);
}

public Vector3f sub(Vector3f v) {
return new Vector3f(x() - v.x(), y() - v.y(), z() - v.z());
}

public Vector3f mul(float x, float y, float z) {
return new Vector3f(x() * x, y() * y, z() * z);
}

public Vector3f mul(Vector3f v) {
return new Vector3f(x() * v.x(), y() * v.y(), z() * v.z());
}

public Vector3f div(float x, float y, float z) {
return new Vector3f(x() / x, y() / y, z() / z);
}

public Vector3f div(Vector3f v) {
return new Vector3f(x() / v.x(), y() / v.y(), z() / v.z());
}

public Vector3d toVector3d() {
return new Vector3d(x, y, z);
}
32 changes: 30 additions & 2 deletions modules/freeworld.math/src/main/java/freeworld/math/Vector3i.java
Original file line number Diff line number Diff line change
@@ -21,12 +21,40 @@ public Vector3i(int d) {
this(d, d, d);
}

public Vector3i add(int x, int y, int z) {
return new Vector3i(x() + x, y() + y, z() + z);
}

public Vector3i add(Vector3i v) {
return new Vector3i(x + v.x, y + v.y, z + v.z);
return new Vector3i(x() + v.x(), y() + v.y(), z() + v.z());
}

public Vector3i sub(int x, int y, int z) {
return new Vector3i(x() - x, y() - y, z() - z);
}

public Vector3i sub(Vector3i v) {
return new Vector3i(x() - v.x(), y() - v.y(), z() - v.z());
}

public Vector3i mul(int x, int y, int z) {
return new Vector3i(x() * x, y() * y, z() * z);
}

public Vector3i mul(Vector3i v) {
return new Vector3i(x() * v.x(), y() * v.y(), z() * v.z());
}

public Vector3i mul(int i) {
return new Vector3i(x * i, y * i, z * i);
return new Vector3i(x() * i, y() * i, z() * i);
}

public Vector3i div(int x, int y, int z) {
return new Vector3i(x() / x, y() / y, z() / z);
}

public Vector3i div(Vector3i v) {
return new Vector3i(x() / v.x(), y() / v.y(), z() / v.z());
}

public Vector3i floorDiv(int i) {
32 changes: 32 additions & 0 deletions modules/freeworld.math/src/main/java/freeworld/math/Vector4f.java
Original file line number Diff line number Diff line change
@@ -21,6 +21,38 @@ public Vector4f(float d) {
this(d, d, d, d);
}

public Vector4f add(float x, float y, float z, float w) {
return new Vector4f(x() + x, y() + y, z() + z, w() + w);
}

public Vector4f add(Vector4f v) {
return new Vector4f(x() + v.x(), y() + v.y(), z() + v.z(), w() + v.w());
}

public Vector4f sub(float x, float y, float z, float w) {
return new Vector4f(x() - x, y() - y, z() - z, w() - w);
}

public Vector4f sub(Vector4f v) {
return new Vector4f(x() - v.x(), y() - v.y(), z() - v.z(), w() - v.w());
}

public Vector4f mul(float x, float y, float z, float w) {
return new Vector4f(x() * x, y() * y, z() * z, w() * w);
}

public Vector4f mul(Vector4f v) {
return new Vector4f(x() * v.x(), y() * v.y(), z() * v.z(), w() * v.w());
}

public Vector4f div(float x, float y, float z, float w) {
return new Vector4f(x() / x, y() / y, z() / z, w() / w);
}

public Vector4f div(Vector4f v) {
return new Vector4f(x() / v.x(), y() / v.y(), z() / v.z(), w() / v.w());
}

public Vector4f mul(Matrix4f mat) {
int prop = mat.properties();
if ((prop & Matrix4f.PROPERTY_IDENTITY) != 0)
32 changes: 32 additions & 0 deletions modules/freeworld.math/src/main/java/freeworld/math/Vector4i.java
Original file line number Diff line number Diff line change
@@ -20,4 +20,36 @@ public record Vector4i(int x, int y, int z, int w) {
public Vector4i(int d) {
this(d, d, d, d);
}

public Vector4i add(int x, int y, int z, int w) {
return new Vector4i(x() + x, y() + y, z() + z, w() + w);
}

public Vector4i add(Vector4i v) {
return new Vector4i(x() + v.x(), y() + v.y(), z() + v.z(), w() + v.w());
}

public Vector4i sub(int x, int y, int z, int w) {
return new Vector4i(x() - x, y() - y, z() - z, w() - w);
}

public Vector4i sub(Vector4i v) {
return new Vector4i(x() - v.x(), y() - v.y(), z() - v.z(), w() - v.w());
}

public Vector4i mul(int x, int y, int z, int w) {
return new Vector4i(x() * x, y() * y, z() * z, w() * w);
}

public Vector4i mul(Vector4i v) {
return new Vector4i(x() * v.x(), y() * v.y(), z() * v.z(), w() * v.w());
}

public Vector4i div(int x, int y, int z, int w) {
return new Vector4i(x() / x, y() / y, z() / z, w() / w);
}

public Vector4i div(Vector4i v) {
return new Vector4i(x() / v.x(), y() / v.y(), z() / v.z(), w() / v.w());
}
}

0 comments on commit da75736

Please sign in to comment.