Jump to content

Teleport active vessel to another vessel


Recommended Posts

This is my first foray into plugin development and I know there are many plugins that do teleport stuff already, but this is a learning experience for me.

Even after checking code from other plugins, I’m having issues getting this to work…

I’ve built two ‘Jump Gates’ - Jump Gate LKO (in 100km orbit) & Jump Gate HKO (in 1000km orbit). Each of these has a Jump Gate Device part attached.

I have player Ship in Kerbin orbit close to Jump Gate LKO.

Jump Gate Device on Jump Gate LKO has been assigned Jump Gate HKO as a destination via right-click menu.

With Player Ship active, I right click the Jump Gate Device attached to Jump Gate LKO and click ‘Jump’.

What is supposed to happen:

Player ship instantly teleports to Jump Gate HKO, matching its orbit. The player ship should teleport, the Jump Gates should not.

What actually happens:

Player ship does teleport to HKO, but the orbit is not set correctly - Player ship is now on escape trajectory. Furthermore - and this is the *really* strange bit - the Jump Gate HKO has also had its orbit changed; it’s also now on an escape trajectory too. Jump Gate LKO is fine though.

Heres the relevant part of the code (part of ‘JumpDevice : PartModule’ class:


[KSPField(isPersistant = true, guiActive = true, guiName = "Target")]
public string targetJumpGateName = "Not set";

[KSPEvent(guiActiveUnfocused = true, externalToEVAOnly = false, unfocusedRange = 50f, guiName = "Jump")]
public void JumpEvent() {

// At this point, targetJumpGateName has been set to a destination; i.e ‘Jump Gate HKO’.
Vessel targetJumpGate = findVesselByName(targetJumpGateName);

// Check the Jump Gate target vessel exists
if(targetJumpGate) {

// Check the Jump Gate target has a Jump Gate Device - i.e is it actually a Jump Gate
if(hasJumpGateDevice(targetJumpGate)) {

Vessel activeVessel = FlightGlobals.ActiveVessel;

activeVessel.GoOnRails();

// I probably need to add some distance between ship and jump gate so as not to collide, but for now they’re not
activeVessel.SetWorldVelocity(targetJumpGate.orbit.GetVel());
activeVessel.SetPosition(targetJumpGate.GetWorldPos3D());

activeVessel.GoOffRails();

} else {

// Target vessel does not have a Jump Gate Device part
}
} else {

// Target not found (might’ve been deleted)
}
}

public Vessel findVesselByName(string _name) {

List<Vessel> allVessels = FlightGlobals.Vessels;

foreach(var vessel in allVessels) {

if(vessel.GetName() == _name) {

return vessel;
}
}

return null;
}

The positioning code is based on plugin source I found here: http://forum.kerbalspaceprogram.com/threads/16684-Hyperjump-System-Work-in-Progress-Models-wanted-Demo-Attached?highlight=teleport+orbit

I’ve also tried creating a new orbit (see below) but I get the same problem (though this time its a sub-orbit straight into Kerbin, and the destination Jump Gate HKO still gets moved too, which it shouldn’t):


Orbit newOrbit = new Orbit(

targetJumpGate.orbit.inclination,
targetJumpGate.orbit.eccentricity,
targetJumpGate.orbit.semiMajorAxis,
targetJumpGate.orbit.LAN,
targetJumpGate.orbit.argumentOfPeriapsis,
targetJumpGate.orbit.meanAnomalyAtEpoch,
targetJumpGate.orbit.epoch - 1, // In theory this’ll give us some distance so the ship doesn’t collide with the destination Jump Gate
targetJumpGate.orbit.referenceBody
);

activeVessel.orbitDriver.orbit = newOrbit;
activeVessel.orbitDriver.orbit.Init();
activeVessel.orbitDriver.orbit.UpdateFromUT(Planetarium.GetUniversalTime());

That orbit code is based on what I could find & understand of the HyperEdit source (https://github.com/Ezriilc/HyperEdit/blob/master/Model/OrbitEditor.cs).

So I need help with two problems really:

1. Getting the ship into a correct orbit nearby the destination Jump Gate

2. Getting the destination Jump Gate to keep its damn orbit

Any help would be much appreciated!

Thanks

Dave

Link to comment
Share on other sites

Thanks for the reply Sashan,

I am already looking at the HyperEdit code; specifically here:

https://github.com/Ezriilc/HyperEdit/blob/master/Model/OrbitEditor.cs

In particular the 'HardsetOrbit' method. However, I must be missing something as when I use very similar code to what I find in HyperEdit, I still have the problem I mentioned above.

I'll try looking over HyperEdit code again and see if I can disseminate the procedure it uses to get a stable orbit.

Link to comment
Share on other sites

I fixed it; I found that the problem was two-fold;

First up, I needed to set the orbit of the vessel like I did in the second section of code I posted above. However, the line:


activeVessel.orbitDriver.orbit = newOrbit;

was doing too much; I only needed to copy certain properties of the orbit, not all of them. So that line is replaced with:


activeVessel.orbitDriver.orbit.inclination = newOrbit.inclination;
activeVessel.orbitDriver.orbit.eccentricity = newOrbit.eccentricity;
activeVessel.orbitDriver.orbit.semiMajorAxis = newOrbit.semiMajorAxis;
activeVessel.orbitDriver.orbit.LAN = newOrbit.LAN;
activeVessel.orbitDriver.orbit.argumentOfPeriapsis = newOrbit.argumentOfPeriapsis;
activeVessel.orbitDriver.orbit.meanAnomalyAtEpoch = newOrbit.meanAnomalyAtEpoch;
activeVessel.orbitDriver.orbit.epoch = newOrbit.epoch;
activeVessel.orbitDriver.orbit.referenceBody = newOrbit.referenceBody;

In addition to copying the orbit, I also needed to set the vessel position and velocity; like so:



// Add distance between player vessel and target jump gate (this is temporary, needs something more intelligent really)
Vector3d offset = new Vector3d(10, 0, 0);

// Set position and velocity of player
activeVessel.SetWorldVelocity(targetJumpGate.orbit.GetVel());
activeVessel.SetPosition(targetJumpGate.GetWorldPos3D() - offset);

Since getting this to work I’ve majorly refactored my code so the above is not quite what I’m using right now. However I thought it’d be helpful to have this here nonetheless.

If/when I upload my Jump Gate plugin I’ll add a link to the full source here.

Dave

Link to comment
Share on other sites

  • 2 weeks later...

Hey

I'm looking forward to getting this mod working properly!

I've spent the past few days learning blender and have made a stock-ish looking Jump Device; this way you can build your own Jump Gate / Beacon out of your own parts rather than being stuck with just one model. You can also place multiple devices on one vessel so that it becomes a sort of intergalactic train station :D

Still got one final problem to fix: the jump beacon you start from is still getting its orbit changed when the teleport completes. I've still no idea why this is happening but I'm working on it.

If I do get this thing working properly I'll post all the code with comments someplace and link it here!

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...