Skip to content

Commit

Permalink
Fixes and changes for functions in LuaBody/Player/Ship
Browse files Browse the repository at this point in the history
Rewrite of GetAltitudeRelTo:
1. This function is moved to body class to support this functionality in C++ code;
2. This function is used in the LUA version of GetAltitudeRelTo, GetGroundPosition and GetGPS for consistency in calculating altitudes;
3. This function supports the choice of which height to calculate (sea level, above ground).

Other change:
1. GetGPS is moved to Ship.lua to support this function for other ships, not only the player. Implementing this function in C++ is considered unnecessary because of slightly different parameter interface and because of this can be simplified a lot;
2. GetGroundPosition also supports the choice of which altitude to calculate (sea level or above terrain).

Fixes for this issues:

1. GetAltitudeRelTo using player pos no matter what first argument is passed;
2. GetGroundPosition returns nil when not in the rotating frame of ref;
3. GetGPS sometimes return different height than GetAltitudeRelTo;
4. Access violation when trying to access frameBody, frameRotation and call GetGroundPosition when body doesn't have frame of ref (ex. when ship is hyperspacing).

Co-Authored-By: Webster Sheets <[email protected]>
  • Loading branch information
Max5377 and sturnclaw committed Nov 16, 2023
1 parent 04c6111 commit 1cb457b
Show file tree
Hide file tree
Showing 9 changed files with 156 additions and 126 deletions.
34 changes: 34 additions & 0 deletions data/libs/Ship.lua
Original file line number Diff line number Diff line change
Expand Up @@ -668,6 +668,40 @@ function Ship:OnScoopCargo(cargoType)
return success
end


--
-- Method: GetGPS
--
-- Get altitude, speed, and position of a ship
--
-- > alt, vspd, lat, long = ship:GetGPS()
--
-- Returns:
--
-- alt - altitude
--
-- vspd - vertical speed
--
-- lat - latitude
--
-- lon - longitude
--
-- Availability:
--
-- November, 2023
--
-- Status:
--
-- experimental
--
function Ship:GetGPS()
local lat, lon, altitude = self:GetGroundPosition()
local vspd = self:GetVelocityRelTo(self.frameBody):dot(self:GetPositionRelTo(self.frameBody):normalized())
lat = math.rad2deg(lat)
lon = math.rad2deg(lon)
return altitude, vspd, lat, lon
end

--
-- Method: Enroll
--
Expand Down
12 changes: 7 additions & 5 deletions data/meta/CoreObject/Body.meta.lua
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,12 @@ function Body:DistanceTo(otherBody) end

--- Get the body's position relative to its parent frame.
---
--- If the parent is a TerrainBody, altitude will be the height above terrain in meters.
--- If the parent is a TerrainBody, altitude will be the height above terrain or sea level in meters.
---@param boolean? terrainRelative
---@return number latitude the latitude of the body in radians
---@return number longitude the longitude of the body in radians
---@return number? altitude altitude above the ground in meters
function Body:GetGroundPosition() end
---@return number? altitude altitude above the ground or sea level in meters
function Body:GetGroundPosition(terrainRelative) end

--- Find the nearest object of a <Constants.PhysicsObjectType> type
---@param type PhysicsObjectType
Expand All @@ -104,11 +105,12 @@ function Body:GetPositionRelTo(other) end

--- Get the body's altitude relative to another body.
---
--- Returns height above terrain if the other body is a TerrainBody or
--- Returns height above terrain or sea level if the other body is a TerrainBody or
--- distance between bodies otherwise.
---@param other Body
---@param boolean? terrainRelative
---@return number altitude
function Body:GetAltitudeRelTo(other) end
function Body:GetAltitudeRelTo(other, terrainRelative) end

---@return number
function Body:GetPhysicalRadius() end
Expand Down
30 changes: 30 additions & 0 deletions src/Body.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,36 @@ vector3d Body::GetVelocityRelTo(const Body *relTo) const
return GetVelocityRelTo(relTo->m_frame) - relTo->GetVelocityRelTo(relTo->m_frame);
}

