Skip to content

Commit 1b6e8d6

Browse files
committed
Docs: Add detailed mathematical explanation of mouse movement algorithm
- Added deep-dive documentation in `docs/move_naturally_full_detailed_explanation.md` covering Bezier curves and Quadratic Ease-In-Out acceleration. - Added visual charts (`chart.png` and `chart2.png`) to illustrate the coordinate system and acceleration profiles. - Updated `README.md` with a high-level summary of the "Human Movement" algorithm and a link to the full technical reference.
1 parent e7db909 commit 1b6e8d6

15 files changed

Lines changed: 281 additions & 2 deletions

README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,21 @@ The software thinks you're at your desk. You're at the coffee machine. Everyone
262262

263263
**Remember**: WASP is for reasonable breaks, not for gaming all day!
264264

265+
---
266+
## 🧠 The Science of "Human" Movement
267+
268+
WASP doesn't just "jump" the mouse from point A to point B. That would be an instant red flag for tracking algorithms. Instead, we realized that human hand movement follows specific laws of physics.
269+
270+
### The Algorithm
271+
We implemented a **Cubic Bezier Curve** algorithm with **Quadratic Ease-In-Out** acceleration.
272+
273+
1. **Curvature**: Real hands move in arcs, not straight lines. We calculate a random control point offset to create a natural curve for every movement.
274+
2. **Acceleration**: Humans have mass. We take time to speed up and slow down. WASP mathematically simulates this inertia, starting slow, accelerating, and decelerating on arrival.
275+
3. **Micro-Jitter**: We add imperceptible randomness to the path, just like the micro-tremors in a human hand.
276+
277+
[**📚 Read the Full Mathematical Explanation**](docs/move_naturally_full_detailed_explanation.md)
278+
*Includes XY graphs, Bezier formulas, and acceleration calculus.*
279+
265280
---
266281

267282
## 🤝 Legal Disclaimer
635 Bytes
Binary file not shown.
0 Bytes
Binary file not shown.
0 Bytes
Binary file not shown.
-32 Bytes
Binary file not shown.
0 Bytes
Binary file not shown.

docs/chart.png

310 KB
Loading

docs/chart2.png

