Jump to content

How to make player slowly look at entity's center bounding box


fernthedev

Recommended Posts

Well basically I want to make the player, when looking at an entity's bounding box, to slowly move to the center. I've got most of it working except I can't get it to move to the center as it can't determine whether it should move the crosshair left of the center or right. My code is currently this:

 private Vec2f handleAimAssist(float targetYaw, float targetPitch)
    {
        Minecraft mc = Minecraft.getInstance();
        PlayerEntity player = mc.player;

        if(player == null)
        {
            return new Vec2f(targetPitch, targetYaw);
        }

        float resultPitch = targetPitch;
        float resultYaw = targetYaw;

        if(mc.objectMouseOver != null && mc.objectMouseOver.getType() == RayTraceResult.Type.ENTITY)
        {
            EntityRayTraceResult entityRayTraceResult = (EntityRayTraceResult) mc.objectMouseOver;
            Entity entity = entityRayTraceResult.getEntity();

            ControllerOptions.AimAssistMode mode = getMode(entity); // Config setting option

            if(entity instanceof LivingEntity && mode != null && mode != ControllerOptions.AimAssistMode.NONE) // Avoid checking entities such as drops or tnt
            {

                Vec3d rayHit = entityRayTraceResult.getHitVec(); // Look where the raytrace of the player's view hits

                AxisAlignedBB targetBox = entity.getBoundingBox().shrink(5);
                Vec3d targetCoords = targetBox.getCenter(); // Get the center of the entity which is a good starting point

                double assistIntensity = (Controllable.getOptions().getAimAssistIntensity() / 100.0); // Can be substituted for 1 for debugging code

                if(mode.sensitivity())
                {
                    resultYaw *= (float) (0.14 * assistIntensity); // Slows the sensitivity to stop slingshotting the bounding box. It can still be slingshotted though if attempted.
                    resultPitch *= (float) (0.12 * assistIntensity); // Slows the sensitivity to stop slingshotting the bounding box. It can still be slingshotted though if attempted.
                }

                if(mode.aim())
                {
                    Vec3d targetXCoords = new Vec3d(targetCoords.x, 0, 0); // To accurately measure distance
                    Vec3d rayXCoords = new Vec3d(rayHit.x, 0, 0); // Measure accurately distance

                    Vec3d targetYCoords = new Vec3d(0, targetCoords.y, 0); // Measure accurately distance
                    Vec3d rayYCoords = new Vec3d(0, rayHit.y, 0); // Measure accurately distance

                    double xDir = changeYDirection(rayHit.x, entity.getBoundingBox().minX, entity.getBoundingBox().maxX, 0.5);
                    // Only modify X if it's leaving box
                    if(xDir != 0)
                    {
                        resultYaw = (float) (toTarget(resultYaw, (float) ((targetXCoords.distanceTo(rayXCoords)) * xDir /* interpolateNegatives(playerLookVec.getX()))*/), (float) targetXCoords.x < rayXCoords.x, 18.0 * assistIntensity) * 0.5);

                        // TODO: Apply rotation to assist since it does not follow the boundary correctly to the orientation of the camera.
                    }

                    double yDir = changeYDirection(rayHit.y, entity.getBoundingBox().minY, entity.getBoundingBox().maxY, 0.25);
                    // Only modify Y level if it's about to leave box
                    if(yDir != 0)
                    {
                        resultPitch = toTarget(resultPitch, (float) (targetYCoords.distanceTo(rayYCoords) * yDir), true, 5.0 * assistIntensity);

                    }
                }
            }
        }

        return new Vec2f(resultPitch, resultYaw);
    }

    private ControllerOptions.AimAssistMode getMode(Entity entity)
    {
        if(entity instanceof PlayerEntity)
        {
            return Controllable.getOptions().getPlayerAimMode();
        }

        if(entity instanceof MonsterEntity)
        {
            return Controllable.getOptions().getHostileAimMode();
        }

        if(entity instanceof AnimalEntity || entity instanceof AmbientEntity)
        {
            return Controllable.getOptions().getAnimalAimMode();
        }

        return null;
    }

    private float toTarget(float current, float distance, boolean direction, double multiplier)
    {
        if(direction)
        {
            return (float) ((current - distance) * multiplier);
        }
        else
        {
            return (float) ((current + distance) * multiplier);
        }
    }

    private double changeYDirection(double hit, double min, double max, double offset)
    {
        if(hit <= min + offset)
        {
            return 1;
        }
        else if(hit >= max - offset)
        {
            return -1;
        }
        else
            return 0;
    }

 

 

This isn't used for hacking, but rather for a controller mod in which to make it easier to kill entities.

 

Edit: The code for pitch value is working acceptably fine, all I need to get fixed is the yaw value which does not correctly follow the boundary box according to the camera's orientation and side of the boundary box.

Edited by fernthedev
Updated code
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.