Godot: carro a control (básico)
Sigo juegando con el Godot Engine a manejar cosas con un control de juegos USB. Esta vez programé un carrito a control, por ahora sin aceleración ni efectos especiales, pero ya se siente bien. Según veo, tengo que repasar física, especialmente cinemática y operaciones con vectores, para mover el carrito con más realismo y entender mejor cómo mover otros objetos en los juegos de distintas formas.
Este es el código del carrito:
extends Sprite # CONSTANTS const SPEED = 300 # px const ANGULAR_SPEED = 1.5 # radians # BUILT-IN METHODS func _process(delta): # Get the direction of the wheels from the gamepad's first axis # (-1 is full left, 1 is full right). var direction = Input.get_joy_axis(0, JOY_AXIS_0) # Invert direction when reversing. if brake_down(): direction *= -1 # Rotate the car accordingly (but not when dry steering). if accelerator_down() or brake_down(): rotation += ANGULAR_SPEED * direction * delta # Move the car forward or backward. var velocity = Vector2.ZERO # (0, 0) a zero vector. if accelerator_down(): velocity = Vector2.UP.rotated(rotation) * SPEED if brake_down(): velocity = Vector2.DOWN.rotated(rotation) * SPEED position += velocity * delta # HELPER METHODS func accelerator_down(): # Return true if the car is moving forward. return Input.is_joy_button_pressed(0, JOY_R2) func brake_down(): # Return true if the car is in reverse gear. return Input.is_joy_button_pressed(0, JOY_L2)
Ya con esto puedo empezar a pulir para que se sienta más real. Hay que agregarle aceleración y desaceleración porque en este momento la velocidad es constante desde que se pisa el pedal hasta que se suelta; deberían verse las ruedas delanteras al girar el timón; el motor debería sonar; las luces, prender y apagar...
Temas relacionados: