Skip to content

Instantly share code, notes, and snippets.

@LasseSkogland
Created January 29, 2025 10:05
Show Gist options
  • Save LasseSkogland/d01c9d635479b15b60441c8633d5d860 to your computer and use it in GitHub Desktop.
Save LasseSkogland/d01c9d635479b15b60441c8633d5d860 to your computer and use it in GitHub Desktop.
Bouncy Mouse - Powershell
using namespace System.Diagnostics
using namespace System.Windows.Forms
Add-Type -AssemblyName System.Windows.Forms
$ErrorActionPreference = 'Stop'
$screens = [Screen]::AllScreens
$speedX = 0;
$speedY = 0;
$gravity = 9.81;
$drag = 0.6;
$bounce = 0.9;
$lastPos = [Cursor]::Position
[long]$ts = [Stopwatch]::GetTimestamp()
function applyDrag([double]$inputvalue, [double]$drag) {
if($inputvalue -gt 0) {
return $inputvalue - $drag
}
if($speed -lt 0) {
return $inputvalue + $drag
}
return $inputvalue;
}
while ($true) {
$pos = [Cursor]::Position
$mouseDelta = $pos - $lastPos
$ticks = [Stopwatch]::GetTimestamp() - $ts
$ts += $ticks
$deltaTime = [double]$ticks / [timespan]::TicksPerSecond;
$speedX += $mouseDelta.X * $deltaTime;
$speedY += ($mouseDelta.Y + $gravity) * $deltaTime;
$dragDelta = $drag * $deltaTime
$speedX = applyDrag $speedX $dragDelta
$speedY = applyDrag $speedY $dragDelta
$lastPos.Offset($speedX, $speedY);
[Cursor]::Position = $lastPos;
foreach ($screen in $screens) {
$bounds = $screen.Bounds;
if ($bounds.Contains([Cursor]::Position)) {
if ($lastPos.X -lt $bounds.Left -or $lastPos.X -gt $bounds.Right) {
$speedX = - ($speedX * $bounce)
}
if ($lastPos.Y -lt $bounds.Top -or $lastPos.Y -gt $bounds.Bottom) {
$speedY = - ($speedY * $bounce)
}
break;
}
}
$lastPos = [Cursor]::Position;
Start-Sleep -Milliseconds 1
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment