About Monkey 2 › Forums › Monkey 2 Programming Help › My controls change when I rotate my player in my 3D game
This topic contains 3 replies, has 2 voices, and was last updated by 
 En929 2 months, 3 weeks ago.
- 
		AuthorPosts
 - 
		
			
				
January 19, 2019 at 10:53 am #15958
In my 3D game, I am having problems with my rotation. When I first start the game, the player goes Forward (Z), Backwards (Z); it rotates to the Right(Y) and rotates the Left (Y) normally. However, when I rotate my player 90 degrees. After that, when I press the up button, the up button starts acting like the Right button, etc. and all of the controls change and they change in every 90 degrees in which my player is rotated. Thus, what do I add to my code to get the controls to go in the way that the player is facing. My code is below. Thanks:
Monkey123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151'Skybox 1015 X 1030 and 2045 X 995 for a 4095 X 3070 sized'pictureNamespace myapp#Import "<std>"#Import "<mojo>"#Import "<mojo3d>"#Import "<mojo3d-loaders>"#Import "Man.b3d"#Import "Man.jpg"#Import "Tower.png"#Import "Maplewood.b3d"Using std..Using mojo..Using mojo3d..Class MyWindow Extends Window'Initialize everything that you're going to useField _scene:SceneGlobal _camera:CameraField _light:LightField _ground:ModelField Man:ModelField Jack_in_the_Box: ModelField _animator:AnimatorField MoveForwardZ: Float = 0.2Field MoveBackwardsZ: Float = -0.2Field ManX:Float = 0 '// x position on the mapField ManY:Float = -40Field ManZ: Float = -100Method New( title:String="The World Doesn't Apply to Him",width:Int=800,height:Int=600,flags:WindowFlags=WindowFlags.Resizable )Super.New( title,width,height,flags )_scene=New SceneMan=Model.LoadBoned( "asset::Man.b3d" )Man.Scale=New Vec3f( .1)Man.Position = New Vec3f(0, 30,10)_camera=New Camera( Man)'_camera.Near= 2_camera.Far=100000_camera.Position = New Vec3f(0, 48,-10) 'create light'_light=New Light_light.Rotate( 75,150,0 ) 'aim directional light 'down' - Pi/2=90 degrees._light.CastsShadow=True_ground=Model.CreateBox( New Boxf( 0,0,0,0,0,0 ),1,1,1,New PbrMaterial( Color.Green ) )_ground.CastsShadow=FalseJack_in_the_Box =Model.Load( "asset::Maplewood.b3d")_animator=Man.AnimatorJack_in_the_Box.Move( 0,40,300)Jack_in_the_Box.Scale = New Vec3f(0.5, 0.5, 0.5)_animator.MasterSpeed=0.5Local anim2:=_animator.Animations[0].Slice( "Idle",1,1,AnimationMode.Looping )Local anim1:=_animator.Animations[0].Slice( "Walk",2,30,AnimationMode.Looping )Local anim3:=_animator.Animations[0].Slice( "Punch",31,99,AnimationMode.OneShot )_animator.Animations.Add( anim1 )_animator.Animations.Add( anim2 )_animator.Animations.Add( anim3 )EndMethod OnRender( canvas:Canvas ) OverrideRequestRender()If _animator.Animating?.Name<>"Punch"If Keyboard.KeyHit( Key.Space ) 'Punch!_animator.Animate( "Punch",.2 )Else If Keyboard.KeyDown( Key.Up ) 'walk_animator.Animate( "Walk",1)Man.MoveZ(0.2, True)Else If Keyboard.KeyDown( Key.Down )_animator.Animate( "Walk",1)Man.MoveZ(-0.2, True)'This is the section that I need help with. When I rotate the man 90 degrees'the controls change.Else If Keyboard.KeyDown( Key.Right) 'walk_animator.Animate( "Walk",1)Man.RotateY(-1, True)Else If Keyboard.KeyDown( Key.Left )_animator.Animate( "Walk",1)Man.RotateY(1, True)Else 'idle_animator.Animate( "Idle",.2 )EndifEndif_scene.Update()_camera.Render( canvas )canvas.Scale(800,600)EndEndFunction Main()New AppInstanceNew MyWindowApp.Run()EndJanuary 22, 2019 at 9:42 am #15992Had to modify the code to run without the assets, but it seems that if you change the Move and Rotate commands to be in local space the problem is solved. Works here.
Example:
Man.MoveZ(0.2, False)
Man.RotateY(1, False)
(uses “false” instead of “true”)As far as I can tell, Mojo3D’s rotation and translation commands’ object space are inverted – True should mean local space, but means world space. If you want the motion to always be relative to where the player is pointing, it need to be in local space (“False”, although it should be “True”).
January 22, 2019 at 9:53 am #15993Monkey123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081Namespace myapp#Import "<std>"#Import "<mojo>"#Import "<mojo3d>"Using std..Using mojo..Using mojo3d..Class MyWindow Extends WindowGlobal _camera:CameraField _scene:SceneField _light:LightField _ground:ModelField Man:ModelField Jack_in_the_Box: ModelField _animator:AnimatorField speed: Float = 1.0Field turn: Float = 1.0Method New( title:String="The World Doesn't Apply to Him",width:Int=800,height:Int=600,flags:WindowFlags=WindowFlags.Resizable )Super.New( title,width,height,flags )_scene=New SceneMan=Model.CreateBox( New Boxf(-2.5,-2.5,-2.5, 2.5, 2.5, 2.5 ), 1, 1, 1, New PbrMaterial( Color.Yellow ) )Man.Position = New Vec3f(0, 2.5,10)_camera=New Camera( Man)_camera.View = Self_camera.Near= 2_camera.Far=100000_camera.Position = New Vec3f(0, 25,-10)_camera.PointAt( Man )'_light=New Light_light.Rotate( 45,150,0 )_light.CastsShadow=True_ground=Model.CreateBox( New Boxf( -100,-1,-100,100,0,100 ),1,1,1,New PbrMaterial( Color.Green ) )_ground.CastsShadow=FalseJack_in_the_Box=Model.CreateBox( New Boxf(-2, -2, -2, 2, 2, 2 ), 1, 1, 1, New PbrMaterial( Color.Red ) )Jack_in_the_Box.Move( 0,2,30)EndMethod OnRender( canvas:Canvas ) OverrideRequestRender()If Keyboard.KeyHit( Key.Space )Else If Keyboard.KeyDown( Key.Up )Man.MoveZ( speed, False )Else If Keyboard.KeyDown( Key.Down )Man.MoveZ( -speed, False )EndIf Keyboard.KeyDown( Key.Right)Man.RotateY( -turn, False )Else If Keyboard.KeyDown( Key.Left )Man.RotateY( turn, False)End_scene.Update()_camera.Render( canvas )EndEndFunction Main()New AppInstanceNew MyWindowApp.Run()EndJanuary 22, 2019 at 10:55 am #15995Ethernaut, that worked. It fixed the problems! Thanks a lot!
 - 
		AuthorPosts
 
You must be logged in to reply to this topic.