首先排除速度。尽管Motion的取值对于大部分实体来说接近速度,但是因为每游戏刻Motion可以变化多次,所以不一定等于这一刻的位移(此处忽略浮点数误差)。例如,TNT的位移是其Motion值(tick末)除以0.98再加上0.04。另外,部分实体不受Motion影响,即使Motion不为零也不会自主移动,如画,物品展示框,末影水晶等。
本帖最后由 晴路卡 于 2021-2-5 10:01 编辑
墙是不能穿过的障碍,
玫瑰是红色的花,
旗帜是胜利的标志,
提问的人是你,
Motion是记录有三个双精度浮点值的列表类型的实体共通标签。
墙是不能穿过的障碍,
玫瑰是红色的花,
旗帜是胜利的标志,
提问的人是你,
Motion是记录有三个双精度浮点值的列表类型的实体共通标签。
本帖最后由 QWERTY_52_38 于 2021-2-5 10:09 编辑
这是net.minecraft.entity.Entity类的代码
motion其实是Vector3D
复制代码
至于TNT这东西,只不过是重写了而已
复制代码
注意到this.setMotion(this.getMotion().scale(0.98D));
这就是TNT的motion要乘以0.98的原因
至于画和展示框……
它们都是HangingEntity的一种
复制代码
复制代码
这种东西一移动就被破坏了
这是net.minecraft.entity.Entity类的代码
motion其实是Vector3D
- private Vector3d motion = Vector3d.ZERO;
- public void move(MoverType typeIn, Vector3d pos) {
- if (this.noClip) {
- this.setBoundingBox(this.getBoundingBox().offset(pos));
- this.resetPositionToBB();
- } else {
- if (typeIn == MoverType.PISTON) {
- pos = this.handlePistonMovement(pos);
- if (pos.equals(Vector3d.ZERO)) {
- return;
- }
- }
- this.world.getProfiler().startSection("move");
- if (this.motionMultiplier.lengthSquared() > 1.0E-7D) {
- pos = pos.mul(this.motionMultiplier);
- this.motionMultiplier = Vector3d.ZERO;
- this.setMotion(Vector3d.ZERO);
- }
- pos = this.maybeBackOffFromEdge(pos, typeIn);
- Vector3d vector3d = this.getAllowedMovement(pos);
- if (vector3d.lengthSquared() > 1.0E-7D) {
- this.setBoundingBox(this.getBoundingBox().offset(vector3d));
- this.resetPositionToBB();
- }
- this.world.getProfiler().endSection();
- this.world.getProfiler().startSection("rest");
- this.collidedHorizontally = !MathHelper.epsilonEquals(pos.x, vector3d.x) || !MathHelper.epsilonEquals(pos.z, vector3d.z);
- this.collidedVertically = pos.y != vector3d.y;
- this.onGround = this.collidedVertically && pos.y < 0.0D;
- BlockPos blockpos = this.getOnPosition();
- BlockState blockstate = this.world.getBlockState(blockpos);
- this.updateFallState(vector3d.y, this.onGround, blockstate, blockpos);
- Vector3d vector3d1 = this.getMotion();
- if (pos.x != vector3d.x) {
- this.setMotion(0.0D, vector3d1.y, vector3d1.z);
- }
- if (pos.z != vector3d.z) {
- this.setMotion(vector3d1.x, vector3d1.y, 0.0D);
- }
- Block block = blockstate.getBlock();
- if (pos.y != vector3d.y) {
- block.onLanded(this.world, this);
- }
- if (this.onGround && !this.isSteppingCarefully()) {
- block.onEntityWalk(this.world, blockpos, this);
- }
- if (this.canTriggerWalking() && !this.isPassenger()) {
- double d0 = vector3d.x;
- double d1 = vector3d.y;
- double d2 = vector3d.z;
- if (!block.isIn(BlockTags.CLIMBABLE)) {
- d1 = 0.0D;
- }
- this.distanceWalkedModified = (float)((double)this.distanceWalkedModified + (double)MathHelper.sqrt(horizontalMag(vector3d)) * 0.6D);
- this.distanceWalkedOnStepModified = (float)((double)this.distanceWalkedOnStepModified + (double)MathHelper.sqrt(d0 * d0 + d1 * d1 + d2 * d2) * 0.6D);
- if (this.distanceWalkedOnStepModified > this.nextStepDistance && !blockstate.isAir(this.world, blockpos)) {
- this.nextStepDistance = this.determineNextStepDistance();
- if (this.isInWater()) {
- Entity entity = this.isBeingRidden() && this.getControllingPassenger() != null ? this.getControllingPassenger() : this;
- float f = entity == this ? 0.35F : 0.4F;
- Vector3d vector3d2 = entity.getMotion();
- float f1 = MathHelper.sqrt(vector3d2.x * vector3d2.x * (double)0.2F + vector3d2.y * vector3d2.y + vector3d2.z * vector3d2.z * (double)0.2F) * f;
- if (f1 > 1.0F) {
- f1 = 1.0F;
- }
- this.playSwimSound(f1);
- } else {
- this.playStepSound(blockpos, blockstate);
- }
- } else if (this.distanceWalkedOnStepModified > this.nextFlap && this.makeFlySound() && blockstate.isAir(this.world, blockpos)) {
- this.nextFlap = this.playFlySound(this.distanceWalkedOnStepModified);
- }
- }
- try {
- this.doBlockCollisions();
- } catch (Throwable throwable) {
- CrashReport crashreport = CrashReport.makeCrashReport(throwable, "Checking entity block collision");
- CrashReportCategory crashreportcategory = crashreport.makeCategory("Entity being checked for collision");
- this.fillCrashReport(crashreportcategory);
- throw new ReportedException(crashreport);
- }
- float f2 = this.getSpeedFactor();
- this.setMotion(this.getMotion().mul((double)f2, 1.0D, (double)f2));
- if (BlockPos.getAllInBox(this.getBoundingBox().shrink(0.001D)).noneMatch((p_233572_0_) -> {
- BlockState state = world.getBlockState(p_233572_0_);
- return state.isIn(BlockTags.FIRE) || state.isIn(Blocks.LAVA) || state.isBurning(world, p_233572_0_);
- }) && this.fire <= 0) {
- this.forceFireTicks(-this.getFireImmuneTicks());
- }
- if (this.isInWaterRainOrBubbleColumn() && this.isBurning()) {
- this.playSound(SoundEvents.ENTITY_GENERIC_EXTINGUISH_FIRE, 0.7F, 1.6F + (this.rand.nextFloat() - this.rand.nextFloat()) * 0.4F);
- this.forceFireTicks(-this.getFireImmuneTicks());
- }
- this.world.getProfiler().endSection();
- }
- }
至于TNT这东西,只不过是重写了而已
- public void tick() {
- if (!this.hasNoGravity()) {
- this.setMotion(this.getMotion().add(0.0D, -0.04D, 0.0D));
- }
- this.move(MoverType.SELF, this.getMotion());
- this.setMotion(this.getMotion().scale(0.98D));
- if (this.onGround) {
- this.setMotion(this.getMotion().mul(0.7D, -0.5D, 0.7D));
- }
- --this.fuse;
- if (this.fuse <= 0) {
- this.remove();
- if (!this.world.isRemote) {
- this.explode();
- }
- } else {
- this.func_233566_aG_();
- if (this.world.isRemote) {
- this.world.addParticle(ParticleTypes.SMOKE, this.getPosX(), this.getPosY() + 0.5D, this.getPosZ(), 0.0D, 0.0D, 0.0D);
- }
- }
- }
注意到this.setMotion(this.getMotion().scale(0.98D));
这就是TNT的motion要乘以0.98的原因
至于画和展示框……
它们都是HangingEntity的一种
- public void move(MoverType typeIn, Vector3d pos) {
- if (!this.world.isRemote && !this.removed && pos.lengthSquared() > 0.0D) {
- this.remove();
- this.onBroken((Entity)null);
- }
- }
- public void onBroken(@Nullable Entity brokenEntity) {
- this.playSound(SoundEvents.ENTITY_ITEM_FRAME_BREAK, 1.0F, 1.0F);
- this.dropItemOrSelf(brokenEntity, true);
- }
这种东西一移动就被破坏了
晴路卡 发表于 2021-2-5 09:58
墙是不能穿过的障碍,
玫瑰是红色的花,
旗帜是胜利的标志,
Pos也是。问的不是归类,是如何定义
QWERTY_52_38 发表于 2021-2-5 10:00
这是net.minecraft.entity.Entity类的代码
motion其实是Vector3D
这些代码近几天都看过,算是复习一遍吧。话说回来,定义是啥?总不能Vector3d(Vector with 3
doubles)吧。
lovexyn0827 发表于 2021-2-5 16:45
这些代码近几天都看过,算是复习一遍吧。话说回来,定义是啥?总不能Vector3d(Vector with 3
doubles) ...
这东西本来就没有统一定义
你可以重写他