Calculate and show the required speed at all points of the route

This commit is contained in:
Martin Asprusten
2025-07-01 14:15:47 +02:00
parent f0b4479664
commit 00db764fd0
5 changed files with 76 additions and 10 deletions
+60 -1
View File
@@ -54,6 +54,7 @@ struct JSNodeInfo {
float positionZ;
float distanceFromStart;
float currentSpeed;
float requiredSpeed;
};
struct JSSearchResult {
@@ -459,7 +460,36 @@ JSSearchResult findAllPathsFromPointJS(int startingNode, float minimumSpeed, flo
return searchResult;
}
std::vector<JSNodeInfo> getPathJS(uint32_t startingNode, uint32_t endNode) {
float calculateRequiredSpeed(float endSpeed, float horizontalDistance, float heightDifference, float dragCoefficient) {
// The bike will never reach faster speeds than terminal velocity in free fall
float maximumSpeed = sqrt(GRAVITY_ACCELERATION / dragCoefficient);
// First, check if we'll reach the required speed no matter what
if (calculate_speed(0.00001, horizontalDistance, heightDifference, 0.0, maximumSpeed, dragCoefficient) >= endSpeed) {
return 0.0;
}
// Divide and conquer
float numerator = 0;
float denominator = 0.5;
float foundEndSpeed = -100.0;
do {
numerator *= 2;
denominator *= 2;
if (foundEndSpeed > endSpeed) {
numerator -= 1;
} else {
numerator += 1;
}
foundEndSpeed = calculate_speed(maximumSpeed * numerator / denominator, horizontalDistance, heightDifference, 0.01, 100.0, dragCoefficient);
} while (fabs(foundEndSpeed - endSpeed) > 0.013);
float requiredSpeed = maximumSpeed * numerator / denominator;
return requiredSpeed;
}
std::vector<JSNodeInfo> getPathJS(uint32_t startingNode, uint32_t endNode, float dragCoefficient) {
std::vector<JSNodeInfo> path;
if (lastSearchResult.startingNode != startingNode) {
@@ -496,6 +526,34 @@ std::vector<JSNodeInfo> getPathJS(uint32_t startingNode, uint32_t endNode) {
path.push_back(nodeInfo);
}
float currentRequiredSpeed = -1.0;
for (auto it = path.rbegin(); it != path.rend(); it++) {
if (currentRequiredSpeed <= -1.0) {
it->requiredSpeed = 1.0;
currentRequiredSpeed = 1.0;
} else {
uint32_t currentNodeId = it->nodeId;
RoadNode currentNode = set.roadNodes[currentNodeId];
uint32_t nextNodeId = (it - 1)->nodeId;
RoadNode nextNode = set.roadNodes[nextNodeId];
float heightDifference = nextNode.position_z - currentNode.position_z;
Connection neighbours[10];
int numberOfNeighbours = 0;
getNeighbourConnections(currentNode, neighbours, numberOfNeighbours);
for (int i = 0; i < numberOfNeighbours; i++) {
Connection neighbour = neighbours[i];
if (neighbour.connected_point_number == nextNodeId) {
float horizontalDistance = neighbour.distance;
currentRequiredSpeed = calculateRequiredSpeed(currentRequiredSpeed, horizontalDistance, heightDifference, dragCoefficient);
it->requiredSpeed = currentRequiredSpeed;
break;
}
}
}
}
return path;
}
@@ -761,6 +819,7 @@ EMSCRIPTEN_BINDINGS(my_module) {
.property("positionY", &JSNodeInfo::positionY)
.property("positionZ", &JSNodeInfo::positionZ)
.property("currentSpeed", &JSNodeInfo::currentSpeed)
.property("requiredSpeed", &JSNodeInfo::requiredSpeed)
.property("distanceFromStart", &JSNodeInfo::distanceFromStart);
emscripten::class_<JSSearchResult>("SearchResult")