Jump to content

[Help] Spawning parts beginning from specific node


Recommended Posts

Hi there,

*"Solved" the problems, ie. found workarounds

*Edit, I made a github https://github.com/TAz00/Mod-KSPSmelter

I'm new to unity, and I'm having quite a bit of troubles with some code.

What I wan't to create is a way to create fuel canisters from ore. Think 3D laser metal printing :P But a fuelcan is useless in itself, so it must have docking nodes to be picked up, attached to a rocket and sent into orbit.

So i'm making a crude cloning vat.

My current approach is to clone the 2,5M service bay, and try to create a new vessel from everything that is attached to the top node inside.

Currently it can spawn the parts which are placed inside, but they are in no way attached to each other. I have a feeling there is a much smarter way to go about it, than recreating the algorithm for cloning ships. (maybe clone the entire vessel and remove everything but the parts i want?)

If anyone could care to take a gander, that would be much appreciated.

https://www.dropbox.com/s/8f65dk4uew9iccr/KSPSmelter.v0.2.7z?dl=0

https://github.com/TAz00/Mod-KSPSmelter

8R1Uuop.jpg


#removed code for brevity, also, it was flawed and a working copy is on github

Best Regards

Edited by TAz00
github, new version, solved!
Link to comment
Share on other sites

So I've been trying for an untold number of hours, and the closest I have come is the following.

I abandonded my attempt to create a vessel from a node, and instead created a small vessel of two docking ports and an FL-400 tank in between. And then load the craft from disk and spawn it next to my current vessel:


private void AlmostSpawnCraftFromFile()
{
Quaternion rotation = this.part.transform.rotation;
Vector3 position = this.part.transform.position;
Int32 OffsetX = 03;
Int32 OffsetY = 00;
Int32 OffsetZ = 00;
position += new Vector3(OffsetX, OffsetY, OffsetZ);
//Posistion is unused for now
Vessel FirstVessel = Utility.TheThingThatAlmostWorked("FL-T400.craft", position, rotation, this.part);

}

/// <summary>
/// //http://forum.kerbalspaceprogram.com/threads/7544-The-official-unoffical-help-a-fellow-plugin-developer-thread/page129
///
/// </summary>
/// <param name="FileName"></param>
/// <param name="Position"></param>
/// <param name="Rotation"></param>
/// <param name="SourcePart"></param>
/// <returns></returns>
public static Vessel SpawnCraftFromCraftFile(string FileName, Vector3 Position, Quaternion Rotation, Part SourcePart)
{
string BasePath = Environment.CurrentDirectory + "\\..\\PluginData\\";
string path = BasePath + FileName;
ProtoVessel protoVessel = null;

//Make a clone of the orbit
//https://github.com/Ezriilc/HyperEdit/blob/master/Model/OrbitEditor.cs
Orbit o = SourcePart.vessel.orbitDriver.orbit;
Orbit cloneOrbit = new Orbit(o.inclination, o.eccentricity, o.semiMajorAxis, o.LAN,
o.argumentOfPeriapsis, o.meanAnomalyAtEpoch, o.epoch, o.referenceBody);
//cloneOrbit = SourcePart.vessel.orbitDriver.orbit;
ConfigNode VesselConfigNode = ProtoVessel.CreateVesselNode("Fuuuuck", VesselType.Debris, cloneOrbit, 0, new ConfigNode[1] { ConfigNode.Load(path) });

//ConfigNodeFree / Debugger Lagg free way
protoVessel = new ProtoVessel(VesselConfigNode, HighLogic.CurrentGame);

//Set posistion, maybe: untested
protoVessel.position = Position;

//Seemed sensible
protoVessel.LoadObjects();

//Load vessel, if we leave it here, vessel gets destroyed on rails at 100.2 atm pressure with no crew
protoVessel.Load(HighLogic.CurrentGame.flightState);

return protoVessel.vesselRef;

}

