mouse velocity help!

Blitz3D Forums/Blitz3D Programming/mouse velocity help!

blade007(Posted 2007) [#1]
im trying to create a code that limits the speed of the mouse which controls a player. this is what it looks like so far
while not keydown(1)
	oldmousex = newmousex
	oldmousey = newmousey
	newmousex = MouseX()
	newmousey = MouseY()
	newmousex=newmousex-oldmousex
	newmousey=newmousey-oldmousey
	newmousex=newmousex/3
	newmousey=newmousey/3
	player_x=player_x+newmousex
	player_y=player_y+newmousey

        ; stuff goes here
wend

but for some reason the player is limited to the positive movements of the mouse axices. Eventually this will cause the player to move to the bottom right corner of the screen and stay there. any suggestions , mods , or entire code rewrites is highly apprecited ,and thanks in advance!


Rob Farley(Posted 2007) [#2]
I'd use the mousxspeed and mouseyspeed commands

Graphics 800,600,32,2

x=GraphicsWidth()/2
y=GraphicsHeight()/2
MoveMouse GraphicsWidth()/2,GraphicsHeight()/2
SetBuffer BackBuffer()

Repeat

	limit = 10
	mx = MouseXSpeed()
	my = MouseYSpeed()
	MoveMouse GraphicsWidth()/2,GraphicsHeight()/2
	If mx>limit Then mx = limit
	If mx<-limit Then mx = -limit
	If my>limit Then my = limit
	If my<-limit Then my=-limit
	
	x=x+mx
	y=y+my
	
	Cls
	Rect x-5,y-5,11,11,True
	Flip

Until KeyHit(1)



blade007(Posted 2007) [#3]
thanks rob u always have the answer (really) lemme try out the code now


blade007(Posted 2007) [#4]
it works but now it looks a little choppy (its almost always diagnal) cause of the
	If mx>limit Then mx = limit
	If mx<-limit Then mx = -limit
	If my>limit Then my = limit
	If my<-limit Then my=-limit



b32(Posted 2007) [#5]
Maybe you could remove those lines, and divide mx and my by for instance three ?

As for your first post, you are substracting oldmousex/y from newmousex/y. Later on, you store newmousex/y in oldmousex/y, which can give strange results. Instead, use two extra variables to store the unaltered mouse position, like this:
while not keydown(1)
	oldmousex = storedmousex
	oldmousey = storedmousey
	storedmousex = MouseX()
	storedmousey = MouseY()

	newmousex=storedmousex-oldmousex
	newmousey=storedmousey-oldmousey
	newmousex=newmousex/3
	newmousey=newmousey/3
	player_x=player_x+newmousex
	player_y=player_y+newmousey

        ; stuff goes here
wend



blade007(Posted 2007) [#6]
i did that anyway, but the though counts ,thanks :)