About Monkey 2 › Forums › Monkey 2 Programming Help › mojo3d: set child to have no parent?
This topic contains 7 replies, has 3 voices, and was last updated by
DruggedBunny
1 year, 3 months ago.
-
AuthorPosts
-
January 17, 2018 at 10:37 pm #12905
I’m trying to create a child entity at a parent entity’s position, then remove the parent/child relationship to leave the child at the same scene hierarchy level as the parent.
Setting child.Parent to Null, however, removes it from the scene, as per Property Parent:Entity() -> Setter().
Logic here is that in Blitz3D, SetParent (entity, 0) would make the world/scene the child’s new parent, freeing it from the parent’s local space yet leaving it in the scene, in global space.
This test shows that ‘parent’ has no parent, yet isn’t removed from the scene, and I basically want ‘child’ to end up in that same state after positioning, ie. it then has no parent, but remains in the scene with equal relationship to scene as ‘parent’…
Monkey123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384#Import "<std>"#Import "<mojo>"#Import "<mojo3d>"Using std..Using mojo..Using mojo3d..Class Game Extends WindowField scene:SceneField light:LightField cam_piv:ModelField camera:CameraField parent:ModelField child:ModelMethod New (title:String, width:Int, height:Int, flags:WindowFlags)Super.New (title, width, height, flags) ' Create windowSeedRnd (Millisecs ()) ' Init random seedSwapInterval = 1 ' V-sync onscene = Scene.GetCurrent ()cam_piv = New Modelcamera = New Camera (cam_piv)light = New Lightparent = Model.CreateBox (New Boxf (-0.5, -0.5, -0.5, 0.5, 0.5, 0.5), 1, 1, 1, New PbrMaterial (Color.Red))child = Model.CreateBox (New Boxf (-0.25, -0.25, -0.25, 0.25, 0.25, 0.25), 1, 1, 1, New PbrMaterial (Color.Blue), parent)parent.Move (5, 0, 5) ' Position parent away from 0, 0, 0child.Move (0, -1, 0) ' Position child 1 unit beneath parentEndMethod OnRender (canvas:Canvas) OverrideIf Keyboard.KeyHit (Key.Space) Then child.Parent = Null ' Removes child from scene!If Keyboard.KeyHit (Key.Escape) Then App.Terminate ()RequestRender ()scene.Render (canvas, camera)If parent.Parentcanvas.DrawText ("Parent (red) has a parent", 0, 0)Elsecanvas.DrawText ("Parent (red) has NO parent", 0, 0)EndifIf child.Parentcanvas.DrawText ("Child (blue) has a parent", 0, 20)Elsecanvas.DrawText ("Child (blue) has NO parent. Child has GONE!", 0, 20)Endifcanvas.DrawText ("Press SPACE to set child's parent to Null", 0, 60)EndEndFunction Run3D (title:String, width:Int, height:Int, flags:WindowFlags = WindowFlags.Center)New AppInstanceNew Game (title, width, height, flags)App.Run ()EndFunction Main ()Run3D ("3D Scene", 960, 540, WindowFlags.Center) ' 1/4 HD!' Run3D ("3D Scene", 1920, 1080, WindowFlags.Fullscreen)EndJanuary 17, 2018 at 10:42 pm #12906I guess I want to make it a member of Scene.RootEntities? Is it safe to just do Scene.RootEntities.Add (entity)? Feels like setting parent to Null should do that, though, like parent’s Null status, above.
EDIT: Nah, that’s not allowed, being private!
January 17, 2018 at 10:46 pm #12907I got round this issue by creating a parentless entity (not a model) which I called hidden.
By parenting any models I didn’t want in the scene to this entity they became hidden and then could be used for templates and the like.
January 17, 2018 at 10:55 pm #12908Hmm, guess I could do that. Wondering if it’s intended, though, as it seems weird not to be able to place into same hierarchy level as default entities.
January 17, 2018 at 11:03 pm #12909That doesn’t do it for me — I create a nonentity:Entity and hit space to set as child’s parent, but it still just disappears:
Monkey1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192' Wall of boxes...#Import "<std>"#Import "<mojo>"#Import "<mojo3d>"Using std..Using mojo..Using mojo3d..Class Game Extends WindowField scene:SceneField light:LightField cam_piv:ModelField camera:CameraField nonentity:EntityField parent:ModelField child:ModelMethod New (title:String, width:Int, height:Int, flags:WindowFlags)Super.New (title, width, height, flags) ' Create windowSeedRnd (Millisecs ()) ' Init random seedSwapInterval = 1 ' V-sync onscene = Scene.GetCurrent ()nonentity = New Entitycam_piv = New Modelcamera = New Camera (cam_piv)light = New Lightparent = Model.CreateBox (New Boxf (-0.5, -0.5, -0.5, 0.5, 0.5, 0.5), 1, 1, 1, New PbrMaterial (Color.Red))child = Model.CreateBox (New Boxf (-0.25, -0.25, -0.25, 0.25, 0.25, 0.25), 1, 1, 1, New PbrMaterial (Color.Blue), parent)parent.Move (5, 0, 5) ' Position parent away from 0, 0, 0child.Move (0, -1, 0) ' Position child 1 unit beneath parentEndMethod OnRender (canvas:Canvas) OverrideIf Keyboard.KeyHit (Key.Space)child.Parent = nonentityEndifIf Keyboard.KeyHit (Key.Escape) Then App.Terminate ()RequestRender ()scene.Render (canvas, camera)If parent.Parentcanvas.DrawText ("Parent (red) has a parent", 0, 0)Elsecanvas.DrawText ("Parent (red) has NO parent", 0, 0)EndifIf child.Parentcanvas.DrawText ("Child (blue) has a parent", 0, 20)Elsecanvas.DrawText ("Child (blue) has NO parent. Child has GONE!", 0, 20)Endifcanvas.DrawText ("Press SPACE to set child's parent to Null", 0, 60)EndEndFunction Run3D (title:String, width:Int, height:Int, flags:WindowFlags = WindowFlags.Center)New AppInstanceNew Game (title, width, height, flags)App.Run ()EndFunction Main ()Run3D ("3D Scene", 960, 540, WindowFlags.Center) ' 1/4 HD!' Run3D ("3D Scene", 1920, 1080, WindowFlags.Fullscreen)EndJanuary 17, 2018 at 11:11 pm #12910Not actually trying to hide things here, by the way — I want child to pick up parent’s position/rotation in the world, then be freed from parent: as in create bullet at end of gun, then free from parent so I can fire independent of parent’s future movement.
In my case, ‘gun’ means 5 units in front of current camera position at current rotation!
January 18, 2018 at 12:14 am #12911There are basically 2 problems here.
Base Entity class defaults to hidden so ‘nonentity’ (and all it’s children) are hidden. I will probably add a ‘Pivot’ style subclass shows itself, but in the meantime if you’re using an entity as a pivot you’ll need to manually set entity.Visible=True. It’s useful right now for entities to default to hidden for various reasons.
Also, mojo3d isn’t really thinking about what an entities matrix should be after changing it’s parent. I’ll probably change this so if you set an entities parent to null, it retains it’s world matrix and if you set it to non-null it retains its local matrix – make sense?
With this is mind, this works for me in v1.1.09:
Monkey123456If Keyboard.KeyHit (Key.Space)Local matrix:=child.Matrixchild.Parent=Nullchild.Matrix=matrixEndifThis stuff should be fixed soon.
January 18, 2018 at 12:54 am #12912In my last version, adding the nonentity.Visible = True suggestion doesn’t actually work (still disappears), though your last matrix code seems to do it.
But, yeah, I think what you’re saying there regarding keeping the world matrix is what I was after!
-
AuthorPosts
You must be logged in to reply to this topic.