About Monkey 2 › Forums › Monkey 2 Programming Help › How Parent/Children works?
This topic contains 6 replies, has 2 voices, and was last updated by
swipis 1 year, 2 months ago.
-
AuthorPosts
-
January 23, 2018 at 7:12 pm #13181
Hello,
I am new to Monkey2 and want to know how can I set object to be a parent and another object to be a child (both objects are Models). And does Parent rotation will apply to its child (if I rotate parent)?
Thank you.
January 23, 2018 at 8:35 pm #13183Use the ‘Parent’ property to change an entity’s parent, eg:
Monkey1234Local model1:=Model.Load( "blah..." )Local model2:=Model.Load( "blah..." )model1.Parent=model2 'make model1 a child of model2You can also set an entity’s parent to ‘null’, meaning the entity is in ‘world space’.
January 23, 2018 at 8:37 pm #13184Thank you Mark! And how about Parent rotation will it apply to its children automatically?
January 23, 2018 at 8:49 pm #13185Yes, it’s very much like blitz3d (if you’ve ever used that)!
A child entity’s position, rotation and scale are all relative to the parent entity. So moving, rotating, scaling the parent will also move, rotate, scale the child.
January 23, 2018 at 8:54 pm #13186Thank you again, I trying to rotate parent but parent and child not rotating
did something wrong. And about blitz no I did not use, I heard about Monkey2 2 days ago and I like so far!
Thank you again!
January 23, 2018 at 9:06 pm #13187Here’s a little demo…
Monkey12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788Namespace myapp3d#Import "<std>"#Import "<mojo>"#Import "<mojo3d>"Using std..Using mojo..Using mojo3d..Class MyWindow Extends WindowField _scene:SceneField _camera:CameraField _light:LightField _ground:ModelField _donut:ModelMethod New( title:String="Simple mojo3d app",width:Int=640,height:Int=480,flags:WindowFlags=WindowFlags.Resizable )Super.New( title,width,height,flags )'create (current) scene'_scene=Scene.GetCurrent()'create camera'_camera=New Camera_camera.Viewport=Rect_camera.AddComponent<FlyBehaviour>()_camera.Move( 0,10,-5 )'create light'_light=New Light_light.CastsShadow=True_light.RotateX( 90 )'create ground'Local groundBox:=New Boxf( -50,-1,-50,50,0,50 )Local groundMaterial:=New PbrMaterial( Color.Green )_ground=Model.CreateBox( groundBox,1,1,1,groundMaterial )_ground.CastsShadow=False'create dount'Local donutMaterial:=New PbrMaterial( Color.Brown )_donut=Model.CreateTorus( 2,.5,48,24,donutMaterial )_donut.Move( 0,10,0 )'Add a child!Local child:=Model.CreateBox( New Boxf(-1,1),1,1,1,New PbrMaterial( Color.Red ) )child.Parent=_donutchild.LocalPosition=New Vec3f( 0,-2,0 )EndMethod OnRender( canvas:Canvas ) OverrideRequestRender()_camera.Viewport=Rect_donut.Rotate( .2,.4,.6 )_scene.Update()_scene.Render( canvas )canvas.DrawText( "FPS="+App.FPS,0,0 )EndEndFunction Main()New AppInstanceNew MyWindowApp.Run()EndJanuary 23, 2018 at 9:27 pm #13188Thank you!
-
AuthorPosts
You must be logged in to reply to this topic.