This results in the vessel being destroyed at 100.2 atm with no crew on rails.

No exception errors this time.

Now please contain yourselves with responses :D

Link to comment
Share on other sites

Alright, so I sort of got it to work. And it's all I ever dreamed of :D

http://i.imgur.com/MO61VpT.jpg

Theres a problem with positioning. When my vessel spawns, it start's in one posistion, but as soon as physics kick in, it warps to a different location, in height, and then either bounces up from the ground or dangles down.

Here's a screenshot of it spawning in the air, and the second one is where physics get enabled, and it warps to/into the ground and bounces up.

http://i.imgur.com/K2llEYT.jpg

See log info

http://i.imgur.com/kwUyQgm.jpg

Otherwise everything is fine, spawning in the right location. And then physics ruin it. Why do I get ground contact error -x.xxx meters?


//Get root index part
Part rootIndexPart = _shipConstruct.parts[0];
Vector3 flipOffset = rootIndexPart.transform.localPosition;
//Sets source part spawn posistion to exact center of current vessel (this explodes the craft)
//Translate rootIndexPart (this applies the posistion to the rootpart)
rootIndexPart.transform.Translate(-flipOffset);

//Apply spawn offset
//ship.Parts[0].transform.Translate(_newVesselSpawnOffset);

//float size = ship.shipSize.magnitude / 2.0f;
//Create backup values
ShipConstruction.CreateBackup(_shipConstruct);
//Transform to ground
Transform launchTransform = Utility.GetLanchTransform(_srcPart.transform);

//Get localRoot root part
Part localRootPart = _shipConstruct.parts[0].localRoot;
//Angle
float angle;
//Axis
Vector3 axis;
//Extract ToAngleAxis data from selected spawning location
launchTransform.rotation.ToAngleAxis(out angle, out axis);
//Transform localRootPart posistion and apply offset
localRootPart.transform.Translate(launchTransform.position + _newVesselSpawnOffset, Space.World);
//Rotate localRootPart in relation to root
localRootPart.transform.RotateAround(launchTransform.position, axis, angle);

Link to comment
Share on other sites

Alright, so I sort of got it to work. And it's all I ever dreamed of :D

http://i.imgur.com/MO61VpT.jpg

Theres a problem with positioning. When my vessel spawns, it start's in one posistion, but as soon as physics kick in, it warps to a different location, in height, and then either bounces up from the ground or dangles down.

Here's a screenshot of it spawning in the air, and the second one is where physics get enabled, and it warps to/into the ground and bounces up.

http://i.imgur.com/K2llEYT.jpg

See log info

http://i.imgur.com/kwUyQgm.jpg

Otherwise everything is fine, spawning in the right location. And then physics ruin it. Why do I get ground contact error -x.xxx meters?


//Get root index part
Part rootIndexPart = _shipConstruct.parts[0];
Vector3 flipOffset = rootIndexPart.transform.localPosition;
//Sets source part spawn posistion to exact center of current vessel (this explodes the craft)
//Translate rootIndexPart (this applies the posistion to the rootpart)
rootIndexPart.transform.Translate(-flipOffset);

//Apply spawn offset
//ship.Parts[0].transform.Translate(_newVesselSpawnOffset);

//float size = ship.shipSize.magnitude / 2.0f;
//Create backup values
ShipConstruction.CreateBackup(_shipConstruct);
//Transform to ground
Transform launchTransform = Utility.GetLanchTransform(_srcPart.transform);

//Get localRoot root part
Part localRootPart = _shipConstruct.parts[0].localRoot;
//Angle
float angle;
//Axis
Vector3 axis;
//Extract ToAngleAxis data from selected spawning location
launchTransform.rotation.ToAngleAxis(out angle, out axis);
//Transform localRootPart posistion and apply offset
localRootPart.transform.Translate(launchTransform.position + _newVesselSpawnOffset, Space.World);
//Rotate localRootPart in relation to root
localRootPart.transform.RotateAround(launchTransform.position, axis, angle);

