Anmeldungsdatum: Mai 2011
Beiträge: 419
Benutzer-Bewertung:
|
Kleines 0815 Release von mir, damit sich hier mal wieder ein klein wenig was in Richtung Programmierung tut
Achtung: Die Funktionen der Klasse arbeiten mit Grad und nicht mit Bogenmaß!
vector.h
CPP Code: #include <math.h> #pragma once #pragma warning (disable:4244) #define M_PI 3.1415926535897932384626433832795 class Vector; class Angle; class Vector { public: float x, y, z; Vector(); Vector(float X, float Y, float Z); Vector operator + (const Vector &add); Vector operator - (const Vector &sub); Vector operator * (const Vector &mul); Vector operator * (const float mul); void operator += (const Vector &add); void operator -= (const Vector &sub); void operator *= (const Vector &mul); void operator *= (const float mul); float operator[](int i); float GetLength(); void GetVectorAngles(Angle *angles); void CrossProduct(const Vector &b, Vector *result); }; class Angle : public Vector { public: Angle() : Vector(){}; Angle(float pitch, float yaw, float roll) : Vector(pitch, yaw, roll){}; Angle(const Vector &v) : Vector(v){}; Angle DegAng_to_RadAng(); Angle RadAng_to_DegAng(); void Normalize(); void GetAngleVectors(Vector *forward, Vector *right, Vector *up); };
vector.cpp
CPP Code: #include "vector.h" Vector::Vector() { x = 0.0f; y = 0.0f; z = 0.0f; } Vector::Vector(float X, float Y, float Z) { x = X; y = Y; z = Z; } Vector Vector::operator + (const Vector &add) { return Vector(x + add.x, y + add.y, z + add.z); } Vector Vector::operator - (const Vector &sub) { return Vector(x - sub.x, y - sub.y, z - sub.z); } Vector Vector::operator * (const Vector &mul) { return Vector(x*mul.x, y*mul.y, z*mul.z); } Vector Vector::operator * (const float mul) { return Vector(x*mul, y*mul, z*mul); } void Vector::operator += (const Vector &add) { x += add.x; y += add.y; z += add.z; } void Vector::operator -= (const Vector &sub) { x -= sub.x; y -= sub.y; z -= sub.z; } void Vector::operator *= (const Vector &mul) { x *= mul.x; y *= mul.y; z *= mul.z; } void Vector::operator *= (const float mul) { x *= mul; y *= mul; z *= mul; } float Vector::operator [] (const int i) { if (i == 0) return x; if (i == 1) return y; if (i == 2) return z; return 0.f; } float Vector::GetLength() { return sqrt(x*x + y*y + z*z); } void Vector::GetVectorAngles(Angle *angles) { float tmp, yaw, pitch; if (y == 0 && x == 0) { yaw = 0; if (z > 0) pitch = 270; else pitch = 90; } else { yaw = (atan2(y, x) * 180 / M_PI); if (yaw < 0) yaw += 360; tmp = sqrt(x * x + y * y); pitch = (atan2(-z, tmp) * 180 / M_PI); if (pitch < 0) pitch += 360; } angles->x = pitch; angles->y = yaw; angles->z = 0; } void Vector::CrossProduct(const Vector &b, Vector *result) { result->x = y*b.z - z*b.y; result->y = z*b.x - x*b.z; result->z = x*b.y - y*b.x; } Angle Angle::DegAng_to_RadAng() { return Angle(x / 180 * M_PI, y / 180 * M_PI, z / 180 * M_PI); } Angle Angle::RadAng_to_DegAng() { return Angle(x / M_PI * 180, y / M_PI * 180, z / M_PI * 180); } void Angle::Normalize() { while (x > 180.f) { x -= 360.f; } while (x < -180.f) { x += 360.f; } while (y > 180.f) { y -= 360.f; } while (y < -180.f) { y += 360.f; } while (z > 180.f) { z -= 360.f; } while (z < -180.f) { z += 360.f; } } void Angle::GetAngleVectors(Vector *forward, Vector *right, Vector *up) { float sr, sp, sy, cr, cp, cy; sy = sin(y / 180 * M_PI); cy = cos(y / 180 * M_PI); sp = sin(x / 180 * M_PI); cp = cos(x / 180 * M_PI); sr = sin(z / 180 * M_PI); cr = cos(z / 180 * M_PI); if (forward) { forward->x = cp*cy; forward->y = cp*sy; forward->z = -sp; } if (right) { right->x = (-1 * sr*sp*cy + -1 * cr*-sy); right->y = (-1 * sr*sp*sy + -1 * cr*cy); right->z = -1 * sr*cp; } if (up) { up->x = (cr*sp*cy + -sr*-sy); up->y = (cr*sp*sy + -sr*cy); up->z = cr*cp; } }
mfg Dr_Pepper
__________________
Da unten ist ein Like-Button, benutze ihn doch
|