first init
This commit is contained in:
40
Assets/shipAiComponent.gd
Normal file
40
Assets/shipAiComponent.gd
Normal file
@@ -0,0 +1,40 @@
|
||||
extends Node
|
||||
class_name ShipAiComponent
|
||||
|
||||
var text: Label3D
|
||||
var colShape: CollisionShape3D
|
||||
var distText = "Distance to collision: "
|
||||
var secText = "Seconds to collision: "
|
||||
|
||||
var dist = 0.0
|
||||
var collision = 0.0
|
||||
|
||||
func _ready():
|
||||
text = $"../Label3D"
|
||||
colShape = $"../CollisionShape3D"
|
||||
|
||||
|
||||
func _physics_process(delta):
|
||||
calcColDist(delta)
|
||||
|
||||
text.text = distText + String("%0.2f" % dist) + " Meters\n" + secText + String("%0.2f" % collision) + " Seconds"
|
||||
|
||||
func calcColDist(_delta) -> void:
|
||||
var rigidBody: RigidBody3D = $".."
|
||||
var rayCast: RayCast3D = $"../RayCast3D"
|
||||
var pos = rigidBody.global_position
|
||||
var linearVelocity = rigidBody.linear_velocity.normalized() * 100
|
||||
|
||||
rayCast.target_position = rigidBody.to_local(linearVelocity)
|
||||
# rayCast.rotation = rigidBody.rotation
|
||||
# rayCast.position = pos
|
||||
if rayCast.is_colliding():
|
||||
dist = (rayCast.get_collision_point() - pos).length() - 2.5 # Ship size-> eg. bounding box
|
||||
collision = dist / rigidBody.linear_velocity.length()
|
||||
if (collision <= 0.0):
|
||||
collision = 0.0
|
||||
|
||||
|
||||
else:
|
||||
dist = 0.0
|
||||
collision = 0.0
|
||||
Reference in New Issue
Block a user