X Tutup
Skip to content

Commit c8f6b39

Browse files
authored
Fix PutShipOnRoute function (#5250)
* Fix PutShipOnRoute function After placing the ship on the route, the possibility of a collision is checked. In case of a possible collision, its position is changed to avoid this. It turns out that this calculation worked incorrectly, because the FromVectors function creates a matrix using vectors as columns, and in the further calculation it is assumed that vectors are rows. Also removed unnecessary Frame object retrieval by FrameId The choice of trajectory correction method depends only on whether we can calculate the tangent to the sphere of the effective radius of the obstructor from the target. We cannot do this if the target is within this sphere. All other conditions are unnecessary.
1 parent 0a67da4 commit c8f6b39

File tree

1 file changed

+7
-9
lines changed

1 file changed

+7
-9
lines changed

src/lua/LuaSpace.cpp

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -253,43 +253,41 @@ static int l_space_put_ship_on_route(lua_State *l)
253253
ship->SetFuel((0.001 * pp.getMass() - ss.static_mass) / st->fuelTankMass);
254254

255255
ship->UpdateFrame();
256-
Frame *shipFrame = Frame::GetFrame(ship->GetFrame());
257-
Frame *targFrame = Frame::GetFrame(targetbody->GetFrame());
258-
259256
// check for collision at spawn position
260257
const vector3d shippos = ship->GetPosition();
261258
const vector3d targpos = targetbody->GetPositionRelTo(ship->GetFrame());
262259
const vector3d relpos = targpos - shippos;
263260
const vector3d reldir = relpos.NormalizedSafe();
264261
const double targdist = relpos.Length();
265-
Body *body = shipFrame->GetBody();
262+
Body *body = Frame::GetFrame(ship->GetFrame())->GetBody();
266263
const double erad = MaxEffectRad(body, ship->GetPropulsion());
267264
const int coll = CheckCollision(ship, reldir, targdist, targpos, 0, erad);
268265
if (coll) {
269266
// need to correct positon, to avoid collision
270-
if (Frame::GetFrame(shipFrame->GetNonRotFrame()) != Frame::GetFrame(targFrame->GetNonRotFrame()) && targpos.Length() > erad) {
271-
// the ship is not in the target's frame or target is above the effective radius of obstructor - rotate the ship's position
267+
if (targpos.Length() > erad) {
268+
// target is above the effective radius of obstructor - rotate the ship's position
272269
// around the target position, so that the obstructor's "effective radius" does not cross the path
273270
// direction obstructor -> target
274271
const vector3d z = targpos.Normalized();
275272
// the axis around which the position of the ship will rotate
276273
const vector3d y = z.Cross(shippos).NormalizedSafe();
277274
// just the third axis of this basis
278275
const vector3d x = y.Cross(z);
276+
279277
// this is the basis in which the position of the ship will rotate
280-
const matrix3x3d corrCS = matrix3x3d::FromVectors(x, y, z);
278+
const matrix3x3d corrCS = matrix3x3d::FromVectors(x, y, z).Transpose();
281279
const double len = targpos.Length();
282280
// two possible positions of the ship, when flying around the obstructor to the right or left
283281
// rotate (in the given basis) the direction from the target to the obstructor, so that it passes tangentially to the obstructor
284282
const vector3d safe1 = corrCS.Transpose() * (matrix3x3d::RotateY(+asin(erad / len)) * corrCS * -targpos).Normalized() * targdist;
285283
const vector3d safe2 = corrCS.Transpose() * (matrix3x3d::RotateY(-asin(erad / len)) * corrCS * -targpos).Normalized() * targdist;
286-
// choose the one that is closer to the current position oh the ship
284+
// choose the one that is closer to the current position of the ship
287285
if ((safe1 + relpos).Length() < (safe2 + relpos).Length())
288286
ship->SetPosition(safe1 + targpos);
289287
else
290288
ship->SetPosition(safe2 + targpos);
291289
} else {
292-
// we are in target's frame, and target below the effective radius of planet. Position the ship direct above the target
290+
// target below the effective radius of obstructor. Position the ship direct above the target
293291
ship->SetPosition(targpos + targpos.Normalized() * targdist);
294292
}
295293
// update velocity direction

0 commit comments

Comments
 (0)
X Tutup