[Solved] Center Mouse With FreeLook Camera

BlitzMax Forums/MiniB3D Module/[Solved] Center Mouse With FreeLook Camera

RustyKristi(Posted 2016) [#1]
I was wondering how you can achieve an always centered mouse when implementing a freelook or mouse look camera.

This works with equivalent B3D code but not with OpenB3D. Somehow it locks the mouse controls while trying to do cursor centering..

Framework Openb3d.B3dglgraphics
Import Brl.Random

Graphics3D 800, 600, 0, 2

light=CreateLight()
camera = CreateCamera()
CameraRange camera, .1, 1000

For i = 1 To 100
cube = CreateCube()
PositionMesh cube, Rnd(100), 0, Rnd(100)
Next

PositionEntity light,-500,-500,-500
PointEntity light, cube
MoveMouse 400,300
While Not KeyDown(KEY_ESCAPE)

	UpdateWorld
	
	RotateEntity camera, EntityPitch(camera) + MouseYSpeed()/2, EntityYaw(camera) - MouseXSpeed()/2, 0
	If KeyDown(KEY_S) Then MoveEntity camera, 0, 0, -.1
	If KeyDown(KEY_W) Then MoveEntity camera, 0, 0, .1
	If KeyDown(KEY_A)  Then MoveEntity camera, -.1, 0, 0
	If KeyDown(KEY_D) Then MoveEntity camera , .1 , 0 , 0

	MoveMouse 400,300
	RenderWorld

        Flip True

Wend

End



TomToad(Posted 2016) [#2]
Problem here is that when you do a MoveMouse(), MousXeSpped() and MouseYSpeed() are set per the distance moved. So if you move the mouse 10 pixels right every frame, you get MouseXSpeed = 10 on the first frame, MouseXSpeed = -10(MoveMouse back to 400) + 10(Amount moved this frame) = 0 on the second frame, etc... Sometimes the mouse is moved a bit further or not as much on the next frame which is why you do see a little jittering, but it is immediately negated on the next MoveMouse call.

I tried to use FlushMouse(), but that seems to only reset the button states, not speed, So I ended up recreating my own MouseXSpeed and MouseYSpeed to get the correct results.



RustyKristi(Posted 2016) [#3]
Thanks TomToad, works like charm! :)