From 7d4e60549251666733c18f12f8ffe5a721aa50bf Mon Sep 17 00:00:00 2001 From: Romain Beaumont Date: Sun, 9 Aug 2015 05:04:35 +0200 Subject: [PATCH] start work on boundingBox (#228) : soulsand seems to work --- lib/plugins/physics.js | 33 ++++++++++++++++++++++++++++----- 1 file changed, 28 insertions(+), 5 deletions(-) diff --git a/lib/plugins/physics.js b/lib/plugins/physics.js index 4904b51df..37a680b53 100644 --- a/lib/plugins/physics.js +++ b/lib/plugins/physics.js @@ -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; @@ -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 @@ -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; @@ -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; @@ -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(