Skip to content

Commit

Permalink
start work on boundingBox (#228) : soulsand seems to work
Browse files Browse the repository at this point in the history
  • Loading branch information
rom1504 committed Aug 9, 2015
1 parent 8c96307 commit 7d4e605
Showing 1 changed file with 28 additions and 5 deletions.
33 changes: 28 additions & 5 deletions lib/plugins/physics.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,12 @@ function inject(bot) {
doPhysicsTimer = null;
}

function typeToBlockHeight(type)
{
if(type===88) return 7/8;
else return 1;
}

function nextFrame(deltaSeconds) {
if(deltaSeconds < EPSILON) return; // too fast
var pos = bot.entity.position;
Expand Down Expand Up @@ -128,7 +134,7 @@ function inject(bot) {

// limit speed
var currentMaxGroundSpeed;
var underBlock=bot.blockAt(pos.offset(0,-1,0));
var underBlock=bot.blockAt(new Vec3(pos.x,Math.ceil(pos.y-1.002),pos.z));
if(underBlock && underBlock.type===88)
currentMaxGroundSpeed=physics.maxGroundSpeedSoulSand;
else
Expand Down Expand Up @@ -170,15 +176,16 @@ function inject(bot) {
}
}


bot.entity.onGround = false;
if(vel.y !== 0) {
pos.y += vel.y * deltaSeconds;
var playerHalfHeight = physics.playerHeight / 2;
var blockY = Math.floor(pos.y + playerHalfHeight + math.sign(vel.y) * playerHalfHeight);
var aBlockY=pos.y + playerHalfHeight + math.sign(vel.y) * playerHalfHeight;
var blockY = Math.floor(aBlockY);
boundingBoxMin = new Vec3(boundingBox.min.x, blockY, boundingBox.min.z);
boundingBoxMax = new Vec3(boundingBox.max.x, blockY, boundingBox.max.z);
if(collisionInRange(boundingBoxMin, boundingBoxMax)) {
var highestBlock=collisionInRangeHighest(boundingBoxMin,boundingBoxMax);
if(highestBlock!==-1 && highestBlock>=(aBlockY-blockY)) {
pos.y = blockY + (vel.y < 0 ? 1 : -physics.playerHeight) * 1.001;
bot.entity.onGround = vel.y < 0 ? true : bot.entity.onGround;
vel.y = 0;
Expand Down Expand Up @@ -206,6 +213,22 @@ function inject(bot) {
return false;
}


function collisionInRangeHighest(boundingBoxMin, boundingBoxMax) {
var cursor = new Vec3(0, 0, 0);
var block;
var max=-1;
for(cursor.x = boundingBoxMin.x; cursor.x <= boundingBoxMax.x; cursor.x++) {
for(cursor.y = boundingBoxMin.y; cursor.y <= boundingBoxMax.y; cursor.y++) {
for(cursor.z = boundingBoxMin.z; cursor.z <= boundingBoxMax.z; cursor.z++) {
block = bot.blockAt(cursor);
if(block && block.boundingBox === 'block') max=Math.max(max,typeToBlockHeight(block.type));
}
}
}
return max;
}

function calcGroundSpeedSquared() {
var vel = bot.entity.velocity;
return vel.x * vel.x + vel.z * vel.z;
Expand All @@ -216,7 +239,7 @@ function inject(bot) {
return {
min: new Vec3(
pos.x - physics.playerApothem,
pos.y,
Math.ceil(pos.y-0.002),
pos.z - physics.playerApothem
).floor(),
max: new Vec3(
Expand Down

1 comment on commit 7d4e605

@Corgano
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The bot seems to continuously sink into the block until the server updates the position, as seen by making the bot navigate to a soul sand block, it will sink into it further than should be possible

My best guess is the onground flag not being set using the less-than-full block height. Because the bot is below the top edge of a full block, onground is not being set to true.

Please sign in to comment.