Jump to content

[SOLVED] Replaying orbit positions from the past


Recommended Posts

Hi, in order to implement a safer interpolation mechanism on LMP I store the orbital positions in a queue and replay them after 2 seconds have passed.

The code looks something like this:
 

var startTime = KspOrbit.epoch;
var targetTime = Target.KspOrbit.epoch;

var currentPos = KspOrbit.getRelativePositionAtUT(startTime);
var targetPos = Target.KspOrbit.getRelativePositionAtUT(targetTime);

var currentVel = KspOrbit.getOrbitalVelocityAtUT(startTime) + KspOrbit.referenceBody.GetFrameVelAtUT(startTime) - Body.GetFrameVelAtUT(startTime);
var targetVel = Target.KspOrbit.getOrbitalVelocityAtUT(targetTime) + Target.KspOrbit.referenceBody.GetFrameVelAtUT(targetTime) - Target.Body.GetFrameVelAtUT(targetTime);

var lerpedPos = Vector3d.Lerp(currentPos, targetPos, LerpPercentage);
var lerpedVel = Vector3d.Lerp(currentVel, targetVel, LerpPercentage);

Vessel.orbit.UpdateFromStateVectors(lerpedPos, lerpedVel, LerpBody, Planetarium.GetUniversalTime());

KSPOrbit is the first packet Orbit, Target.KSPOrbit is the next packet, and on every fixed update frame that code is called. In order to ignore FlightIntegration issues I only call this when vessels are PACKED (OnRails) (I will deal with FI later)

The LerpPercentage is a value between 0 and 1 that increases on every fixedupdate call 

The movement is very fluid BUT the positions are displaced kind of "backwards"
This is for example a screenshot I took with 2 clients side by side:

Here the player 1 is flying above that wide line in the runway:

https://imgur.com/0oPcYHH

But the player 2 sees this:

https://imgur.com/HlEmoxJ

I understand the GREEN distance as the vessel is 2 seconds behind and it will take some time to reach the runway but I don't understand why it's displaced sideways (red distance)

Also, I tried to set this code:

var lerpTime = LunaMath.Lerp(startTime, targetTime, LerpPercentage);

Vessel.orbit.UpdateFromStateVectors(lerpedPos, lerpedVel, LerpBody, lerpTime);

But then the orbit is rendered in real time (not 2 seconds behind in time) and the vessel goes inside Kerbin a lot of times and there are weird movements.

I think there's some orbital calculation that I'm missing but I can't find how should I proceed on this...

I even tried specifying the position manually as:

Vector3d vector3d = Vessel.orbitDriver.driverTransform.rotation * Vessel.localCoM;
Vessel.SetPosition((planetPos + Vessel.orbitDriver.pos) - vector3d);

But I didn't had luck :(

Does someone have an idea of how I should implement this? It's honestly one of the oldest LMP bugs and I've been dealing with this for months and not making much progress.

