Created
March 30, 2016 23:23
-
-
Save mackthehobbit/fa3c4722fc0518c4a48a5193abce8a92 to your computer and use it in GitHub Desktop.
Minecraft PE Vector3 struct, with useful methods + implementation. Compatible with the game
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "Vec3.h" | |
float Vec3::lengthSquared() const { | |
return x * x + y * y + z * z; | |
} | |
float Vec3::length() const { | |
return sqrtf(lengthSquared()); | |
} | |
void Vec3::normalize() { | |
*this /= length(); | |
} | |
void Vec3::setLength(float newLength) { | |
*this /= (length() / newLength); | |
} | |
Vec3 Vec3::operator+(Vec3 const& other) const { | |
return Vec3(x + other.x, y + other.y, z + other.z); | |
} | |
void Vec3::operator+=(Vec3 const& other) { | |
x += other.x; | |
y += other.y; | |
z += other.z; | |
} | |
Vec3 Vec3::operator-(Vec3 const& other) const { | |
return Vec3(x - other.x, y - other.y, z - other.z); | |
} | |
void Vec3::operator-=(Vec3 const& other) { | |
x -= other.x; | |
y -= other.y; | |
z -= other.z; | |
} | |
Vec3 Vec3::operator*(float factor) const { | |
return Vec3(x * factor, y * factor, z * factor); | |
} | |
void Vec3::operator*=(float factor) { | |
x *= factor; | |
y *= factor; | |
z *= factor; | |
} | |
Vec3 Vec3::operator/(float factor) const { | |
return Vec3(x / factor, y / factor, z / factor); | |
} | |
void Vec3::operator/=(float factor) { | |
x /= factor; | |
y /= factor; | |
z /= factor; | |
} | |
Vec3 Vec3::operator-() const { | |
return Vec3(-x, -y, -z); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
#include <cmath> | |
class BlockPos; | |
struct Vec3 { | |
float x, y, z; | |
static const Vec3 ZERO; | |
static const Vec3 ONE; | |
static const Vec3 UNIT_X, UNIT_Y, UNIT_Z; | |
static const Vec3 NEG_UNIT_X, NEG_UNIT_Y, NEG_UNIT_Z; | |
Vec3(float _x, float _y, float _z) : x(_x), y(_y), z(_z) {} | |
Vec3(const BlockPos&); | |
float lengthSquared() const; | |
float length() const; | |
void normalize(); | |
void setLength(float newLength); | |
Vec3 operator+(Vec3 const& other) const; | |
void operator+=(Vec3 const& other); | |
Vec3 operator-(Vec3 const& other) const; | |
void operator-=(Vec3 const& other); | |
Vec3 operator*(float factor) const; | |
void operator*=(float factor); | |
Vec3 operator/(float factor) const; | |
void operator/=(float factor); | |
Vec3 operator-() const; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Very nice. Mind if I use this in my header library for my mods? I will comment in credit to you for this of course.