Jump to content

My Entity jumping like a slime can't turn


MCArTyR

Recommended Posts

My model has body, head and legFrontLeft, legFrontRight..., but legs and body are empty inside. In head alone, there are three elements.

Spoiler

FrogModel



public class FrogModel<T extends FrogEntity> extends EntityModel<T> {
    private final ModelRenderer head;
    private final ModelRenderer body;
    private final ModelRenderer legFrontLeft;
    private final ModelRenderer legFrontRight;
    private final ModelRenderer legBackLeft;
    private final ModelRenderer legBackRight;

    public FrogModel() {
        texWidth = 32;
        texHeight = 32;

        head = new ModelRenderer(this);
        head.setPos(0.0F, 24.0F, 0.0F);
        head.texOffs(0, 0).addBox(-4.0F, -8.0F, -4.0F, 8.0F, 8.0F, 8.0F, 0.0F, false);
        head.texOffs(0, 2).addBox(-3.0F, -9.0F, -2.0F, 1.0F, 1.0F, 1.0F, 0.0F, false);
        head.texOffs(0, 0).addBox(2.0F, -9.0F, -2.0F, 1.0F, 1.0F, 1.0F, 0.0F, false);

        body = new ModelRenderer(this);
        body.setPos(0.0F, 24.0F, 0.0F);


        legFrontLeft = new ModelRenderer(this);
        legFrontLeft.setPos(0.0F, 24.0F, 0.0F);


        legFrontRight = new ModelRenderer(this);
        legFrontRight.setPos(0.0F, 24.0F, 0.0F);


        legBackLeft = new ModelRenderer(this);
        legBackLeft.setPos(0.0F, 24.0F, 0.0F);


        legBackRight = new ModelRenderer(this);
        legBackRight.setPos(0.0F, 24.0F, 0.0F);

    }

    @Override
    public void setupAnim(T entityIn, float limbSwing, float limbSwingAmount, float ageInTicks, float netHeadYaw, float headPitch) {
        this.head.xRot = headPitch * ((float)Math.PI / 180F);
        this.head.yRot = netHeadYaw * ((float)Math.PI / 180F);
        this.body.xRot = ((float)Math.PI / 2F);
        this.legFrontRight.xRot = MathHelper.cos(limbSwing * 0.6662F) * 1.4F * limbSwingAmount;
        this.legFrontLeft.xRot = MathHelper.cos(limbSwing * 0.6662F + (float)Math.PI) * 1.4F * limbSwingAmount;
        this.legBackRight.xRot = MathHelper.cos(limbSwing * 0.6662F + (float)Math.PI) * 1.4F * limbSwingAmount;
        this.legBackLeft.xRot = MathHelper.cos(limbSwing * 0.6662F) * 1.4F * limbSwingAmount;
    }

    @Override
    public void renderToBuffer(MatrixStack matrixStack, IVertexBuilder buffer, int packedLight, int packedOverlay, float red, float green, float blue, float alpha){
        head.render(matrixStack, buffer, packedLight, packedOverlay);
        body.render(matrixStack, buffer, packedLight, packedOverlay);
        legFrontLeft.render(matrixStack, buffer, packedLight, packedOverlay);
        legFrontRight.render(matrixStack, buffer, packedLight, packedOverlay);
        legBackLeft.render(matrixStack, buffer, packedLight, packedOverlay);
        legBackRight.render(matrixStack, buffer, packedLight, packedOverlay);
    }

    public void setRotationAngle(ModelRenderer modelRenderer, float x, float y, float z) {
        modelRenderer.xRot = x;
        modelRenderer.yRot = y;
        modelRenderer.zRot = z;
    }
}

 

Spoiler

FrogRenderer



public class FrogRenderer extends MobRenderer<FrogEntity, FrogModel<FrogEntity>> {

    protected static final ResourceLocation TEXTURE = new ResourceLocation(Main.MOD_ID, "textures/entity/frog.png");

    public FrogRenderer(EntityRendererManager renderManagerIn) {
        super(renderManagerIn, new FrogModel<>(), 0.7F);
    }

    @Override
    public ResourceLocation getTextureLocation(FrogEntity p_110775_1_) {
        return TEXTURE;
    }
}

 

Spoiler

FrogEntity



public class FrogEntity extends AnimalEntity {

    public FrogEntity(EntityType<? extends AnimalEntity> type, World worldIn) {
        super(type, worldIn);
        this.moveControl = new FrogMoveController(this);
    }

    public int getJumpDelay() {
        return this.random.nextInt(20) + 10;
    }

