Using Sin/Cos Calculations

Hey Everyone,

So, this should be quite brief, but I simply wanted another person’s suggestion if necessary.

In short, I am rotating my camera around a dummy node by rotating the dummy node itself. Now, since this rotation does not actually rotate the coordinate plane in use (and I am not positive that I would want to do that) I calculate the new angles using sin/cos to generate the angle for my new “forward” movement when pressing “w” etc. However, this means as I rotate my view, it’s calculating sin/cos every frame to make sure my forward movement is always straight ahead.

My question then is that will this strain the engine too much my having such calculations every frame. Granted, the calculations only occur when the player decides to rotate his camera. But this may happen quite frequently as it will be a third-person shooter.

I sincerely doubt that it will be a problem.

That said, it shouldn’t be difficult to test: activate the frame-rate meter (perhaps setting it to show frame-times rather than frame-rates) and then try your code both with and without the sine/cosine operations.

1 Like

Ok wonderful, that is reassuring! But I will check it in any case to be sure. Thank you!

1 Like

Not a problem! Good luck with the testing. :slight_smile:

1 Like

As a heads up, Panda has a lot of builtin utilities for this kind of stuff. Being used to other engines I usually instinctively reach for trig or linear algebra to solve these kinds of things, but then I stop myself and take a look at the API. For example, there is NodePath.look_at() or the form of NodePath.getPos() that takes another NodePath to give relative position vectors.

However, the calculations you describe should not be a performance problem.


Also those sort of problems were at the heart of Doom or Quake and they found a SUPER fast way to do that math. You might look that up, it is famous and you could also just precalculate a loop up table for every degree or 5 or whatever you need.

1 Like

You could indeed. (And they did come up with some really neat tricks for those games!)

Still, unless the program has extremely tight CPU requirements, or were running a great many sine/cosine calls per frame, I sincerely doubt that it would be called for on modern hardware.

1 Like

This is one of those “don’t waste time optimizing until it shows up on your profiler graph” kind of situations.


Thank you all for the neat tips and tricks!

I think I will do just this unless I begin to see a problem, and then I will certainly look into the other solutions that yall have offered. Thank you again everyone!