Jump to content

Direct object Reference to CelestialBody object


Recommended Posts

7 hours ago, Li0n said:

trackedcb is the CelestialBody or the MapObject representing the CelestialBody ? It needs to be the MapObject.

huh - it's a CelestialBody. and I didn't relise it hap to be that.

Def:
CelestialBody trackedcb;

onMapEnterEvent:
trackedcb = Planetarium.fetch.Home;

update:
 myConObj.transform.SetParent(trackedcb.transform);
myConObj.transform.localPosition = trackedcb.transform.localPosition;

Question: Is mapobject just the selected item or is there a way to select Kerbin as a mapobject even though it's not selected? Reason as that the cone I want it to be displayed  even if the body is not selected.

Link to comment
Share on other sites

Parenting attach the object to it s parent and localPosition is the position relative to the parent. So if you want your object to be where the parent is then you need to set localPosition to Vector3.zero.

Link to comment
Share on other sites

13 hours ago, wile1411 said:

Already had layer 10. I was thinking to use rotate around, but Visual Studio is telling that Transform.RotateAround" is deprecated and to use transform.rotate instead.

I tried the following to set the parent and thankfully the cone renders, but it's waaaaaay off in the distance and slipping away quickly.

                myConObj.transform.SetParent(trackedcb.transform);
                myConObj.transform.localPosition = trackedcb.transform.localPosition;

 

Ok - As far as I can tell, I think I need to use slerp somehow to get the cone to around in an orbit. This is mainly because I can't work out how parenting works for an object that isn't selected/focused . Also I'm sure I'm messing up the local / scaledspace details.

As far as I know, I'm moving the cone around in ScaledSpace as that's where the planet Kerbin seems to be. Both the cone and Kerbin are positioned in the same location and I have the cone big enough that it pokes through the surface to see it.

With no parent, I can offset the cone so it's fully outside the planet with an additional vector to it's current position:
myConObj.transform.localPosition = myConObj.transform.localPosition + new Vector3(0.0f, 0.0f, 90.0f);

 

Back on the parenting attempt. Here's all I have in the latethannever process

                trackedcb = Planetarium.fetch.Home;

                cone_multiplier = 2500f;  //so I can find the cone when it's not where it should be

                Vector3 scale = transform.localScale;
                scale.Set(cone_multiplier, cone_multiplier, cone_multiplier);
                myConObj.transform.localScale = scale;

                myConObj.transform.SetParent(trackedcb.transform);
                myConObj.transform.position = trackedcb.transform.position;
                myConObj.transform.localPosition = Vector3.zero;

Once that is done and the scene it loaded, I check the debug info I've set up to output anytime I select an object:
(top bit that got cut off is supposed to say "Scaled Kerbin") Cone is nowhere to be seen.
6sKOBHah.png

Link to comment
Share on other sites

9 hours ago, wile1411 said:

Question: Is mapobject just the selected item or is there a way to select Kerbin as a mapobject even though it's not selected? Reason as that the cone I want it to be displayed  even if the body is not selected.

MapObject is the "type" of every object you see in the mapView : celestial body, vessels, orbit line, maneuver node, etc...

They always exist, no matter what is selected/focused.

 

Def:
// CelestialBody trackedcb;
MapObject trackedcbmp

onMapEnterEvent:
// trackedcb = Planetarium.fetch.Home;
trackedcbmp = Planetarium.fetch.Home.MapObject;
myConObj.transform.SetParent(trackedcbmp.trf);
myConObj.transform.position = trackedcbmp.trf.position;
myConObj.transform.localPosition = Vector3.zero;

update:
// myConObj.transform.SetParent(trackedcb.transform); // parenting need to be done just once, not every frame
// myConObj.transform.localPosition = trackedcb.transform.localPosition; // with parenting, no need to update the position

With this code your cone should be at the center of Kerbin and follow it. It will also rotate with it.

Link to comment
Share on other sites

1 hour ago, Li0n said:

MapObject is the "type" of every object you see in the mapView : celestial body, vessels, orbit line, maneuver node, etc...

They always exist, no matter what is selected/focused.

With this code your cone should be at the center of Kerbin and follow it. It will also rotate with it.

Oh I'm such a...... idiot. I'd love you use more words there, but... public forum and all. :/

The the process of testing, I seem to have managed to remove this little line here. :blush::blush:
cone_renderer.enabled = true;

All works now. Thanks for the suggestion @Li0n works like a charm and I'm onto the relative movement. 

When I move the local positon to tilt it to the equator that worked no probs.
myConObj.transform.Rotate(myConObj.transform.right, 90f);

