Camera type affecting setLocalRotation ?!?

Hi guys,



I am getting some very strange behaviour, that I am struggeling to explain.



I have implemented my own aim at algorithm that calculates quaternions and assign them to object to “aim” them… The strange thing is that my objects rotate differently with the same resulting quaternions - I found that when I use a flybycamera my rotations are good, but when I use a static mounted camera looking down in direction [ 0, -1, 0 ] my resulting rotations are messed up…



My initial guess is that my “up vector” is affected by the camera, but that does not make sence, since I use Vector3f.UNIT_Y as my up vector in my aim calculation… Besides, I do get that same rotation matrix results - just not the same rotation visually ( looks like there is a 90 degree rotation for some reason )…



I really dont get how the camera could influence the rotations applied to an object. BTW, the objects are attached to the rootNode.



Any ideas what might be causing this?



Hope you can help.



Cheers,



NiHal

The camera does not affect rotations in general. How and why should it do so? Give your code snippet that is responsible for the calculation and maybe one or two screenshots to give a better explanation.

The fact you use UNIT_Y is the problem I guess. When the other vector isn’t perpendicular to the Y axis then its always wrong using UNIT_Y

1 Like

Hi guys,

Dodikles, I know it doesnt make sense. I am thinking just the same as you - why would it affect it, but fact is that if I change the camera type as the only thing, it completely changes the behavior of my gun platform. See the below image:





As you can see in the first image, the gun platform is turned on its back ( it does not rotate about its y axis anymore… ) on the 2nd image the cannon is tracking the target perfectly, without mistake, I can move the target in 3d space and even move the cannon in 3d space and it always points perfectly at the target, rotating the base and elevating the turrents…



Normen, I use the UNIT_Y vector because for the platform, the Y axis is always up - but actually it is a two stage calculation, calculating the rotation of the base and calculating the rotations of the barrels.



I have just eliminated the aiming algorithm as the culpit, I tried aiming at the same target: ( 2, 1, 5 ), and I get excactly the same quaternions - independent of which camera I use. BUT when I look at the two resulting rotations I get what is shown in the image above… One is bend into a weird configuration and another is prefectly good…



First thing I will check when I get home is if other objects are affected like this for complex rotations - I do have other objects at the moment, and they do not seem affected, but I will have to confirm by looking at their quaternions…



I think this is in my top 10 of weirdest bugs I have encountered / produced… :stuck_out_tongue: From experience I am the one at fault… Error #40 as we said @ uni…



Thank you for your replies so far.

Cheers

NiHal

Okay,



I managed to fix the problem - though I still dont understand excactly what was wrong.



I had a class creating my scene with a line of code going:



[java]

this.getCamera().setLocation( new Vector3f( 0, 10, 0 ) );

this.getCamera().setRotation( Quaternion.IDENTITY.fromAngles( FastMath.HALF_PI + FastMath.DEG_TO_RAD*5, 0, 0 ) );

[/java]



and another in a class extending the class above going:

[java]

this.cam.setLocation( new Vector3f( 0, 10, 0 ) );

this.cam.setRotation( Quaternion.IDENTITY.fromAngles( FastMath.HALF_PI, 0, 0 ) );

[/java]



Removing code from the first class resolved the issue… Though I still do not understand why…



Thanks for the input though.



Cheers,



NiHal

Yikes!



Quaternion.IDENTITY.fromAngles( FastMath.HALF_PI, 0, 0 )



That will MODIFY the IDENTITY Quaternion so that it is no longer the identity. This will strangely affect everything that uses the IDENTITY quaternion. fromAngles() changes the quaternion that it is called on. Super bad dangerous.



The Javadoc for those methods should probably state that in big giant letters but as a rule it’s bad to call methods on the static ‘constants’.



What you really want is:

new Quaternion().fromAngles( FastMath.HALF_PI, 0, 0 )

1 Like

Oh my god… It was in plain sight! Sorry, I cannot say how I missed that… :S



Turned out to be error 40 as I expected… ( Source of the problem sitting 40 cm from the screen, that is ) :stuck_out_tongue:



Thank you very much pspeed! Thumbs up!



Cheers,



Nihal

Well, instantiating a new Quaternion automatically generates that quaternion’s identity… And as Paul said, it’s very bad form to use Class.staticVar.stuffWithIt() unless it’s designed for that kind of use.

Yes, I realize that - it’s embarrassing. My ignorance will be forever on display on the jmonkeyengine forums… :’/



Thanks for the help guys.

nihal said:
Yes, I realize that - it's embarrassing. My ignorance will be forever on display on the jmonkeyengine forums.. :'/

Oh don't worry, you should see some of the posts that I made. The above is nothing compare to those. I'm still ashamed after all that time. (Actually I should shut up now before anyone goes and look for those damnable posts.);)

For a long time in my early software development career, I would recycle my online identity every few years. You would not believe some of the incredibly stupid points I argued vehemently for only to have been handed my head. The above is nothing. :slight_smile:



At some point, I started using my actual real identity therefore secure in the knowledge that all of my mistakes would haunt me all the way to the grave. :slight_smile: For a particularly recent example, note that the “utility class” Filters in com.jme3.network didn’t have any static methods. D’oh.



It’s these things that we should remember as to keep us humble so that when someone else does something “stupid” in front of us we are more likely to lend a hand than to laugh and jeer. Or that’s what I tell myself. :wink: