From 8c43b39f01acf1287c264ea37ecef9577221e300 Mon Sep 17 00:00:00 2001 From: Peery Date: Sat, 19 Nov 2022 00:42:35 +0100 Subject: [PATCH] Added space scene and Railgun Added an empty space with the nebula as a skybox. Also created the railgun subscene with scripts to follow a given target with the turret. --- scenes/entities/DebugLookAtTarget.gd | 23 ++++++++ scenes/entities/railgun.gd | 85 ++++++++++++++++++++++++++++ scenes/entities/railgun.tscn | 54 ++++++++++++++++++ scenes/space.tscn | 46 +++++++++++++++ 4 files changed, 208 insertions(+) create mode 100644 scenes/entities/DebugLookAtTarget.gd create mode 100644 scenes/entities/railgun.gd create mode 100644 scenes/entities/railgun.tscn create mode 100644 scenes/space.tscn diff --git a/scenes/entities/DebugLookAtTarget.gd b/scenes/entities/DebugLookAtTarget.gd new file mode 100644 index 0000000..38144cc --- /dev/null +++ b/scenes/entities/DebugLookAtTarget.gd @@ -0,0 +1,23 @@ +extends Position3D + + +var maxX = 10 +var minX = -10 + +var SPEED = 1 + +var state = -1 + +# Called when the node enters the scene tree for the first time. +func _ready(): + pass + + +func _process(delta): + if self.translation.x < minX or self.translation.x > maxX: + self.state *= -1 + + if state == -1: # moving negative + self.transform.origin += Vector3(1, 0, 0) * SPEED * delta + elif state == 1: + self.transform.origin += Vector3(-1, 0, 0) * SPEED * delta diff --git a/scenes/entities/railgun.gd b/scenes/entities/railgun.gd new file mode 100644 index 0000000..45ad80d --- /dev/null +++ b/scenes/entities/railgun.gd @@ -0,0 +1,85 @@ +extends Spatial + +export(NodePath) var LookAtTarget setget _set_lookAtTarget_path + +### Railgun turret entity controller + + +onready var skeleton = $Armature/Skeleton +onready var turret_yaw_bone = skeleton.find_bone("turret head") +onready var turret_pitch_bone = skeleton.find_bone("vertical joint") +onready var vert_eyes = $VertEyes +onready var turret_eyes = $Armature/Skeleton/turretAttachement/TurretEyes + +var first_call: bool = true +var target: Position3D + +#var additional_rotation = Vector3(90, 0, 0) + +func _set_lookAtTarget_path(new_value): + # Because get_node doesn't work in the first call, we just want to assign instead. + # This is to get around a issue with NodePaths exposed to the editor. + if first_call: + LookAtTarget = new_value + return + + # Assign skeleton_path to whatever value is passed. + LookAtTarget = new_value + + if LookAtTarget == null: + return + + # Get the node at that location, if there is one. + var temp = get_node(LookAtTarget) + if temp != null: + if temp is Position3D: + target = get_node(LookAtTarget) + return + else: + print("Railgun.gd: Got invalid LookAtTarget: ", LookAtTarget) + return + else: + print("Railgun.gd: Got no LookAtTarget: ", LookAtTarget) + +func _ready(): + pass # configuring skeletonIK + + +func _process(delta): + # Calculating the two points where the vertical joint should point to to aim at the target + """ + var T = target.transform.origin + var C = skeleton.get_bone_global_pose(turret_pitch_bone).origin # Vector3 root of the pitch bone + + var radius_C = (skeleton.get_bone_global_pose(turret_yaw_bone).origin - C).length() + var normal_C = (T-C).normalized() + + var normal_C_angle = acos(Vector3(0, normal_C.y, normal_C.z).dot(normal_C)) # to transform the T point into a 2D plane and THEN do 2D circle intersection + + var radius_T = (T - C).length() # length of CT which will be the radius of the circle around T""" + if first_call: + first_call = false + if target == null: + _set_lookAtTarget_path(LookAtTarget) + + var turret_height_diff = (turret_eyes.global_transform.origin - vert_eyes.global_transform.origin) + + vert_eyes.look_at((target.global_transform.origin-turret_height_diff), Vector3.UP) + skeleton.set_bone_pose(turret_pitch_bone, Basis(Vector3.RIGHT, -1*vert_eyes.rotation.x)) + + var vert_bone_direction = skeleton.get_bone_pose(turret_pitch_bone) * Vector3.UP + + var target_pos = skeleton.global_transform.xform_inv(target.global_transform.origin) + + var rest = skeleton.get_bone_global_pose(turret_yaw_bone) + rest = rest.looking_at(target_pos, vert_bone_direction) + + # Get the rotation euler of the bone and of this node. + #var rest_euler = rest.basis.get_euler() + + # Apply additional rotation stored in additional_rotation to the bone. + + # Make a new basis with the, potentially, changed euler angles. + #rest.basis = Basis(rest_euler) + + skeleton.set_bone_global_pose_override(turret_yaw_bone, rest, 1, true) diff --git a/scenes/entities/railgun.tscn b/scenes/entities/railgun.tscn new file mode 100644 index 0000000..8de2572 --- /dev/null +++ b/scenes/entities/railgun.tscn @@ -0,0 +1,54 @@ +[gd_scene load_steps=7 format=2] + +[ext_resource path="res://assets/entities/railgun/railgun.glb" type="PackedScene" id=1] +[ext_resource path="res://scenes/entities/railgun.gd" type="Script" id=2] + +[sub_resource type="PrismMesh" id=2] + +[sub_resource type="Skin" id=3] + +[sub_resource type="SpatialMaterial" id=7] +albedo_color = Color( 0.956863, 0.635294, 0.635294, 1 ) + +[sub_resource type="SphereMesh" id=8] +material = SubResource( 7 ) + +[node name="railgun" instance=ExtResource( 1 )] +script = ExtResource( 2 ) + +[node name="Skeleton" parent="Armature" index="0"] +bones/1/rest = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0 ) + +[node name="turretAttachement" type="BoneAttachment" parent="Armature/Skeleton" index="1"] +transform = Transform( -1, 0, 1.50996e-07, 0, 1, 0, -1.50996e-07, 0, -1, 0, 2.92139, 0 ) +bone_name = "turret head" + +[node name="TurretEyes" type="Spatial" parent="Armature/Skeleton/turretAttachement" index="0"] +transform = Transform( 1, 0, 0, 0, -4.37114e-08, -1, 0, 1, -4.37114e-08, 0, 0, 0 ) +__meta__ = { +"_editor_description_": "Helper Node which gets the rotation to face a target with look_at() to be copied by a bone" +} + +[node name="MeshInstance" type="MeshInstance" parent="Armature/Skeleton/turretAttachement/TurretEyes" index="0"] +visible = false +mesh = SubResource( 2 ) +skin = SubResource( 3 ) + +[node name="RayCast" type="RayCast" parent="Armature/Skeleton/turretAttachement/TurretEyes" index="1"] +transform = Transform( 0.1, -1.50996e-08, -8.74228e-08, -1.50996e-08, -0.1, -4.37114e-08, -8.74228e-09, 4.37114e-09, -1, 0, 0, 0 ) + +[node name="MeshInstance" type="MeshInstance" parent="Armature/Skeleton/turretAttachement/TurretEyes/RayCast" index="0"] +transform = Transform( 1, -8.5412e-22, -1.73472e-18, -2.71051e-20, -4.37114e-09, 70, 7.10543e-15, -0.1, -3.0598e-06, 1.3415e-11, 69.095, 0 ) +mesh = SubResource( 8 ) + +[node name="AnimationPlayer" type="AnimationPlayer" parent="." index="1"] + +[node name="VertEyes" type="Spatial" parent="." index="2"] +transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0 ) +__meta__ = { +"_editor_description_": "Helper Node which gets the rotation to face a target with look_at() to be copied by a bone" +} + +[node name="MeshInstance" type="MeshInstance" parent="VertEyes" index="0"] +mesh = SubResource( 2 ) +skin = SubResource( 3 ) diff --git a/scenes/space.tscn b/scenes/space.tscn new file mode 100644 index 0000000..01c196d --- /dev/null +++ b/scenes/space.tscn @@ -0,0 +1,46 @@ +[gd_scene load_steps=7 format=2] + +[ext_resource path="res://assets/environment/skyboxes/nebula_space/panorama_image.png" type="Texture" id=1] +[ext_resource path="res://scenes/entities/railgun.tscn" type="PackedScene" id=2] +[ext_resource path="res://scenes/entities/DebugLookAtTarget.gd" type="Script" id=3] + +[sub_resource type="PanoramaSky" id=1] +panorama = ExtResource( 1 ) + +[sub_resource type="Environment" id=2] +background_mode = 2 +background_sky = SubResource( 1 ) + +[sub_resource type="SphereMesh" id=3] + +[node name="world" type="Node"] + +[node name="WorldEnvironment" type="WorldEnvironment" parent="."] +environment = SubResource( 2 ) + +[node name="entities" type="Node" parent="."] + +[node name="railgun" parent="entities" instance=ExtResource( 2 )] +transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 7.50682, 0, -7.59585 ) +LookAtTarget = NodePath("LookAtTarget2") + +[node name="LookAtTarget2" type="Position3D" parent="entities/railgun"] +transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 8.694, 4.553 ) +script = ExtResource( 3 ) + +[node name="MeshInstance" type="MeshInstance" parent="entities/railgun/LookAtTarget2"] +mesh = SubResource( 3 ) + +[node name="lighting" type="Node" parent="."] + +[node name="main_light" type="DirectionalLight" parent="lighting"] +transform = Transform( 0.784772, 0.331552, 0.523648, -0.612234, 0.54618, 0.571714, -0.0964525, -0.76926, 0.631614, 7.76443, 10.2646, 7.06444 ) +light_color = Color( 1, 0.941176, 0.854902, 1 ) + +[node name="rim_light" type="DirectionalLight" parent="lighting/main_light"] +transform = Transform( 0.921065, 0.320717, -0.22086, 0.2725, -0.936009, -0.222781, -0.278177, 0.145011, -0.94952, -3.6761, -8.54977, -15.0604 ) +light_color = Color( 0.411765, 0.854902, 0.92549, 1 ) +light_energy = 0.2 + +[node name="Camera" type="Camera" parent="."] +transform = Transform( 0.8208, 0.16956, -0.545469, 0, 0.954927, 0.296842, 0.571215, -0.243648, 0.783804, -4.41776, 10.7973, 11.901 )