Jump to content
Frequently Asked Questions
  • Are you not able to open the client? Try following our getting started guide
  • Still not working? Try downloading and running JarFix
  • Help! My bot doesn't do anything! Enable fresh start in client settings and restart the client
  • How to purchase with PayPal/OSRS/Crypto gold? You can purchase vouchers from other users
  • Custom Mouse algorithm implementing Fitts Law and Bezier curves


    Deep Slayer

    Recommended Posts

    import org.dreambot.api.input.Mouse;
    import org.dreambot.api.input.event.impl.mouse.MouseButton;
    import org.dreambot.api.input.mouse.algorithm.MouseAlgorithm;
    import org.dreambot.api.input.mouse.destination.AbstractMouseDestination;
    import org.dreambot.api.methods.Calculations;
    import org.dreambot.api.utilities.Logger;
    
    import java.awt.*;
    import java.util.Random;
    
    public class FittsLawMouseAlgorithmWithBezierCurves implements MouseAlgorithm {
        private Random random = new Random();
    
        @Override
        public boolean handleMovement(AbstractMouseDestination abstractMouseDestination) {
            Point suitPos = abstractMouseDestination.getSuitablePoint();
            mouseMovement(suitPos);
            return distance(Mouse.getPosition(), suitPos) < 2;
        }
    
        @Override
        public boolean handleClick(MouseButton mouseButton) {
            return Mouse.getDefaultMouseAlgorithm().handleClick(mouseButton);
        }
    
        public void mouseMovement(Point target) {
            Point start = Mouse.getPosition();
            double distance = distance(start, target);
            double targetWidth = 10; 
            double time = fittsLawTime(distance, targetWidth) * 1000; 
    
            Point[] controlPoints = generateControlPoints(start, target, distance);
            int steps = Calculations.random(50, 80); // Increase steps for smoother curves
            long startTime = System.currentTimeMillis();
            Point current = start;
    
            for (int i = 0; i <= steps && System.currentTimeMillis() - startTime < time; i++) {
                double t = (double) i / (double) steps;
                int x = (int) (Math.pow(1 - t, 3) * controlPoints[0].x +
                        3 * Math.pow(1 - t, 2) * t * controlPoints[1].x +
                        3 * (1 - t) * Math.pow(t, 2) * controlPoints[2].x +
                        Math.pow(t, 3) * controlPoints[3].x);
                int y = (int) (Math.pow(1 - t, 3) * controlPoints[0].y +
                        3 * Math.pow(1 - t, 2) * t * controlPoints[1].y +
                        3 * (1 - t) * Math.pow(t, 2) * controlPoints[2].y +
                        Math.pow(t, 3) * controlPoints[3].y);
    
                Mouse.hop(new Point(x, y));
                sleep(Calculations.random(5, 15));
                current = new Point(x, y);
            }
            
            Mouse.hop(target);
        }
    
        private double fittsLawTime(double distance, double width) {
            double a = 0.1; 
            double b = 0.1;
            return a + b * Math.log(1 + (distance / width)) / Math.log(2);
        }
    
        /**
         * Generate control points for Bézier curve
         *
         * @param start The start point
         * @param end   The end point
         * @param distance The distance between the start and end points
         * @return Array of control points
         */
        private Point[] generateControlPoints(Point start, Point end, double distance) {
            Point[] controlPoints = new Point[4];
            controlPoints[0] = start;
            controlPoints[3] = end;
    
            int midX = (start.x + end.x) / 2;
            int midY = (start.y + end.y) / 2;
    
            int deviation = Math.max(2, (int) (distance * 1.5)); //adjust constant to increase or decrease curve of Bezier
    
            controlPoints[1] = new Point(midX + random.nextInt(deviation) - deviation / 2, midY + random.nextInt(deviation) - deviation / 2);
            controlPoints[2] = new Point(midX + random.nextInt(deviation) - deviation / 2, midY + random.nextInt(deviation) - deviation / 2);
    
            return controlPoints;
        }
    
        public double distance(Point p1, Point p2) {
            return Math.sqrt((p2.y - p1.y) * (p2.y - p1.y) + (p2.x - p1.x) * (p2.x - p1.x));
        }
    
        public static void sleep(int ms) {
            try {
                Thread.sleep(ms);
            } catch (InterruptedException e) {
                Logger.log(e.getMessage());
            }
        }
    }
    
    Link to comment
    Share on other sites

    @Meteorite

    It will hop to meet the time requirement of Fitts Law. You would have to reduce the targetWidth ,  and increase the number of steps to find a good balance.

    Fitts' law states that the amount of time required for a person to move a pointer (e.g., mouse cursor) to a target area is a function of the distance to the target divided by the size of the target. Thus, the longer the distance and the smaller the target's size, the longer it takes.

     

    try something like 0.1 for the widt and 50-80 steps, and keep refining until you like it

    Link to comment
    Share on other sites

    Posted (edited)

    its a pretty cool algorithm because it automatically handles the speed via the number of steps and target size..

     

    ..and on top of that youve got bezier curves

    Edited by Deep Slayer
    Link to comment
    Share on other sites

    Create an account or sign in to comment

    You need to be a member in order to leave a comment

    Create an account

    Sign up for a new account in our community. It's easy!

    Register a new account

    Sign in

    Already have an account? Sign in here.

    Sign In Now
    ×
    ×
    • Create New...

    Important Information

    We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.