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
  • WindMouse - Custom Mouse Movement Algorithm


    holic

    Recommended Posts

    Nice example code to do a custom mouse thingy, it saved me some time so ty!

    It seems like the code is almost always crashing into 

    Math.hypot(veloX, veloY) > maxStep
    
    

    so going to do some re-implementation, but the example and concepts are very useful :)

    Link to comment
    Share on other sites

    On 2/19/2021 at 11:03 AM, Aeglen said:

    Nice example code to do a custom mouse thingy, it saved me some time so ty!

    It seems like the code is almost always crashing into 

    
    Math.hypot(veloX, veloY) > maxStep
    
    

    so going to do some re-implementation, but the example and concepts are very useful :)

    Interesting, I haven't had any errors with this code but maybe I made some tweaks after posting this. I'll check and if I have, I'll update the OP.

    I don't think I had those privileges yet when I originally posted this.

    Link to comment
    Share on other sites

    • 3 weeks later...
    • 1 year later...

    I'm trying to implement this in my script, but it seems that Client.getMousePosition has been replaced with Client.getCanvas().getMousePosition()

    I made that change in the code, but now when I run my script I get erratic mouse movement and these errors. It seems for some reason Client.getCanvas().getMousePosition() is returning null. Could you help me solve this?

    9:02:48 PM: [WARN] This script threw an error, contact the script writer.
    9:02:48 PM: [ERROR] Exception has occurred while running! Please report error to developer if problem persists:
        java.lang.NullPointerException: Cannot read field "y" because "p2" is null
        at WindMouse.distance(WindMouse.java:247)
        at WindMouse.windMouse2(WindMouse.java:62)
        at WindMouse.handleMovement(WindMouse.java:35)
        at org.dreambot.api.input.event.impl.mouse.impl.MoveEvent.run(MoveEvent.java:41)
        at org.dreambot.api.input.Mouse.0(Mouse.java:615)
        at org.dreambot.api.input.Mouse.move(Mouse.java:523)
        at org.dreambot.api.input.Mouse.click(Mouse.java:374)
        at org.dreambot.api.input.Mouse.click(Mouse.java:346)
        at org.dreambot.api.input.event.impl.InteractionEvent.1(InteractionEvent.java:399)
        at org.dreambot.api.input.event.impl.InteractionEvent.interact(InteractionEvent.java:206)
        at org.dreambot.api.input.event.impl.InteractionEvent.interact(InteractionEvent.java:91)
        at org.dreambot.api.wrappers.widgets.WidgetChild.interact(WidgetChild.java:683)
        at org.dreambot.api.methods.magic.Magic.castSpell(Magic.java:81)
        at Main.onLoop(Main.java:44)
        at org.dreambot.api.script.AbstractScript.run(AbstractScript.java:224)
        at java.base/java.lang.Thread.run(Thread.java:833)
     

    Link to comment
    Share on other sites

    54 minutes ago, DudeBot1 said:

    I'm trying to implement this in my script, but it seems that Client.getMousePosition has been replaced with Client.getCanvas().getMousePosition()

    I made that change in the code, but now when I run my script I get erratic mouse movement and these errors. It seems for some reason Client.getCanvas().getMousePosition() is returning null. Could you help me solve this?

    9:02:48 PM: [WARN] This script threw an error, contact the script writer.
    9:02:48 PM: [ERROR] Exception has occurred while running! Please report error to developer if problem persists:
        java.lang.NullPointerException: Cannot read field "y" because "p2" is null
        at WindMouse.distance(WindMouse.java:247)
        at WindMouse.windMouse2(WindMouse.java:62)
        at WindMouse.handleMovement(WindMouse.java:35)
        at org.dreambot.api.input.event.impl.mouse.impl.MoveEvent.run(MoveEvent.java:41)
        at org.dreambot.api.input.Mouse.0(Mouse.java:615)
        at org.dreambot.api.input.Mouse.move(Mouse.java:523)
        at org.dreambot.api.input.Mouse.click(Mouse.java:374)
        at org.dreambot.api.input.Mouse.click(Mouse.java:346)
        at org.dreambot.api.input.event.impl.InteractionEvent.1(InteractionEvent.java:399)
        at org.dreambot.api.input.event.impl.InteractionEvent.interact(InteractionEvent.java:206)
        at org.dreambot.api.input.event.impl.InteractionEvent.interact(InteractionEvent.java:91)
        at org.dreambot.api.wrappers.widgets.WidgetChild.interact(WidgetChild.java:683)
        at org.dreambot.api.methods.magic.Magic.castSpell(Magic.java:81)
        at Main.onLoop(Main.java:44)
        at org.dreambot.api.script.AbstractScript.run(AbstractScript.java:224)
        at java.base/java.lang.Thread.run(Thread.java:833)
     

    Try using Mouse.getPosition() instead

    Link to comment
    Share on other sites

    • 4 months later...
    • 3 weeks later...

    Been getting this error when entering the Mining Guild F2P
    I have the same thing with EaseMouse
     

    2023-05-09 01:56:16 [WARN] This script threw an error, contact the script writer.
    2023-05-09 01:56:16 [ERROR] Exception has occurred while running! Please report error to developer if problem persists
    java.lang.NullPointerException: Cannot invoke "org.dreambot.api.wrappers.interactive.Entity.getGridX()" because "<local2>" is null
    	at org.dreambot.api.input.mouse.destination.impl.EntityDestination.getSuitablePoint(EntityDestination.java:53)
    	at src.Algorithms.WindMouse.handleMovement(WindMouse.java:29)
    	at org.dreambot.api.input.event.impl.mouse.impl.MoveEvent.run(MoveEvent.java:41)
    	at org.dreambot.api.input.Mouse.0(Mouse.java:624)
    	at org.dreambot.api.input.Mouse.move(Mouse.java:532)
    	at org.dreambot.api.input.Mouse.move(Mouse.java:505)
    	at src.tasks.MiningTask.execute(MiningTask.java:145)
    	at org.dreambot.api.script.impl.TaskScript.onLoop(TaskScript.java:83)
    	at org.dreambot.api.script.AbstractScript.run(AbstractScript.java:237)
    	at java.base/java.lang.Thread.run(Thread.java:833)

     

    Link to comment
    Share on other sites

    • 4 weeks later...

    The reason why skiddies still use BenLand's algorithm is because they have no clue where to start writing an own mouse implementation. Why don't you come up with your own implementation using something like Bezier curves or another more challeging spline algorithm? Eventually all you need is to create a spline between start and end point which has some twists in it using control points. I have very good experience using TCB splines (Kochanek Bartels spline) and adjusting the tension, bias and continiuty factors to generate decent humanlike mouse splines which can overshoot the target point. See wikipedia.

    For every mouse spline algorithm I love to use Fitt's law to determine how much time it takes to move to the "object". Afaik Fitt's law is used in every RuneScape bot since 2010.

    BenLand's algorithm written for Kaitnieks dates back from early 2000's. It's way more than 10 years old.

    Example using TCB splines:

    https://tinypic.host/i/oud9tx

    https://tinypic.host/i/oudOQ9

    https://tinypic.host/i/oudRlh

    Edited by Zenz101
    Link to comment
    Share on other sites

    • 4 weeks later...

    I had some issues with this code relating to mouse dragging that caused very erratic camera movement. To fix this, I had to switch a couple of Mouse#hop calls into setMousePosition, and add a canvas event to the setMousePosition for when the mouse should be dragged instead of moved. Here is a fixed version:

     

    /**
     * WindMouse from SMART by Benland100
     * Copyright to Benland100, (Benjamin J. Land)
     * Originally modified for use with DreamBot 3 by holic
     * Mouse dragging issues fixed by Hero with help from Pandemic
     * <p>
     * Prepped for DreamBot 3
     **/
    
    import org.dreambot.api.input.Mouse;
    import org.dreambot.api.input.mouse.algorithm.MouseMovementAlgorithm;
    import org.dreambot.api.input.mouse.destination.AbstractMouseDestination;
    import org.dreambot.api.methods.Calculations;
    import org.dreambot.api.methods.ViewportTools;
    import org.dreambot.api.methods.input.mouse.MouseSettings;
    import org.dreambot.api.utilities.Logger;
    import org.dreambot.core.Instance;
    
    import java.awt.*;
    import java.awt.event.MouseEvent;
    
    public class WindMouse implements MouseMovementAlgorithm { // https://dreambot.org/forums/index.php?/topic/21147-windmouse-custom-mouse-movement-algorithm/
        private int _mouseSpeed = MouseSettings.getSpeed() > 15 ? MouseSettings.getSpeed() - 10 : 15;
        private final int _mouseSpeedLow = Math.round(_mouseSpeed / 2);
        private int _mouseGravity = Calculations.random(4, 20);
        private int _mouseWind = Calculations.random(1, 10);
    
        @Override
        public boolean handleMovement(AbstractMouseDestination abstractMouseDestination) {
            //Get a suitable point for the mouse's destination
            Point suitPos = abstractMouseDestination.getSuitablePoint();
    
            // Select which implementation of WindMouse you'd like to use
            // by uncommenting out the line you want to use below:
    
            //windMouse(suitPos.x, suitPos.y); //Original implementation
            windMouse2(suitPos); //Tweaked implementation
    
            return distance(Mouse.getPosition(), suitPos) < 2;
        }
    
        public static void sleep(int min, int max) {
            try {
                Thread.sleep(Calculations.random(min, max));
            } catch (InterruptedException e) {
                Logger.log(e.getMessage());
            }
        }
    
        public static void sleep(int ms) {
            try {
                Thread.sleep(ms);
            } catch (InterruptedException e) {
                Logger.log(e.getMessage());
            }
        }
    
        /**
         * Tweaked implementation of WindMouse
         * Moves to a mid point on longer moves to seem a little more human-like
         * Remove the if statement below if you'd rather straighter movement
         *
         * @param point The destination point
         */
        public void windMouse2(Point point) {
            Point curPos = Mouse.getPosition();
            if (distance(point, curPos) > 250 && Calculations.random(1) == 2) {
                Point rp = randomPoint(point, curPos);
                windMouse2(curPos.x, curPos.y, rp.x, rp.y, _mouseGravity, _mouseWind, _mouseSpeed, Calculations.random(5, 25));
                sleep(1, 150);
            }
            windMouse2(curPos.x, curPos.y, point.x, point.y, _mouseGravity, _mouseWind, _mouseSpeed, Calculations.random(5, 25));
            _mouseGravity = Calculations.random(4, 20);
            _mouseWind = Calculations.random(1, 10);
            _mouseSpeed = Calculations.random(_mouseSpeedLow, MouseSettings.getSpeed());
        }
    
        /**
         * Tweaked implementation of WindMouse by holic
         * All credit to Benjamin J. Land for the original. (see below)
         *
         * @param xs         The x start
         * @param ys         The y start
         * @param xe         The x destination
         * @param ye         The y destination
         * @param gravity    Strength pulling the position towards the destination
         * @param wind       Strength pulling the position in random directions
         * @param targetArea Radius of area around the destination that should
         *                   trigger slowing, prevents spiraling
         */
        private void windMouse2(double xs, double ys, double xe, double ye, double gravity, double wind, double speed, double targetArea) {
    
            double dist, veloX = 0, veloY = 0, windX = 0, windY = 0;
    
            double sqrt2 = Math.sqrt(2);
            double sqrt3 = Math.sqrt(3);
            double sqrt5 = Math.sqrt(5);
    
            int tDist = (int) distance(xs, ys, xe, ye);
            long t = System.currentTimeMillis() + 10000;
    
            while (!(Math.hypot((xs - xe), (ys - ye)) < 1)) {
                if (System.currentTimeMillis() > t) break;
    
                dist = Math.hypot((xs - xe), (ys - ye));
                wind = Math.min(wind, dist);
                if ((dist < 1)) {
                    dist = 1;
                }
    
                long d = (Math.round((Math.round(((double) (tDist))) * 0.3)) / 7);
                if ((d > 25)) {
                    d = 25;
                }
    
                if ((d < 5)) {
                    d = 5;
                }
    
                double rCnc = Calculations.random(6);
                if ((rCnc == 1)) {
                    d = 2;
                }
    
                double maxStep = (Math.min(d, Math.round(dist))) * 1.5;
                if ((dist >= targetArea)) {
                    windX = (windX / sqrt3) + ((Calculations.random((int) ((Math.round(wind) * 2) + 1)) - wind) / sqrt5);
                    windY = (windY / sqrt3) + ((Calculations.random((int) ((Math.round(wind) * 2) + 1)) - wind) / sqrt5);
                } else {
                    windX = (windX / sqrt2);
                    windY = (windY / sqrt2);
                }
    
                veloX += windX + gravity * (xe - xs) / dist;
                veloY += windY + gravity * (ye - ys) / dist;
    
                if ((Math.hypot(veloX, veloY) > maxStep)) {
                    maxStep = ((maxStep / 2) < 1) ? 2 : maxStep;
    
                    double randomDist = (maxStep / 2) + Calculations.random((int) (Math.round(maxStep) / 2));
                    double veloMag = Math.sqrt(((veloX * veloX) + (veloY * veloY)));
                    veloX = (veloX / veloMag) * randomDist;
                    veloY = (veloY / veloMag) * randomDist;
                }
    
                int lastX = ((int) (Math.round(xs)));
                int lastY = ((int) (Math.round(ys)));
                xs += veloX;
                ys += veloY;
                if ((lastX != Math.round(xs)) || (lastY != Math.round(ys))) {
                    setMousePosition(new Point((int) Math.round(xs), (int) Math.round(ys)));
                }
    
                int w = Calculations.random((int) (Math.round(100 / speed))) * 6;
                if ((w < 5)) {
                    w = 5;
                }
    
                w = (int) Math.round(w * 0.9);
                sleep(w);
            }
    
            if (((Math.round(xe) != Math.round(xs)) || (Math.round(ye) != Math.round(ys)))) {
                setMousePosition(new Point((int) Math.round(xs), (int) Math.round(ys)));
            }
        }
    
        /**
         * Internal mouse movement algorithm from SMART. Do not use this without credit to either
         * Benjamin J. Land or BenLand100. This was originally synchronized to prevent multiple
         * motions and bannage but functions poorly with DB3.
         * <p>
         * BEST USED IN FIXED MODE
         *
         * @param xs         The x start
         * @param ys         The y start
         * @param xe         The x destination
         * @param ye         The y destination
         * @param gravity    Strength pulling the position towards the destination
         * @param wind       Strength pulling the position in random directions
         * @param minWait    Minimum relative time per step
         * @param maxWait    Maximum relative time per step
         * @param maxStep    Maximum size of a step, prevents out of control motion
         * @param targetArea Radius of area around the destination that should
         *                   trigger slowing, prevents spiraling
         * @result The actual end point
         */
        private Point windMouseImpl(double xs, double ys, double xe, double ye, double gravity, double wind, double minWait, double maxWait, double maxStep, double targetArea) {
            final double sqrt3 = Math.sqrt(3);
            final double sqrt5 = Math.sqrt(5);
    
            double dist, veloX = 0, veloY = 0, windX = 0, windY = 0;
            while ((dist = Math.hypot(xs - xe, ys - ye)) >= 1) {
                wind = Math.min(wind, dist);
                if (dist >= targetArea) {
                    windX = windX / sqrt3 + (2D * Math.random() - 1D) * wind / sqrt5;
                    windY = windY / sqrt3 + (2D * Math.random() - 1D) * wind / sqrt5;
                } else {
                    windX /= sqrt3;
                    windY /= sqrt3;
                    if (maxStep < 3) {
                        maxStep = Math.random() * 3D + 3D;
                    } else {
                        maxStep /= sqrt5;
                    }
                }
                veloX += windX + gravity * (xe - xs) / dist;
                veloY += windY + gravity * (ye - ys) / dist;
                double veloMag = Math.hypot(veloX, veloY);
                if (veloMag > maxStep) {
                    double randomDist = maxStep / 2D + Math.random() * maxStep / 2D;
                    veloX = (veloX / veloMag) * randomDist;
                    veloY = (veloY / veloMag) * randomDist;
                }
                int lastX = ((int) (Math.round(xs)));
                int lastY = ((int) (Math.round(ys)));
                xs += veloX;
                ys += veloY;
                if ((lastX != Math.round(xs)) || (lastY != Math.round(ys))) {
                    setMousePosition(new Point((int) Math.round(xs), (int) Math.round(ys)));
                }
                double step = Math.hypot(xs - lastX, ys - lastY);
                sleep((int) Math.round((maxWait - minWait) * (step / maxStep) + minWait));
            }
            return new Point((int) xs, (int) ys);
        }
    
        /**
         * Moves the mouse from the current position to the specified position.
         * Approximates human movement in a way where smoothness and accuracy are
         * relative to speed, as it should be.
         *
         * @param x The x destination
         * @param y The y destination
         * @result The actual end point
         */
        public Point windMouse(int x, int y) {
            Point c = Mouse.getPosition();
            double speed = (Math.random() * 15D + 15D) / 10D;
            return windMouseImpl(c.x, c.y, x, y, 9D, 3D, 5D / speed, 10D / speed, 10D * speed, 8D * speed);
        }
    
        private void setMousePosition(Point endPoint) {
            if (Mouse.getMouseSettings().isDrag()) { // pan told me to do this for dragging. more info in the DreamBot EDU server: https://discord.com/channels/426346091173380096/462764560772759563/1125126497343123566
                Instance.dispatchCanvasEvent(new MouseEvent(Instance.getCanvas(),
                        MouseEvent.MOUSE_DRAGGED,
                        System.currentTimeMillis(),
                        0,
                        endPoint.x,
                        endPoint.y,
                        ViewportTools.getAbsoluteXCoordinate() + endPoint.x,
                        ViewportTools.getAbsoluteYCoordinate() + endPoint.y,
                        0,
                        false,
                        MouseEvent.BUTTON2));
            } else {
                Mouse.hop(endPoint);
            }
        }
    
        private static double distance(double x1, double y1, double x2, double y2) {
            return Math.sqrt((Math.pow((Math.round(x2) - Math.round(x1)), 2) + Math.pow((Math.round(y2) - Math.round(y1)), 2)));
        }
    
        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 float randomPointBetween(float corner1, float corner2) {
            if (corner1 == corner2) {
                return corner1;
            }
            float delta = corner2 - corner1;
            float offset = Calculations.getRandom().nextFloat() * delta;
            return corner1 + offset;
        }
    
        public Point randomPoint(Point p1, Point p2) {
            int randomX = (int) randomPointBetween(p1.x, p2.x);
            int randomY = (int) randomPointBetween(p1.y, p2.y);
            return new Point(randomX, randomY);
        }
    
    }

     

    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.