-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdvGPstat.lua
More file actions
75 lines (65 loc) · 3.26 KB
/
Copy pathdvGPstat.lua
File metadata and controls
75 lines (65 loc) · 3.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
behaviour("dvGsim")
function dvGsim:Start()
self.vehicle = self.gameObject.GetComponent(Vehicle)
self.gText = self.targets.gText.GetComponent(Text)
self.aoaText = self.targets.aoaText.GetComponent(Text)
self.machText = self.targets.machText.GetComponent(Text)
self.altitudeText = self.targets.altitudeText.GetComponent(Text) -- Added Altitude text target
self.animator = self.targets.animator.GetComponent(Animator)
self.gValue = 0
self.previousVelocity = Vector3.zero
self.updateInterval = 0.1
self.timeSinceLastUpdate = 0
self.speedOfSound = 343 -- Speed of sound in m/s at sea level (can vary with altitude)
end
function dvGsim:FixedUpdate()
if self.vehicle.hasDriver and self.vehicle.driver == Player.actor then
local rb = self.vehicle.gameObject.GetComponent(Rigidbody)
local currentVelocity = rb.velocity
local speed = currentVelocity.magnitude
local acceleration = (currentVelocity - self.previousVelocity) / Time.fixedDeltaTime
-- Calculate Mach number
local machNumber = speed / self.speedOfSound
-- Get water level and calculate altitude
local waterLevel = Water.GetWaterLevel(self.vehicle.transform.position)
local altitude = Mathf.RoundToInt(self.vehicle.transform.position.y - waterLevel)
-- Use vehicle's downward direction
local gravityDirection = Vector3(0, 1, 0)
local totalAcceleration = acceleration + gravityDirection * 9.8
local gForce = totalAcceleration.magnitude / 9.8
local sign = Vector3.Dot(totalAcceleration, self.vehicle.transform.up) >= 0 and 1 or -1
gForce = gForce * sign
self.gValue = gForce
-- Calculate the angle of attack (AoA)
local forwardDirection = self.vehicle.transform.forward
local velocityDirection = currentVelocity.normalized
local aoaRadians = Mathf.Asin(Vector3.Dot(Vector3.Cross(forwardDirection, velocityDirection), self.vehicle.transform.right))
local aoaDegrees = Mathf.Rad2Deg * aoaRadians
-- Update animator with AoA and G-value
self.animator:SetFloat("AoA", aoaDegrees)
self.animator:SetFloat("Gvalue", self.gValue)
-- Update text every 0.1 second
self.timeSinceLastUpdate = self.timeSinceLastUpdate + Time.fixedDeltaTime
if self.timeSinceLastUpdate >= self.updateInterval then
self.timeSinceLastUpdate = 0
local gForceText = string.format("%.1f", self.gValue)
local aoaText = string.format("%.1f", aoaDegrees)
local machText = string.format("%.2f", machNumber)
local altitudeText = string.format("%d", altitude) -- Altitude as integer
if self.gText ~= nil then
self.gText.text = gForceText
end
if self.aoaText ~= nil then
self.aoaText.text = aoaText
end
if self.machText ~= nil then
self.machText.text = machText
end
if self.altitudeText ~= nil then
self.altitudeText.text = altitudeText
end
end
-- Store current velocity for next frame
self.previousVelocity = currentVelocity
end
end