Skip to content

Commit

Permalink
Merge pull request #22 from grgp/bidirectional-door-swing-using-rotat…
Browse files Browse the repository at this point in the history
…ion-tween

Add option for bi-directional door swing (using rotation tween)
  • Loading branch information
Phazorknight authored Jan 19, 2024
2 parents 8b0315e + 862aa8d commit 37166b4
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 6 deletions.
16 changes: 16 additions & 0 deletions COGITO/DemoScenes/COGITO_Demo_01.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -736,6 +736,10 @@ text = "This crate is carriable."
transform = Transform3D(-4.37114e-08, 0, 1, 0, 1, 0, -1, 0, -4.37114e-08, -4.7952, 2.21944, 9.62556)
text = "This is a basic door."

[node name="Txt_DoorBiDirect" type="Label3D" parent="STATIC_ENVIRONMENT"]
transform = Transform3D(-4.37114e-08, 0, -1, 0, 1, 0, 1, 0, -4.37114e-08, 4.78901, 2.63174, 9.71411)
text = "This door swings in both directions"

[node name="Txt_DoorBasic2" type="Label3D" parent="STATIC_ENVIRONMENT"]
transform = Transform3D(-4.37114e-08, 0, 1, 0, 1, 0, -1, 0, -4.37114e-08, -14.5952, 4.81944, 6.82556)
text = "Work in progress."
Expand Down Expand Up @@ -1071,6 +1075,11 @@ transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -33.2, 0.6, 18.8)
size = Vector3(0.4, 1.2, 0.4)
material = ExtResource("1_lsda4")

[node name="Corner4" type="CSGBox3D" parent="STATIC_ENVIRONMENT"]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 2.47068, 0.6, 9.2)
size = Vector3(0.2, 1.2, 0.2)
material = ExtResource("1_lsda4")

[node name="LightzoneCeiling" type="StaticBody3D" parent="STATIC_ENVIRONMENT"]
transform = Transform3D(0, 0, -1, 0, 1, 0, 1, 0, 0, -6, 2.975, 3.8)

Expand Down Expand Up @@ -1132,6 +1141,13 @@ open_rotation_deg = -95.0
closed_position = Vector3(-14.9, 0, 9.2)
open_position = Vector3(-14.9, 0, 8.309)

[node name="Door_BiDirect1" parent="INTERACTIVE_OBJECTS" instance=ExtResource("8_xabdk")]
transform = Transform3D(1, 0, -1.74846e-07, 0, 1, 0, 1.74846e-07, 0, 1, -7.53747, 0, 9.29669)
bidirectional_swing = true
open_rotation_deg = -95.0
closed_position = Vector3(-14.9, 0, 9.2)
open_position = Vector3(-14.9, 0, 8.309)

[node name="Lamp_Standing" parent="INTERACTIVE_OBJECTS" instance=ExtResource("18_oxm1j")]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -16.7066, 0, 5.95227)

Expand Down
21 changes: 15 additions & 6 deletions COGITO/Scripts/Interact_Door.gd
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ extends Node3D
@export var is_sliding : bool
## Rotation axis to use. True = use Z axis. False = use Y axis:
@export var use_z_axis : bool = false
## Set this to true if the door should swing opposite the direction of the interactor
@export var bidirectional_swing : bool = false
## Rotation Y when the door is open. In degrees.
@export var open_rotation_deg : float = 0.0
## Rotation Y when the door is closed. In degrees.
Expand Down Expand Up @@ -72,17 +74,17 @@ func _ready():
interaction_text = interaction_text_when_closed


func interact(interactor):
func interact(interactor: Node3D):
if !is_locked:
if !is_open:
open_door()
open_door(interactor)

for nodepath in doors_to_sync_with:
if nodepath != null:
var object = get_node(nodepath)
object.open_door()
else:
close_door()
close_door(interactor)

for nodepath in doors_to_sync_with:
if nodepath != null:
Expand Down Expand Up @@ -134,14 +136,21 @@ func unlock_door():
is_locked = false
interaction_text = interaction_text_when_closed

func open_door():
func open_door(interactor: Node3D):
audio_stream_player_3d.stream = open_sound
audio_stream_player_3d.play()

if is_animation_based:
anim_player.play(opening_animation)
elif !is_sliding:
target_rotation_rad = deg_to_rad(open_rotation_deg)
var swing_direction: int = 1

if bidirectional_swing:
var offset: Vector3 = interactor.global_transform.origin - global_transform.origin
var offset_dot_product: float = offset.dot(global_transform.basis.x)
swing_direction = -1 if offset_dot_product < 0 else 1

target_rotation_rad = deg_to_rad(open_rotation_deg * swing_direction)
is_moving = true
else:
var tween_door = get_tree().create_tween()
Expand All @@ -150,7 +159,7 @@ func open_door():
is_open = true
interaction_text = interaction_text_when_open

func close_door():
func close_door(interactor: Node3D):
audio_stream_player_3d.stream = close_sound
audio_stream_player_3d.play()

Expand Down

0 comments on commit 37166b4

Please sign in to comment.