    public static AttributeModifierMap.MutableAttribute setCustomAttributes() {
        return MobEntity.createMobAttributes()
                .add(Attributes.MAX_HEALTH, 10.0)
                .add(Attributes.MOVEMENT_SPEED, 0.5);
    }

    @Override
    protected void registerGoals() {
        super.registerGoals();
        this.goalSelector.addGoal(0, new SwimGoal(this));
        this.goalSelector.addGoal(1, new PanicGoal(this, 1.25));
        this.goalSelector.addGoal(2, new BreedGoal(this, 1.1));
        this.goalSelector.addGoal(3, new TemptGoal(this, 1.1, Ingredient.of(Items.KELP), false));
        this.goalSelector.addGoal(4, new FollowParentGoal(this, 1.1));
        this.goalSelector.addGoal(5, new RandomSwimmingGoal(this, 0.5, 200));
        this.goalSelector.addGoal(6, new RandomWalkingGoal(this, 0.5, 200));
        this.goalSelector.addGoal(7, new LookAtGoal(this, PlayerEntity.class, 6.0F));
        this.goalSelector.addGoal(8, new LookRandomlyGoal(this));
    }

    @Override
    protected int getExperienceReward(PlayerEntity p_70693_1_) {
        return 1 + this.level.random.nextInt(5);
    }

    @Override
    public boolean isFood(ItemStack item) {
        return item.getItem().equals(Items.KELP);
    }

    @Nullable
    @Override
    public AgeableEntity getBreedOffspring(ServerWorld world, AgeableEntity p_241840_2_) {
        return RegistryHandler.FROG_ENTITY.get().create(world);
    }
}

 

Spoiler

FrogMoveController



public class FrogMoveController extends MovementController {
    private float yRot;
    private int jumpDelay;
    private final FrogEntity frog;
    private boolean isAggressive;

    public FrogMoveController(FrogEntity p_i45821_1_) {
        super(p_i45821_1_);
        this.frog = p_i45821_1_;
        this.yRot = 180.0F * p_i45821_1_.yRot / (float)Math.PI;
    }

    public void setDirection(float p_179920_1_, boolean p_179920_2_) {
        this.yRot = p_179920_1_;
        this.isAggressive = p_179920_2_;
    }

    public void setWantedMovement(double p_179921_1_) {
        this.speedModifier = p_179921_1_;
        this.operation = MovementController.Action.MOVE_TO;
    }

    public void tick() {
        this.mob.yRot = this.rotlerp(this.mob.yRot, this.yRot, 0.0F);
        this.mob.yHeadRot = this.mob.yRot;
        this.mob.yBodyRot = this.mob.yRot;
        if (this.operation != MovementController.Action.MOVE_TO) {
            this.mob.setZza(0.0F);
        } else {
            this.operation = MovementController.Action.WAIT;
            if (this.mob.isOnGround()) {
                this.mob.setSpeed((float)(this.speedModifier * this.mob.getAttributeValue(Attributes.MOVEMENT_SPEED)));
                if (this.jumpDelay-- <= 0) {
                    this.jumpDelay = this.frog.getJumpDelay();
                    if (this.isAggressive) {
                        this.jumpDelay /= 3;
                    }

                    this.frog.getJumpControl().jump();
//                    if (this.entity.doPlayJumpSound()) {
//                        this.entity.playSound(this.entity.getJumpSound(), this.entity.getSoundVolume(), this.entity.getSoundPitch());
//                    }
                } else {
                    this.frog.xxa = 0.0F;
                    this.frog.zza = 0.0F;
                    this.mob.setSpeed(0.0F);
                }
            } else {
                this.mob.setSpeed((float)(this.speedModifier * this.mob.getAttributeValue(Attributes.MOVEMENT_SPEED)));
            }

        }
    }
}

 

After /summon, Frog can only move one way, and cannot turn to go left, right, or back.

Edited by MCArTyR
Link to comment
Share on other sites

  • MCArTyR changed the title to My Entity jumping like a slime can't turn

At the moment I ended up with this FrogMoveController:

public class FrogMoveController extends MovementController {
    private float yRot;
    private int jumpDelay;
    private final FrogEntity frog;
    private boolean isAggressive;