double Body::GetAltitudeRelTo(const Body* relTo, AltitudeType altType)
{
vector3d pos = GetPositionRelTo(relTo);
double center_dist = pos.Length();
if (relTo && relTo->IsType(ObjectType::TERRAINBODY)) {
const TerrainBody* terrain = static_cast<const TerrainBody*>(relTo);
vector3d surface_pos = pos.Normalized();
double radius;
if (altType != AltitudeType::DEFAULT)
{
radius = altType == AltitudeType::SEA_LEVEL ? terrain->GetSystemBody()->GetRadius() :
terrain->GetTerrainHeight(surface_pos);
}
else
{
radius = terrain->GetSystemBody()->GetRadius();
if (center_dist <= 3.0 * terrain->GetMaxFeatureRadius()) {
radius = terrain->GetTerrainHeight(surface_pos);
}
}
double altitude = center_dist - radius;
if (altitude < 0)
altitude = 0;
return altitude;
}
else {
return center_dist;
}
}

void Body::OrientOnSurface(double radius, double latitude, double longitude)
{
vector3d up = vector3d(cos(latitude) * cos(longitude), sin(latitude) * cos(longitude), sin(longitude));
Expand Down
8 changes: 8 additions & 0 deletions src/Body.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,13 @@ enum class ObjectType { // <enum name=PhysicsObjectType scope='ObjectType' publi
HYPERSPACECLOUD // <enum skip>
};

enum class AltitudeType { // <enum name=AltitudeType scope='AltitudeType' public>
//SEA_LEVEL if distant, ABOVE_TERRAIN otherwise
DEFAULT,
SEA_LEVEL,
ABOVE_TERRAIN
};

#define OBJDEF(__thisClass, __parentClass, __TYPE) \
static constexpr ObjectType StaticType() { return ObjectType::__TYPE; } \
static constexpr ObjectType SuperType() { return __parentClass::StaticType(); } \
Expand Down Expand Up @@ -175,6 +182,7 @@ class Body : public DeleteEmitter, public PropertiedObject {
vector3d GetInterpPositionRelTo(FrameId relToId) const;
vector3d GetInterpPositionRelTo(const Body *relTo) const;
matrix3x3d GetInterpOrientRelTo(FrameId relToId) const;
double GetAltitudeRelTo(const Body* relTo, AltitudeType altType = AltitudeType::DEFAULT);

// should set m_interpolatedTransform to the smoothly interpolated value
// (interpolated by 0 <= alpha <=1) between the previous and current physics tick
Expand Down
9 changes: 9 additions & 0 deletions src/enum_table.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,13 @@ const struct EnumItem ENUM_PhysicsObjectType[] = {
{ 0, 0 },
};

const struct EnumItem ENUM_AltitudeType[] = {
{ "DEFAULT", int(AltitudeType::DEFAULT) },
{ "SEA_LEVEL", int(AltitudeType::SEA_LEVEL) },
{ "ABOVE_TERRAIN", int(AltitudeType::ABOVE_TERRAIN) },
{ 0, 0 },
};

const struct EnumItem ENUM_ShipAIError[] = {
{ "NONE", int(Ship::AIERROR_NONE) },
{ "GRAV_TOO_HIGH", int(Ship::AIERROR_GRAV_TOO_HIGH) },
Expand Down Expand Up @@ -308,6 +315,7 @@ const struct EnumItem ENUM_ShipControllerFlightControlState[] = {

const struct EnumTable ENUM_TABLES[] = {
{ "PhysicsObjectType", ENUM_PhysicsObjectType },
{ "AltitudeType", ENUM_AltitudeType },
{ "ShipAIError", ENUM_ShipAIError },
{ "ShipFlightState", ENUM_ShipFlightState },
{ "ShipJumpStatus", ENUM_ShipJumpStatus },
Expand Down Expand Up @@ -338,6 +346,7 @@ const struct EnumTable ENUM_TABLES[] = {

const struct EnumTable ENUM_TABLES_PUBLIC[] = {
{ "PhysicsObjectType", ENUM_PhysicsObjectType },
{ "AltitudeType", ENUM_AltitudeType },
{ "ShipAIError", ENUM_ShipAIError },
{ "ShipFlightState", ENUM_ShipFlightState },
{ "ShipJumpStatus", ENUM_ShipJumpStatus },
Expand Down
1 change: 1 addition & 0 deletions src/enum_table.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ struct EnumTable {
};

extern const struct EnumItem ENUM_PhysicsObjectType[];
extern const struct EnumItem ENUM_AltitudeType[];
extern const struct EnumItem ENUM_ShipAIError[];
extern const struct EnumItem ENUM_ShipFlightState[];
extern const struct EnumItem ENUM_ShipJumpStatus[];
Expand Down
Loading

0 comments on commit 1cb457b

Please sign in to comment.