Zachary Carter
Zachary Carter

Reputation: 373

Projectile rotation based on camera rotation

I've been working on a little space simulator demo using three.js and flightcontrols.js (an example that ships with three.js)

The demo is viewable at - http://www.zacharycarter.com/PrivateerRedux/demo.html

The control scheme is pretty simple - the mouse controls the camera and you left click to fire a projectile.

I'm having a little bit of trouble with how I'm spawning my projectiles. Currently I'm setting their rotation to the same rotation of my camera which I believe is what is causing my problems.

You'll notice that as you rotate the camera around the game world, and continue firing projectiles, that the position and orientation of the projectiles quickly becomes whacky.

I'm using quaternions to represent my camera's rotation but I also derrive the Euler angles from the quaternion before setting my projectiles rotation to match that of my cameras.

Any ideas as to what I'm doing wrong here? I have a feeling I need to figure out a constant front facing rotation from my camera rotation but I'm not quite sure how to do that.

Thanks!

Upvotes: 0

Views: 251

Answers (1)

FanerYedermann
FanerYedermann

Reputation: 444

If I understand this correctly, I think your problem is that you're applying world transformations and local transformations (i.e rotation in world space to have bullets fly in the right direction and rotation of bullets around the bullets' local axes) incorrectly. It seems they're all being applied in the same space.

Instead of writing up a whole new example, however, I'll leave it to the old, trusty internet to hand us some guidance on the issue using the common example of a solar system.

This might enlighten you:

http://docs.techsoft3d.com/visualize/3df/latest/Hoops3DGS/prog_guide/03_3_viewing_modelling_matrices.html (have a look at figure 3.3.2.b)

...and here's an OpenGL example demonstrating the same thing:

http://www.codemiles.com/c-opengl-examples/solar-system-transformations-t7292.html

There's the theory anyway, hope that helps!

Upvotes: 1

Related Questions