However, when setting the offset in the same direction as the cone was oriented, it didn't go where I expected - so I think my axis's are misaligned.
tq9f6TSh.png


The last float I thought was 'z' and should of pushed the cone in/out of the planet relative to the center.
myConObj.transform.localPosition = myConObj.transform.localPosition + new Vector3(0.0f, 0.0f, 1000.0f);

Edited by wile1411
Link to comment
Share on other sites

Yep - misaligned axis was my issue. Cone object 'right' was not the same and the Kerbin object 'right'. Got around that with just using parents right. Happy to learn if there was an better way to do this. (I was able to set the axis the same with cone.transform.right = trackedcbmp.transform.right, but the below code skipped that step to reduce the code lines by 1.

            trackedcbmp = Planetarium.fetch.Home.MapObject;
            myConObj.transform.SetParent(trackedcbmp.trf);
            myConObj.transform.position = trackedcbmp.trf.position;
            myConObj.transform.localPosition = Vector3.zero;

            //Rotate down to the equator using Kerbin RightAxis
            myConObj.transform.Rotate(myConObj.transform.parent.transform.right, 90f);
            // Now move the cone Forward away from Kerbin
            myConObj.transform.localPosition = new Vector3(0f, 0f, 1000f);

Thanks all for your fantastic help on this. I've still a lot to learn, but thanks so much for your patience. I hope I can bring my idea for a mod to light.

 

qnZ7amDh.png

Link to comment
Share on other sites

Gah! - more learning for me. I tired to un-parent to cone in it's offset location using    myConObj.transform.parent  = null;

While it did do as expected, (nothing moved) however, the now parent-less cone is desynced with the camera to the point where zooming in and out doesn't effect the size of the cone. It seems to have fallen into it's own worldspace not related to the current scene.

Link to comment
Share on other sites

10 minutes ago, wile1411 said:

Gah! - more learning for me. I tired to un-parent to cone in it's offset location using    myConObj.transform.parent  = null;

While it did do as expected, (nothing moved) however, the now parent-less cone is desynced with the camera to the point where zooming in and out doesn't effect the size of the cone. It seems to have fallen into it's own worldspace not related to the current scene.

Coordinate in the mapView works a bit strangely, the camera is always at Vector3(0, 0, 0) and every object gets its position changed when you move/zoom the camera. I guess you need to position your cone relatively to another object and update its position every frame.

That puzzled me at first too...

Link to comment
Share on other sites

6 hours ago, Li0n said:

Coordinate in the mapView works a bit strangely, the camera is always at Vector3(0, 0, 0) and every object gets its position changed when you move/zoom the camera. I guess you need to position your cone relatively to another object and update its position every frame.

That puzzled me at first too...

Thanks for that - I think it make sense to me on my first intake of the information. The locationposition value of kerbin and the cone look fine after the un-parenting. I think It's the .position value that not correct and probably the bit that KSP needs to know relative movement when the camera moves things in the scene.

Kerbins position is something in the hundreds of thousands. In trying to get the cone somewhere near those coordinates, I tried this using the LocalToScaledSpace function but it didn't do what I thought it would. I figured it'd take the number in the few hundreds and translate that to the give it the relative coordinates in the hundreds of thousands. As I just typed that sentence, I realized that's probably a stupid thought as the precision of the resulting number wouldn't be anywhere near where I'd want it.

myConObj.transform.position = ScaledSpace.LocalToScaledSpace(myConObj.transform.position);

Best idea I can come up with is to store my original position in a "shadow/scaled gameobj' before parenting it, and make the same relative offsets to both the shadow and the Kerbin.parented cone. That way I figure I could flip back and forth between  being a child of Kerbin using the shadow cone and a reference point and the game wouldn't lose where I was supposed to be in the game space.

Link to comment
Share on other sites

10 hours ago, wile1411 said:

Best idea I can come up with is to store my original position in a "shadow/scaled gameobj' before parenting it, and make the same relative offsets to both the shadow and the Kerbin.parented cone. That way I figure I could flip back and forth between  being a child of Kerbin using the shadow cone and a reference point and the game wouldn't lose where I was supposed to be in the game space.

That should work, I think.

You could also try to make your cone a MapObject on its own, I think some method help with positioning things right in the mapView, check the doc. I have no idea if this is possible (or of any interest to what you want) tho...

Link to comment
Share on other sites

47 minutes ago, Li0n said:

That should work, I think.

You could also try to make your cone a MapObject on its own, I think some method help with positioning things right in the mapView, check the doc. I have no idea if this is possible (or of any interest to what you want) tho...

Thankyou for the point in a direction. I'll mull over that info and investigate the mapobject doc for a while. It's been awesome for the information you've given and very much appreciated.

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