About Monkey 2 › Forums › Monkey 2 Programming Help › Gun.Rotation = New Vec3f( -Mouse.Y, -Mouse.X,0)
This topic contains 1 reply, has 2 voices, and was last updated by
Ethernaut
3 months, 1 week ago.
-
AuthorPosts
-
January 2, 2019 at 4:03 pm #15838
I have my gun on screen. How do I clamp the values of Mouse.X/Y to only allow a certain amount of rotation? Also, How can I centre the mouse onscreen so that even if the mouse reaches the end of desktop width it still applies motion to the 3d world?
January 8, 2019 at 8:11 pm #15864You need to compare the current mouse coordinates with the coordinates in the previous frame (stored in a variable), and only apply that difference multiplied by a “mouseSensitivity” variable. If you’ve already reached the rotation you want to limit to, don’t rotate anymore! (something like: If Entity.Rotation.Y < limit Then RotateFunctionGoesHere() )
Not on my laptop, but you can specify the mouse coordinates using Mouse.Location = New Vec2f, I think.
My old code that handles first person mouse look is this (out of context):Monkey1234567891011121314151617181920212223If firstPerson'De-initializes mouselook if escape is pressedIf Keyboard.KeyHit( Key.Escape )_prevMouse = NullMouse.PointerVisible = TrueEndIf _prevMouse'Only starts updating if _prevMouse is initializedLocal deltaX := ( Float(Mouse.Y) - _prevMouse.Y ) * mouseSensitivityLocal deltaY := ( Float(Mouse.X) - _prevMouse.X ) * -mouseSensitivityEntity.RotateY( deltaY )If firstPersonCamerafirstPersonCamera.RotateX( deltaX )EndElse'initializes _prevMouse only if mouse is clicked and _prevMouse is nullIf Mouse.ButtonHit( MouseButton.Left )_prevMouse = New Vec2f( Float( Mouse.X ), Float( Mouse.Y ) )Mouse.PointerVisible = FalseEndEndEndAnd at the end of the update, it resets the mouse location like this:
Monkey123456789101112131415Method OnEndUpdate() OverrideIf firstPerson'Mouse centering. Does not run if _prevMouse is not initialized (first click hasn't happened)If _prevMouseLocal threshold := App.ActiveWindow.Height / 10Local center := New Vec2i( App.ActiveWindow.Width/2, App.ActiveWindow.Height/2 )Local limit := New Recti( center.X - threshold, center.Y - threshold, center.X + threshold, center.Y + threshold )If Mouse.X < limit.Left Or Mouse.X > limit.Right Or Mouse.Y < limit.Top Or Mouse.Y > limit.BottomMouse.Location = New Vec2i( center.X, center.Y )End_prevMouse = New Vec2f( Float( Mouse.X ), Float( Mouse.Y ) )EndEndEndI’m in the process of cleaning up and rewriting a CharacterController component that can be used in first person games, but the old version is here:
https://github.com/DoctorWhoof/FPSTestCheers.
-
AuthorPosts
You must be logged in to reply to this topic.