# Moving a space ship in 3 dimensions, having a camera follow.

8 replies to this topic

### #1JRowe47

New Member

• Members
• 6 posts

Posted 03 November 2008 - 01:23 AM

I have a spaceship model that can move around in 3 dimensions.

I've constructed a player node with the following objects:
Base node (invisible 'root' of my player object)
Camera node (attached to the root)
Model node (attached to the root)
Attractor node (attached to the root)

I move the attractor node up to 40 units in any direction around the center of the root node. Each frame, I set the center of the root node to the absolute position of the attractor node.

I use the attractor node as an expression of the forces acting on my player node. If its center matches the center of the root node, the ship doesn't move. Each frame, I move the attractor node slowly back toward the center, to emulate deceleration. Overall, it works pretty well :)

I move the attractor node forward and back, left and right by using WASD, and up/down by moving the mouse to the top 15% or bottom 15% of the screen.

My problem is that I am dependent on a particular orientation; If I change my orientation, forward/back and left/right lose their context, so WASD controls don't move the ship correctly in relation to the camera.

How can I make a movement system that moves through 3 dimensions according to the orientation of the camera?

Is the attractor node idea a bad way to go about it?

### #2Reedbeta

DevMaster Staff

• 5344 posts
• LocationSanta Clara, CA

Posted 03 November 2008 - 02:10 AM

The attractor node is a bit unorthodox but it sounds like a reasonable way to go about it to me.

In any case, to be able to move in camera-relative directions, you'll have to use some trig and/or matrix math to compute the forward/right/up vectors based on the camera angle. If you have access to (or are already computing) the camera matrix, you can extract the vectors straightforwardly from it. I can't tell you how in detail without knowing how your coordinate system is oriented. You didn't say if you're using an engine or something you built on your own, but if it's an engine it may well have functions built in to get these vectors already.

Btw, I find a useful extension to WSAD is to use E and C for up and down.
reedbeta.com - developer blog, OpenGL demos, and other projects

### #3monjardin

Senior Member

• Members
• 1033 posts

Posted 03 November 2008 - 02:11 AM

EDIT: Reed's simultaneous post covers everything useful I had to add. ;)
monjardin's JwN Meter (1,2,3,4,5,6):
|----|----|----|----|----|----|----|----|----|----|
*

### #4JRowe47

New Member

• Members
• 6 posts

Posted 03 November 2008 - 02:35 AM

Translation, etc, would be what I'm looking for, Monjardin. Thanks Reedbeta!

For reference, the language I'm using is AutoIt script, and the engine is an Irrlicht plugin done by A.Percy of the AutoIt community. If you'd like to see what I have, http://www.autoitscr...showtopic=83709 has my code, and links to the project files. You'll also need AutoIt installed, which is a good idea in any case. ;)

Disclaimer: I'm not very familiar with 3D math or programming. I've gotten to the point where I've got the node moving around correctly if I don't mess with the camera. It's been mostly by trial and error, and a very cursory understanding of what I'm doing.

I can get and set rotation of any node along any axis, as well as get/set position of any node along any axis.

I can calculate rotation along any axis relative to the world, and I can get the "absolute" coordinates for a node as well.

The only vector object I have access to is the "Up Vector" for the camera. To be honest, I'm not sure what it is...

Is there a vector tutorial or general explanation you could point me at that covers issues like this? Thanks for the input!

Also, to clarify what I wanted from my "attractor node" idea:
My thought was to manually rotate the ship node along different axes to represent pitch, yaw, and roll. I wanted to calculate those values based on the relative position of the attractor Node, and set the camera look target toward the center of the ship node, and the position of the camera 100 units behind the ship node along the relative X axis.

Actual motion through the world occurs by setting the position of the root node which the attractor, ship, and camera node are attached to.

I was trying to come up with a way to skip learning about vectors, but since I have to learn anyway, I'd like to follow through and see how it works, anyway.

### #5Reedbeta

DevMaster Staff

• 5344 posts
• LocationSanta Clara, CA

Posted 03 November 2008 - 02:53 AM

Yeah, we've got some articles on our wiki about vectors and matrices. These two are probably the most immediately relevant for you:

http://www.devmaster...rs_and_matrices
http://www.devmaster...rdinate_systems

Here is a basic one about the physics of motion:

http://www.devmaster.../Simple_physics
reedbeta.com - developer blog, OpenGL demos, and other projects

### #6JRowe47

New Member

• Members
• 6 posts

Posted 03 November 2008 - 03:17 AM

Thanks!

### #7JRowe47

New Member

• Members
• 6 posts

Posted 05 November 2008 - 10:40 AM

Hello again! I discovered that my earlier instinct was indeed correct, and that I was very close to achieving what I'd wanted without actively calculating vectors and using matrices to translate the attractor node location.

I did, however, need to understand the math, and I thank you again for pointing me at those tutorials.

I'm working out a simple vehicle system based on what I've learned, and it's giving the ships both a very realistic feel and look to the movement.

There is one quick piece of vector math I'm confused about, though. I need to compensate for my relative pitch. My angle is causing my pitch to reverse around my Up Vector... at 180 degrees, it works correctly, moving up and down, but at 360 degrees, it works in the opposite manner. I believe what I need is a sin or cos function operating on my degree of rotation, then applied to what I want my pitch to be, but... it is confusticating me, lol.

It's probably too late, and I'll see everything clearly tomorrow, but thanks anways! :)

### #8JRowe47

New Member

• Members
• 6 posts

Posted 05 November 2008 - 12:11 PM

Ahh ha! Relative Rotations and translations aren't commutative. I forgot to watch the order I was setting my angles, and ended up throwing in an extra rotation.

I'll post some code sometime soon. :)

### #9Savage

New Member

• Members
• 1 posts

Posted 16 November 2008 - 07:40 PM

This may or may not help, but I wrote a small C-Sharp example of this for XNA.
It implements 6 cameras and allows the user to switch between them at run-time. 1 of the cameras is a rubber band type system where it falls a little behind when the object speeds away and then settles into position once the object stops. The camera types it supports are Fixed, Tracking, First Person and ThirdPerson, Top Down Fixed and Top Down Tracking cameras.

The post about it can be found here
http://forums.xna.co...2188/10897.aspx
and the zip files that contains both C-Sharp and Object Pascal project can be downloaded here - http://chrome.pascal...as_Tutorial.zip

It is probably not the best implementation of such a thing, but it helped me understand cameras.

#### 1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users