Rotation -180 to +180

Blitz3D Forums/Blitz3D Programming/Rotation -180 to +180

John Blackledge(Posted 2005) [#1]
I'm moving the mouse from left to right to rotate an object.

The calculation can give an answer from e.g.-anything up to +anything, which Blitz handles just fine.

But I need to display a figure between -180 and +180.

I've obviously got brain-lock.
Has anyone got an algorythm?


Stevie G(Posted 2005) [#2]
Assuming that the range 'Anything' is preset , and Answer is the figure between -anything and +anything.

Display# = 180.0 * ( Answer / Anything )


[Edit] Sswift's will work better as Anything does not have to be defined.


sswift(Posted 2005) [#3]
Display# = (Answer# Mod 360.0) - 180.0

And in case Mod doesn't work right with floats:

ModF# = Answer# - Floor(Answer# / ModBy#)*Answer#

So:

Display# = (Answer# - Floor(Answer# / 360.0)*Answer#) - 180.0


One last thing. You may need to pre-subtract or add 180 to the answer to get the right answer in the end. I'm not sure.


Rook Zimbabwe(Posted 2005) [#4]
You go SSwifty!!!


John Blackledge(Posted 2005) [#5]
Thanks sswift.
That's how many pints I owe you now?

Unfortunately it still isn't working, but that's not your fault - I can see that your algorithm works, so I'll have to look further into my code.

(Basically I'm 'stroking' the mouse from left-right and right-left in order to rotate an entity, but the range can be pretty vast, both plus and minus.)


John Blackledge(Posted 2005) [#6]
I should have added that whatever value the 'mouse-stroke' returns I need to convert it to a number which is in the range -180 to +180.
Blitz will obviously react to this, and this is the format of the onscreen readout of object rotation.

You can see the problem in this test code:
Const KEY_UP=200 
Const KEY_DOWN=208 
Repeat
	If KeyDown(KEY_UP) Then angle = angle + 1
	If KeyDown(KEY_DOWN) Then angle = angle - 1
	a = (angle Mod 360)-180
	Cls
	Text 0,0,angle+ " > "+a
	Flip
Until KeyHit(1)
End



sswift(Posted 2005) [#7]
Like I said, you might need to pre add 180 to the answer to get the right result. :-)

a = ((angle+180) Mod 360) - 180

I've been sick lately, so I can't think straight. :-)


Stevie G(Posted 2005) [#8]
John,

Why not use a dummy pivot instead as it's entityyaw() is automatically converted to -180 .. +180 angles ...

Graphics3D 800,600,16,3
Const KEY_UP=200 
Const KEY_DOWN=208 
Global Pivot = CreatePivot()
Repeat
	If KeyDown(KEY_UP) Then angle = angle + 1
	If KeyDown(KEY_DOWN) Then angle = angle - 1
	a = (angle Mod 360)-180
	
	RotateEntity Pivot, 0, angle, 0
	
	Cls
	Text 0,0,angle+ " > "+a
	Text 0,10,angle+" > "+EntityYaw( pivot )
	
	
	Flip
Until KeyHit(1)
End



Stevie


John Blackledge(Posted 2005) [#9]
Damn.
StevieG, if what you say is true then that would be the obvious answer; thanks for that.
In the meantime (like in the a.m.) I've implemented this, using sswifts mod 360 etc:
If KeyDown(KEY_LEFT_SHIFT)
	If rotflag = 0
		rotflag = 1
		startmx = MouseX()
		startrot# = Model(choice)\w# ; get starting yaw
	Else
		xdir = MouseX() - startmx
		rotangle# = startrot# + xdir
		; \/ Implement new rotation, including previous pitch(\p#) and roll(\o#)
		RotateEntity Model(choice)\hent, Model(choice)\p#, rotangle#, Model(choice)\o#
		; \/ Now recalc for range of -180 to +180, and store.
		val = rotangle# Mod 360
		If val => -180 And val <=180
			; it's ok
		ElseIf val > 180
			diff = val - 180
			val = -180 + diff
		ElseIf val < -180
			diff = val + 180
			val = 180 + diff
		EndIf
		Model(choice)\w# = val ; save the new yaw
	EndIf
Else
	rotflag = 0
EndIf

I wonder if something like my little algo is what Mark does internally if you ask for a yaw of, say, 5000.
Still, your answer is more elegant Stevie.

Thanks to you all.
As usual, the Blitz community helped me get there one way or the other.