About Monkey 2 › Forums › Monkey 2 Code Library › Smooth FlyBehaviour for mojo3D
This topic contains 0 replies, has 1 voice, and was last updated by
cocon 1 year, 2 months ago.
-
AuthorPosts
-
January 27, 2018 at 6:27 pm #13287
I have made some simple changes to the mojo3D fly behaviour so it becomes more handy. It relies on Keyboard and Mouse combo and it tries to produce a smooth effect for movement and rotation. It will avoid mouse locking, so this means in order to rotate, the left mouse button needs to be pressed, also it features a hack I learnt from user “abakobo” that it supposed to handle AZERTY keyboards as well (I am a QWERTY user so I don’t have a clue about this
so if you test and see something weird about it, mention it). However it is not useful for mobile devices since the movement is done with keyboard, perhaps a future addition would be to detect either a desktop or mobile device, and if on mobile device it would get the movement vector from the left half of the screen and the rotation vector from the half right.
Monkey1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889Namespace mojo3dClass FlyBehaviour Extends BehaviourMethod New( entity:Entity )Super.New( entity )EndMethod OnUpdate( elapsed:Float ) Override' rotationIf Mouse.ButtonPressed( MouseButton.Left )screenStart = Mouse.LocationEndIf Mouse.ButtonDown( MouseButton.Left )Local vector:Vec2f = (screenStart - Mouse.Location) * 0.1rotationX.Speed += -vector.Y * elapsedrotationY.Speed += vector.X * elapsedEndrotationX.Update()rotationY.Update()Entity.RotateX(rotationX.Speed)Entity.RotateY(rotationY.Speed, True)' positionIf Keyboard.KeyDown(Key.Raw|Key.A) Then moveX.MoveToNegative()If Keyboard.KeyDown(Key.Raw|Key.D) Then moveX.MoveToPositive()If Keyboard.KeyDown(Key.Raw|Key.Q) Then moveY.MoveToNegative()If Keyboard.KeyDown(Key.Raw|Key.E) Then moveY.MoveToPositive()If Keyboard.KeyDown(Key.Raw|Key.S) Then moveZ.MoveToNegative()If Keyboard.KeyDown(Key.Raw|Key.W) Then moveZ.MoveToPositive()moveX.Update()moveY.Update()moveZ.Update()Entity.MoveX(moveX.Speed)Entity.MoveY(moveY.Speed, True)Entity.MoveZ(moveZ.Speed)EndPrivateField screenStart := New Vec2iField screenArea:Float = 200.0Field rotationX := New ValueSpeed(0.0, 2.0)Field rotationY := New ValueSpeed(0.0, 2.0)Field moveX := New ValueSpeed(0.02, 2.0)Field moveY := New ValueSpeed(0.02, 2.0)Field moveZ := New ValueSpeed(0.02, 2.0)EndPrivateClass ValueSpeed' a simple contraption to handle values related to speed' Speed = the actual value' SpeedIncrement = increments "Speed"' SpeedCooldown = multiplied with "Speed" to bring it down to zero' SpeedClamp = it will clamp "Speed" to -x and xField Speed:Float = 0.0Field SpeedIncrement:FloatField SpeedCooldown:Float = 0.91Field SpeedClamp:FloatMethod New(incrementSpeed:Float = 0.1, maxSpeed:Float = 2.0)SpeedIncrement = incrementSpeedSpeedClamp = maxSpeedEndMethod MoveToNegative()Speed -= SpeedIncrementEndMethod MoveToPositive()Speed += SpeedIncrementEndMethod Update()Speed = Clamp(Speed, -SpeedClamp, SpeedClamp)Speed *= SpeedCooldownEndEnd -
AuthorPosts
You must be logged in to reply to this topic.