Help MouseXSpeed() and MouseYSpeed()

Blitz3D Forums/Blitz3D Programming/Help MouseXSpeed() and MouseYSpeed()

Yue(Posted 2010) [#1]
Hi there if someone helps give meaning to this command, since the google translation is not help me much, the idea is that I have a camera hitches a car, and always about the car using the command PoinEntity, I need is that through mause when moving the camera moves around the car.


A greeting.


_PJ_(Posted 2010) [#2]
The commands essentially return the distance travelled by the Mouse pointer (I'm pretty sure in Pixels) since it was last callled.
Since it only returns the distance since it was last called, it reflects the average speed (pixels/frame) the pointer has moved, and will be 0 if the pointer hasn't moved since it was last called.

Finally, movement to the left or upwards is negative (for X and Y respectively)

This little code should help show how they work:
Graphics 800,600,32,6
SetBuffer BackBuffer()
ShowPointer() ; Not needed really, but here for completeness
MoveMouse 400,300
While Not KeyDown(1)
	Cls
	If (GetMouse()=1) Then MoveMouse 400,300
	X=MouseXSpeed()
	Y=MouseYSpeed()
	Text 0,0,"X="
	Text 20,0,Str(X)
	Text 60,0,"Y="
	Text 80,0,Str(Y)
	Flip
Wend	



As for the problem with the camera revolving around the car, I recommend parenting the camera to a Pivot which is then parented to the car.

Use the mouse functions to turn the pivot, and keep the camera Pointed at the pivot. This way, as the pivot roptates, the camera will revolve around the car.
Here's an example:
Graphics3D 800,600,32,6
SetBuffer BackBuffer()

Global CAR_SPEED#=0.5
Global CAM_TURN_SPEED#=0.1

; Set up a scene
AmbientLight 160,160,192
Global Sun=CreateLight()
Global Ground=CreatePlane(16)
EntityColor Ground,64,128,32

Dim Trees(101)
Local IterTree%
For IterTree= 0 To 100
	Trees(IterTree)=CreateCone(3)
	EntityColor Trees(IterTree),Rand(0,64),Rand(160,240),Rand(0,8)
	PositionEntity Trees(IterTree),Rand(0-CAR_SPEED*30,CAR_SPEED*30),0,Rand(20,100)
Next

Global Car=CreateCube()
EntityColor Car,160,32,32
EntityShininess Car,0.4
Global CameraPivot=CreatePivot(Car)		;Pivot Parented to Car
Global Cam=CreateCamera(CameraPivot)	;Camera Parented to Pivot
CameraClsColor Cam,32,16,240
MoveEntity Cam,0,1,-10					; Put some distance between the Camera and its Pivot (and therefore the Car too)





While Not KeyDown(1)

	MoveEntity Car,0,0,CAR_SPEED					;vroooooooooooom!

	;Get the Mouse Speed
	Local X%=MouseXSpeed()
	Local Y%=MouseYSpeed()
	Local Z%=MouseZSpeed()
	
	; Use the Mouse Speed values to rotate Pivot
	TurnEntity CameraPivot,Y*CAM_TURN_SPEED,X*CAM_TURN_SPEED,0	;Horizontal Mouse turns YAW, Vertical Mouse turns PITCH

	;Keep the camera aligned to the Pivot (and therefore to the Car)
	PointEntity Cam,CameraPivot

	; MouseZSpeed() refers to the MouseWheel - we can use that to Zoom in/out
	MoveEntity Cam,0,0,Z*CAM_TURN_SPEED*5

	;Recycle Trees (ecologically concious!)
	For IterTree=0 To 100
		If (EntityDistance(Trees(iterTree),Cam)>20) And (Not(EntityInView(Trees(IterTree),Cam)))
			PositionEntity Trees(IterTree),Rand(0-CAR_SPEED*30,CAR_SPEED*30)+EntityX(Car,1),0,EntityZ(Car,1)+Rand(20,100)
		End If
	Next
	UpdateWorld()
	RenderWorld()
	Flip
Wend



jhocking(Posted 2010) [#3]
The tricky thing about these commands is that they don't do anything useful the first time they are called, it's on the second call that they start being useful. Since they tell you the distance moved since last call, on the first call there isn't a last call.


Yue(Posted 2010) [#4]
:)


D4NM4N(Posted 2010) [#5]
These commands work best by assigning their values to global vars once per loop (preferably b4 "something to do" eg. before your delta timer or whatever u use before reading them, if using tweening then assign the values per screen update). That way you get a decent/consistant read. If you want to do it in a modal loop with little or no logic then add at least 100 ms delay between moving the cursor and reading the speed. Also, don't forget to assign them once before the loop starts to avoid a jump, especially if doing fps cams :)

Last edited 2010