1.08 MB
Loading
Lines changed: 266 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,266 @@
1+
# Mathematical Explanation of Human-Like Mouse Movement Algorithm
2+
3+
This document provides a **complete, step-by-step mathematical and
4+
visual explanation** of how the `move_naturally` function computes
5+
**Bezier curve control points** and moves the mouse in a human-like
6+
manner.
7+
8+
Everything explained here corresponds directly to the code logic.
9+
10+
------------------------------------------------------------------------
11+
12+
## 1. What Problem Are We Solving?
13+
14+
Robotic mouse movement looks like this:
15+
16+
- Constant speed
17+
- Straight lines
18+
- No curvature
19+
20+
Humans move a mouse like this:
21+
22+
- Accelerate → cruise → decelerate
23+
- Slight curves
24+
- Small randomness
25+
26+
The solution used here is a **Cubic Bezier Curve**.
27+
28+
------------------------------------------------------------------------
29+
30+
## 2. XY Coordinate System
31+
32+
We use a standard XY graph:
33+
34+
- X axis → horizontal
35+
- Y axis → vertical
36+
37+
Example values used throughout this explanation:
38+
39+
Point Meaning Coordinates
40+
------- --------- -------------
41+
S Start (100, 100)
42+
E End (400, 300)
43+
44+
------------------------------------------------------------------------
45+
46+
## 3. Step 1 -- Direction Vector (Straight Line)
47+
48+
From the code:
49+
50+
``` python
51+
dx = x - start_x
52+
dy = y - start_y
53+
```
54+
55+
Calculation:
56+
57+
dx = 400 - 100 = 300
58+
dy = 300 - 100 = 200
59+
60+
So the straight-line direction vector is:
61+
62+
SE = (300, 200)
63+
64+
Distance between points:
65+
66+
distance = sqrt(dx² + dy²)
67+
= sqrt(300² + 200²)
68+
= sqrt(130000)
69+
≈ 360.56
70+
71+
------------------------------------------------------------------------
72+
73+
## 4. Step 2 -- Perpendicular Vector (Creates Curvature)
74+
75+
To avoid straight movement, a perpendicular vector is created:
76+
77+
``` python
78+
px = -dy
79+
py = dx
80+
```
81+
82+
Calculation:
83+
84+
px = -200
85+
py = 300
86+
87+
This vector points sideways relative to the direct path.
88+
89+
------------------------------------------------------------------------
90+
91+
## 5. Step 3 -- Normalize the Perpendicular Vector
92+
93+
Normalize to length 1:
94+
95+
length = sqrt(px² + py²)
96+
= sqrt(200² + 300²)
97+
= sqrt(130000)
98+
≈ 360.56
99+
100+
Normalized vector:
101+
102+
px = -200 / 360.56 ≈ -0.555
103+
py = 300 / 360.56 ≈ 0.832
104+
105+
------------------------------------------------------------------------
106+
107+
## 6. Step 4 -- Random Arc Offset
108+
109+
From the code:
110+
111+
``` python
112+
arc_scale = random.uniform(-0.2, 0.2)
113+
offset_x = px * distance * arc_scale
114+
offset_y = py * distance * arc_scale
115+
```
116+
117+
Assume:
118+
119+
arc_scale = 0.1
120+
121+
Then:
122+
123+
offset_x = -0.555 * 360.56 * 0.1 ≈ -20
124+
offset_y = 0.832 * 360.56 * 0.1 ≈ 30
125+
126+
This offset bends the path.
127+
128+
------------------------------------------------------------------------
129+
130+
## 7. Step 5 -- Compute Bezier Control Points
131+
132+
Control points are placed **along the path** but shifted sideways.
133+
134+
### Control Point C1 (25% of the path)
135+
136+
C1_x = start_x + 0.25*dx + offset_x
137+
= 100 + 75 - 20
138+
= 155
139+
140+
C1_y = start_y + 0.25*dy + offset_y
141+
= 100 + 50 + 30
142+
= 180
143+
144+
C1 = (155, 180)
145+
146+
------------------------------------------------------------------------
147+
148+
### Control Point C2 (75% of the path)
149+
150+
C2_x = start_x + 0.75*dx + offset_x
151+
= 100 + 225 - 20
152+
= 305
153+
154+
C2_y = start_y + 0.75*dy + offset_y
155+
= 100 + 150 + 30
156+
= 280
157+
158+
C2 = (305, 280)
159+
160+
------------------------------------------------------------------------
161+
162+
## 8. XY Graph Diagram
163+
164+
![Coordinate System Diagram](chart.png)
165+
166+
* **Dashed line**: Robotic straight path
167+
* **Curved line**: Human-like Bezier movement
168+
* **Start/End**: The mouse journey
169+
* **C1/C2**: Invisible control points pulling the curve
170+
171+
------------------------------------------------------------------------
172+
173+
## 9. Bezier Curve Mathematical Formula
174+
175+
Cubic Bezier equation:
176+
177+
B(t) = (1−t)³S
178+
+ 3(1−t)²tC1
179+
+ 3(1−t)t²C2
180+
+ t³E
181+
182+
Where: - `t ∈ [0, 1]` - `B(0) = Start` - `B(1) = End`
183+
184+
------------------------------------------------------------------------
185+
186+
## 10. Example: Bezier Point at t = 0.5
187+
188+
### X coordinate:
189+
190+
B_x(0.5) =
191+
(0.5³ * 100)
192+
+ 3*(0.5²*0.5*155)
193+
+ 3*(0.5*0.5²*305)
194+
+ (0.5³ * 400)
195+
196+
≈ 235
197+
198+
### Y coordinate:
199+
200+
B_y(0.5) =
201+
(0.5³ * 100)
202+
+ 3*(0.5²*0.5*180)
203+
+ 3*(0.5*0.5²*280)
204+
+ (0.5³ * 300)
205+
206+
≈ 223
207+
208+
So the midpoint lies at:
209+
210+
(235, 223)
211+
212+
------------------------------------------------------------------------
213+
214+
## 11. Acceleration (Ease-In-Out)
215+
216+
The function applies an **Ease-In-Out Quadratic** easing to the time variable `t`. This transforms linear time (constant speed) into "perceived" time `alpha` (variable speed).
217+
218+
![Acceleration Graph](chart2.png)
219+
220+
### The Math
221+
222+
We use a piecewise function based on the progress `t` (from 0 to 1):
223+
224+
1. **Acceleration Phase** (Start to Halfway, `t < 0.5`):
225+
```math
226+
alpha = 2 * t^2
227+
```
228+
* At `t=0`, speed is 0.
229+
* Velocity increases linearly.
230+
231+
2. **Deceleration Phase** (Halfway to End, `t >= 0.5`):
232+
```math
233+
alpha = -1 + (4 - 2 * t) * t
234+
```
235+
* This is an inverted parabola.
236+
* Velocity decreases linearly to 0 at the end.
237+
238+
### Why this specific formula?
239+
240+
At the midpoint `t = 0.5`:
241+
* **Formula 1:** $2 * (0.5)^2 = 0.5$
242+
* **Formula 2:** $-1 + (4 - 1) * 0.5 = 0.5$
243+
244+
The values match perfectly, and more importantly, the **derivative (speed)** matches, ensuring a perfectly smooth transition with no "jerk" at the halfway point. This mimics the physics of a hand starting a movement, reaching peak speed, and slowing down to stop.
245+
246+
## 12. Why This Looks Human
247+
248+
Feature Effect
249+
---------------------- ---------------
250+
Perpendicular offset Curved motion
251+
Random arc No repetition
252+
Bezier curve Smooth path
253+
Ease-in-out Natural speed
254+
255+
------------------------------------------------------------------------
256+
257+
## 13. Final Result
258+
259+
✔ Natural-looking mouse movement\
260+
✔ Hard to distinguish from real user\
261+
✔ Smooth curves and timing\
262+
✔ No sharp jumps
263+
264+
------------------------------------------------------------------------
265+
266+
### End of Detailed Explanation
-32 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)