Skip to content

Instantly share code, notes, and snippets.

@mackthehobbit
Created March 30, 2016 23:23
Show Gist options
  • Save mackthehobbit/fa3c4722fc0518c4a48a5193abce8a92 to your computer and use it in GitHub Desktop.
Save mackthehobbit/fa3c4722fc0518c4a48a5193abce8a92 to your computer and use it in GitHub Desktop.
Minecraft PE Vector3 struct, with useful methods + implementation. Compatible with the game
#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);
}
#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;
};
@mackthehobbit
Copy link
Author

With the overloaded operators you can do things like:
Vec3 a(3, 4, 5);
Vec3 b(1, 1, 1)
Vec3 c = a + b; // c is now (4, 5, 6)
c /= 2; // c is now (2, 2.5, 3)

@ThePixelGamer
Copy link

This is actually pretty interesting. Keep up the good work!

@mackthehobbit
Copy link
Author

Also note: there are a few functions in MCPE with implementation as of 0.14.0 that aren't in this header yet (clamp, directionFromRotation etc.) but I haven't found use-cases yet.
Dot and/or cross product could be added too, but again they just aren't used very often. Add them if you need.

@TrinityDevelopers
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment