3D {SOLVED} Using quaternions to make a 3rd person camera

Kentae

Member
So I'm making a game where I'm using quaternions to handle a spaceships orientation in 3D space. I'm using some quaternion-scripts that I found some time ago made by TheSnidr. So far I have been able to use these scripts to get the ship to rotate like I want it to, by using the mouse, the Q-key and the E-key to rotate it, Much like the rotation in Space Engineers.
However I am a bit stumped as to how I can use these scripts to make the camera stay behind the ship at all times. At first I thought I could just use the orientation of the ship and add a value to it to make the camera stay behind it but that didn't quite work the way I wanted.

I guess my question here is;
Is there a way i can use the ships orientation quaternion to make a camera stay some distance behind the ship at the right orientation?
 
N

Never Mind

Guest
Yes Quaternions make it super easy.
TheSnidr has a script specifically for getting facing vectors out of a Quaternion.
That's all you need.
Once you have the vector representing your ships forward orientation, multiply it by a negative distance you want the camera to trail behind, then multiply that by the current location vector of the ship. This will give you the cameras new location.

To set up your projection just use the cameras location as the FROM vector and the ships location as the TO vector.
For the UP just run the same get facing script but this time find the ships upward facing vector.
 
Last edited by a moderator:

Kentae

Member
Cool, Thanks a bunch for telling me ^^ but do you know where I can get this script? You have a link to it or something like that? :)
 
N

Never Mind

Guest
I thought TheSnidr was the one that posted quaternion scripts, but it was actually slayer 64..
Here's slayer 64s post: Quaternions - Scripts [GML]

The two script you need are:
quaternionGetx(qw,qx,qy,qz)
quaternionGetz(qw,qx,qy,qz)

That page is on the old forum. Someone should request it be brought to the new one so it's not so dang hard to find!
 

Kentae

Member
Haha, well, that explains a lot xD I've been spending hours going through pretty much everything TheSnidr has posted in order to find those scripts and I suppose this explains why I never found them xD

Thanks a lot for all the help you have given, appreaciate it greatly :)
 
Top