77
88/**
99 * ControlWASD is a movement component that allows moving a node in the scene graph using keyboard
10- * input (W/A/S/D). It integrates with the scene's transformation system to control position
11- * changes, allowing basic 3D navigation in a scene graph.
10+ * input (W/A/S/D) or custom key mappings . It integrates with the scene's transformation system to
11+ * control position changes, allowing basic 3D navigation in a scene graph.
1212 *
1313 * <p>Movement is normalized to ensure consistent speed, even when moving diagonally. This class
1414 * supports velocity adjustments via the speed multiplier and works by translating the owning node
1515 * within the 3D world space.
1616 *
17- * <p>Example usage: Attach this component to a {@link SceneNode} to allow movement using WASD
18- * keyboard inputs. The position updates depend on the elapsed time per frame to ensure smooth and
19- * consistent movement over varying frame rates.
17+ * <p>Example usage: Attach this component to a {@link SceneNode} to allow movement using keyboard
18+ * inputs. The position updates depend on the elapsed time per frame to ensure smooth and consistent
19+ * movement over varying frame rates.
2020 */
2121public class ControlWASD extends AbstractComponent {
2222
@@ -26,6 +26,18 @@ public class ControlWASD extends AbstractComponent {
2626 /** The Input instance to monitor keyboard events (injected dependency). */
2727 private Input input ;
2828
29+ /** Key used for moving left. Default is 'A'. */
30+ private Key leftKey = Key .A ;
31+
32+ /** Key used for moving right. Default is 'D'. */
33+ private Key rightKey = Key .D ;
34+
35+ /** Key used for moving forward. Default is 'W'. */
36+ private Key forwardKey = Key .W ;
37+
38+ /** Key used for moving backward. Default is 'S'. */
39+ private Key backwardKey = Key .S ;
40+
2941 /**
3042 * Constructs a new ControlWASD component, injecting the Input logic needed for detecting key
3143 * presses.
@@ -63,7 +75,7 @@ public ControlWASD(Input input, float speed) {
6375 /**
6476 * Updates the movement of the owning node based on keyboard input.
6577 *
66- * <p>This method calculates the velocity vector by processing the WASD input and applies it to
78+ * <p>This method calculates the velocity vector by processing the input keys and applies it to
6779 * move the node smoothly in the 3D space by adjusting its position over time with respect to
6880 * `tpf` (time per frame).
6981 *
@@ -81,19 +93,20 @@ public void update(float tpf) {
8193 }
8294
8395 /**
84- * Processes keyboard input to determine velocity. Handles the W/A/S/D keys to allow movement in
85- * the 3D plane. Normalizes the vector to ensure diagonal movement doesn't lead to faster speeds.
96+ * Processes keyboard input to determine velocity. Handles the configured keys to allow movement
97+ * in the 3D plane. Normalizes the vector to ensure diagonal movement doesn't lead to faster
98+ * speeds.
8699 *
87100 * @return A velocity vector representing the computed movement direction and speed.
88101 */
89102 private Vector3f handleInput () {
90103 Vector3f velocity = new Vector3f ();
91104
92105 // Check for movement inputs
93- if (input .isKeyPressed (Key . W )) velocity .addLocal (0 , 0 , -1 );
94- if (input .isKeyPressed (Key . A )) velocity .addLocal (-1 , 0 , 0 );
95- if (input .isKeyPressed (Key . S )) velocity .addLocal (0 , 0 , 1 );
96- if (input .isKeyPressed (Key . D )) velocity .addLocal (1 , 0 , 0 );
106+ if (input .isKeyPressed (forwardKey )) velocity .addLocal (0 , 0 , -1 );
107+ if (input .isKeyPressed (leftKey )) velocity .addLocal (-1 , 0 , 0 );
108+ if (input .isKeyPressed (backwardKey )) velocity .addLocal (0 , 0 , 1 );
109+ if (input .isKeyPressed (rightKey )) velocity .addLocal (1 , 0 , 0 );
97110
98111 // Normalize diagonal movement to prevent unintended speed boosts
99112 if (velocity .length () > 0 ) {
@@ -128,9 +141,49 @@ public void setSpeed(float speed) {
128141 this .speed = speed ;
129142 }
130143
144+ /**
145+ * Sets the key used to move left.
146+ *
147+ * @param leftKey The new key to use for left movement.
148+ */
149+ public void setLeftKey (Key leftKey ) {
150+ this .leftKey = leftKey ;
151+ }
152+
153+ /**
154+ * Sets the key used to move right.
155+ *
156+ * @param rightKey The new key to use for right movement.
157+ */
158+ public void setRightKey (Key rightKey ) {
159+ this .rightKey = rightKey ;
160+ }
161+
162+ /**
163+ * Sets the key used to move forward.
164+ *
165+ * @param forwardKey The new key to use for forward movement.
166+ */
167+ public void setForwardKey (Key forwardKey ) {
168+ this .forwardKey = forwardKey ;
169+ }
170+
171+ /**
172+ * Sets the key used to move backward.
173+ *
174+ * @param backwardKey The new key to use for backward movement.
175+ */
176+ public void setBackwardKey (Key backwardKey ) {
177+ this .backwardKey = backwardKey ;
178+ }
179+
180+ /** Called when the component is attached to a {@link SceneNode}. Override for custom behavior. */
131181 @ Override
132182 public void onAttach () {}
133183
184+ /**
185+ * Called when the component is detached from a {@link SceneNode}. Override for custom behavior.
186+ */
134187 @ Override
135188 public void onDetach () {}
136189}
0 commit comments