This JavaScript code implements a robot path planner using the p5.js library. The application provides a user interface for planning and visualizing the movement of a robot on a grid-based field.
- The
setup
function initializes the canvas, loads images, and sets up initial configurations, including the robot's starting position and waypoints.
- The
initHTML
function creates input fields, buttons, and event listeners to interact with the robot and waypoints.
- The
getImage
function loads images based on the selected season from the dropdown menu.
- The
Robot
class represents the robot with methods for setting its position, displaying it, and checking for user interactions. - The
Waypoint
class defines waypoints with coordinates and angles.
- The
doPath
function handles the logic for moving the robot along the waypoints, adjusting rotation and position.
- Mouse events (
mousePressed
,mouseDragged
,mouseReleased
) allow users to interact with the robot and waypoints, adding, removing, or dragging them.
- Functions like
drawGrid
,drawPath
,drawWaypoints
handle the visualization of the grid, robot path, and waypoints.
- The
keyTyped
andkeyPressed
functions provide keyboard shortcuts for toggling image visibility, restarting, and pausing/resuming path planning.
- Functions like
convert
,updatePos
,updateRotation
, andupdateRotationInput
handle input conversion and updating robot properties.
- The
restart
function resets the robot's path, and theisPaused
variable controls whether the path planning is paused or active.
-
Adding Waypoints:
- Left-click on the field to add a waypoint.
- Right-click on a waypoint to remove it.
- Center-click / CTRL + Left-click to add a default waypoint.
-
Moving Robot:
- Adjust the rotation angle using the input field.
-
Controlling Path Planning:
- Toggle between different seasons using the dropdown menu.
-
Keyboard Shortcuts:
- Press 'z' to toggle the field image visibility.
- Press 'r' to restart the robot's path.
- Press 'k' to pause or resume path planning.
This code provides a foundation for a visual robot path planner, allowing users to experiment with different scenarios and field configurations.