Skip to content

Commit

Permalink
Per-block collision instead of being based on floor
Browse files Browse the repository at this point in the history
  • Loading branch information
Nixinova committed Jul 25, 2024
1 parent 95aef90 commit 0a8ab05
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 32 deletions.
5 changes: 5 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog

## Next
- Collision is now based per-block instead of from the lowest ground in a column
- Changed player height to 1.8 blocks
- Removed "floor" debug text line

## 0.0.18
*2024-07-25 00:39*
- Added block face culling
Expand Down
9 changes: 9 additions & 0 deletions src/com/nixinova/coords/Coord3.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,15 @@ private Coord3(double x, double y, double z) {
this.z = Coord1.fromPx(z);
}

@Override
public String toString() {
String result = "";
result += "block:" + "(" + this.x.toBlock() + "," + this.y.toBlock() + "," + this.z.toBlock() + ")";
result += ",";
result += "px:" + "(" + this.x.toPx() + "," + this.y.toPx() + "," + this.z.toPx() + ")";
return "{ " + result + " }";
}

// World pixel

public static Coord3 fromPx(double x, double y, double z) {
Expand Down
4 changes: 0 additions & 4 deletions src/com/nixinova/graphics/ScreenText.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,6 @@ public void drawMainInfo(Game game) {
graphics.drawString("FPS: " + fpsFmt, INDENT, SEP * ++i);
// Player block at foot
graphics.drawString("Block: " + formatCoord(position), INDENT, SEP * ++i);
// Lowest ground Y value
var blockPos = position.toBlock();
var groundY = game.world.getGroundY(blockPos.x, blockPos.z);
graphics.drawString("Floor: " + groundY, INDENT, SEP * ++i);
// Camera pos and angle
var camPosFmt = formatCoord(game.controls.getCameraPosition());
graphics.drawString("Camera: " + camPosFmt, INDENT, SEP * ++i);
Expand Down
65 changes: 39 additions & 26 deletions src/com/nixinova/player/Controller.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.nixinova.coords.Coord1;
import com.nixinova.coords.Coord3;
import com.nixinova.coords.PxCoord;
import com.nixinova.coords.TxCoord;
import com.nixinova.input.InputHandler;
import com.nixinova.input.Keys;
import com.nixinova.main.Game;
Expand Down Expand Up @@ -101,25 +102,40 @@ public void tick(InputHandler input) {

// Ground checks
if (this.game.player.isWithinWorld(this.game.world)) {
if (onGround()) {
// Above ground
if (aboveGround()) {
// Fall due to gravity
yMove -= Options.gravity;
// Bounce back if put below ground
if (belowGround())
yMove += Options.gravity;
}
// Below ground
else if (belowGround()) {
// Shove player back to surface
BlockCoord curBlock = this.pos.toBlock();
PxCoord curPx = this.pos.toPx();
int newBlockY = this.game.world.getMinGroundY(curBlock.x, curBlock.z);
newBlockY += 1; // to put player above the block
this.pos = Coord3.fromPx(curPx.x, Coord1.blockToPx(newBlockY), curPx.z);
}
// On ground
else {
// Allow jumping
if (kbd.pressedKey(Keys.JUMP)) {
isJumping = true;
this.isJumping = true;
kbd.startKeyCooldown(Keys.JUMP);
}
} else if (aboveGround()) {
yMove -= Options.gravity;
} else if (belowGround()) {
// Push player up to be on the ground
this.pos = Coord3.fromPx(this.pos.toPx().x, getGroundY().toPx(), this.pos.toPx().z);
yMove = 0.001D;
}
} else {
}
// Outside the world
else {
// Fall when outside of world
if (yMove == 0)
yMove = -0.5;
// Acceleration due to gravity
yMove *= 1 + Math.pow(1 + Options.gravity, 2);
}
// Acceleration due to gravity
yMove *= 1 + Math.pow(1 + Options.gravity, 2);

// Mouse look boundaries
double maxTilt = Math.toRadians(90);
Expand All @@ -133,7 +149,7 @@ public void tick(InputHandler input) {
System.exit(1);
}
if (kbd.pressedKey(Keys.F3)) {
debugShown = !debugShown;
this.debugShown = !this.debugShown;
kbd.startKeyCooldown(Keys.F3);
}

Expand Down Expand Up @@ -198,23 +214,20 @@ public Vector3 getViewDirection() {
return new Vector3(x, y, z);
}

private boolean onGround() {
final double groundBuffer = 0.1;

return Math.abs(this.pos.toPx().y - getGroundY().toPx()) < groundBuffer;
}

private boolean aboveGround() {
return !this.onGround() && this.pos.toPx().y > getGroundY().toPx();
// Above the ground if the block one texel beneath the player's feet is air
TxCoord curTx = this.pos.toTx();
BlockCoord blockOneTxDown = Coord3.fromTx(curTx.x,curTx.y-1,curTx.z).toBlock();
boolean belowTxIsAir = this.game.world.isAir(blockOneTxDown.x, blockOneTxDown.y, blockOneTxDown.z);
return belowTxIsAir;
}

private boolean belowGround() {
return !this.onGround() & !this.aboveGround();
}

private Coord1 getGroundY() {
BlockCoord blockCoord = this.getFootPosition().toBlock();
int groundY = this.game.world.getGroundY(blockCoord.x, blockCoord.z);
return Coord1.fromSubBlock(groundY);
// Below the ground if the block one texel above the player's feet is air
TxCoord footTx = this.pos.toTx();
BlockCoord blockOneTxUp = Coord3.fromTx(footTx.x, footTx.y + 1, footTx.z).toBlock();
boolean inVoid = footTx.y <= 0;
boolean aboveTxIsAir = this.game.world.isAir(blockOneTxUp.x, blockOneTxUp.y, blockOneTxUp.z);
return inVoid || !aboveTxIsAir;
}
}
2 changes: 1 addition & 1 deletion src/com/nixinova/player/Player.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import com.nixinova.world.World;

public class Player {
public static final double PLAYER_HEIGHT = 2.0;
public static final double PLAYER_HEIGHT = 1.8;

private Coord3 position;
private BlockCoord lookingAtBlock;
Expand Down
2 changes: 1 addition & 1 deletion src/com/nixinova/world/World.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public boolean isWithinWorld(int blockX, int blockY, int blockZ) {
}

/** returns -1 if all is air */
public int getGroundY(int blockX, int blockZ) {
public int getMinGroundY(int blockX, int blockZ) {
for (int i = 0; i < SKY_Y; i++) {
if (this.getTextureAt(blockX, i, blockZ) == null) {
return i - 1;
Expand Down

0 comments on commit 0a8ab05

Please sign in to comment.