Jump to content

1.12.2 Custom Train Entity not rotating as expected


MrRumpell

Recommended Posts

Hey all!

I've made a custom train entity that extends the EntityMinecart class. However i'm having some issues with the Yaw when on rails (north-south vs east-west) and the rotation around curves seem backwards (counterclockwise when should be clockwise and visa versa) 

Ive made multiple adjustments to no avail. Is there anyone in here that would mind taking a glance at this code and seeing if you can see anything I'm missing? 

 

public class TrainEntity extends EntityMinecart implements IWorldNameable{
    private boolean isFirstUpdate = true;

    public TrainEntity(World world) {
        super(world);
    }

    @Override
    public EntityMinecart.Type getType() {
        return EntityMinecart.Type.RIDEABLE;
    }

    @Override
    public void onUpdate() {
        super.onUpdate();
        if (isFirstUpdate) {
            adjustInitialRotation();
            isFirstUpdate = false;
        } else {
            adjustRotationOnCurve();
        }
    }

    private void adjustInitialRotation() {
        BlockPos pos = new BlockPos(this.posX, this.posY - 1, this.posZ);
        IBlockState state = this.world.getBlockState(pos);
        if (state.getBlock() instanceof BlockRailBase) {
            BlockRailBase.EnumRailDirection railDirection = ((BlockRailBase) state.getBlock()).getRailDirection(world, pos, state, this);
            this.rotationYaw = getInitialYaw(railDirection);
        }
    }

    private void adjustRotationOnCurve() {
        BlockPos pos = new BlockPos(this.posX, this.posY - 1, this.posZ);
        IBlockState state = this.world.getBlockState(pos);
        if (state.getBlock() instanceof BlockRailBase) {
            BlockRailBase.EnumRailDirection railDirection = ((BlockRailBase) state.getBlock()).getRailDirection(world, pos, state, this);
            if (isCurve(railDirection)) {
                this.rotationYaw = getCurveYaw(railDirection, this.rotationYaw);
            }
        }
    }

    private boolean isCurve(BlockRailBase.EnumRailDirection railDirection) {
        return railDirection == BlockRailBase.EnumRailDirection.NORTH_EAST ||
               railDirection == BlockRailBase.EnumRailDirection.NORTH_WEST ||
               railDirection == BlockRailBase.EnumRailDirection.SOUTH_EAST ||
               railDirection == BlockRailBase.EnumRailDirection.SOUTH_WEST;
    }

    private float getInitialYaw(BlockRailBase.EnumRailDirection railDirection) {
        switch (railDirection) {
            case EAST_WEST:
                return 90f;
            case NORTH_SOUTH:
                return 0f;
            // Add cases for diagonals if your model is designed to align with them initially
            default:
                return 0f; // Default North
        }
    }

    private float getCurveYaw(BlockRailBase.EnumRailDirection railDirection, float currentYaw) {
        // This simplified approach assumes a 90-degree rotation for curves.
        // You'll need to adjust this based on your rail's actual geometry and how you want the train to appear when entering/exiting curves.
        switch (railDirection) {
            case NORTH_EAST:
                return (currentYaw + 90) % 360;
            case NORTH_WEST:
                return (currentYaw - 90 + 360) % 360;
            case SOUTH_EAST:
                return (currentYaw + 90) % 360;
            case SOUTH_WEST:
                return (currentYaw - 90 + 360) % 360;
            default:
                return currentYaw; // No adjustment for straight paths
        }
    }
}

 

Edited by MrRumpell
added version tag
Link to comment
Share on other sites

  • MrRumpell changed the title to 1.12.2 Custom Train Entity not rotating as expected

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.



×
×
  • Create New...

Important Information

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