# Making model go where it's facing

## Recommended Posts

I have programmed the basic helicopter motion so that they keys "W,A,S,D" move the helicopter in the xy plane. The "W" key moves the heli forward (the helicopter is facing towards the top of the screen), the "S" key moves it backward, "A" to the left, and "D" to the right. This looks proper because while moving the helicopter, the camera is stationary. The problem comes when I rotate the helicopter using code. When I do that, the helicopter's model rotates fine, however, the model's axes don't rotate with it, just the mesh. So, if I rotate the helicopter so it's front faces to the right, the W key still moves the helicopter towards the top of the screen, not to the right, where the helicopter is facing. A simple solution, I think, would be to rotate the helicopter's coordinate system, along with rotating the mesh. This way, the pressing 'W', for instance, will make the helicopter go where it's facing. Is this the correct method of accomplishing what I'm looking for, rotating the helicopter's coordinate system? Would doing this have unpleasant side effects? What would be another method (or the correct method) solving this?

##### Share on other sites
supervlad, I assume you have a variable storing which direction the helicopter is facing? Now how are you with trigonometry?

Assuming upwards has a direction = 0, then you can do something like this pseudo code.

if(W is pressed){      Y += cos(Direction) * Speed;   // adds zero if heli is facing horizontally, adds Speed if facing vertically      X += sin(Direction) * Speed;   // this is the opposite      // Anywhere in between will change both X and Y appropriately, though you may need to change a + to a - sign to get it to move correctly.}

EDIT: Depending on whether you are storing your direction in degrees or in radians, you may need to convert it. I *think* sin and cos take radians in C++ but I don't remember.

Also, to access these functions you can include <Math.h>.

##### Share on other sites
Having a rotation parameter sounds like the way to go.

I tried a couple of versions of that, but it's not working. Those equations look very simple... maybe too simple.

##### Share on other sites
post a snippet of the code, so maybe we can figure out what's wrong.

##### Share on other sites
The equations are simple, sure, but that's all there is to it. You need to post some code at this stage.

What language are you using by the way?

Quote:
 Having a rotation parameter sounds like the way to go.

If you are visually rotating the helicopter, then you must already have some variable for this? How do you know in which direction to draw the helicopter?

As long as you are updating the direction variable properly, the only thing I can think of without seeing your code is that you have a degree/radian mismatch happening. Some functions need radians, not degrees and vice versa.

To convert degrees to radians you can do this:
radians = PI * (degrees / 180)

##### Share on other sites
Store an orthogonal basis, that is forward, right and up vectors (you can store these in a matrix or a quaternion). You can use this for rendering the model correctly too.

When you move its then just a case of doing position += forward*speed;

Where forward is one of the axis in your basis matrix.

I strongly suggest you try to use matrices or quaternions it will make your life alot easier. You can load both from "axis angle" so it should be too hard to change what you have now.

It looks like your z asis is "up", this is the matrix for z axis rotation:

cos(angle) | -sin(angle)| 0
sin(angle) | cos(angle) | 0
0 | 0 | 1

You need to multiply that by your forward vector (which I guess is y since cos(0) = 1)

Which gives you a forward vector of (-sin(angle), cos(angle), )

if we do position += forward*speed we get:

x += -sin(angle)*speed;
y += cos(angle)*speed;
z += 0

edit: I realize I Was looking at someone elses code when I replied to this.

##### Share on other sites
Quote:
 Original post by szecspost a snippet of the code, so maybe we can figure out what's wrong.

I'm using a scripting language, wrapping an engine written in C++. I have access to standard functions like sin() and cos(). And if not, I can create them in C++ and expose them to the scripting language.

This is the code that moves the model back and forth according to the keyboard keys pressed. The motion is smooth, and I like it.

position.x += self._speedX
position.y += self._speedY
position.z += self._speedZ

'position' is a vector representing the absolute location of the model in 3d space (position, a vector, is being used as a point). Therefore, setting position to something like:

position.x = 5
position.y = 10
position.z = 0

would position the model 10 units forward, 5 sideways, and 0 units vertically.

The issue I'm currently having with the rotation, as I explain in the original post.

I can rotate the model from the origin on the xy plan (therefore, it's being rotated around the z axis, which points up) using the following code, but it doesn't do me much good because it doesn't maintain the current coordinate rotation for the next displacement:

x = position.x + self._speedX
y = position.y + self._speedY
position.x = x * Math.cos(angle) - y * Math.sin(angle)
position.y = x * Math.sin(angle) + y * Math.cos(angle)
position.z += self._speedZ

##### Share on other sites
if( pressed('W') )    self_speed = SPEED;else    self_speed = 0;if( pressed('A') )    self_angle += ANGLE_INCREMENT;...position.x += -sin(self_angle)*self_speed * delta_time;position.y +=  cos(self_angle)*self_speed * delta_time;//self_angle and self_speed are global variables and scalars, not vectors.
That's all! It's that simple. self_angle must be in radians.

##### Share on other sites
i've tried all the answers in this thread, but they eather produce the wrong results, or do nothing at all.

This forum doesn't let me post pictures (how 1995), so here's a link to a forum where I explained the problem. Please look at the post I made which has slides.

http://www.terathon.com/forums/viewtopic.php?f=3&t=7806&p=69177#p69177

Thanks

##### Share on other sites
The forum does allow you to post pictures, just use the proper image tags.

Regarding your problem I would recommend you begin to learn vector and matrix math as your going to need it for dealing with anything in 3D. Essentially what you need to do is as people said earlier define a set of basis vectors for your helicopter and store them in a 3x3 rotational matrix. When applying rotations to this matrix you can always pull your forward vector from them and use this to move your helicopter in the appropriate direction.

##### Share on other sites
Do what I told you to do.
That's the simplest motion one can imagine, I have no idea what's wrong.
position.x -= sin(self_angle)*self_speed * delta_time;position.y += cos(self_angle)*self_speed * delta_time;//play around with the signs, maybe ... += sin(...)...//Rendering, whatever syntax in scripting:ViewTransformations();  //Camera move rotate...DrawWorld();            //static objects, terrain, trees whateverglPushMatrix();glTranslatef(position.x,position.y,position.z);//since Z isn't altered, I suppose you initialized it.glRotatef(self_angle,0,0,1); //self_angle in degrees!!!DrawHelicopter();glPopMatrix();Other_Object_Transformation();Draw_Other_Objects();...

My helicopter model has to be oriented the same way:
X is pointing forward, Z is upwards.

##### Share on other sites
Thanks for the info, everyone.

## Create an account or sign in to comment

You need to be a member in order to leave a comment

## Create an account

Sign up for a new account in our community. It's easy!

Register a new account

• ## Partner Spotlight

• ### Forum Statistics

• Total Topics
627657
• Total Posts
2978471

• 10
• 12
• 22
• 13
• 33