angle.h
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #if !defined(INCLUDED_ANGLE_H)
00023 #define INCLUDED_ANGLE_H
00024
00025 #include "ientity.h"
00026
00027 #include "math/quaternion.h"
00028 #include "generic/callback.h"
00029 #include "stringio.h"
00030
00031 const float ANGLEKEY_IDENTITY = 0;
00032
00033 inline void default_angle (float& angle)
00034 {
00035 angle = ANGLEKEY_IDENTITY;
00036 }
00037 inline void normalise_angle (float& angle)
00038 {
00039 angle = static_cast<float> (float_mod(angle, 360.0));
00040 }
00041 inline void read_angle (float& angle, const std::string& value)
00042 {
00043 if (!string_parse_float(value.c_str(), angle)) {
00044 angle = 0;
00045 } else {
00046 normalise_angle(angle);
00047 }
00048 }
00049 inline void write_angle (float angle, Entity* entity)
00050 {
00051 if (angle == 0) {
00052 entity->setKeyValue("angle", "");
00053 } else {
00054 char value[64];
00055 sprintf(value, "%g", angle);
00056 entity->setKeyValue("angle", value);
00057 }
00058 }
00059
00060 class AngleKey
00061 {
00062 Callback m_angleChanged;
00063 public:
00064 float m_angle;
00065
00066 AngleKey (const Callback& angleChanged) :
00067 m_angleChanged(angleChanged), m_angle(ANGLEKEY_IDENTITY)
00068 {
00069 }
00070
00071 void angleChanged (const std::string& value)
00072 {
00073 read_angle(m_angle, value);
00074 m_angleChanged();
00075 }
00076 typedef MemberCaller1<AngleKey, const std::string&, &AngleKey::angleChanged> AngleChangedCaller;
00077
00078 void write (Entity* entity) const
00079 {
00080 write_angle(m_angle, entity);
00081 }
00082 };
00083
00084 inline float angle_rotated (float angle, const Quaternion& rotation)
00085 {
00086 return matrix4_get_rotation_euler_xyz_degrees(matrix4_multiplied_by_matrix4(matrix4_rotation_for_z_degrees(angle),
00087 matrix4_rotation_for_quaternion_quantised(rotation))).z();
00088 }
00089
00090 #endif