Add camera rotation controls

main
Ensar Sarajčić 2022-01-24 20:04:29 +01:00
parent de0a8111d1
commit 2df1e29640
6 changed files with 91 additions and 7 deletions

View File

@ -10,7 +10,7 @@ var _cameras = []
func attach_cameras_to(player_node: BuggedVehicle) -> void: func attach_cameras_to(player_node: BuggedVehicle) -> void:
var cockpit_camera = STATIC_CAMERA.instance() var cockpit_camera = STATIC_CAMERA.instance()
player_node.get_cockpit_position().add_child(cockpit_camera) player_node.get_cockpit_position().add_child(cockpit_camera)
cockpit_camera.rotate(Vector3.UP, deg2rad(180)) cockpit_camera.reset()
var follow_camera = FOLLOW_CAMERA.instance() var follow_camera = FOLLOW_CAMERA.instance()
follow_camera.global_transform = player_node.global_transform.translated( follow_camera.global_transform = player_node.global_transform.translated(
-player_node.global_transform.basis.z * 100 -player_node.global_transform.basis.z * 100
@ -19,14 +19,13 @@ func attach_cameras_to(player_node: BuggedVehicle) -> void:
player_node.get_parent().add_child(follow_camera) player_node.get_parent().add_child(follow_camera)
var bumpera_camera = STATIC_CAMERA.instance() var bumpera_camera = STATIC_CAMERA.instance()
player_node.get_bumper_position().add_child(bumpera_camera) player_node.get_bumper_position().add_child(bumpera_camera)
bumpera_camera.rotate(Vector3.UP, deg2rad(180))
var hood_camera = STATIC_CAMERA.instance() var hood_camera = STATIC_CAMERA.instance()
player_node.get_hood_position().add_child(hood_camera) player_node.get_hood_position().add_child(hood_camera)
hood_camera.rotate(Vector3.UP, deg2rad(180))
var static_follow_camera = STATIC_CAMERA.instance() var static_follow_camera = STATIC_CAMERA.instance()
player_node.get_static_follow_position().add_child(static_follow_camera) player_node.get_static_follow_position().add_child(static_follow_camera)
static_follow_camera.rotate(Vector3.UP, deg2rad(180))
_cameras = [follow_camera, cockpit_camera, hood_camera, bumpera_camera, static_follow_camera] _cameras = [follow_camera, cockpit_camera, hood_camera, bumpera_camera, static_follow_camera]
for cam in _cameras:
cam.reset()
select_camera(GlobalSettings.selected_camera) select_camera(GlobalSettings.selected_camera)
@ -41,3 +40,15 @@ func select_camera(camera_index: int) -> void:
func next_camera() -> void: func next_camera() -> void:
select_camera(GlobalSettings.selected_camera + 1) select_camera(GlobalSettings.selected_camera + 1)
func update_camera(horizontal: float, vertical: float, look_back: bool) -> void:
horizontal = clamp(horizontal, -1.0, 1.0)
vertical = clamp(vertical, -1.0, 1.0)
if look_back:
horizontal = 2.0
for cam in _cameras:
cam.reset()
cam.update_rotation(horizontal, vertical)

View File

@ -6,6 +6,8 @@ export var target_height: float = 2.0
var follow_target: Node = null var follow_target: Node = null
var last_lookat: Vector3 var last_lookat: Vector3
var target_angle_horizontal = 0
var target_angle_vertical = 0
# Called when the node enters the scene tree for the first time. # Called when the node enters the scene tree for the first time.
@ -27,10 +29,28 @@ func _physics_process(delta):
else: else:
target_pos.y = follow_target.global_transform.origin.y + target_height target_pos.y = follow_target.global_transform.origin.y + target_height
global_transform.origin = global_transform.origin.linear_interpolate(target_pos, delta * 20.0)
last_lookat = last_lookat.linear_interpolate( last_lookat = last_lookat.linear_interpolate(
follow_target.global_transform.origin, delta * 20.0 follow_target.global_transform.origin, delta * 20.0
) )
var original_diff = target_pos - follow_target.global_transform.origin
var rotation_adjustment = (
original_diff.rotated(Vector3.DOWN, target_angle_horizontal).rotated(
Vector3.LEFT, target_angle_vertical
)
- original_diff
)
target_pos += rotation_adjustment
global_transform.origin = global_transform.origin.linear_interpolate(target_pos, delta * 20.0)
look_at(last_lookat, Vector3(0.0, 1.0, 0.0)) look_at(last_lookat, Vector3(0.0, 1.0, 0.0))
func reset() -> void:
pass
func update_rotation(horizontal: float, vertical: float) -> void:
target_angle_horizontal = horizontal * deg2rad(90)
target_angle_vertical = vertical * deg2rad(90)

View File

@ -0,0 +1,11 @@
extends Camera
func reset() -> void:
rotation = Vector3.ZERO
rotate(Vector3.UP, deg2rad(180))
func update_rotation(horizontal: float, vertical: float) -> void:
rotate(Vector3.DOWN, horizontal * deg2rad(90))
rotate(Vector3.LEFT, vertical * deg2rad(90))

View File

@ -1,4 +1,7 @@
[gd_scene format=2] [gd_scene load_steps=2 format=2]
[ext_resource path="res://player/cameras/static_camera.gd" type="Script" id=1]
[node name="camera" type="Camera"] [node name="camera" type="Camera"]
far = 1000.0 far = 1000.0
script = ExtResource( 1 )

View File

@ -144,6 +144,36 @@ next_camera={
, Object(InputEventJoypadButton,"resource_local_to_scene":false,"resource_name":"","device":0,"button_index":2,"pressure":0.0,"pressed":false,"script":null) , Object(InputEventJoypadButton,"resource_local_to_scene":false,"resource_name":"","device":0,"button_index":2,"pressure":0.0,"pressed":false,"script":null)
] ]
} }
turn_camera_right={
"deadzone": 0.5,
"events": [ Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":76,"physical_scancode":0,"unicode":0,"echo":false,"script":null)
, Object(InputEventJoypadMotion,"resource_local_to_scene":false,"resource_name":"","device":0,"axis":2,"axis_value":1.0,"script":null)
]
}
turn_camera_left={
"deadzone": 0.5,
"events": [ Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":0,"physical_scancode":74,"unicode":0,"echo":false,"script":null)
, Object(InputEventJoypadMotion,"resource_local_to_scene":false,"resource_name":"","device":0,"axis":2,"axis_value":-1.0,"script":null)
]
}
turn_camera_up={
"deadzone": 0.5,
"events": [ Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":73,"physical_scancode":0,"unicode":0,"echo":false,"script":null)
, Object(InputEventJoypadMotion,"resource_local_to_scene":false,"resource_name":"","device":0,"axis":3,"axis_value":-1.0,"script":null)
]
}
turn_camera_down={
"deadzone": 0.5,
"events": [ Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":75,"physical_scancode":0,"unicode":0,"echo":false,"script":null)
, Object(InputEventJoypadMotion,"resource_local_to_scene":false,"resource_name":"","device":0,"axis":3,"axis_value":1.0,"script":null)
]
}
look_backwards={
"deadzone": 0.5,
"events": [ Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":66,"physical_scancode":0,"unicode":0,"echo":false,"script":null)
, Object(InputEventJoypadButton,"resource_local_to_scene":false,"resource_name":"","device":0,"button_index":10,"pressure":0.0,"pressed":false,"script":null)
]
}
[physics] [physics]

View File

@ -65,6 +65,15 @@ func _process(_delta: float) -> void:
if Input.is_action_just_released("next_camera"): if Input.is_action_just_released("next_camera"):
camera_controller.next_camera() camera_controller.next_camera()
camera_controller.update_camera(
(
Input.get_action_strength("turn_camera_right")
- Input.get_action_strength("turn_camera_left")
),
Input.get_action_strength("turn_camera_up") - Input.get_action_strength("turn_camera_down"),
Input.is_action_pressed("look_backwards")
)
func _on_ResetArea_body_entered(body: Node) -> void: func _on_ResetArea_body_entered(body: Node) -> void:
if body.get_groups().has("car"): if body.get_groups().has("car"):