Can you add costs to the created parts?

Link to comment
Share on other sites

Bit late on the reply.

But why yes I could. As in, they would take ore and electricity to manufacture.

Here's an incredibly boring video of me having an 'all in one craft' mine, build and launch supply ships.

I'm just currently bogged down in posistioning.


//This function is inconsistent, input is the same every time, yet it returns 0 0 0 when resuming from cache/save, instead of
//returning correct value
Vector3 calculatedPosWhenResume = this.part.vessel.mainBody.GetWorldSurfacePosition(this.part.vessel.latitude, this.part.vessel.longitude, this.part.vessel.altitude);
//Same goes for
Vector3 worldPos = this.part.vessel.GetWorldPos3D();

Link to comment
Share on other sites

Yes, if you save them as a craft from the VAB and move the saved .craft file to PluginData and replace one of the existing .craft files.

Individually? maybe, if they can be saved as a .craft in the VAB

Yes, havn't tried KAS yet, but it should work aswell as Infernal robotics. I'm not familiar enough with KIS to say.

Link to comment
Share on other sites

Yes, if you save them as a craft from the VAB and move the saved .craft file to PluginData and replace one of the existing .craft files.

Individually? maybe, if they can be saved as a .craft in the VAB

Yes, havn't tried KAS yet, but it should work aswell as Infernal robotics. I'm not familiar enough with KIS to say.

That sounds a bit... complicated?

Oh, and if something works with KAS, it'll 100% work with KIS.

Link to comment
Share on other sites

So I think I've fixed the position warping. Although spawning an entire craft was not my original goal, this works well enough for what I had in mind.

To be able to build and launch refuel missions on another planet.

Here's the latest version, and I also updated the github sources

https://www.dropbox.com/s/8f65dk4uew9iccr/KSPSmelter.v0.2.7z?dl=0

Anyone know a good modeler / animator :D

Link to comment
Share on other sites

So I think I've fixed the position warping. Although spawning an entire craft was not my original goal, this works well enough for what I had in mind.

To be able to build and launch refuel missions on another planet.

Here's the latest version, and I also updated the github sources

https://www.dropbox.com/s/8f65dk4uew9iccr/KSPSmelter.v0.2.7z?dl=0

Anyone know a good modeler / animator :D

While developing my own career mod, Project ROCK, I found that I needed to use something to build rockets outside of Kerbin. You can read more about it in the link in my sig, but my line of thought is the following:

-Extraplanetary Launchpads does everything I'm asking for, but it is very complex and not at all compatible with Project's ROCK's Ore-to-everything philosophy.

-I can slim down EPL to fit my own mod, but it would take a lot of work and discussions with the original creator on a bloated thread, plus there are lots of things that can go wrong.

-You mod, however, should work perfectly for what I have in mind ( a simple rocket building tool), but I have a few questions:

Can the player use this mod without exiting the KSP client?

Can the player reasonably expect the spawn an entire spaceship on the surface of another planet without having to break it up into small pieces and dock each piece together (part count would explode)?

Can I define the Ore consumption ratio using .cfg files? The previous video doesn't seem to use any Ore or Electricity to spawn parts.

In return, I can create a model for your spawner in blender and port it over to KSP. I'll install a simple texture, but I can't do animations yet :(

Link to comment
Share on other sites

  • 2 weeks later...

The ore consumption is defined in CFG,

Time to build it, 30 secs, is defined in code.

You could modify it to spawn an entire ship, but I went with realism, and tried make somehting which could be possible IRL. Spawning a whole ship would not be possible irl :P, but the mod does not place any restrictions on it.

And it "should" not explode, but it might :)

Thanks for the offer, if you whip something up, ill be sure to include it.

Also, upon further testing, spawn position is still not correct :( I dno where to go from 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...