camera rotation

This commit is contained in:
Luca 2024-09-16 20:37:14 +02:00
parent b20f3b5124
commit ccd3ef577c
3 changed files with 60 additions and 15 deletions

View file

@ -0,0 +1,43 @@
extends Control
var touch_start: Vector2 = Vector2(0, 0)
var pressed: bool = false
var previous_position: Vector2 = Vector2(0,0)
var touch_delta: Vector2 = Vector2(0, 0)
var current_delta: Vector2 = Vector2()
var dampening_factor: float = 0.1 # Lower values = more dampening (0-1 range)
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void:
current_delta = lerp(current_delta, Vector2(0, 0), dampening_factor)
print(current_delta)
simulate_joystick_motion(JOY_AXIS_RIGHT_X, -current_delta.x * 0.1, 0)
func _on_gui_input(event: InputEvent) -> void:
if event is not InputEventScreenTouch and event is not InputEventScreenDrag:
return
if event is InputEventScreenTouch and pressed == false and event.is_pressed() == true:
pressed = true
touch_start = event.position
#print("touch start")
if event is InputEventScreenTouch and pressed == true and event.is_pressed() == false:
pressed = false
#simulate_joystick_motion(JOY_AXIS_RIGHT_X, 0, 0)
#print("touch end")
if pressed and event is InputEventScreenDrag:
touch_delta = event.position - previous_position
current_delta = touch_delta #lerp(current_delta, touch_delta, 1.0 - dampening_factor)
previous_position = event.position
func simulate_joystick_motion(axis: int, value: float, device_id: int = 0) -> void:
var joystick_event = InputEventJoypadMotion.new()
joystick_event.device = device_id
joystick_event.axis = axis # Axis index (e.g., 0 for X-axis, 1 for Y-axis)
joystick_event.axis_value = value # Value of the axis, typically between -1 and 1
Input.parse_input_event(joystick_event) # Process the event

View file

@ -18,25 +18,14 @@ var rotation_delta: Vector2 = Vector2(0, 0)
func _ready() -> void:
thumbstick.custom_minimum_size = Vector2(size.x, size.x) * thumbstick_size_factor
thumbstick.position = middle - thumbstick.size / 2.0
print(thumbstick)
func _unhandled_input(event: InputEvent) -> void:
if event is not InputEventScreenDrag:
return
if event is InputEventScreenDrag:
var touch_position = event.position
rotation_delta = get_viewport_rect().size / 2 - touch_position
simulate_joystick_motion(JOY_AXIS_RIGHT_X, -rotation_delta.x * 0.4, 0)
print(rotation_delta)
func _on_gui_input(event: InputEvent) -> void:
if event is not InputEventScreenTouch and event is not InputEventScreenDrag:
return
if event.is_pressed() == false:
simulate_joystick_motion(0, Vector2.ZERO.x)
simulate_joystick_motion(1, Vector2.ZERO.y)
simulate_joystick_motion(JOY_AXIS_LEFT_X, Vector2.ZERO.x)
simulate_joystick_motion(JOY_AXIS_LEFT_Y, Vector2.ZERO.y)
thumbstick.position = middle - thumbstick.size / 2.0
thumbstick.texture = talk_a_texture