#### Archived

This topic is now archived and is closed to further replies.

# Help Slacker with Rotations?

This topic is 5305 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I'm having some trouble understanding my trig completely. If anyone can shed more light onto this for me, I will definitely appreciate it. I know sin() returns the width (or one axis deminsion) of a circle, and cos() returns the other. The absolute values should always add up to 1.0. Right? So if I wanted to move in a specific direction based on an angle, I do this:
Pos.X += sin(angle) * velocity;
Pos.Z += cos(angle) * velocity;  
Right? Or at least something close to that? What I'm having trouble understanding is how to mix another deminsion into it. To move in 3 deminsions, I need another number to add with the other two, and the total of all 3 numbers still need to add up to 1.0. Is that right? This part totally escapes me. If I knew what was happening inside the cosine and sine methods, perhaps I could piece it together. But that may be even further over my head. Anyone have the patience to explain this to me? [edited by - Jiia on May 9, 2004 8:03:17 AM]

##### Share on other sites
I would suggest looking into using vectors instead of trig.

##### Share on other sites
I am using vectors. I''m not sure what you mean?

##### Share on other sites
No, they don''t always add up to 1. Consider pi/4.

##### Share on other sites
If you were you wouln't need any trig.

With vectors you could do this:
Position += Direction * Velocity;

Where direction is a normalized vector and velocity a scalar.

Edit: AP is correct. They do not add up. Though sin(a)^2 + cos(a)^2 is always 1.

[edited by - Lantz on May 9, 2004 8:12:47 AM]

##### Share on other sites
quote:
Original post by Anonymous Poster
No, they don''t always add up to 1. Consider pi/4.

A vector does not have to have unit length.

##### Share on other sites
I need to have my camera eye rotate to face the direction that an object is moving. The camera only rotates if the object is moving, and it does so slowly based on time. Aka GTA3 when outside of a car, except in 3 deminsions (I think GTA only does this with X and Z rotations, keeping Y slightly above the character).

So I have my object's "ahead" vector. But I need to see what rotations around the object the camera should be at to face it's back (simple as -(ahead vector) * distance). Then I need to slowly rotate the camera to face that direction (not quite simple for me yet).

I'm not sure how to do this with vectors. The only way I know to do it would be to get the angles, modify them, then translate them back into positions. Hence, I have the problem explained above. I don't know how to use the 3rd deminsion in the angle math.

Is this a dumb way to do it? Can I do it with vectors? Thanks for any suggestions.

[edited by - Jiia on May 9, 2004 10:01:42 AM]

##### Share on other sites
quote:
Original post by Jiia
Can I do it with vectors?

The easiest way is to perform a weighted avarage between the two vectors every frame. Eg:
Vector objectDirVector eyeDir// Every frame, do:float weight = 0.98;eyeDir = (eyeDir * weight) + (objectDir * (1-weight));
Your eye dir will slowly align itself with the object direction. Note that you usually need a weight very close to 1 to get a nice smooth movement (anything lower tends to ''snap'' into place too quickly at a high framerate). Added bonus for taking into account frame time for consistant movement

##### Share on other sites
Ike!! It''s not too often something so great comes out of 3 lines of code! Thank you very much Pouya!

I''ve done this exact math between color components to calculate alpha blending routines. 1 + 1 = 2. Go figure.

Now I just need to figure out how to adjust the weight to rely on the timer delay between frames. If it''s less than 0.5, it would work backwards, right?

Thanks a lot, seriously. I''m off to study the possibilities.

##### Share on other sites
No, if it''s less than 0 it would work backwards.

This construct is an IIR filter with a single pole and no zero, by the way. If you want to learn more, google for that :-)

To account for differences in time step, use this:

  #include <math.h>  float const factor_per_second = 0.98f;  float factor = powf( factor_per_second, frame_time );  new_value = (old_value * factor) + (desired_value * (1-factor));

1. 1
Rutin
41
2. 2
3. 3
4. 4
5. 5

• 16
• 18
• 12
• 14
• 9
• ### Forum Statistics

• Total Topics
633362
• Total Posts
3011527
• ### Who's Online (See full list)

There are no registered users currently online

×