Joystick commands

Blitz3D Forums/Blitz3D Beginners Area/Joystick commands

seferey(Posted 2006) [#1]
After an hour of writing and experimenting I wrote these commands myself and was wondering if there's a way to smooth it out the turning just so it's not rough


;Create a joystick that moves  
JX=JoyX()
JY=JoyX()

JX=JX*20
JY=JY*20

JX=JoyXDir()*20
JY=JoyYDir()*20
RotateEntity Camera,JY,-JX,0
;TranslateEntity Camera,0,0,0




GfK(Posted 2006) [#2]
;Create a joystick that moves  
;JX=JoyX()
;JY=JoyX()

;JX=JX*20
;JY=JY*20

JX=JoyXDir()*20
JY=JoyYDir()*20
RotateEntity Camera,JY,-JX,0
;TranslateEntity Camera,0,0,0

Um.... the first four lines of your code are completely redundant, as commented out above. I can't even tell what the rest of your code is trying to do, so I'll leave it alone.

Make it so that your joystick does not directly control the camera. Introduce a 'turnSpeed#' variable. Limit it to +/- 20. If you push the joystick left, do turnSpeed# = turnSpeed - 0.1. Push it right, add on 0.1.

Then check if the joystick is 'dead'. To do this do NOT use "If Joyx() = 0". Use "If Abs(Joyx()) < 0.05" etc - reason being that even a correctly calibrated joystick is never ever going to show exactly 0 when its centered.

If the stick is not being pushed either direction, then you need to gradually return the turnspeed variable to 0. If its less than 0, add on 0.1. If its more than 0, subtract 0.1.

Then use TurnEntity Entity,0,turnspeed,0.

Why didn't I just write the code for you? Two reasons. First, I can't be bothered. Second, you'll learn more by being guided in the right direction then doing the job yourself.

[edit]

Quick example because I'm bored today:
Graphics3D 800,600

sphere = CreateSphere()
ScaleEntity sphere,2,1,1

camera = CreateCamera()
light = CreateLight()
RotateEntity light,0,90,0
TranslateEntity camera,0,5,-20
Global turnSpeed# = 0
Global maxTurnSpeed = 20

Global port% = 0

While Not KeyDown(1)
	Cls
	UpdateStick()
	TurnEntity sphere,0,turnSpeed,0
	RenderWorld
	Flip
Wend

Function UpdateStick()
	Local xs# = JoyX(port)
	turnSpeed = turnSpeed + (1 * xs)
	If turnSpeed < 0-maxTurnSpeed Then turnSpeed = 0-MaxTurnSpeed
	If turnSpeed > MaxTurnSpeed Then turnSpeed = MaxTurnSpeed
	If Abs(turnSpeed) < 0.1 Then turnspeed = 0
	If Abs(xs) < 0.1;joystick is dead
		If turnSpeed < 0 Then turnSpeed = turnSpeed + 1
		If turnSpeed > 0 Then turnSpeed = turnSpeed - 1
		If Abs(turnSpeed) < 1 Then turnSpeed = 0
	EndIf
End Function



seferey(Posted 2006) [#3]
I messed with it a bit and learned alot from it I'm not very good with math

how do u make it turn up and down?


_PJ_(Posted 2006) [#4]
TurnEntity sphere,0,turnSpeed,0


This is the code that actually turns the sphere.

If you check the manual, you will see that the parameters entered are:

TurnEntity OBJECT,PITCH#, YAW#, ROLL#

where pitch would be the 'up/down' you're after.
As pitch and roll in the above example are set to 0, and the yaw is set to 'urnnspeed', the joystick will rotate the sphere 'side-to-side' only.


seferey(Posted 2006) [#5]
Thanks I forget about pitch yaw roll

Well what I wanted to do was make joystick up and down left and right all in one command it's very hard I copied

and call updatestick2 function underneath the first one So I worked out It works good but when I push up or down it curves to the right like it's got too much roll in it or something

Function UpdateStick2()
	Local xy# = JoyY(port)
	Updown = Updown + (1* xy)
	If Updown < 0-MaxUpdown Then Updown = 0-MaxUpdown
	If Updown > MaxUpdown Then Updown = MaxUpdown
	If Abs(Updown) < 0.1 Then Updown = 0
	If Abs(xy) < 0.1 ;joystick is dead
		If Updown < 0 Then Updown = Updown + 1
		If Updown > 0 Then Updown = Updown - 1
		;If ACos(Updown) < 1 Then Updown = 0
		;If ACos(Updown) > 1 Then Updown = 0
	EndIf
End Function



even when I got rid of the

;If ACos(Updown) < 1 Then Updown = 0
;If ACos(Updown) > 1 Then Updown = 0


it still does it with about every math command like abs asin etc.

well finall I got to stop curving somewhat but it still does it
but only this time when I move the stick it does it then