if(dy != 0.0D) onGround = dy < 0.0D && dy != movedY;
stuckHorizontally = false; if(Math.abs(dz) > Math.abs(dx)) { if(movedZ == 0.0D) stuckHorizontally = true; } else { if(movedX == 0.0D) stuckHorizontally = true; }
float secDiff = (System.currentTimeMillis() - lastTick) / 1000.0F; // , if(secDiff > 0.5F) // , , // , , secDiff = 0.5F; lastTick = System.currentTimeMillis(); // moveForward(speed * secDiff); if(stuckHorizontally) { // if(onGround) { // if(lastJumping + 500 < System.currentTimeMillis()) { jump(); // lastJumping = System.currentTimeMillis(); // . :) } } } applyForces(secDiff); // motX, motY motZ
protected void moveForward(float distance) { /* * . * — distance — , */ moveSpeedX = (float) -Math.sin(this.yaw / 180.0F * (float) Math.PI) * distance; moveSpeedZ = (float) Math.cos(this.yaw / 180.0F * (float) Math.PI) * distance; // — Java, - Java, -0.0F ... if(moveSpeedX == -0.0F) moveSpeedX = 0.0F; if(moveSpeedZ == -0.0F) moveSpeedZ = 0.0F; // , ... if(moveSpeedX != 0.0F || moveSpeedZ != 0.0F) this.move(moveSpeedX, 0.0D, moveSpeedZ); // , , . , , Minecraft }
As I said before, after executing this piece of code, the move function should set the stuckHorizontally flag according to the conditions above. It does not change the onGround flag, because dy is 0, this flag will then be applied by applyForces . If stuckHorizontally is set to true and onGround too, then you need to jump. The jump function will do this: protected void jump() { this.motY = jumpSpeed; }
Just give the object speed up ... Well, we use force . Here you can show creativity, depending on the world, the environment in which the object is located, friction and other interesting buns. I will give a simple example: private void applyForces(float secDiff) { // , -. if(Math.abs(this.motX) < 0.1D) this.motX = 0.0F; if(Math.abs(this.motY) < 0.1D) this.motY = 0.0F; if(Math.abs(this.motZ) < 0.1D) this.motZ = 0.0F; /** * . , * , * . . */ float friction = Constants.ENTITY_FLYING_FRICTION; // 0.98. — . 0.0 — . if(this.motY * secDiff != 0.0D || this.motX * secDiff != 0.0D || this.motZ * secDiff != 0.0D) // 0, , this.move(this.motX * secDiff, this.motY * secDiff, this.motZ * secDiff); this.motY -= Constants.GRAVITATION * secDiff; // /* * */ this.motX -= this.motX * friction * secDiff; this.motY -= this.motY * friction * secDiff; this.motZ -= this.motZ * friction * secDiff; }
Basically, that's the whole algorithm. (Be careful with friction ... my function is not designed for secDiff more than 1 or friction more than 1!) public class Entity { public float speed = 3.0F; public float jumpSpeed = 6.0F; private long lastJumping = 0; private boolean stuckHorizontally = false; public boolean onGround = true; protected float moveSpeedX; protected float moveSpeedZ; public float motX; public float motY; public float motZ; public void tick() { float secDiff = (System.currentTimeMillis() - lastTick) / 1000.0F; // , if(secDiff > 0.5F) // , , // , , secDiff = 0.5F; lastTick = System.currentTimeMillis(); // moveForward(speed * secDiff); if(stuckHorizontally) { // if(onGround) { // if(lastJumping + 500 < System.currentTimeMillis()) { jump(); // lastJumping = System.currentTimeMillis(); // . :) } } } applyForces(secDiff); // motX, motY motZ } private void moveForward(float distance) { /* * . * — distance — , */ moveSpeedX = (float) -Math.sin(this.yaw / 180.0F * (float) Math.PI) * distance; moveSpeedZ = (float) Math.cos(this.yaw / 180.0F * (float) Math.PI) * distance; // — Java, - Java, -0.0F ... if(moveSpeedX == -0.0F) moveSpeedX = 0.0F; if(moveSpeedZ == -0.0F) moveSpeedZ = 0.0F; // , ... if(moveSpeedX != 0.0F || moveSpeedZ != 0.0F) this.move(moveSpeedX, 0.0D, moveSpeedZ); // , , . , , Minecraft } private void applyForces(float secDiff) { // , -. if(Math.abs(this.motX) < 0.1D) this.motX = 0.0F; if(Math.abs(this.motY) < 0.1D) this.motY = 0.0F; if(Math.abs(this.motZ) < 0.1D) this.motZ = 0.0F; /** * . , * , * . . */ float friction = Constants.ENTITY_FLYING_FRICTION; // 0.98. — . 0.0 — . if(this.motY * secDiff != 0.0D || this.motX * secDiff != 0.0D || this.motZ * secDiff != 0.0D) // 0, , this.move(this.motX * secDiff, this.motY * secDiff, this.motZ * secDiff); this.motY -= Constants.GRAVITATION * secDiff; // /* * */ this.motX -= this.motX * friction * secDiff; this.motY -= this.motY * friction * secDiff; this.motZ -= this.motZ * friction * secDiff; } public abstract void move(float dx, float dy, float dz); }
Source: https://habr.com/ru/post/165631/
All Articles