Trivial Question

BlitzMax Forums/BlitzMax Programming/Trivial Question

BLaBZ(Posted 2014) [#1]
Hi all -

I have a trivial question and situation, I feel like there's a better way to handle this mathematically but can't seem to think of of how.


TFormPoint 0, 0, 0, dest, Mesh
Local ax:Float = TFormedX()

'Determine Yaw
If ax > 0.0
	TurnEntity(Mesh,0,1.0,0)
EndIf
If ax < 0.0
	TurnEntity(Mesh,0,-1.0,0)
End If



I'm finding the position of dest in Mesh's local coordinates, If dest is to the left, then turn left, if dest is to the right turn right.

Is there a way I could eliminate these if statements and still have the entity turn 1 unit towards the correct direction?


Kryzon(Posted 2014) [#2]
In BlitzMax, those boolean expressions evaluate to '0' or '1,' which are numerical integer values.

You can use this behaviour to condense that block as a single line.
TFormPoint( 0, 0, 0, dest, Mesh )

Local speed:Float = 1.0
Local aX:Float = TFormedX()

TurnEntity( Mesh, speed * ( aX > 0 ) - speed * ( aX < 0 ), 0, 0 )
Note that this sacrifices readability, as discussed in this thread: http://www.blitzbasic.com/Community/posts.php?topic=101937
In order to use tricks like this it is best to add comments so that you understand what it is doing.


BLaBZ(Posted 2014) [#3]
Ahh interesting, would this provide a performance increase?

I imagine it would be nominal at best.


Floyd(Posted 2014) [#4]
There is a built-in operator for the sign of a number. So you could shorten this to

TurnEntity( Mesh, speed * Sgn( aX ), 0, 0 )



Kryzon(Posted 2014) [#5]
Would this provide a performance increase?

We could speculate that it would, but the only way to be sure is to test something like one hundred thousand calls to each way, and see which is computationally the fastest:



EDIT: Corrected the code, something was wrong.


BLaBZ(Posted 2014) [#6]
Oh wow, nice! More than twice as fast!


Derron(Posted 2014) [#7]
You could even save that "ax = TFormedX()" if you replace "Sgn( ax )" with "sgn( TFormedX() )".


PS: Sgn(x) is really rarely used ... should check my code if it could be of use somewhere :D


bye
Ron


John G(Posted 2014) [#8]
@Derron: "PS: Sgn(x) is really rarely used..." What do you mean? Don't understand. Thanks.


Derron(Posted 2014) [#9]
When I read others BlitzMax code the "sgn" command isn't used that often .. albeit it is a inbuild command.


bye
Ron