    public FrogMoveController(FrogEntity p_i45821_1_) {
        super(p_i45821_1_);
        this.frog = p_i45821_1_;
        //this.yRot = 180.0F * p_i45821_1_.yRot / (float)Math.PI;
    }
    public void setWantedPosition(double p_75642_1_, double p_75642_3_, double p_75642_5_, double p_75642_7_) {
        this.wantedX = p_75642_1_;
        this.wantedY = p_75642_3_;
        this.wantedZ = p_75642_5_;
        this.speedModifier = p_75642_7_;
        if (this.operation != MovementController.Action.JUMPING) {
            this.operation = MovementController.Action.MOVE_TO;
        }

    }
    private boolean isWalkable(float p_234024_1_, float p_234024_2_) {
        PathNavigator pathnavigator = this.mob.getNavigation();
        if (pathnavigator != null) {
            NodeProcessor nodeprocessor = pathnavigator.getNodeEvaluator();
            if (nodeprocessor != null && nodeprocessor.getBlockPathType(this.mob.level, MathHelper.floor(this.mob.getX() + (double)p_234024_1_), MathHelper.floor(this.mob.getY()), MathHelper.floor(this.mob.getZ() + (double)p_234024_2_)) != PathNodeType.WALKABLE) {
                return false;
            }
        }

        return true;
    }
    public void tick() {
        if (this.operation == MovementController.Action.STRAFE) {
            float f = (float)this.mob.getAttributeValue(Attributes.MOVEMENT_SPEED);
            float f1 = (float)this.speedModifier * f;
            float f2 = this.strafeForwards;
            float f3 = this.strafeRight;
            float f4 = MathHelper.sqrt(f2 * f2 + f3 * f3);
            if (f4 < 1.0F) {
                f4 = 1.0F;
            }

            f4 = f1 / f4;
            f2 = f2 * f4;
            f3 = f3 * f4;
            float f5 = MathHelper.sin(this.mob.yRot * ((float)Math.PI / 180F));
            float f6 = MathHelper.cos(this.mob.yRot * ((float)Math.PI / 180F));
            float f7 = f2 * f6 - f3 * f5;
            float f8 = f3 * f6 + f2 * f5;
            if (!this.isWalkable(f7, f8)) {
                this.strafeForwards = 1.0F;
                this.strafeRight = 0.0F;
            }

            this.mob.setSpeed(f1);
            this.mob.setZza(this.strafeForwards);
            this.mob.setXxa(this.strafeRight);
            this.operation = MovementController.Action.WAIT;
        } else if (this.operation == MovementController.Action.MOVE_TO) {
            this.operation = MovementController.Action.WAIT;
            double d0 = this.wantedX - this.mob.getX();
            double d1 = this.wantedZ - this.mob.getZ();
            double d2 = this.wantedY - this.mob.getY();
            double d3 = d0 * d0 + d2 * d2 + d1 * d1;

            float f9 = (float)(MathHelper.atan2(d1, d0) * (double)(180F / (float)Math.PI)) - 90.0F;
            this.mob.yRot = this.rotlerp(this.mob.yRot, f9, 90.0F);
            this.mob.setSpeed((float)(this.speedModifier * this.mob.getAttributeValue(Attributes.MOVEMENT_SPEED)));
            BlockPos blockpos = this.mob.blockPosition();
            BlockState blockstate = this.mob.level.getBlockState(blockpos);
            Block block = blockstate.getBlock();
            VoxelShape voxelshape = blockstate.getCollisionShape(this.mob.level, blockpos);
            if (this.mob.isOnGround()) {
                this.mob.setSpeed((float)(this.speedModifier * this.mob.getAttributeValue(Attributes.MOVEMENT_SPEED)));
                if (this.jumpDelay-- <= 0) {
                    this.jumpDelay = this.frog.getJumpDelay();
                    if (this.isAggressive) {
                        this.jumpDelay /= 3;
                    }

                    this.frog.getJumpControl().jump();
                    //playSound
                } else {
                    this.mob.setSpeed(0.0F);
                }
            } else {
                this.mob.setSpeed((float)(this.speedModifier * this.mob.getAttributeValue(Attributes.MOVEMENT_SPEED)));
            }
            if (d2 > (double)this.mob.maxUpStep && d0 * d0 + d1 * d1 < (double)Math.max(1.0F, this.mob.getBbWidth()) || !voxelshape.isEmpty() && this.mob.getY() < voxelshape.max(Direction.Axis.Y) + (double)blockpos.getY() && !block.is(BlockTags.DOORS) && !block.is(BlockTags.FENCES)) {
                this.mob.getJumpControl().jump();
                this.operation = MovementController.Action.JUMPING;
            }
        } else if (this.operation == MovementController.Action.JUMPING) {
            this.mob.setSpeed((float)(this.speedModifier * this.mob.getAttributeValue(Attributes.MOVEMENT_SPEED)));
            if (this.mob.isOnGround()) {
                this.operation = MovementController.Action.WAIT;
            }
        } else {
            this.mob.setZza(0.0F);
        }

    }
}

But sometimes during the jump, his head rotates 360 or even 720 degrees. And sometimes he jumps in the wrong direction.

Edited by MCArTyR
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.