EDIT: If I reduce the interpolation delay time to 300ms or so the movement is almost perfect (there's still that sideway distance altough much shorter) but I want to set a 1 or 2 seconds delay for bad connections

Edited by Dagger
Link to comment
Share on other sites

Does the track always move east? That looks like about 350 meters - suspiciously close to the distance a point on the surface would travel in 2 seconds as Kerbin rotates.

Edited by wasml
Link to comment
Share on other sites

3 hours ago, wasml said:

Does the track always move east? That looks like about 350 meters - suspiciously close to the distance a point on the surface would travel in 2 seconds as Kerbin rotates.

Yes, I would say that it's always east. Or at least when flying around KSC it's always like that

I also tried to fix that by setting this code but with no success:

planetPos = Vessel.orbitDriver.referenceBody.getPositionAtUT(lerpTime)

Vector3d vector3d = Vessel.orbitDriver.driverTransform.rotation * Vessel.localCoM;
Vessel.SetPosition((planetPos + Vessel.orbitDriver.pos) - vector3d);

I'm very sure I'm missing something but my math knowledge and specially KSP positioning is really bad :(

Edited by Dagger
Link to comment
Share on other sites

It does sound like planetary rotation is not being factored in.  I don't know if this will help, but in planetshine there is some code that accounts for that. given the goal it has  to retrograde the orbit slightly over time, even with vessels on rails. I suggest you take a peek in that mod to see if you can find something that may be useful.

Edited by Papa_Joe
Link to comment
Share on other sites

14 minutes ago, Papa_Joe said:

It does sound like planetary rotation is not being factored in.  I don't know if this will help, but in planetshine there is some code that accounts for that. given the goal it has  to retrograde the orbit slightly over time, even with vessels on rails. I suggest you take a peek in that mod to see if you can find something that may be useful.

Thanks, I will have a look at that :)

Link to comment
Share on other sites

@Dagger

So just a tidbit to add to what the other lads have mentioned ... Kerbin in constantly rotating which means the world location coordinates are constantly moving with the rotation

This will cause exactly what you are seeing (we've come across this in the BDAc team) 

As for a possible solution, if you know the rate of rotation of an orbital body you should be able to adjust for that rotation .... trick is to find the amount the coords move relative to the rotation and then shift the vessels geo coords to account for it

How to do this ... get the radius of the planet/moon (celestialbody.whatever) then do some math to get the circumference, after which you divide the circumference by the time it takes for one full rotaion and viola, you have how many degrees of shift per amount of time (in your case divide it into seconds)

Now that you have the degrees per second of rotation you should then be able to subtract/add to the geo coords as needed to account for the shift.... do note that the direction that a craft is travelling in relation to the planets rotation will change based on the vector of the craft (more complicated math but doable)

The above should get you close to a solution ... ping me if you have any questions :)

Link to comment
Share on other sites

34 minutes ago, DoctorDavinci said:

@Dagger

So just a tidbit to add to what the other lads have mentioned ... Kerbin in constantly rotating which means the world location coordinates are constantly moving with the rotation

This will cause exactly what you are seeing (we've come across this in the BDAc team) 

As for a possible solution, if you know the rate of rotation of an orbital body you should be able to adjust for that rotation .... trick is to find the amount the coords move relative to the rotation and then shift the vessels geo coords to account for it

How to do this ... get the radius of the planet/moon (celestialbody.whatever) then do some math to get the circumference, after which you divide the circumference by the time it takes for one full rotaion and viola, you have how many degrees of shift per amount of time (in your case divide it into seconds)

Now that you have the degrees per second of rotation you should then be able to subtract/add to the geo coords as needed to account for the shift.... do note that the direction that a craft is travelling in relation to the planets rotation will change based on the vector of the craft (more complicated math but doable)

The above should get you close to a solution ... ping me if you have any questions :)

I'm terrible at maths but definitely, tomorrow I will try to do it and contact you if I get stuck. Thanks a lot for the guidelines :)

Link to comment
Share on other sites

5 minutes ago, Dagger said:

I'm terrible at maths but definitely, tomorrow I will try to do it and contact you if I get stuck. Thanks a lot for the guidelines :)

I have some code in DCK FutureTech that will give you part of what I describe above .... 

Look in here .... not sure which line but look for GetSatInfo()

The code in there will give you distance per degree based on sea level, should be an easy modification to change it from distance per degree to distance per time.deltatime

https://github.com/DoctorDavinci/DCK-Future-Tech/blob/master/DCK_FutureTech_Plugin/Modules/ModuleDCKGPSSat.cs

Edited by DoctorDavinci
Link to comment
Share on other sites

3 hours ago, DoctorDavinci said:

I have some code in DCK FutureTech that will give you part of what I describe above .... 

Look in here .... not sure which line but look for GetSatInfo()

The code in there will give you distance per degree based on sea level, should be an easy modification to change it from distance per degree to distance per time.deltatime

https://github.com/DoctorDavinci/DCK-Future-Tech/blob/master/DCK_FutureTech_Plugin/Modules/ModuleDCKGPSSat.cs

Just had a look at the code and it seems nice the issue is that I play with orbital parameters and not lat/lon/alt as they allow me to have a more fluid movement.

I guess that in this case I should fix the "LAN" orbital parameter only? I think that's the only one that can be affected by the planet rotation considering there's no axial tilt...

Picture for reference:

 Image result for orbital parameters

Link to comment
Share on other sites

2 hours ago, Dagger said:

Just had a look at the code and it seems nice the issue is that I play with orbital parameters and not lat/lon/alt as they allow me to have a more fluid movement.

I guess that in this case I should fix the "LAN" orbital parameter only? I think that's the only one that can be affected by the planet rotation considering there's no axial tilt...

Picture for reference:

 Image result for orbital parameters

The point I was making is that you should be able to do the same thing I am doing with the world coords with the orbital parameters you are using ... the math and logic would be essentially the same ;)

Edited by DoctorDavinci
Link to comment
Share on other sites

4 minutes ago, DoctorDavinci said:

The point I was making is that you should be able to do the same thing I am doing with the world coords with the orbital parameters you are using ... the math and logic would be essentially the same ;)

Thanks a lot!! It worked.

By trial and error I've found that adding "0.033" to the LAN value of the received data and with an interpolation delay of 2 seconds the positioning is almost perfect!

Now I will try to do the maths and find how to get such value :)

Edited by Dagger
Link to comment
Share on other sites

1 hour ago, Dagger said:

Thanks a lot!! It worked.

By trial and error I've found that adding "0.033" to the LAN value of the received data and having an interpolation delay of 2 seconds the positioning is almost perfect!

Now I will try to do the maths and find how to get such value :)

That is awesome ... now you have the variables you need to make an equation that will cover every orbital body since each of them will rotate at different speeds if I'm not mistaken

Glad to have helped :)

Edited by DoctorDavinci
Link to comment
Share on other sites

5 minutes ago, DoctorDavinci said:

That is awesome ... now you have the variables you need to make an equation that will cover every orbital body since each of them will rotate at different speeds if I'm not mistaken

Glad to have helped :)

Found it!

Using the sidereal rotation period it's a simple rule of 3.

Example:
https://wiki.kerbalspaceprogram.com/wiki/Kerbin
Kerbin sidereal is 21549.425 seconds so we know it makes a 360º turn in that time

If we have a interpolation of 2 seconds--->
21549.425 ----------- 360
2           --------------- X

2*360/21549.425 = 0.3341

That's the amount of degrees that I should add to the LAN that a player sent me :)

Thank you all for your help!!

 

Link to comment
Share on other sites

This thread is quite old. Please consider starting a new thread rather than reviving this one.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...